// Open a new audio output stream // Returns true when successful public bool Open(uint samplingRate, uint bufferSize, OnNextProc onNext, OnDoneProc onDone) { // Error checking if (IsOpen() || samplingRate == 0 || onNext == null || onDone == null) { return(false); } // Configure the audio output stream for 16-bit stereo PCM WAVEFORMATEX fmt; fmt.wFormatTag = WAVE_FORMAT_PCM; fmt.nChannels = 2; fmt.nSamplesPerSec = samplingRate; fmt.nBlockAlign = 4; fmt.nAvgBytesPerSec = samplingRate * 4; fmt.wBitsPerSample = 16; fmt.cbSize = 0; // Open the audio output stream if (waveOutOpen(out WaveHandle, new IntPtr(WAVE_MAPPER), ref fmt, Callback, IntPtr.Zero, CALLBACK_FUNCTION) != MMSYSERR_NOERROR) { return(false); } // Configure instance fields OnDone = onDone; OnNext = onNext; State = Stopped; // Configure buffers uint frames = Math.Max(bufferSize, 1); SamplesF = new float[frames * 2]; SamplesS = new short[frames * 2]; InitBuffer(ref Buffers[0], frames); InitBuffer(ref Buffers[1], frames); // No errors occurred, so return success return(true); }
// Open a new audio output stream, omitting the OnDone parameter public bool Open(uint samplingRate, uint bufferSize, OnNextProc onNext) { return(Open(samplingRate, bufferSize, onNext, () => { })); }