Change CPortAudioCommon to be static class only to allow inheritance in record and playback classes (later)

This commit is contained in:
Flamefire committed 2014-03-20 20:23:47 +01:00
1 parent 2f1b2d604a
commit 9054f529a0
4 files changed
+39 -57

No files matched your search

+1 -19
View File
@@ -38,9 +38,6 @@ using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Microsoft.Reliability", "CA2000:Objekte verwerfen, bevor Bereich verloren geht", Scope = "member", Target = "Vocaluxe.Lib.Draw.CDirect3D.#CopyScreen()") [assembly: SuppressMessage("Microsoft.Reliability", "CA2000:Objekte verwerfen, bevor Bereich verloren geht", Scope = "member", Target = "Vocaluxe.Lib.Draw.CDirect3D.#CopyScreen()")
] ]
[assembly: SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue", Scope = "type", Target = "Vocaluxe.Lib.Input.WiiMote.InputReport")] [assembly: SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue", Scope = "type", Target = "Vocaluxe.Lib.Input.WiiMote.InputReport")]
[assembly: SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue", Scope = "type", Target = "PortAudioSharp.PortAudio+PaDeviceIndex")]
[assembly: SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue", Scope = "type", Target = "PortAudioSharp.PortAudio+PaSampleFormat")]
[assembly: SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue", Scope = "type", Target = "PortAudioSharp.PortAudio+PaStreamCallbackFlags")]
[assembly: [assembly:
SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope = "member", SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope = "member",
Target = "Vocaluxe.Lib.Video.Gstreamer.CGstreamerVideoWrapper.#GetFrame(System.Int32,System.Single)")] Target = "Vocaluxe.Lib.Video.Gstreamer.CGstreamerVideoWrapper.#GetFrame(System.Int32,System.Single)")]
@@ -49,19 +46,4 @@ using System.Diagnostics.CodeAnalysis;
Target = "Vocaluxe.Lib.Input.CHIDAPI.#ReadTimeout(System.IntPtr,System.Byte[]&,System.Int32,System.Int32)")] Target = "Vocaluxe.Lib.Input.CHIDAPI.#ReadTimeout(System.IntPtr,System.Byte[]&,System.Int32,System.Int32)")]
[assembly: [assembly:
SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope = "member", SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope = "member",
Target = "Vocaluxe.Lib.Input.CHIDAPI.#Read(System.IntPtr,System.Byte[]&,System.Int32)")] Target = "Vocaluxe.Lib.Input.CHIDAPI.#Read(System.IntPtr,System.Byte[]&,System.Int32)")]
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope = "member", Target = "PortAudioSharp.PortAudio.#Pa_GetVersionText()")]
[assembly:
SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope = "member",
Target = "PortAudioSharp.PortAudio.#Pa_GetErrorText(PortAudioSharp.PortAudio+PaError)")]
[assembly:
SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope = "member",
Target = "PortAudioSharp.PortAudio.#Pa_GetHostApiInfo(System.Int32)")]
[assembly:
SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope = "member", Target = "PortAudioSharp.PortAudio.#Pa_GetLastHostErrorInfo()")]
[assembly:
SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope = "member",
Target = "PortAudioSharp.PortAudio.#Pa_GetDeviceInfo(System.Int32)")]
[assembly:
SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope = "member",
Target = "PortAudioSharp.PortAudio.#Pa_GetStreamInfo(System.IntPtr)")]
+20 -20
View File
@@ -12,7 +12,7 @@ namespace Vocaluxe.Lib.Sound
/// DO NEVER use following Pa_* functions other than the ones from this class: /// DO NEVER use following Pa_* functions other than the ones from this class:
/// Initialize, Terminate, OpenStream, CloseStream /// Initialize, Terminate, OpenStream, CloseStream
/// </summary> /// </summary>
abstract class CPortAudioCommon static class CPortAudioCommon
{ {
private static int _RefCount; private static int _RefCount;
private static readonly object _Mutex = new object(); private static readonly object _Mutex = new object();
@@ -22,13 +22,13 @@ namespace Vocaluxe.Lib.Sound
/// MUST call CloseDriver when done /// MUST call CloseDriver when done
/// </summary> /// </summary>
/// <returns>True on success, falso if not initialized (log written)</returns> /// <returns>True on success, falso if not initialized (log written)</returns>
protected static bool _InitDriver() public static bool InitDriver()
{ {
lock (_Mutex) lock (_Mutex)
{ {
if (_RefCount == 0) if (_RefCount == 0)
{ {
if (_CheckError("Initialize", PortAudio.Pa_Initialize())) if (CheckError("Initialize", PortAudio.Pa_Initialize()))
return false; return false;
} }
_RefCount++; _RefCount++;
@@ -40,7 +40,7 @@ namespace Vocaluxe.Lib.Sound
/// Safe method to close PortAudio /// Safe method to close PortAudio
/// MUST be called _exactly_ once after successfull InitDriver call /// MUST be called _exactly_ once after successfull InitDriver call
/// </summary> /// </summary>
protected static void _CloseDriver() public static void CloseDriver()
{ {
lock (_Mutex) lock (_Mutex)
{ {
@@ -51,9 +51,9 @@ namespace Vocaluxe.Lib.Sound
} }
} }
protected static PortAudio.PaError _OpenStream(out IntPtr stream, ref PortAudio.PaStreamParameters? inputParameters, ref PortAudio.PaStreamParameters? outputParameters, public static PortAudio.PaError OpenStream(out IntPtr stream, ref PortAudio.PaStreamParameters? inputParameters, ref PortAudio.PaStreamParameters? outputParameters,
double sampleRate, uint framesPerBuffer, PortAudio.PaStreamFlags streamFlags, double sampleRate, uint framesPerBuffer, PortAudio.PaStreamFlags streamFlags,
PortAudio.PaStreamCallbackDelegate streamCallback, IntPtr userData) PortAudio.PaStreamCallbackDelegate streamCallback, IntPtr userData)
{ {
lock (_Mutex) lock (_Mutex)
{ {
@@ -72,14 +72,14 @@ namespace Vocaluxe.Lib.Sound
/// <param name="streamCallback"></param> /// <param name="streamCallback"></param>
/// <param name="userData"></param> /// <param name="userData"></param>
/// <returns>True on success</returns> /// <returns>True on success</returns>
protected static bool _OpenInputStream(out IntPtr stream, ref PortAudio.PaStreamParameters? inputParameters, public static bool OpenInputStream(out IntPtr stream, ref PortAudio.PaStreamParameters? inputParameters,
double sampleRate, uint framesPerBuffer, PortAudio.PaStreamFlags streamFlags, double sampleRate, uint framesPerBuffer, PortAudio.PaStreamFlags streamFlags,
PortAudio.PaStreamCallbackDelegate streamCallback, IntPtr userData) PortAudio.PaStreamCallbackDelegate streamCallback, IntPtr userData)
{ {
PortAudio.PaStreamParameters? outputParameters = null; PortAudio.PaStreamParameters? outputParameters = null;
return return
!_CheckError("OpenInputStream", !CheckError("OpenInputStream",
_OpenStream(out stream, ref inputParameters, ref outputParameters, sampleRate, framesPerBuffer, streamFlags, streamCallback, userData)); OpenStream(out stream, ref inputParameters, ref outputParameters, sampleRate, framesPerBuffer, streamFlags, streamCallback, userData));
} }
/// <summary> /// <summary>
@@ -93,17 +93,17 @@ namespace Vocaluxe.Lib.Sound
/// <param name="streamCallback"></param> /// <param name="streamCallback"></param>
/// <param name="userData"></param> /// <param name="userData"></param>
/// <returns>True on success</returns> /// <returns>True on success</returns>
protected static bool _OpenOutputStream(out IntPtr stream, ref PortAudio.PaStreamParameters? outputParameters, public static bool OpenOutputStream(out IntPtr stream, ref PortAudio.PaStreamParameters? outputParameters,
double sampleRate, uint framesPerBuffer, PortAudio.PaStreamFlags streamFlags, double sampleRate, uint framesPerBuffer, PortAudio.PaStreamFlags streamFlags,
PortAudio.PaStreamCallbackDelegate streamCallback, IntPtr userData) PortAudio.PaStreamCallbackDelegate streamCallback, IntPtr userData)
{ {
PortAudio.PaStreamParameters? inputParameters = null; PortAudio.PaStreamParameters? inputParameters = null;
return return
!_CheckError("OpenOutputStream", !CheckError("OpenOutputStream",
_OpenStream(out stream, ref inputParameters, ref outputParameters, sampleRate, framesPerBuffer, streamFlags, streamCallback, userData)); OpenStream(out stream, ref inputParameters, ref outputParameters, sampleRate, framesPerBuffer, streamFlags, streamCallback, userData));
} }
protected static void _CloseStream(IntPtr stream) public static void CloseStream(IntPtr stream)
{ {
lock (_Mutex) lock (_Mutex)
{ {
@@ -118,7 +118,7 @@ namespace Vocaluxe.Lib.Sound
/// <param name="action">Action identifier (E.g. openStream)</param> /// <param name="action">Action identifier (E.g. openStream)</param>
/// <param name="errorCode">Result returned by Pa_* call</param> /// <param name="errorCode">Result returned by Pa_* call</param>
/// <returns>True on error</returns> /// <returns>True on error</returns>
protected static bool _CheckError(String action, PortAudio.PaError errorCode) public static bool CheckError(String action, PortAudio.PaError errorCode)
{ {
if (errorCode != PortAudio.PaError.paNoError) if (errorCode != PortAudio.PaError.paNoError)
{ {
@@ -140,7 +140,7 @@ namespace Vocaluxe.Lib.Sound
/// Selects the most appropriate host api /// Selects the most appropriate host api
/// </summary> /// </summary>
/// <returns>The most appropriate host api</returns> /// <returns>The most appropriate host api</returns>
protected static int _GetHostApi() public static int GetHostApi()
{ {
//Caller has to hold a reference anyway so no locking needed //Caller has to hold a reference anyway so no locking needed
if (_RefCount < 1) if (_RefCount < 1)
@@ -8,7 +8,7 @@ using VocaluxeLib;
namespace Vocaluxe.Lib.Sound.Playback.PortAudio namespace Vocaluxe.Lib.Sound.Playback.PortAudio
{ {
class CPortAudioStream : CPortAudioCommon, IDisposable class CPortAudioStream : IDisposable
{ {
private const long _Bufsize = 1000000L; private const long _Bufsize = 1000000L;
private const long _Beginrefill = 800000L; private const long _Beginrefill = 800000L;
@@ -187,12 +187,12 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
{ {
Paused = false; Paused = false;
_AfterFadeAction = EStreamAction.Nothing; _AfterFadeAction = EStreamAction.Nothing;
_CheckError("StartStream", PortAudioSharp.PortAudio.Pa_StartStream(_Stream)); CPortAudioCommon.CheckError("StartStream", PortAudioSharp.PortAudio.Pa_StartStream(_Stream));
} }
public void Stop() public void Stop()
{ {
_CheckError("StopStream (playback)", PortAudioSharp.PortAudio.Pa_StopStream(_Stream)); CPortAudioCommon.CheckError("StopStream (playback)", PortAudioSharp.PortAudio.Pa_StopStream(_Stream));
Skip(0f); Skip(0f);
} }
@@ -213,11 +213,11 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
bool driverInitialized = false; bool driverInitialized = false;
try try
{ {
if (!_InitDriver()) if (!CPortAudioCommon.InitDriver())
return false; return false;
driverInitialized = true; driverInitialized = true;
int hostApi = _GetHostApi(); int hostApi = CPortAudioCommon.GetHostApi();
_ApiInfo = PortAudioSharp.PortAudio.Pa_GetHostApiInfo(hostApi); _ApiInfo = PortAudioSharp.PortAudio.Pa_GetHostApiInfo(hostApi);
_OutputDeviceInfo = PortAudioSharp.PortAudio.Pa_GetDeviceInfo(_ApiInfo.defaultOutputDevice); _OutputDeviceInfo = PortAudioSharp.PortAudio.Pa_GetDeviceInfo(_ApiInfo.defaultOutputDevice);
if (_OutputDeviceInfo.defaultLowOutputLatency < 0.1) if (_OutputDeviceInfo.defaultLowOutputLatency < 0.1)
@@ -228,7 +228,7 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
catch (Exception) catch (Exception)
{ {
if (driverInitialized) if (driverInitialized)
_CloseDriver(); CPortAudioCommon.CloseDriver();
CLog.LogError("Error Init PortAudio Playback"); CLog.LogError("Error Init PortAudio Playback");
return false; return false;
} }
@@ -240,7 +240,7 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
SFormatInfo format = _Decoder.GetFormatInfo(); SFormatInfo format = _Decoder.GetFormatInfo();
if (format.SamplesPerSecond == 0) if (format.SamplesPerSecond == 0)
{ {
_CloseDriver(); CPortAudioCommon.CloseDriver();
_Decoder.Close(); _Decoder.Close();
_Decoder = null; _Decoder = null;
CLog.LogError("Error Init PortAudio Playback (samples=0)"); CLog.LogError("Error Init PortAudio Playback (samples=0)");
@@ -262,7 +262,7 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
hostApiSpecificStreamInfo = IntPtr.Zero hostApiSpecificStreamInfo = IntPtr.Zero
}; };
if (!_OpenOutputStream( if (!CPortAudioCommon.OpenOutputStream(
out _Stream, out _Stream,
ref outputParams, ref outputParams,
format.SamplesPerSecond, format.SamplesPerSecond,
@@ -271,7 +271,7 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
_PaStreamCallback, _PaStreamCallback,
IntPtr.Zero) || _Stream == IntPtr.Zero) IntPtr.Zero) || _Stream == IntPtr.Zero)
{ {
_CloseDriver(); CPortAudioCommon.CloseDriver();
_Decoder.Close(); _Decoder.Close();
_Decoder = null; _Decoder = null;
return false; return false;
@@ -410,8 +410,8 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
private void _DoFree() private void _DoFree()
{ {
_CloseStream(_Stream); CPortAudioCommon.CloseStream(_Stream);
_CloseDriver(); CPortAudioCommon.CloseDriver();
_Decoder.Close(); _Decoder.Close();
_Closeproc(ID); _Closeproc(ID);
@@ -24,7 +24,7 @@ using Vocaluxe.Base;
namespace Vocaluxe.Lib.Sound.Record.PortAudio namespace Vocaluxe.Lib.Sound.Record.PortAudio
{ {
class CPortAudioRecord : CPortAudioCommon, IRecord class CPortAudioRecord : IRecord
{ {
private bool _Initialized; private bool _Initialized;
private readonly List<CRecordDevice> _Devices = new List<CRecordDevice>(); private readonly List<CRecordDevice> _Devices = new List<CRecordDevice>();
@@ -52,11 +52,11 @@ namespace Vocaluxe.Lib.Sound.Record.PortAudio
try try
{ {
if (!_InitDriver()) if (!CPortAudioCommon.InitDriver())
return false; return false;
_Devices.Clear(); _Devices.Clear();
int hostAPI = _GetHostApi(); int hostAPI = CPortAudioCommon.GetHostApi();
int numDevices = PortAudioSharp.PortAudio.Pa_GetDeviceCount(); int numDevices = PortAudioSharp.PortAudio.Pa_GetDeviceCount();
for (int i = 0; i < numDevices; i++) for (int i = 0; i < numDevices; i++)
{ {
@@ -128,7 +128,7 @@ namespace Vocaluxe.Lib.Sound.Record.PortAudio
suggestedLatency = PortAudioSharp.PortAudio.Pa_GetDeviceInfo(_Devices[dev].ID).defaultLowInputLatency, suggestedLatency = PortAudioSharp.PortAudio.Pa_GetDeviceInfo(_Devices[dev].ID).defaultLowInputLatency,
hostApiSpecificStreamInfo = IntPtr.Zero hostApiSpecificStreamInfo = IntPtr.Zero
}; };
if (!_OpenInputStream( if (!CPortAudioCommon.OpenInputStream(
out _RecHandle[dev], out _RecHandle[dev],
ref inputParams, ref inputParams,
44100, 44100,
@@ -138,7 +138,7 @@ namespace Vocaluxe.Lib.Sound.Record.PortAudio
new IntPtr(dev))) new IntPtr(dev)))
return false; return false;
if (_CheckError("Start Stream (rec)", PortAudioSharp.PortAudio.Pa_StartStream(_RecHandle[dev]))) if (CPortAudioCommon.CheckError("Start Stream (rec)", PortAudioSharp.PortAudio.Pa_StartStream(_RecHandle[dev])))
return false; return false;
} }
} }
@@ -168,8 +168,8 @@ namespace Vocaluxe.Lib.Sound.Record.PortAudio
return; return;
foreach (IntPtr handle in _RecHandle) foreach (IntPtr handle in _RecHandle)
_CloseStream(handle); CPortAudioCommon.CloseStream(handle);
_CloseDriver(); CPortAudioCommon.CloseDriver();
_Initialized = false; _Initialized = false;