static DirectSound.WaveFormat ConvertWaveFormat(Qaryan.Audio.WaveFormat fmt) { DirectSound.WaveFormat result = new Microsoft.DirectX.DirectSound.WaveFormat(); result.AverageBytesPerSecond = (int)fmt.AverageBytesPerSecond; result.BitsPerSample = (short)fmt.BitsPerSample; result.BlockAlign = (short)fmt.BlockAlign; result.Channels = (short)fmt.Channels; result.FormatTag = (DirectSound.WaveFormatTag)fmt.FormatTag; result.SamplesPerSecond = (int)fmt.SamplesPerSecond; return(result); }
public DSoundPlayer(Control owner, PullAudio pullAudio, Qaryan.Audio.WaveFormat format) { this.pullAudio = pullAudio; this.soundDevice = new Device(); this.soundDevice.SetCooperativeLevel(owner, CooperativeLevel.Normal); // Set up our wave format to 44,100Hz, with 16 bit resolution DirectSound.WaveFormat wf = ConvertWaveFormat(format); // wf.FormatTag = DirectSound.WaveFormatTag.Pcm; // wf.SamplesPerSecond = 44100; // wf.BitsPerSample = 16; // wf.Channels = channels; // wf.BlockAlign = (short)(wf.Channels * wf.BitsPerSample / 8); // wf.AverageBytesPerSecond = wf.SamplesPerSecond * wf.BlockAlign; this.samplesPerUpdate = 1024; // Create a buffer with 5 seconds of sample data bufferDesc = new BufferDescription(wf); bufferDesc.BufferBytes = this.samplesPerUpdate * wf.BlockAlign * 5; bufferDesc.ControlPositionNotify = true; bufferDesc.GlobalFocus = true; this.soundBuffer = new SecondaryBuffer(bufferDesc, this.soundDevice); Notify notify = new Notify(this.soundBuffer); fillEvent[0] = new AutoResetEvent(false); fillEvent[1] = new AutoResetEvent(false); // Set up two notification events, one at halfway, and one at the end of the buffer BufferPositionNotify[] posNotify = new BufferPositionNotify[2]; posNotify[0] = new BufferPositionNotify(); posNotify[0].Offset = bufferDesc.BufferBytes / 2 - 1; posNotify[0].EventNotifyHandle = fillEvent[0].SafeWaitHandle.DangerousGetHandle(); posNotify[1] = new BufferPositionNotify(); posNotify[1].Offset = bufferDesc.BufferBytes - 1; posNotify[1].EventNotifyHandle = fillEvent[1].SafeWaitHandle.DangerousGetHandle(); notify.SetNotificationPositions(posNotify); this.thread = new Thread(new ThreadStart(SoundPlayback)); this.thread.SetApartmentState(ApartmentState.STA); this.thread.Name = "SoundPlayback"; this.thread.Priority = ThreadPriority.Highest; this.Pause(); this.running = true; this.thread.Start(); }
public WinWaveFormat(Qaryan.Audio.WaveFormat wf) : this((int)wf.SamplesPerSecond, wf.BitsPerSample, wf.Channels) { }