public static StreamingSourceVoice Create(XAudio2 xaudio2, IWaveSource waveSource, int bufferSize = 100) { var voiceCallback = new VoiceCallback(); IntPtr ptr = xaudio2.CreateSourceVoicePtr(waveSource.WaveFormat, VoiceFlags.None, XAudio2.DefaultFrequencyRatio, voiceCallback, null, null); return(new StreamingSourceVoice(ptr, voiceCallback, waveSource, bufferSize)); }
/// <summary> /// Initializes a new instance of the <see cref="StreamingSourceVoice" /> class. /// </summary> /// <param name="ptr">Pointer to a <see cref="XAudio2SourceVoice" /> object.</param> /// <param name="voiceCallback"> /// <see cref="VoiceCallback" /> instance which receives notifications from the /// <see cref="XAudio2SourceVoice" /> which got passed as a pointer (see the <paramref name="ptr" /> argument). /// </param> /// <param name="waveSource"><see cref="IWaveSource" /> which provides the audio data to stream.</param> /// <param name="bufferSize"> /// Buffersize of the internal used buffers in milliseconds. Values in the range from 70ms to /// 200ms are recommended. /// </param> /// <remarks>It is recommended to use the <see cref="Create" /> method instead of the this constructor.</remarks> public StreamingSourceVoice(IntPtr ptr, VoiceCallback voiceCallback, IWaveSource waveSource, int bufferSize) { BasePtr = ptr; _voiceCallback = voiceCallback; _waveSource = waveSource; var maxBufferBytes = (int) waveSource.WaveFormat.MillisecondsToBytes(bufferSize); _buffer = new byte[maxBufferBytes]; for (int i = 0; i < _buffers.Length; i++) { var buffer = new XAudio2Buffer(maxBufferBytes); _buffers[i] = buffer; } InitializeForStreaming(); }
internal StreamingSourceVoice(XAudio2 xaudio2, IWaveSource waveSource, VoiceCallback voiceCallback, int bufferSize) : base(CreateSourceVoice(xaudio2, waveSource, voiceCallback), xaudio2.Version) { _voiceCallback = voiceCallback; _waveSource = waveSource; var maxBufferBytes = (int)waveSource.WaveFormat.MillisecondsToBytes(bufferSize); _buffer = new byte[maxBufferBytes]; for (int i = 0; i < _buffers.Length; i++) { var buffer = new XAudio2Buffer(maxBufferBytes); _buffers[i] = buffer; } InitializeForStreaming(); }
/// <summary> /// Initializes a new instance of the <see cref="StreamingSourceVoice" /> class. /// </summary> /// <param name="ptr">Pointer to a <see cref="XAudio2SourceVoice" /> object.</param> /// <param name="voiceCallback"> /// <see cref="VoiceCallback" /> instance which receives notifications from the /// <see cref="XAudio2SourceVoice" /> which got passed as a pointer (see the <paramref name="ptr" /> argument). /// </param> /// <param name="waveSource"><see cref="IWaveSource" /> which provides the audio data to stream.</param> /// <param name="bufferSize"> /// Buffersize of the internal used buffers in milliseconds. Values in the range from 70ms to /// 200ms are recommended. /// </param> /// <remarks>It is recommended to use the <see cref="Create(XAudio2,IWaveSource,int)" /> method instead of the this constructor.</remarks> internal StreamingSourceVoice(IntPtr ptr, VoiceCallback voiceCallback, IWaveSource waveSource, int bufferSize) { BasePtr = ptr; _voiceCallback = voiceCallback; _waveSource = waveSource; var maxBufferBytes = (int)waveSource.WaveFormat.MillisecondsToBytes(bufferSize); _buffer = new byte[maxBufferBytes]; for (int i = 0; i < _buffers.Length; i++) { var buffer = new XAudio2Buffer(maxBufferBytes); _buffers[i] = buffer; } InitializeForStreaming(); }
private static IntPtr CreateSourceVoice(XAudio2 xaudio2, IWaveSource waveSource, VoiceCallback callback) { if (xaudio2 == null) { throw new ArgumentNullException("xaudio2"); } if (waveSource == null) { throw new ArgumentNullException("waveSource"); } return(xaudio2.CreateSourceVoicePtr(waveSource.WaveFormat, VoiceFlags.None, XAudio2.DefaultFrequencyRatio, callback, null, null)); }
public void CanRegisterSourceVoiceCallback() { const int lengthInSeconds = 2; bool b0 = false, b1 = false; var callback = new VoiceCallback(); using (var masteringVoice = _xaudio2.CreateMasteringVoice()) using (var sourceVoice = _xaudio2.CreateSourceVoice(_source.WaveFormat, VoiceFlags.None, CSCore.XAudio2.XAudio2.DefaultFrequencyRatio, callback, null, null)) { callback.BufferStart += (s,e) => b0 = true; callback.BufferEnd += (s,e) => b1 = true; byte[] rawBuffer = new byte[_source.WaveFormat.BytesPerSecond * lengthInSeconds]; int read = _source.Read(rawBuffer, 0, rawBuffer.Length); var buffer = new XAudio2Buffer(read); using (var stream = buffer.GetStream()) { stream.Write(rawBuffer, 0, read); } sourceVoice.SubmitSourceBuffer(buffer); sourceVoice.Start(); Thread.Sleep(lengthInSeconds * 1000 + 2000); Assert.IsTrue(b0, "No BufferStart."); Assert.IsTrue(b1, "No BufferEnd."); } }
/// <summary> /// Creates an instance of the <see cref="StreamingSourceVoice" /> class. /// </summary> /// <param name="xaudio2">Instance of the <see cref="XAudio2" /> class.</param> /// <param name="waveSource"><see cref="IWaveSource" /> which provides the audio data to stream.</param> /// <param name="bufferSize"> /// Buffersize of the internal used buffers in milliseconds. Values in the range from 70ms to /// 200ms are recommended. /// </param> /// <returns>Configured <see cref="StreamingSourceVoice" /> instance.</returns> public static StreamingSourceVoice Create(XAudio2 xaudio2, IWaveSource waveSource, int bufferSize = 100) { var voiceCallback = new VoiceCallback(); IntPtr ptr = xaudio2.CreateSourceVoicePtr(waveSource.WaveFormat, VoiceFlags.None, XAudio2.DefaultFrequencyRatio, voiceCallback, null, null); return new StreamingSourceVoice(ptr, voiceCallback, waveSource, bufferSize); }
private static IntPtr CreateSourceVoice(XAudio2 xaudio2, IWaveSource waveSource, VoiceCallback callback) { if (xaudio2 == null) throw new ArgumentNullException("xaudio2"); if (waveSource == null) throw new ArgumentNullException("waveSource"); return xaudio2.CreateSourceVoicePtr(waveSource.WaveFormat, VoiceFlags.None, XAudio2.DefaultFrequencyRatio, callback, null, null); }