initialize portaudio playback only once

This commit is contained in:
Alexander Eckart committed 2013-01-06 15:07:08 +01:00
1 parent 9bcf209d49
commit 57d301018e
1 file changed
+36 -8
+36 -8
View File
@@ -388,7 +388,9 @@ namespace Vocaluxe.Lib.Sound
const long BEGINREFILL = 800000L; const long BEGINREFILL = 800000L;
private CSyncTimer _SyncTimer; private CSyncTimer _SyncTimer;
private bool _Initialized; private static bool _Initialized = false;
private static int _NumStreams = 0;
private static Object _Mutex = new object();
private int _ByteCount = 4; private int _ByteCount = 4;
private float _Volume = 1f; private float _Volume = 1f;
private float _VolumeMax = 1f; private float _VolumeMax = 1f;
@@ -403,8 +405,8 @@ namespace Vocaluxe.Lib.Sound
private bool _fading = false; private bool _fading = false;
private PortAudio.PaHostApiInfo _apiInfo; private static PortAudio.PaHostApiInfo _apiInfo;
private PortAudio.PaDeviceInfo _outputDeviceInfo; private static PortAudio.PaDeviceInfo _outputDeviceInfo;
private IntPtr _Ptr = new IntPtr(0); private IntPtr _Ptr = new IntPtr(0);
private Stopwatch _FadeTimer = new Stopwatch(); private Stopwatch _FadeTimer = new Stopwatch();
@@ -446,7 +448,6 @@ namespace Vocaluxe.Lib.Sound
public PortAudioStream() public PortAudioStream()
{ {
_Initialized = false;
_SyncTimer = new CSyncTimer(0f, 1f, 0.02f); _SyncTimer = new CSyncTimer(0f, 1f, 0.02f);
_DecoderThread = new Thread(Execute); _DecoderThread = new Thread(Execute);
} }
@@ -578,12 +579,18 @@ namespace Vocaluxe.Lib.Sound
{ {
Paused = false; Paused = false;
_pauseStreamAfterFade = false; _pauseStreamAfterFade = false;
lock (_Mutex)
{
errorCheck("StartStream", PortAudio.Pa_StartStream(_Ptr)); errorCheck("StartStream", PortAudio.Pa_StartStream(_Ptr));
} }
}
public void Stop() public void Stop()
{ {
errorCheck("StartStream", PortAudio.Pa_StopStream(_Ptr)); lock (_Mutex)
{
errorCheck("StopStream (playback)", PortAudio.Pa_StopStream(_Ptr));
}
Skip(0f); Skip(0f);
} }
@@ -608,19 +615,25 @@ namespace Vocaluxe.Lib.Sound
_Decoder.Init(); _Decoder.Init();
try try
{
lock (_Mutex)
{
if (!_Initialized)
{ {
if (errorCheck("Initialize", PortAudio.Pa_Initialize())) if (errorCheck("Initialize", PortAudio.Pa_Initialize()))
return -1; return -1;
_Initialized = true; _Initialized = true;
int hostApi = apiSelect(); int hostApi = apiSelect();
_apiInfo = PortAudio.Pa_GetHostApiInfo(hostApi); _apiInfo = PortAudio.Pa_GetHostApiInfo(hostApi);
_outputDeviceInfo = PortAudio.Pa_GetDeviceInfo(_apiInfo.defaultOutputDevice); _outputDeviceInfo = PortAudio.Pa_GetDeviceInfo(_apiInfo.defaultOutputDevice);
_paStreamCallback = new PortAudio.PaStreamCallbackDelegate(_PaStreamCallback);
if (_outputDeviceInfo.defaultLowOutputLatency < 0.1) if (_outputDeviceInfo.defaultLowOutputLatency < 0.1)
_outputDeviceInfo.defaultLowOutputLatency = 0.1; _outputDeviceInfo.defaultLowOutputLatency = 0.1;
} }
}
_paStreamCallback = new PortAudio.PaStreamCallbackDelegate(_PaStreamCallback);
}
catch (Exception) catch (Exception)
{ {
@@ -634,6 +647,9 @@ namespace Vocaluxe.Lib.Sound
_Duration = _Decoder.GetLength(); _Duration = _Decoder.GetLength();
FormatInfo format = _Decoder.GetFormatInfo(); FormatInfo format = _Decoder.GetFormatInfo();
if (format.SamplesPerSecond == 0)
return -1;
_ByteCount = 2 * format.ChannelCount; _ByteCount = 2 * format.ChannelCount;
_BytesPerSecond = format.SamplesPerSecond * _ByteCount; _BytesPerSecond = format.SamplesPerSecond * _ByteCount;
_CurrentTime = 0f; _CurrentTime = 0f;
@@ -650,6 +666,8 @@ namespace Vocaluxe.Lib.Sound
outputParams.suggestedLatency = _outputDeviceInfo.defaultLowOutputLatency; outputParams.suggestedLatency = _outputDeviceInfo.defaultLowOutputLatency;
uint bufsize = (uint)CConfig.AudioBufferSize; uint bufsize = (uint)CConfig.AudioBufferSize;
lock (_Mutex)
{
errorCheck("OpenDefaultStream (playback)", PortAudio.Pa_OpenStream( errorCheck("OpenDefaultStream (playback)", PortAudio.Pa_OpenStream(
out _Ptr, out _Ptr,
IntPtr.Zero, IntPtr.Zero,
@@ -659,11 +677,13 @@ namespace Vocaluxe.Lib.Sound
PortAudio.PaStreamFlags.paNoFlag, PortAudio.PaStreamFlags.paNoFlag,
_paStreamCallback, _paStreamCallback,
data)); data));
}
stream.handle = _Ptr.ToInt32(); stream.handle = _Ptr.ToInt32();
if (stream.handle != 0) if (stream.handle != 0)
{ {
_NumStreams++;
_Paused = true; _Paused = true;
_waiting = true; _waiting = true;
_FileOpened = true; _FileOpened = true;
@@ -808,7 +828,15 @@ namespace Vocaluxe.Lib.Sound
if (_Initialized) if (_Initialized)
{ {
Stop(); Stop();
_NumStreams--;
if (_NumStreams == 0)
{
lock (_Mutex)
{
PortAudio.Pa_Terminate(); PortAudio.Pa_Terminate();
_Initialized = false;
}
}
} }
_Closeproc(_StreamID); _Closeproc(_StreamID);