public override void SetFormat(AudioFormat format) { // Don't need to recreate device if it's the same if (Format.Equals(format)) { return; } Format = format; Console.WriteLine("init"); DisposeDevice(); WaveFormatEx fmt = default(WaveFormatEx); fmt.FormatTag = 1; // WAVE_FORMAT_PCM fmt.Channels = (ushort)format.Channels; fmt.BitsPerSample = (ushort)format.BitsPerSample; fmt.BlockAlign = (ushort)(fmt.Channels * fmt.BitsPerSample / 8); fmt.SampleRate = (uint)format.SampleRate; fmt.AverageBytesPerSecond = (int)fmt.SampleRate * fmt.BlockAlign; uint devices = WinMM.waveOutGetNumDevs(); if (devices == 0) { throw new InvalidOperationException("No audio devices found"); } uint result = WinMM.waveOutOpen(out devHandle, (IntPtr)(-1), ref fmt, IntPtr.Zero, UIntPtr.Zero, 0); CheckError(result, "Open"); }
void InitWinMm( AudioChunk chunk ) { handle = IntPtr.Zero; WaveFormatEx format = new WaveFormatEx(); format.Channels = (ushort)chunk.Channels; format.ExtraSize = 0; format.FormatTag = WaveFormatTag.Pcm; format.BitsPerSample = (ushort)chunk.BitsPerSample; format.BlockAlign = (ushort)( format.Channels * format.BitsPerSample / 8 ); format.SampleRate = (uint)chunk.Frequency; format.AverageBytesPerSecond = chunk.Frequency * format.BlockAlign; WaveOpenFlags flags = WaveOpenFlags.CallbackFunction; callback = ProcessWaveOutCallback; uint result = Open( out handle, new UIntPtr( (uint)0xFFFF ), ref format, callback, UIntPtr.Zero, flags ); CheckError( result ); }
static extern uint Open( out IntPtr handle, UIntPtr deviceID, ref WaveFormatEx format, WaveOutCallback callback, UIntPtr callbackInstance, WaveOpenFlags flags);
internal static extern uint waveOutOpen(out IntPtr handle, IntPtr deviceID, ref WaveFormatEx format, IntPtr callback, UIntPtr callbackInstance, uint flags);