Fix volume setting

This commit is contained in:
Flamefire committed 2014-03-16 19:44:25 +01:00
1 parent 658a3068b0
commit 374d075bf6
3 files changed
+37 -82

No files matched your search

@@ -148,7 +148,8 @@ namespace Vocaluxe.Lib.Sound.Playback.GstreamerSharp
public void Fade(float targetVolume, float seconds) public void Fade(float targetVolume, float seconds)
{ {
_Fading = new CFading(_Volume, targetVolume, seconds); targetVolume.Clamp(0f, 100f);
_Fading = new CFading(_Volume, targetVolume / 100f, seconds);
_AfterFadeAction = EStreamAction.Nothing; _AfterFadeAction = EStreamAction.Nothing;
} }
@@ -175,19 +176,26 @@ namespace Vocaluxe.Lib.Sound.Playback.GstreamerSharp
get { return _Volume * 100f; } get { return _Volume * 100f; }
set set
{ {
value.Clamp(0f, 100f);
_Volume = value / 100f; _Volume = value / 100f;
if (_Element != null) _SetElementVolume();
_Element["volume"] = (_Volume * _MaxVolume);
} }
} }
private void _SetElementVolume()
{
if (_Element != null)
_Element["volume"] = (_Volume * _MaxVolume);
}
public float MaxVolume public float MaxVolume
{ {
get { return _MaxVolume * 100f; } get { return _MaxVolume * 100f; }
set set
{ {
value.Clamp(0f, 100f);
_MaxVolume = value / 100f; _MaxVolume = value / 100f;
Volume = _Volume; _SetElementVolume();
} }
} }
@@ -251,7 +259,8 @@ namespace Vocaluxe.Lib.Sound.Playback.GstreamerSharp
if (_Fading != null) if (_Fading != null)
{ {
bool finished; bool finished;
Volume = _Fading.GetValue(out finished); _Volume = _Fading.GetValue(out finished);
_SetElementVolume();
if (finished) if (finished)
{ {
switch (_AfterFadeAction) switch (_AfterFadeAction)
@@ -57,19 +57,13 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
private bool _SetSkip; private bool _SetSkip;
private bool _Terminated; private bool _Terminated;
private readonly Thread _DecoderThread; private Thread _DecoderThread;
private AutoResetEvent _EventDecode = new AutoResetEvent(false); private AutoResetEvent _EventDecode = new AutoResetEvent(false);
private readonly Object _MutexData = new Object(); private readonly Object _MutexData = new Object();
private readonly Object _MutexSyncSignals = new Object(); private readonly Object _MutexSyncSignals = new Object();
public COpenAlStream()
{
_Initialized = false;
_DecoderThread = new Thread(_Execute) {Name = "OpenAL Decode"};
}
public void Free(Closeproc closeProc, int streamID, Object closeMutex) public void Free(Closeproc closeProc, int streamID, Object closeMutex)
{ {
_Closeproc = closeProc; _Closeproc = closeProc;
@@ -107,12 +101,8 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
{ {
lock (_MutexData) lock (_MutexData)
{ {
value.Clamp(0f, 100f);
_Volume = value / 100f; _Volume = value / 100f;
if (_Volume < 0f)
_Volume = 0f;
if (_Volume > 1f)
_Volume = 1f;
} }
} }
} }
@@ -124,12 +114,8 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
{ {
lock (_MutexData) lock (_MutexData)
{ {
value.Clamp(0f, 100f);
_VolumeMax = value / 100f; _VolumeMax = value / 100f;
if (_VolumeMax < 0f)
_VolumeMax = 0f;
if (_VolumeMax > 1f)
_VolumeMax = 1f;
} }
} }
} }
@@ -170,7 +156,8 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
public void Fade(float targetVolume, float fadeTime) public void Fade(float targetVolume, float fadeTime)
{ {
_Fading = new CFading(_Volume, targetVolume, fadeTime); targetVolume.Clamp(0f, 100f);
_Fading = new CFading(_Volume, targetVolume / 100f, fadeTime);
_AfterFadeAction = EStreamAction.Nothing; _AfterFadeAction = EStreamAction.Nothing;
} }
@@ -251,8 +238,7 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
_FileOpened = true; _FileOpened = true;
_Data = new CRingBuffer(_Bufsize); _Data = new CRingBuffer(_Bufsize);
_NoMoreData = false; _NoMoreData = false;
_DecoderThread.Priority = ThreadPriority.Normal; _DecoderThread = new Thread(_Execute) {Priority = ThreadPriority.Normal, Name = Path.GetFileName(fileName)};
_DecoderThread.Name = Path.GetFileName(fileName);
_DecoderThread.Start(); _DecoderThread.Start();
return stream.Handle; return stream.Handle;
@@ -428,7 +414,7 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
{ {
AL.GetSource(_Source, ALGetSourcei.BuffersProcessed, out processedCount); AL.GetSource(_Source, ALGetSourcei.BuffersProcessed, out processedCount);
doit = false; doit = false;
Console.WriteLine("Buffers Processed on Stream " + _Source + " = " + processedCount); //Console.WriteLine("Buffers Processed on Stream " + _Source + " = " + processedCount);
if (processedCount < 1) if (processedCount < 1)
return; return;
} }
@@ -443,29 +429,15 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
{ {
_Data.Read(buf); _Data.Read(buf);
//We want to scale all values. No matter how many channels we have (_ByteCount=2 or 4) we have short values
var b = new byte[2]; //So just process 2 bytes a time
for (int i = 0; i < buf.Length; i += _ByteCount) for (int i = 0; i < buf.Length; i += 2)
{ {
b[0] = buf[i]; byte[] b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(buf, i) * _Volume * _VolumeMax));
b[1] = buf[i + 1];
b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(b, 0) * _Volume * _VolumeMax));
buf[i] = b[0]; buf[i] = b[0];
buf[i + 1] = b[1]; buf[i + 1] = b[1];
if (_ByteCount == 4)
{
b[0] = buf[i + 2];
b[1] = buf[i + 3];
b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(b, 0) * _Volume * _VolumeMax));
buf[i + 2] = b[0];
buf[i + 3] = b[1];
}
} }
int buffer; int buffer;
if (!doit) if (!doit)
buffer = AL.SourceUnqueueBuffer(_Source); buffer = AL.SourceUnqueueBuffer(_Source);
@@ -479,7 +451,6 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
{ {
ALFormat alFormat = (_Format.ChannelCount == 2) ? ALFormat.Stereo16 : ALFormat.Mono16; ALFormat alFormat = (_Format.ChannelCount == 2) ? ALFormat.Stereo16 : ALFormat.Mono16;
AL.BufferData(buffer, alFormat, buf, buf.Length, _Format.SamplesPerSecond); AL.BufferData(buffer, alFormat, buf, buf.Length, _Format.SamplesPerSecond);
Console.WriteLine("Write to Buffer: " + buffer);
AL.SourceQueueBuffer(_Source, buffer); AL.SourceQueueBuffer(_Source, buffer);
} }
} }
@@ -491,8 +462,7 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
} }
_CurrentTime = _TimeCode - _Data.BytesNotRead / _BytesPerSecond - 0.1f; _CurrentTime = _TimeCode - _Data.BytesNotRead / _BytesPerSecond - 0.1f;
_Timer.Reset(); _Timer.Restart();
_Timer.Start();
} }
public void Dispose() public void Dispose()
@@ -54,7 +54,7 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
private bool _SetSkip; private bool _SetSkip;
private bool _Terminated; private bool _Terminated;
private readonly Thread _DecoderThread; private Thread _DecoderThread;
private AutoResetEvent _EventDecode = new AutoResetEvent(false); private AutoResetEvent _EventDecode = new AutoResetEvent(false);
@@ -64,7 +64,6 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
public CPortAudioStream() public CPortAudioStream()
{ {
_SyncTimer = new CSyncTimer(0f, 1f, 0.02f); _SyncTimer = new CSyncTimer(0f, 1f, 0.02f);
_DecoderThread = new Thread(_Execute) {Name = "PortAudio Decode"};
} }
~CPortAudioStream() ~CPortAudioStream()
@@ -108,12 +107,8 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
{ {
lock (_LockData) lock (_LockData)
{ {
value.Clamp(0f, 100f);
_Volume = value / 100f; _Volume = value / 100f;
if (_Volume < 0f)
_Volume = 0f;
if (_Volume > 1f)
_Volume = 1f;
} }
} }
} }
@@ -125,12 +120,8 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
{ {
lock (_LockData) lock (_LockData)
{ {
value.Clamp(0f, 100f);
_VolumeMax = value / 100f; _VolumeMax = value / 100f;
if (_VolumeMax < 0f)
_VolumeMax = 0f;
if (_VolumeMax > 1f)
_VolumeMax = 1f;
} }
} }
} }
@@ -170,13 +161,15 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
public void Fade(float targetVolume, float fadeTime) public void Fade(float targetVolume, float fadeTime)
{ {
_Fading = new CFading(_Volume, targetVolume, fadeTime); targetVolume = targetVolume.Clamp(0f, 100f);
_Fading = new CFading(_Volume, targetVolume / 100f, fadeTime);
_AfterFadeAction = EStreamAction.Nothing; _AfterFadeAction = EStreamAction.Nothing;
} }
public void FadeAndPause(float targetVolume, float fadeTime) public void FadeAndPause(float targetVolume, float fadeTime)
{ {
Fade(targetVolume, fadeTime); Fade(targetVolume, fadeTime);
_AfterFadeAction = EStreamAction.Pause;
} }
public void FadeAndClose(float targetVolume, float fadeTime, Closeproc closeProc, int streamID) public void FadeAndClose(float targetVolume, float fadeTime, Closeproc closeProc, int streamID)
@@ -228,9 +221,6 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
if (!File.Exists(fileName)) if (!File.Exists(fileName))
return -1; return -1;
if (_FileOpened)
return -1;
_Decoder = new CAudioDecoderFFmpeg(); _Decoder = new CAudioDecoderFFmpeg();
_Decoder.Init(); _Decoder.Init();
@@ -310,8 +300,7 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
_FileOpened = true; _FileOpened = true;
_Data = new CRingBuffer(_Bufsize); _Data = new CRingBuffer(_Bufsize);
_NoMoreData = false; _NoMoreData = false;
_DecoderThread.Priority = ThreadPriority.Normal; _DecoderThread = new Thread(_Execute) {Priority = ThreadPriority.Normal, Name = Path.GetFileName(fileName)};
_DecoderThread.Name = Path.GetFileName(fileName);
_DecoderThread.Start(); _DecoderThread.Start();
return stream.Handle; return stream.Handle;
@@ -490,26 +479,13 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
if (_NoMoreData || _Data.BytesNotRead >= buf.Length) if (_NoMoreData || _Data.BytesNotRead >= buf.Length)
{ {
_Data.Read(buf); _Data.Read(buf);
//We want to scale all values. No matter how many channels we have (_ByteCount=2 or 4) we have short values
var b = new byte[2]; //So just process 2 bytes a time
for (int i = 0; i < buf.Length; i += _ByteCount) for (int i = 0; i < buf.Length; i += 2)
{ {
b[0] = buf[i]; byte[] b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(buf, i) * _Volume * _VolumeMax));
b[1] = buf[i + 1];
b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(b, 0) * _Volume * _VolumeMax));
buf[i] = b[0]; buf[i] = b[0];
buf[i + 1] = b[1]; buf[i + 1] = b[1];
if (_ByteCount == 4)
{
b[0] = buf[i + 2];
b[1] = buf[i + 3];
b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(b, 0) * _Volume * _VolumeMax));
buf[i + 2] = b[0];
buf[i + 3] = b[1];
}
} }
} }