Apply R# clean-up for reliability (#877)
* Apply R# clean-up for reliability - Migrate to NUnit4 Assert.That - Update dependencise versions - Fix LangVersion to 9.0 (until net core migration) - Reformat code - Apply 'var' style (easier for refactoring) - Remove redundant parentheses - Add braces on every condition blocks (1 line) - Uses new null checking patterns - Uses AutoProperties when possible - Uses string scalar type instead of class - Clean usings - Add .orig files in git ignore - Rename some properties to respect defined conventions and US format definition - ID -> Id - BPM -> Bpm - Finish -> End * Update R# settings according to the clean-up made
This commit is contained in:
1 parent
b9b0f7c8df
commit
6ffa63b923
236 files changed
+14194
-7278
No files matched your search
+106
-24
@@ -34,7 +34,7 @@ namespace VocaluxeLib.Utils
|
||||
//Number of samples (per channel)
|
||||
public int NumSamples
|
||||
{
|
||||
get { return (DataSize / (BitsPerSample / 8 * NumChannels)); }
|
||||
get { return DataSize / (BitsPerSample / 8 * NumChannels); }
|
||||
}
|
||||
//Number of samples left to be read (per channel)
|
||||
public int NumSamplesLeft { get; private set; }
|
||||
@@ -57,7 +57,10 @@ namespace VocaluxeLib.Utils
|
||||
public void Create(string fileName, short numChannels, int sampleRate, short bitsPerSample)
|
||||
{
|
||||
if (IsOpen)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
_File = new FileStream(fileName, FileMode.Create);
|
||||
FileName = fileName;
|
||||
NumChannels = numChannels;
|
||||
@@ -73,15 +76,19 @@ namespace VocaluxeLib.Utils
|
||||
public void Close()
|
||||
{
|
||||
if (!IsOpen)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_IsWritable)
|
||||
{
|
||||
BinaryWriter writer = new BinaryWriter(_File);
|
||||
var writer = new BinaryWriter(_File);
|
||||
writer.Seek(4, SeekOrigin.Begin);
|
||||
writer.Write((int)_File.Length - 8);
|
||||
writer.Seek(_DataPos - 4, SeekOrigin.Begin);
|
||||
writer.Write(DataSize);
|
||||
}
|
||||
|
||||
_File.Dispose();
|
||||
_File = null;
|
||||
IsOpen = false;
|
||||
@@ -90,40 +97,70 @@ namespace VocaluxeLib.Utils
|
||||
public bool Open(string fileName)
|
||||
{
|
||||
if (IsOpen)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
if (!File.Exists(fileName))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
FileName = fileName;
|
||||
_File = new FileStream(FileName, FileMode.Open);
|
||||
try
|
||||
{
|
||||
BinaryReader reader = new BinaryReader(_File);
|
||||
var reader = new BinaryReader(_File);
|
||||
if (Encoding.ASCII.GetString(reader.ReadBytes(4)) != "RIFF")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
reader.ReadInt32(); //Chunksize (file)
|
||||
if (Encoding.ASCII.GetString(reader.ReadBytes(4)) != "WAVE")
|
||||
{
|
||||
return false;
|
||||
long curPos = _File.Position;
|
||||
}
|
||||
|
||||
var curPos = _File.Position;
|
||||
if (_SeekTo(reader, "fmt ") < 0)
|
||||
{
|
||||
return false;
|
||||
short audioFormat = reader.ReadInt16();
|
||||
}
|
||||
|
||||
var audioFormat = reader.ReadInt16();
|
||||
if (audioFormat != 1) //PCM
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
NumChannels = reader.ReadInt16();
|
||||
SampleRate = reader.ReadInt32();
|
||||
int bytesPerSec = reader.ReadInt32();
|
||||
short blockAlign = reader.ReadInt16();
|
||||
var bytesPerSec = reader.ReadInt32();
|
||||
var blockAlign = reader.ReadInt16();
|
||||
BitsPerSample = reader.ReadInt16();
|
||||
if (BitsPerSample != 8 && BitsPerSample != 16)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (blockAlign != NumChannels * (BitsPerSample / 8))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (bytesPerSec != blockAlign * SampleRate)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_File.Position = curPos;
|
||||
DataSize = _SeekTo(reader, "data");
|
||||
if (DataSize <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_DataPos = (int)_File.Position;
|
||||
DataSize = Math.Min(DataSize, (int)_File.Length - _DataPos);
|
||||
NumSamplesLeft = NumSamples;
|
||||
@@ -133,6 +170,7 @@ namespace VocaluxeLib.Utils
|
||||
_File.Dispose();
|
||||
throw;
|
||||
}
|
||||
|
||||
IsOpen = true;
|
||||
return true;
|
||||
}
|
||||
@@ -146,23 +184,32 @@ namespace VocaluxeLib.Utils
|
||||
public byte[] GetNextSamples8Bit(int numSamples, int channel = 0)
|
||||
{
|
||||
if (channel < 0 || channel > NumChannels || BitsPerSample != 8)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (numSamples > NumSamplesLeft)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] result;
|
||||
BinaryReader reader = new BinaryReader(_File);
|
||||
var reader = new BinaryReader(_File);
|
||||
if (channel == 0 || NumChannels == 1)
|
||||
{
|
||||
result = reader.ReadBytes(numSamples * NumChannels);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = new byte[numSamples];
|
||||
for (int i = 0; i < numSamples; i++)
|
||||
for (var i = 0; i < numSamples; i++)
|
||||
{
|
||||
_File.Seek(channel - 1, SeekOrigin.Current);
|
||||
result[i] = reader.ReadByte();
|
||||
_File.Seek(NumChannels - channel, SeekOrigin.Current);
|
||||
}
|
||||
}
|
||||
|
||||
NumSamplesLeft -= numSamples;
|
||||
return result;
|
||||
}
|
||||
@@ -176,29 +223,38 @@ namespace VocaluxeLib.Utils
|
||||
public short[] GetNextSamples16Bit(int numSamples, int channel = 0)
|
||||
{
|
||||
if (channel < 0 || channel > NumChannels || BitsPerSample != 16)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (numSamples > NumSamplesLeft)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
short[] result;
|
||||
BinaryReader reader = new BinaryReader(_File);
|
||||
var reader = new BinaryReader(_File);
|
||||
try
|
||||
{
|
||||
if (channel == 0 || NumChannels == 1)
|
||||
{
|
||||
result = new short[numSamples * NumChannels];
|
||||
for (int i = 0; i < numSamples * NumChannels; i++)
|
||||
for (var i = 0; i < numSamples * NumChannels; i++)
|
||||
{
|
||||
result[i] = reader.ReadInt16();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = new short[numSamples];
|
||||
for (int i = 0; i < numSamples; i++)
|
||||
for (var i = 0; i < numSamples; i++)
|
||||
{
|
||||
_File.Seek((channel - 1) * 2, SeekOrigin.Current);
|
||||
result[i] = reader.ReadInt16();
|
||||
_File.Seek((NumChannels - channel) * 2, SeekOrigin.Current);
|
||||
}
|
||||
}
|
||||
|
||||
NumSamplesLeft -= numSamples;
|
||||
}
|
||||
catch (EndOfStreamException e)
|
||||
@@ -206,15 +262,19 @@ namespace VocaluxeLib.Utils
|
||||
Console.WriteLine("Error: " + e);
|
||||
return null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] GetNextSamples16BitAsBytes(int numSamples, int channel = 0)
|
||||
{
|
||||
short[] samples = GetNextSamples16Bit(numSamples, channel);
|
||||
var samples = GetNextSamples16Bit(numSamples, channel);
|
||||
if (samples == null)
|
||||
{
|
||||
return null;
|
||||
byte[] samplesByte = new byte[samples.Length * 2];
|
||||
}
|
||||
|
||||
var samplesByte = new byte[samples.Length * 2];
|
||||
Buffer.BlockCopy(samples, 0, samplesByte, 0, samplesByte.Length);
|
||||
return samplesByte;
|
||||
}
|
||||
@@ -222,19 +282,25 @@ namespace VocaluxeLib.Utils
|
||||
public bool WriteSamples(byte[] samples, int channel = 0)
|
||||
{
|
||||
if (!_IsWritable || channel < 0 || channel > NumChannels || BitsPerSample != 8)
|
||||
{
|
||||
return false;
|
||||
BinaryWriter writer = new BinaryWriter(_File);
|
||||
}
|
||||
|
||||
var writer = new BinaryWriter(_File);
|
||||
if (channel == 0 || NumChannels == 1)
|
||||
{
|
||||
writer.Write(samples);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (byte sample in samples)
|
||||
foreach (var sample in samples)
|
||||
{
|
||||
_File.Seek(channel - 1, SeekOrigin.Current);
|
||||
writer.Write(sample);
|
||||
_File.Seek(NumChannels - channel, SeekOrigin.Current);
|
||||
}
|
||||
}
|
||||
|
||||
DataSize += samples.Length;
|
||||
return true;
|
||||
}
|
||||
@@ -242,22 +308,28 @@ namespace VocaluxeLib.Utils
|
||||
public bool Write16BitSamples(short[] samples, int channel = 0)
|
||||
{
|
||||
if (!_IsWritable || channel < 0 || channel > NumChannels || BitsPerSample != 16)
|
||||
{
|
||||
return false;
|
||||
BinaryWriter writer = new BinaryWriter(_File);
|
||||
}
|
||||
|
||||
var writer = new BinaryWriter(_File);
|
||||
if (channel == 0 || NumChannels == 1)
|
||||
{
|
||||
foreach (short sample in samples)
|
||||
foreach (var sample in samples)
|
||||
{
|
||||
writer.Write(sample);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (short sample in samples)
|
||||
foreach (var sample in samples)
|
||||
{
|
||||
_File.Seek((channel - 1) * 2, SeekOrigin.Current);
|
||||
writer.Write(sample);
|
||||
_File.Seek((NumChannels - channel) * 2, SeekOrigin.Current);
|
||||
}
|
||||
}
|
||||
|
||||
DataSize += samples.Length * 2;
|
||||
return true;
|
||||
}
|
||||
@@ -265,8 +337,11 @@ namespace VocaluxeLib.Utils
|
||||
public bool Write16BitSamples(byte[] samples, int channel = 0)
|
||||
{
|
||||
if (samples.Length == 0)
|
||||
{
|
||||
return true;
|
||||
short[] samplesShort = new short[samples.Length / 2];
|
||||
}
|
||||
|
||||
var samplesShort = new short[samples.Length / 2];
|
||||
Buffer.BlockCopy(samples, 0, samplesShort, 0, samples.Length);
|
||||
return Write16BitSamples(samplesShort, channel);
|
||||
}
|
||||
@@ -274,8 +349,11 @@ namespace VocaluxeLib.Utils
|
||||
private void _WriteHeader()
|
||||
{
|
||||
if (!IsOpen || !_IsWritable)
|
||||
{
|
||||
return;
|
||||
BinaryWriter writer = new BinaryWriter(_File);
|
||||
}
|
||||
|
||||
var writer = new BinaryWriter(_File);
|
||||
writer.Write(Encoding.ASCII.GetBytes("RIFF"));
|
||||
writer.Write(0);
|
||||
writer.Write(Encoding.ASCII.GetBytes("WAVE"));
|
||||
@@ -285,7 +363,7 @@ namespace VocaluxeLib.Utils
|
||||
writer.Write(NumChannels); // Channels
|
||||
writer.Write(SampleRate); // Sample rate
|
||||
writer.Write(SampleRate * NumChannels * (BitsPerSample / 8)); // Average bytes per second
|
||||
writer.Write((short)((BitsPerSample / 8) * NumChannels)); // block align
|
||||
writer.Write((short)(BitsPerSample / 8 * NumChannels)); // block align
|
||||
writer.Write(BitsPerSample); // bits per sample
|
||||
writer.Write(Encoding.ASCII.GetBytes("data"));
|
||||
writer.Write(DataSize);
|
||||
@@ -299,12 +377,16 @@ namespace VocaluxeLib.Utils
|
||||
{
|
||||
do
|
||||
{
|
||||
string curChunkId = Encoding.ASCII.GetString(reader.ReadBytes(4));
|
||||
int chunkSize = reader.ReadInt32();
|
||||
var curChunkId = Encoding.ASCII.GetString(reader.ReadBytes(4));
|
||||
var chunkSize = reader.ReadInt32();
|
||||
if (curChunkId == chunkId)
|
||||
{
|
||||
return chunkSize;
|
||||
}
|
||||
|
||||
reader.BaseStream.Seek(chunkSize, SeekOrigin.Current);
|
||||
} while (reader.BaseStream.Position < reader.BaseStream.Length);
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,14 +40,25 @@ namespace VocaluxeLib.Utils.Combinatorics
|
||||
public static int Count(int n, int k)
|
||||
{
|
||||
if (n < 0 || k < 0)
|
||||
{
|
||||
throw new ArgumentException("Parameters must not be negative");
|
||||
}
|
||||
|
||||
if (n < k)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Try to optimize because (n k) = (n n-k)
|
||||
if (n - k < k)
|
||||
{
|
||||
k = n - k;
|
||||
}
|
||||
|
||||
if (k == 0) // or k == n but both are the same after the optimization above
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
long result = n;
|
||||
for (long i = 1; i < k; i++)
|
||||
@@ -57,6 +68,7 @@ namespace VocaluxeLib.Utils.Combinatorics
|
||||
result = result * (n - i) / (i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return (int)result;
|
||||
}
|
||||
|
||||
@@ -75,7 +87,7 @@ namespace VocaluxeLib.Utils.Combinatorics
|
||||
/// <returns></returns>
|
||||
public List<List<T>> GetAll()
|
||||
{
|
||||
List<List<T>> result = new List<List<T>>(Count());
|
||||
var result = new List<List<T>>(Count());
|
||||
result.AddRange(this);
|
||||
return result;
|
||||
}
|
||||
@@ -105,32 +117,46 @@ namespace VocaluxeLib.Utils.Combinatorics
|
||||
Debug.Assert(_N >= 0 && _K >= 0);
|
||||
}
|
||||
|
||||
public void Dispose() {}
|
||||
public void Dispose() { }
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (_N < _K)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_CurSet == null)
|
||||
{
|
||||
_CurSet = new int[_K];
|
||||
for (int i = 0; i < _K; i++)
|
||||
for (var i = 0; i < _K; i++)
|
||||
{
|
||||
_CurSet[i] = i;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if we reached the end
|
||||
if (_K == 0 || _CurSet[0] == _N - _K)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Search for element to increment
|
||||
int idx = _K - 1;
|
||||
var idx = _K - 1;
|
||||
while (idx > 0 && _CurSet[idx] == _N - _K + idx)
|
||||
{
|
||||
idx--;
|
||||
}
|
||||
|
||||
_CurSet[idx]++;
|
||||
// Increment all elements "right" of this to 1+their left neighbour
|
||||
for (int j = idx; j < _K - 1; j++)
|
||||
for (var j = idx; j < _K - 1; j++)
|
||||
{
|
||||
_CurSet[j + 1] = _CurSet[j] + 1;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -143,7 +169,7 @@ namespace VocaluxeLib.Utils.Combinatorics
|
||||
{
|
||||
get
|
||||
{
|
||||
List<T> result = new List<T>(_K);
|
||||
var result = new List<T>(_K);
|
||||
result.AddRange(_CurSet.Select(i => _Parent._Values[i]));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VocaluxeLib.Utils
|
||||
{
|
||||
@@ -27,9 +23,15 @@ namespace VocaluxeLib.Utils
|
||||
public CObjectPool(Func<T> objectGenerator, int poolSize)
|
||||
{
|
||||
if (objectGenerator == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(objectGenerator));
|
||||
}
|
||||
|
||||
if (poolSize <= 0)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(poolSize));
|
||||
}
|
||||
|
||||
_Objects = new ConcurrentBag<T>();
|
||||
_ObjectGenerator = objectGenerator;
|
||||
_Poolsize = poolSize;
|
||||
@@ -42,7 +44,11 @@ namespace VocaluxeLib.Utils
|
||||
public T GetObject()
|
||||
{
|
||||
T item;
|
||||
if (_Objects.TryTake(out item)) return item;
|
||||
if (_Objects.TryTake(out item))
|
||||
{
|
||||
return item;
|
||||
}
|
||||
|
||||
return _ObjectGenerator();
|
||||
}
|
||||
|
||||
@@ -52,10 +58,14 @@ namespace VocaluxeLib.Utils
|
||||
/// <param name="item">The object instance that is given back to the pool.</param>
|
||||
public void PutObject(T item)
|
||||
{
|
||||
if(_Objects.Count < _Poolsize)
|
||||
if (_Objects.Count < _Poolsize)
|
||||
{
|
||||
_Objects.Add(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
(item as IDisposable)?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,25 +35,32 @@ namespace VocaluxeLib.Utils.Player
|
||||
set
|
||||
{
|
||||
if (_VideoEnabled == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_VideoEnabled = value;
|
||||
if (_VideoEnabled)
|
||||
{
|
||||
_LoadVideo();
|
||||
}
|
||||
else
|
||||
{
|
||||
_CloseVideo();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int SongID
|
||||
public int SongId
|
||||
{
|
||||
get { return _Song == null ? -1 : _Song.ID; }
|
||||
get { return _Song == null ? -1 : _Song.Id; }
|
||||
}
|
||||
|
||||
public bool SongHasVideo
|
||||
{
|
||||
get
|
||||
{
|
||||
return _Song != null && !String.IsNullOrEmpty(_Song.Folder) && !String.IsNullOrEmpty(_Song.VideoFileName) &&
|
||||
return _Song != null && !string.IsNullOrEmpty(_Song.Folder) && !string.IsNullOrEmpty(_Song.VideoFileName) &&
|
||||
File.Exists(Path.Combine(_Song.Folder, _Song.VideoFileName));
|
||||
}
|
||||
}
|
||||
@@ -62,8 +69,11 @@ namespace VocaluxeLib.Utils.Player
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_Song != null && !String.IsNullOrEmpty(_Song.Artist) && !String.IsNullOrEmpty(_Song.Title))
|
||||
if (_Song != null && !string.IsNullOrEmpty(_Song.Artist) && !string.IsNullOrEmpty(_Song.Title))
|
||||
{
|
||||
return _Song.Artist + " - " + _Song.Title;
|
||||
}
|
||||
|
||||
return base.ArtistAndTitle;
|
||||
}
|
||||
}
|
||||
@@ -73,30 +83,39 @@ namespace VocaluxeLib.Utils.Player
|
||||
get { return _Song == null ? CBase.Cover.GetNoCover() : _Song.CoverTextureBig; }
|
||||
}
|
||||
|
||||
public CSongPlayer(bool loop = false) : base(loop) {}
|
||||
public CSongPlayer(bool loop = false) : base(loop) { }
|
||||
|
||||
public CTextureRef GetVideoTexture()
|
||||
{
|
||||
if (_Video == null || _Song == null)
|
||||
{
|
||||
return null;
|
||||
if (CBase.Video.GetFrame(_Video, CBase.Sound.GetPosition(_StreamID)))
|
||||
}
|
||||
|
||||
if (CBase.Video.GetFrame(_Video, CBase.Sound.GetPosition(_StreamId)))
|
||||
{
|
||||
if (_VideoFading != null)
|
||||
{
|
||||
bool finished;
|
||||
_Video.Texture.Color.A = _VideoFading.GetValue(out finished);
|
||||
if (finished)
|
||||
{
|
||||
_VideoFading = null;
|
||||
}
|
||||
}
|
||||
|
||||
return _Video.Texture;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Load(CSong song, float position = 0f, bool autoplay = false)
|
||||
{
|
||||
if (song == null)
|
||||
{
|
||||
throw new ArgumentNullException("song");
|
||||
}
|
||||
|
||||
Load(song.GetMP3(), position, autoplay);
|
||||
_Song = song;
|
||||
@@ -106,15 +125,21 @@ namespace VocaluxeLib.Utils.Player
|
||||
private void _LoadVideo()
|
||||
{
|
||||
if (_Song == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_Video != null || !SongHasVideo)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string videoFilePath = Path.Combine(_Song.Folder, _Song.VideoFileName);
|
||||
var videoFilePath = Path.Combine(_Song.Folder, _Song.VideoFileName);
|
||||
_Video = CBase.Video.Load(videoFilePath);
|
||||
if (_Video == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_VideoFading = new CFading(0f, 1f, 3f);
|
||||
|
||||
@@ -124,48 +149,64 @@ namespace VocaluxeLib.Utils.Player
|
||||
CBase.Video.Resume(_Video);
|
||||
}
|
||||
else
|
||||
{
|
||||
CBase.Video.Skip(_Video, 0f, _Song.VideoGap);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool Play()
|
||||
{
|
||||
if (!base.Play())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_Video != null)
|
||||
{
|
||||
CBase.Video.Skip(_Video, Position, _Song.VideoGap);
|
||||
CBase.Video.Resume(_Video);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool Pause()
|
||||
{
|
||||
if (!base.Pause())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_Video != null)
|
||||
{
|
||||
CBase.Video.Pause(_Video);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool Stop()
|
||||
{
|
||||
if (!base.Stop())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_Video != null)
|
||||
{
|
||||
CBase.Video.Pause(_Video);
|
||||
CBase.Video.Skip(_Video, 0f, _Song.VideoGap);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void _CloseVideo()
|
||||
{
|
||||
if (_Video != null)
|
||||
{
|
||||
CBase.Video.Close(ref _Video);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
@@ -176,4 +217,4 @@ namespace VocaluxeLib.Utils.Player
|
||||
_CloseVideo();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ namespace VocaluxeLib.Utils.Player
|
||||
{
|
||||
public class CSoundPlayer
|
||||
{
|
||||
protected int _StreamID = -1;
|
||||
protected int _StreamId = -1;
|
||||
protected readonly float _FadeTime = CBase.Settings.GetSoundPlayerFadeTime();
|
||||
public string FilePath { get; private set; }
|
||||
|
||||
@@ -35,27 +35,30 @@ namespace VocaluxeLib.Utils.Player
|
||||
set
|
||||
{
|
||||
if (!SoundLoaded)
|
||||
{
|
||||
return;
|
||||
CBase.Sound.SetPosition(_StreamID, value);
|
||||
}
|
||||
|
||||
CBase.Sound.SetPosition(_StreamId, value);
|
||||
}
|
||||
get { return !SoundLoaded ? -1 : CBase.Sound.GetPosition(_StreamID); }
|
||||
get { return !SoundLoaded ? -1 : CBase.Sound.GetPosition(_StreamId); }
|
||||
}
|
||||
|
||||
public float Length
|
||||
{
|
||||
get { return !SoundLoaded ? -1 : CBase.Sound.GetLength(_StreamID); }
|
||||
get { return !SoundLoaded ? -1 : CBase.Sound.GetLength(_StreamId); }
|
||||
}
|
||||
|
||||
public bool IsPlaying { get; private set; }
|
||||
|
||||
public bool IsFinished
|
||||
{
|
||||
get { return !Loop && (CBase.Sound.IsFinished(_StreamID) || !IsPlaying); }
|
||||
get { return !Loop && (CBase.Sound.IsFinished(_StreamId) || !IsPlaying); }
|
||||
}
|
||||
|
||||
public bool SoundLoaded
|
||||
{
|
||||
get { return _StreamID != -1; }
|
||||
get { return _StreamId != -1; }
|
||||
}
|
||||
|
||||
public virtual string ArtistAndTitle
|
||||
@@ -72,14 +75,22 @@ namespace VocaluxeLib.Utils.Player
|
||||
{
|
||||
Close();
|
||||
|
||||
_StreamID = CBase.Sound.Load(file, false, true);
|
||||
if (_StreamID < 0)
|
||||
_StreamId = CBase.Sound.Load(file, false, true);
|
||||
if (_StreamId < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FilePath = file;
|
||||
if (position > 0f)
|
||||
{
|
||||
Position = position;
|
||||
}
|
||||
|
||||
if (autoplay)
|
||||
{
|
||||
Play();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -89,11 +100,13 @@ namespace VocaluxeLib.Utils.Player
|
||||
public virtual bool Play()
|
||||
{
|
||||
if (!SoundLoaded || IsPlaying)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
CBase.Sound.SetStreamVolume(_StreamID, 0);
|
||||
CBase.Sound.Fade(_StreamID, 100, _FadeTime);
|
||||
CBase.Sound.Play(_StreamID);
|
||||
CBase.Sound.SetStreamVolume(_StreamId, 0);
|
||||
CBase.Sound.Fade(_StreamId, 100, _FadeTime);
|
||||
CBase.Sound.Play(_StreamId);
|
||||
IsPlaying = true;
|
||||
return true;
|
||||
}
|
||||
@@ -104,10 +117,12 @@ namespace VocaluxeLib.Utils.Player
|
||||
/// <returns>True if state changed, false if nothing loaded or already paused</returns>
|
||||
public virtual bool Pause()
|
||||
{
|
||||
if (!SoundLoaded || CBase.Sound.IsPaused(_StreamID))
|
||||
if (!SoundLoaded || CBase.Sound.IsPaused(_StreamId))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
CBase.Sound.Fade(_StreamID, 0, _FadeTime, EStreamAction.Pause);
|
||||
CBase.Sound.Fade(_StreamId, 0, _FadeTime, EStreamAction.Pause);
|
||||
IsPlaying = false;
|
||||
return true;
|
||||
}
|
||||
@@ -119,9 +134,11 @@ namespace VocaluxeLib.Utils.Player
|
||||
public virtual bool Stop()
|
||||
{
|
||||
if (!SoundLoaded)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
CBase.Sound.Fade(_StreamID, 0, _FadeTime, EStreamAction.Stop);
|
||||
CBase.Sound.Fade(_StreamId, 0, _FadeTime, EStreamAction.Stop);
|
||||
IsPlaying = false;
|
||||
return true;
|
||||
}
|
||||
@@ -129,10 +146,12 @@ namespace VocaluxeLib.Utils.Player
|
||||
public virtual void Close()
|
||||
{
|
||||
if (!SoundLoaded)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CBase.Sound.Fade(_StreamID, 0, _FadeTime, EStreamAction.Close);
|
||||
_StreamID = -1;
|
||||
CBase.Sound.Fade(_StreamId, 0, _FadeTime, EStreamAction.Close);
|
||||
_StreamId = -1;
|
||||
FilePath = "";
|
||||
IsPlaying = false;
|
||||
}
|
||||
@@ -140,9 +159,11 @@ namespace VocaluxeLib.Utils.Player
|
||||
public void Update()
|
||||
{
|
||||
if (!IsPlaying)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool finished = CBase.Sound.IsFinished(_StreamID);
|
||||
var finished = CBase.Sound.IsFinished(_StreamId);
|
||||
if (Loop)
|
||||
{
|
||||
if (finished)
|
||||
@@ -151,14 +172,17 @@ namespace VocaluxeLib.Utils.Player
|
||||
Stop();
|
||||
Play();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
float len = CBase.Sound.GetLength(_StreamID);
|
||||
float timeToPlay = (len > 0f) ? len - CBase.Sound.GetPosition(_StreamID) : _FadeTime + 1f;
|
||||
var len = CBase.Sound.GetLength(_StreamId);
|
||||
var timeToPlay = len > 0f ? len - CBase.Sound.GetPosition(_StreamId) : _FadeTime + 1f;
|
||||
|
||||
if (timeToPlay <= _FadeTime || finished)
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user