public Audio(int inputChannels, int outputChannels, int frequency, uint framesPerBuffer, PortAudio.PaStreamCallbackDelegate paStreamCallback) { log("Initializing..."); this.inputChannels = inputChannels; this.outputChannels = outputChannels; this.frequency = frequency; this.framesPerBuffer = framesPerBuffer; this.paStreamCallback = paStreamCallback; if (errorCheck("Initialize",PortAudio.Pa_Initialize())) { this.disposed = true; // if Pa_Initialize() returns an error code, // Pa_Terminate() should NOT be called. throw new Exception("Can't initialize audio"); } int apiCount = PortAudio.Pa_GetHostApiCount(); for (int i = 0; i < apiCount; i++) { PortAudio.PaHostApiInfo availableApiInfo = PortAudio.Pa_GetHostApiInfo(i); log("available API index: " + i + "\n" + availableApiInfo.ToString()); } this.hostApi = apiSelect(); log("selected Host API: " + this.hostApi); this.apiInfo = PortAudio.Pa_GetHostApiInfo(this.hostApi); this.inputDeviceInfo = PortAudio.Pa_GetDeviceInfo(apiInfo.defaultInputDevice); this.outputDeviceInfo = PortAudio.Pa_GetDeviceInfo(apiInfo.defaultOutputDevice); log("input device:\n" + inputDeviceInfo.ToString()); log("output device:\n" + outputDeviceInfo.ToString()); }
public Audio(int inputChannels, int outputChannels, int frequency, uint framesPerBuffer, PortAudio.PaStreamCallbackDelegate paStreamCallback) { log("Initializing..."); this.inputChannels = inputChannels; this.outputChannels = outputChannels; this.frequency = frequency; this.framesPerBuffer = framesPerBuffer; this.paStreamCallback = paStreamCallback; if (errorCheck("Initialize", PortAudio.Pa_Initialize())) { this.disposed = true; // if Pa_Initialize() returns an error code, // Pa_Terminate() should NOT be called. throw new Exception("Can't initialize audio"); } int apiCount = PortAudio.Pa_GetHostApiCount(); for (int i = 0; i < apiCount; i++) { PortAudio.PaHostApiInfo availableApiInfo = PortAudio.Pa_GetHostApiInfo(i); log("available API index: " + i + "\n" + availableApiInfo.ToString()); } this.hostApi = apiSelect(); log("selected Host API: " + this.hostApi); this.apiInfo = PortAudio.Pa_GetHostApiInfo(this.hostApi); this.inputDeviceInfo = PortAudio.Pa_GetDeviceInfo(apiInfo.defaultInputDevice); this.outputDeviceInfo = PortAudio.Pa_GetDeviceInfo(apiInfo.defaultOutputDevice); log("input device:\n" + inputDeviceInfo.ToString()); log("output device:\n" + outputDeviceInfo.ToString()); }
public void Record() { IntPtr userdata = IntPtr.Zero; //intptr.zero is essentially just a null pointer callbackDelegate = new PortAudio.PaStreamCallbackDelegate(myPaStreamCallback); PortAudio.Pa_Initialize(); PortAudio.PaStreamParameters outputparams = new PortAudio.PaStreamParameters(); outputparams.channelCount = 1; outputparams.sampleFormat = PortAudio.PaSampleFormat.paInt16; outputparams.device = PortAudio.Pa_GetDefaultInputDevice(); outputparams.suggestedLatency = PortAudio.Pa_GetDeviceInfo(outputparams.device).defaultLowInputLatency; outputparams.hostApiSpecificStreamInfo = IntPtr.Zero; PortAudio.PaStreamParameters a = new PortAudio.PaStreamParameters(); //uninteresting output params cause i cant give it null a.channelCount = 1; a.sampleFormat = PortAudio.PaSampleFormat.paInt16; a.device = PortAudio.Pa_GetDefaultOutputDevice(); a.suggestedLatency = PortAudio.Pa_GetDeviceInfo(a.device).defaultLowOutputLatency; a.hostApiSpecificStreamInfo = IntPtr.Zero; PortAudio.PaError error = PortAudio.Pa_OpenStream(out stream, ref outputparams, ref a, this.sampleRate, (uint)NUM_SAMPLES, PortAudio.PaStreamFlags.paClipOff, callbackDelegate, IntPtr.Zero); this.isRecording = true; PortAudio.Pa_StartStream(stream); Thread myThread = new Thread(new ThreadStart(record_loop)); myThread.Start(); }
/// <summary> /// Init PortAudio and list record devices /// </summary> /// <returns>true if success</returns> public bool Init() { _Devices = new List <SRecordDevice>(); try { if (_initialized) { CloseAll(); } if (errorCheck("Initialize", PortAudio.Pa_Initialize())) { return(false); } _initialized = true; int hostAPI = apiSelect(); int numDevices = PortAudio.Pa_GetDeviceCount(); for (int i = 0; i < numDevices; i++) { PortAudio.PaDeviceInfo info = PortAudio.Pa_GetDeviceInfo(i); if (info.hostApi == hostAPI && info.maxInputChannels > 0) { SRecordDevice dev = new SRecordDevice(); dev.ID = i; dev.Name = info.name; dev.Driver = info.name + i.ToString(); dev.Inputs = new List <SInput>(); SInput inp = new SInput(); inp.Name = "Default"; inp.Channels = info.maxInputChannels; if (inp.Channels > 2) { inp.Channels = 2; //more are not supported in vocaluxe } dev.Inputs.Add(inp); _Devices.Add(dev); } } _recHandle = new IntPtr[_Devices.Count]; _myRecProc = new PortAudio.PaStreamCallbackDelegate(myPaStreamCallback); _DeviceConfig = _Devices.ToArray(); } catch (Exception e) { _initialized = false; CLog.LogError("Error initializing PortAudio: " + e.Message); return(false); } return(true); }
/// <summary> /// Convenience method to safely open an output stream and log potential error /// </summary> /// <param name="stream"></param> /// <param name="outputParameters"></param> /// <param name="sampleRate"></param> /// <param name="framesPerBuffer"></param> /// <param name="streamFlags"></param> /// <param name="streamCallback"></param> /// <param name="userData"></param> /// <returns>True on success</returns> public bool OpenOutputStream(out IntPtr stream, ref PortAudio.PaStreamParameters?outputParameters, double sampleRate, uint framesPerBuffer, PortAudio.PaStreamFlags streamFlags, PortAudio.PaStreamCallbackDelegate streamCallback, IntPtr userData) { PortAudio.PaStreamParameters?inputParameters = null; return (!CheckError("OpenOutputStream", OpenStream(out stream, ref inputParameters, ref outputParameters, sampleRate, framesPerBuffer, streamFlags, streamCallback, userData))); }
/// <summary> /// Init PortAudio and list record devices /// </summary> /// <returns>true if success</returns> public bool Init() { _Devices = new List<SRecordDevice>(); try { if (_initialized) CloseAll(); if (errorCheck("Initialize", PortAudio.Pa_Initialize())) return false; _initialized = true; int hostAPI = apiSelect(); int numDevices = PortAudio.Pa_GetDeviceCount(); for (int i = 0; i < numDevices; i++) { PortAudio.PaDeviceInfo info = PortAudio.Pa_GetDeviceInfo(i); if (info.hostApi == hostAPI && info.maxInputChannels > 0) { SRecordDevice dev = new SRecordDevice(); dev.ID = i; dev.Name = info.name; dev.Driver = info.name + i.ToString(); dev.Inputs = new List<SInput>(); SInput inp = new SInput(); inp.Name = "Default"; inp.Channels = info.maxInputChannels; if (inp.Channels > 2) inp.Channels = 2; //more are not supported in vocaluxe dev.Inputs.Add(inp); _Devices.Add(dev); } } _recHandle = new IntPtr[_Devices.Count]; _myRecProc = new PortAudio.PaStreamCallbackDelegate(myPaStreamCallback); _DeviceConfig = _Devices.ToArray(); } catch (Exception e) { _initialized = false; CLog.LogError("Error initializing PortAudio: " + e.Message); return false; } return true; }
public PortAudio.PaError OpenStream(out IntPtr stream, ref PortAudio.PaStreamParameters?inputParameters, ref PortAudio.PaStreamParameters?outputParameters, double sampleRate, uint framesPerBuffer, PortAudio.PaStreamFlags streamFlags, PortAudio.PaStreamCallbackDelegate streamCallback, IntPtr userData) { lock (_Mutex) { if (_Disposed) { throw new ObjectDisposedException("PortAudioHandle already disposed"); } PortAudio.PaError res = PortAudio.Pa_OpenStream(out stream, ref inputParameters, ref outputParameters, sampleRate, framesPerBuffer, streamFlags, streamCallback, userData); if (res == PortAudio.PaError.paNoError) { _Streams.Add(stream); } return(res); } }
public void Play() { IntPtr userdata = IntPtr.Zero; //intptr.zero is essentially just a null pointer callbackDelegate = new PortAudio.PaStreamCallbackDelegate(myPaStreamCallback); PortAudio.Pa_Initialize(); uint sampleFormat = 0; switch (bitDepth) { case 8: sampleFormat = 16; break; case 16: sampleFormat = 8; break; case 24: sampleFormat = 4; break; case 32: sampleFormat = 2; break; default: Console.WriteLine("broken WAV"); break; } //not sure why framesPerBuffer is so strange. PortAudio.PaError error = PortAudio.Pa_OpenDefaultStream(out stream, inputChannels, outputChannels, sampleFormat, sampleRate / outputChannels, (uint)(NUM_SAMPLES / (frameSize * 2)), callbackDelegate, userdata); PortAudio.Pa_StartStream(stream); Thread myThread = new Thread(new ThreadStart(play_loop)); myThread.Start(); }
public void OpenStream() { if (Config == null) { throw new Exception("Configuration settings have not been applied. You must first configure the host"); } if (StreamState != StreamState.Closed) { throw new Exception("Trying to Open a stream that does not have State: Closed. State is " + StreamState.ToString()); } callbackDelegate = RealtimeCallback; streamFinishedDelegate = StreamFinished; var err = PortAudio.Pa_OpenStream( out Config.Stream, ref Config.inputParameters, ref Config.outputParameters, Samplerate, BufferSize, (PortAudio.PaStreamFlags.paClipOff | PortAudio.PaStreamFlags.paDitherOff), callbackDelegate, new IntPtr(0) ); if (err != PortAudio.PaError.paNoError) { throw new Exception(PortAudio.Pa_GetErrorText(err)); } err = PortAudio.Pa_SetStreamFinishedCallback(Config.Stream, streamFinishedDelegate); if (err != PortAudio.PaError.paNoError) { throw new Exception(PortAudio.Pa_GetErrorText(err)); } StreamState = StreamState.Open; }
public int Open(string FileName) { if (_FileOpened) { return(-1); } if (!System.IO.File.Exists(FileName)) { return(-1); } if (_FileOpened) { return(-1); } _Decoder = new CAudioDecoderFFmpeg(); _Decoder.Init(); try { if (errorCheck("Initialize", PortAudio.Pa_Initialize())) { return(-1); } _Initialized = true; int hostApi = apiSelect(); _apiInfo = PortAudio.Pa_GetHostApiInfo(hostApi); _outputDeviceInfo = PortAudio.Pa_GetDeviceInfo(_apiInfo.defaultOutputDevice); _paStreamCallback = new PortAudio.PaStreamCallbackDelegate(_PaStreamCallback); if (_outputDeviceInfo.defaultLowOutputLatency < 0.1) { _outputDeviceInfo.defaultLowOutputLatency = 0.1; } } catch (Exception) { _Initialized = false; CLog.LogError("Error Init PortAudio Playback"); return(-1); } _FileName = FileName; _Decoder.Open(FileName); _Duration = _Decoder.GetLength(); FormatInfo format = _Decoder.GetFormatInfo(); _ByteCount = 2 * format.ChannelCount; _BytesPerSecond = format.SamplesPerSecond * _ByteCount; _CurrentTime = 0f; _SyncTimer.Time = _CurrentTime; AudioStreams stream = new AudioStreams(0); IntPtr data = new IntPtr(0); PortAudio.PaStreamParameters outputParams = new PortAudio.PaStreamParameters(); outputParams.channelCount = format.ChannelCount; outputParams.device = _apiInfo.defaultOutputDevice; outputParams.sampleFormat = PortAudio.PaSampleFormat.paInt16; outputParams.suggestedLatency = _outputDeviceInfo.defaultLowOutputLatency; errorCheck("OpenDefaultStream", PortAudio.Pa_OpenStream( out _Ptr, IntPtr.Zero, ref outputParams, format.SamplesPerSecond, (uint)CConfig.AudioBufferSize, PortAudio.PaStreamFlags.paNoFlag, _paStreamCallback, data)); stream.handle = _Ptr.ToInt32(); if (stream.handle != 0) { _Paused = true; _waiting = true; _FileOpened = true; _data = new RingBuffer(BUFSIZE); _NoMoreData = false; _DecoderThread.Priority = ThreadPriority.Normal; _DecoderThread.Name = Path.GetFileName(FileName); _DecoderThread.Start(); return(stream.handle); } return(-1); }
private unsafe IntPtr OpenStream(SoundEffect soundEffect) { IntPtr stream; PortAudio.PaStreamCallbackDelegate callback = new PortAudio.PaStreamCallbackDelegate(soundEffect.CallBack); PortAudio.PaErrorCode errorCode = PortAudio.Pa_OpenDefaultStream(out stream, 0, 2, (uint)PortAudio.PaSampleFormat.paFloat32, 22000, 256, callback, IntPtr.Zero); return stream; }
public void OpenStream() { if (Config == null) throw new Exception("Configuration settings have not been applied. You must first configure the host"); if (StreamState != StreamState.Closed) throw new Exception("Trying to Open a stream that does not have State: Closed. State is " + StreamState.ToString()); callbackDelegate = RealtimeCallback; streamFinishedDelegate = StreamFinished; var err = PortAudio.Pa_OpenStream( out Config.Stream, ref Config.inputParameters, ref Config.outputParameters, Samplerate, BufferSize, (PortAudio.PaStreamFlags.paClipOff | PortAudio.PaStreamFlags.paDitherOff), callbackDelegate, new IntPtr(0) ); if (err != PortAudio.PaError.paNoError) throw new Exception(PortAudio.Pa_GetErrorText(err)); err = PortAudio.Pa_SetStreamFinishedCallback(Config.Stream, streamFinishedDelegate); if (err != PortAudio.PaError.paNoError) throw new Exception(PortAudio.Pa_GetErrorText(err)); StreamState = StreamState.Open; }
public int Open(string FileName) { if (_FileOpened) return -1; if (!System.IO.File.Exists(FileName)) return -1; if (_FileOpened) return -1; _Decoder = new CAudioDecoderFFmpeg(); _Decoder.Init(); try { if (errorCheck("Initialize", PortAudio.Pa_Initialize())) return -1; _Initialized = true; int hostApi = apiSelect(); _apiInfo = PortAudio.Pa_GetHostApiInfo(hostApi); _outputDeviceInfo = PortAudio.Pa_GetDeviceInfo(_apiInfo.defaultOutputDevice); _paStreamCallback = new PortAudio.PaStreamCallbackDelegate(_PaStreamCallback); if (_outputDeviceInfo.defaultLowOutputLatency < 0.1) _outputDeviceInfo.defaultLowOutputLatency = 0.1; } catch (Exception) { _Initialized = false; CLog.LogError("Error Init PortAudio Playback"); return -1; } _FileName = FileName; _Decoder.Open(FileName); _Duration = _Decoder.GetLength(); FormatInfo format = _Decoder.GetFormatInfo(); _ByteCount = 2 * format.ChannelCount; _BytesPerSecond = format.SamplesPerSecond * _ByteCount; _CurrentTime = 0f; _SyncTimer.Time = _CurrentTime; AudioStreams stream = new AudioStreams(0); IntPtr data = new IntPtr(0); PortAudio.PaStreamParameters outputParams = new PortAudio.PaStreamParameters(); outputParams.channelCount = format.ChannelCount; outputParams.device = _apiInfo.defaultOutputDevice; outputParams.sampleFormat = PortAudio.PaSampleFormat.paInt16; outputParams.suggestedLatency = _outputDeviceInfo.defaultLowOutputLatency; uint bufsize = (uint)CConfig.AudioBufferSize; errorCheck("OpenDefaultStream", PortAudio.Pa_OpenStream( out _Ptr, IntPtr.Zero, ref outputParams, format.SamplesPerSecond, bufsize, PortAudio.PaStreamFlags.paNoFlag, _paStreamCallback, data)); stream.handle = _Ptr.ToInt32(); if (stream.handle != 0) { _Paused = true; _waiting = true; _FileOpened = true; _data = new RingBuffer(BUFSIZE); _NoMoreData = false; _DecoderThread.Priority = ThreadPriority.Normal; _DecoderThread.Name = Path.GetFileName(FileName); _DecoderThread.Start(); return stream.handle; } return -1; }