public unsafe WaveDuplex(int deviceIndex, double sampleRate, int framesPerBuffer, AudioBufferAvailableDelegate bufferNeededDelegate) { this._bufferAvailable = bufferNeededDelegate; PaStreamParameters paStreamParameters = new PaStreamParameters { device = deviceIndex, channelCount = 2, suggestedLatency = 0.0, sampleFormat = PaSampleFormat.PaFloat32 }; PaError paError = PortAudioAPI.Pa_IsFormatSupported(ref paStreamParameters, ref paStreamParameters, sampleRate); if (paError != 0) { throw new ApplicationException(paError.ToString()); } this._gcHandle = GCHandle.Alloc(this); paError = PortAudioAPI.Pa_OpenStream(out this._streamHandle, ref paStreamParameters, ref paStreamParameters, sampleRate, (uint)framesPerBuffer, PaStreamFlags.PaNoFlag, this._paCallback, (IntPtr)this._gcHandle); if (paError != 0) { this._gcHandle.Free(); throw new ApplicationException(paError.ToString()); } paError = PortAudioAPI.Pa_StartStream(this._streamHandle); if (paError == PaError.paNoError) { return; } PortAudioAPI.Pa_CloseStream(this._streamHandle); this._gcHandle.Free(); throw new ApplicationException(paError.ToString()); }
public unsafe WavePlayer(int deviceIndex, double sampleRate, int framesPerBuffer, AudioBufferNeededDelegate bufferNeededDelegate) { Console.WriteLine("WavePlayer started, samplerate=" + sampleRate.ToString() + ", frameCount=" + framesPerBuffer.ToString()); this._bufferNeeded = bufferNeededDelegate; PaStreamParameters paStreamParameters = new PaStreamParameters { device = deviceIndex, channelCount = 2, suggestedLatency = 0.0, sampleFormat = PaSampleFormat.PaFloat32 }; PaError paError = PortAudioAPI.Pa_IsFormatSupported(IntPtr.Zero, ref paStreamParameters, sampleRate); if (paError != 0) { throw new ApplicationException("Init audio output device: " + paError.ToString()); } this._gcHandle = GCHandle.Alloc(this); paError = PortAudioAPI.Pa_OpenStream(out this._streamHandle, IntPtr.Zero, ref paStreamParameters, sampleRate, (uint)framesPerBuffer, PaStreamFlags.PaNoFlag, this._paCallback, (IntPtr)this._gcHandle); if (paError != 0) { this._gcHandle.Free(); throw new ApplicationException("Open audio output device: " + paError.ToString()); } paError = PortAudioAPI.Pa_StartStream(this._streamHandle); if (paError == PaError.paNoError) { return; } PortAudioAPI.Pa_CloseStream(this._streamHandle); this._gcHandle.Free(); throw new ApplicationException("Start audio output device: " + paError.ToString()); }
public AudioPlayer(int deviceIndex, double sampleRate, uint framesPerBuffer, SamplesAvailableEvent samplesAvailableCallback) { _bufferNeeded = samplesAvailableCallback; PaStreamParameters ouputParams = new PaStreamParameters(); ouputParams.device = deviceIndex; ouputParams.channelCount = 2; ouputParams.suggestedLatency = 0; ouputParams.sampleFormat = PaSampleFormat.paFloat32; PaError paErr = Pa_IsFormatSupported(IntPtr.Zero, ref ouputParams, sampleRate); if (paErr != PaError.paNoError) { throw new ApplicationException(paErr.ToString()); } _gcHandle = GCHandle.Alloc(this); paErr = Pa_OpenStream(out _streamHandle, IntPtr.Zero, ref ouputParams, sampleRate, framesPerBuffer, PaStreamFlags.paNoFlag, _paCallback, (IntPtr)_gcHandle); if (paErr != PaError.paNoError) { _gcHandle.Free(); throw new ApplicationException(paErr.ToString()); } paErr = Pa_StartStream(_streamHandle); if (paErr != PaError.paNoError) { Pa_CloseStream(_streamHandle); _gcHandle.Free(); throw new ApplicationException(paErr.ToString()); } }