async Task IAudioProvider.PlaySilenceAsync(TimeSpan timeSpan) { _player = WavePlayer.PlaySilence(timeSpan); await _player.WaitAsync(); _player = null; }
internal static WavePlayer PlaySilence(TimeSpan timeSpan) { var player = new WavePlayer(-1); Task.Delay(timeSpan).ContinueWith((t) => player._semaphoreInner.Release()); player._semaphoreInner.WaitAsync().ContinueWith((t) => player._semaphoreOuter.Release()); return(player); }
public static WavePlayer Play(int device, byte[] buffer, double volume) { WavePlayer player = new WavePlayer(device); Debug.Assert(BitConverter.ToInt32(buffer, 0) == 0x46464952, "We have a RIFF file"); var formatChunkOffset = 12; Debug.Assert(BitConverter.ToInt32(buffer, formatChunkOffset) == 0x20746D66, "We have a fmt chunk"); player._format = new WaveApi.WAVEFORMATEX { wFormatTag = BitConverter.ToInt16(buffer, formatChunkOffset + 8), nChannels = BitConverter.ToInt16(buffer, formatChunkOffset + 10), nSamplesPerSec = BitConverter.ToInt32(buffer, formatChunkOffset + 12), nAvgBytesPerSec = BitConverter.ToInt32(buffer, formatChunkOffset + 16), nBlockAlign = BitConverter.ToInt16(buffer, formatChunkOffset + 20), wBitsPerSample = BitConverter.ToInt16(buffer, formatChunkOffset + 22), cbSize = BitConverter.ToInt16(buffer, formatChunkOffset + 24) }; var dataChunkOffset = formatChunkOffset + 8 + BitConverter.ToInt32(buffer, formatChunkOffset + 4); Debug.Assert(BitConverter.ToInt32(buffer, dataChunkOffset) == 0x61746164, "We have a data chunk"); var dataLength = BitConverter.ToInt32(buffer, dataChunkOffset + 4); // Potentially we might want to truncate the buffer to remove leading and training silence. var chunkStart = dataChunkOffset + 8; var chunkEnd = chunkStart + dataLength; // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (TrimSpeach) { while (chunkStart < chunkEnd && buffer[chunkStart + 0] == 0 && buffer[chunkStart + 1] == 0) { chunkStart += 2; } while (chunkStart < chunkEnd && buffer[chunkEnd - 1] == 0 && buffer[chunkEnd - 2] == 0) { chunkEnd -= 2; } } player._hGlobal = Marshal.AllocHGlobal(dataLength); Marshal.Copy(buffer, chunkStart, player._hGlobal, chunkEnd - chunkStart); player._header = new WaveApi.WAVEHDR { lpData = player._hGlobal, dwBufferLength = chunkEnd - chunkStart }; ThreadPool.QueueUserWorkItem((o) => player.Play(volume)); return(player); }
async Task IAudioProvider.PlayAsync(byte[] buffer, double volume) { var index = AudioProviderFactory.GetMysteryIndex(_settings); if (index != Null) { _player = WavePlayer.Play(index, buffer, volume); await _player.WaitAsync(); _player = null; } }