internal override void Begin(byte[] wfx) { if (_deviceOpen) { throw new InvalidOperationException(); } WAVEFORMATEX.AvgBytesPerSec(wfx, out _nAvgBytesPerSec, out _blockAlign); MMSYSERR mMSYSERR; lock (_noWriteOutLock) { mMSYSERR = SafeNativeMethods.waveOutOpen(ref _hwo, _curDevice, wfx, _delegate, IntPtr.Zero, 196608u); if (_fPaused && mMSYSERR == MMSYSERR.NOERROR) { mMSYSERR = SafeNativeMethods.waveOutPause(_hwo); } _aborted = false; _deviceOpen = true; } if (mMSYSERR != 0) { throw new AudioException(mMSYSERR); } _bytesWritten = 0; _evt.Set(); }
internal static AudioCodec TypeOf(WAVEFORMATEX format) { AudioCodec result = AudioCodec.Undefined; switch (format.wFormatTag) { case 1: switch (format.nBlockAlign / format.nChannels) { case 1: result = AudioCodec.PCM8; break; case 2: result = AudioCodec.PCM16; break; } break; case 6: result = AudioCodec.G711A; break; case 7: result = AudioCodec.G711U; break; } return(result); }
void ISpEngineSite.LoadResource(string uri, ref string mediaType, out IStream stream) { mediaType = null; try { Stream stream2 = _site.LoadResource(new Uri(uri, UriKind.RelativeOrAbsolute), mediaType); BinaryReader br = new BinaryReader(stream2); byte[] waveFormat = AudioBase.GetWaveFormat(br); mediaType = null; if (waveFormat != null) { WAVEFORMATEX wAVEFORMATEX = WAVEFORMATEX.ToWaveHeader(waveFormat); WaveFormatId wFormatTag = (WaveFormatId)wAVEFORMATEX.wFormatTag; if (wFormatTag == WaveFormatId.Pcm || (uint)(wFormatTag - 6) <= 1u) { mediaType = "audio/x-wav"; } } stream2.Position = 0L; stream = new SpStreamWrapper(stream2); } catch { stream = null; } }
internal bool PrepareConverter(ref WAVEFORMATEX inWavFormat, ref WAVEFORMATEX outWavFormat) { bool result = true; if (inWavFormat.nSamplesPerSec <= 0 || inWavFormat.nChannels > 2 || inWavFormat.nChannels <= 0 || outWavFormat.nChannels <= 0 || outWavFormat.nSamplesPerSec <= 0 || outWavFormat.nChannels > 2) { throw new FormatException(); } _iInFormatType = AudioFormatConverter.TypeOf(inWavFormat); _iOutFormatType = AudioFormatConverter.TypeOf(outWavFormat); if (_iInFormatType < AudioCodec.G711U || _iOutFormatType < AudioCodec.G711U) { throw new FormatException(); } if (outWavFormat.nSamplesPerSec == inWavFormat.nSamplesPerSec && _iOutFormatType == _iInFormatType && outWavFormat.nChannels == inWavFormat.nChannels) { result = false; } else { if (inWavFormat.nSamplesPerSec != outWavFormat.nSamplesPerSec) { CreateResamplingFilter(inWavFormat.nSamplesPerSec, outWavFormat.nSamplesPerSec); } _inWavFormat = inWavFormat; _outWavFormat = outWavFormat; } return(result); }
private short[] Resample(WAVEFORMATEX inWavFormat, WAVEFORMATEX outWavFormat, short[] pnBuff, float[] memory) { if (inWavFormat.nSamplesPerSec != outWavFormat.nSamplesPerSec) { float[] inSamples = Short2Float(pnBuff); inSamples = Resampling(inSamples, memory); pnBuff = Float2Short(inSamples); } return(pnBuff); }
internal override void Begin(byte[] wfx) { if (_deviceOpen) { throw new InvalidOperationException(); } _wfxIn = WAVEFORMATEX.ToWaveHeader(wfx); _doConversion = _pcmConverter.PrepareConverter(ref _wfxIn, ref _wfxOut); if (_totalByteWrittens == 0 && _hasHeader) { AudioBase.WriteWaveHeader(_stream, _wfxOut, _startStreamPosition, 0); } _bytesWritten = 0; _aborted = false; _deviceOpen = true; }
internal static WAVEFORMATEX ToWaveHeader(byte[] waveHeader) { GCHandle gCHandle = GCHandle.Alloc(waveHeader, GCHandleType.Pinned); IntPtr ptr = gCHandle.AddrOfPinnedObject(); WAVEFORMATEX result = default(WAVEFORMATEX); result.wFormatTag = Marshal.ReadInt16(ptr); result.nChannels = Marshal.ReadInt16(ptr, 2); result.nSamplesPerSec = Marshal.ReadInt32(ptr, 4); result.nAvgBytesPerSec = Marshal.ReadInt32(ptr, 8); result.nBlockAlign = Marshal.ReadInt16(ptr, 12); result.wBitsPerSample = Marshal.ReadInt16(ptr, 14); result.cbSize = Marshal.ReadInt16(ptr, 16); if (result.cbSize != 0) { throw new InvalidOperationException(); } gCHandle.Free(); return(result); }
internal byte[] WaveFormat(byte[] targetWaveFormat) { if (targetWaveFormat == null && _waveFormat == null && VoiceInfo.SupportedAudioFormats.Count > 0) { targetWaveFormat = VoiceInfo.SupportedAudioFormats[0].WaveFormat; } if (targetWaveFormat == null && _waveFormat != null) { return(_waveFormat); } if (_waveFormat == null || !object.Equals(targetWaveFormat, _waveFormat)) { IntPtr intPtr = IntPtr.Zero; GCHandle gCHandle = default(GCHandle); if (targetWaveFormat != null) { gCHandle = GCHandle.Alloc(targetWaveFormat, GCHandleType.Pinned); } try { intPtr = _engine.GetOutputFormat((targetWaveFormat != null) ? gCHandle.AddrOfPinnedObject() : IntPtr.Zero); } finally { if (targetWaveFormat != null) { gCHandle.Free(); } } if (intPtr != IntPtr.Zero) { _waveFormat = WAVEFORMATEX.ToBytes(intPtr); Marshal.FreeCoTaskMem(intPtr); } else { _waveFormat = WAVEFORMATEX.Default.ToBytes(); } } return(_waveFormat); }
internal AudioFileOut(Stream stream, SpeechAudioFormatInfo formatInfo, bool headerInfo, IAsyncDispatch asyncDispatch) { _asyncDispatch = asyncDispatch; _stream = stream; _startStreamPosition = _stream.Position; _hasHeader = headerInfo; _wfxOut = default(WAVEFORMATEX); if (formatInfo != null) { _wfxOut.wFormatTag = (short)formatInfo.EncodingFormat; _wfxOut.wBitsPerSample = (short)formatInfo.BitsPerSample; _wfxOut.nSamplesPerSec = formatInfo.SamplesPerSecond; _wfxOut.nChannels = (short)formatInfo.ChannelCount; } else { _wfxOut = WAVEFORMATEX.Default; } _wfxOut.nBlockAlign = (short)(_wfxOut.nChannels * _wfxOut.wBitsPerSample / 8); _wfxOut.nAvgBytesPerSec = _wfxOut.wBitsPerSample * _wfxOut.nSamplesPerSec * _wfxOut.nChannels / 8; }
internal static void WriteWaveHeader(Stream stream, WAVEFORMATEX waveEx, long position, int cData) { RIFFHDR rIFFHDR = new RIFFHDR(0); BLOCKHDR bLOCKHDR = new BLOCKHDR(0); DATAHDR dATAHDR = new DATAHDR(0); int num = Marshal.SizeOf((object)rIFFHDR); int num2 = Marshal.SizeOf((object)bLOCKHDR); int length = waveEx.Length; int num3 = Marshal.SizeOf((object)dATAHDR); int num4 = num + num2 + length + num3; using (MemoryStream memoryStream = new MemoryStream()) { BinaryWriter binaryWriter = new BinaryWriter(memoryStream); try { rIFFHDR._len = num4 + cData - 8; binaryWriter.Write(rIFFHDR._id); binaryWriter.Write(rIFFHDR._len); binaryWriter.Write(rIFFHDR._type); bLOCKHDR._len = length; binaryWriter.Write(bLOCKHDR._id); binaryWriter.Write(bLOCKHDR._len); binaryWriter.Write(waveEx.ToBytes()); dATAHDR._len = cData; binaryWriter.Write(dATAHDR._id); binaryWriter.Write(dATAHDR._len); stream.Seek(position, SeekOrigin.Begin); stream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Length); } finally { ((IDisposable)binaryWriter).Dispose(); } } }
internal void PlayWaveFile(AudioData audio) { try { if (!string.IsNullOrEmpty(audio._mimeType)) { WAVEFORMATEX wAVEFORMATEX = default(WAVEFORMATEX); wAVEFORMATEX.nChannels = 1; wAVEFORMATEX.nSamplesPerSec = 8000; wAVEFORMATEX.nAvgBytesPerSec = 8000; wAVEFORMATEX.nBlockAlign = 1; wAVEFORMATEX.wBitsPerSample = 8; wAVEFORMATEX.cbSize = 0; string mimeType = audio._mimeType; if (!(mimeType == "audio/basic")) { if (!(mimeType == "audio/x-alaw-basic")) { throw new FormatException(SR.Get(SRID.UnknownMimeFormat)); } wAVEFORMATEX.wFormatTag = 6; } else { wAVEFORMATEX.wFormatTag = 7; } Begin(wAVEFORMATEX.ToBytes()); try { byte[] array = new byte[(int)audio._stream.Length]; audio._stream.Read(array, 0, array.Length); Play(array); } finally { WaitUntilDone(); End(); } } else { BinaryReader binaryReader = new BinaryReader(audio._stream); try { byte[] waveFormat = GetWaveFormat(binaryReader); if (waveFormat == null) { throw new FormatException(SR.Get(SRID.NotValidAudioFile, audio._uri.ToString())); } Begin(waveFormat); try { while (true) { DATAHDR dATAHDR = default(DATAHDR); if (audio._stream.Position + 8 >= audio._stream.Length) { break; } dATAHDR._id = binaryReader.ReadUInt32(); dATAHDR._len = binaryReader.ReadInt32(); if (dATAHDR._id == 1635017060) { byte[] buffer = Helpers.ReadStreamToByteArray(audio._stream, dATAHDR._len); Play(buffer); } else { audio._stream.Seek(dATAHDR._len, SeekOrigin.Current); } } } finally { WaitUntilDone(); End(); } } finally { ((IDisposable)binaryReader).Dispose(); } } } finally { audio.Dispose(); } }