public void Start(AudioVideoFlow flow, LyncServer server) { _flow = flow; _server = server; Recorder recorder = new Recorder(); recorder.AttachFlow(_flow); WmaFileSink sink = new WmaFileSink("voice_input.wma"); recorder.SetSink(sink); recorder.Start(); var wmafileReader = new WMAFileReader("voice_input.wma"); WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(wmafileReader); int reader = 0; byte[] buffer = new byte[4096]; var header = GetWaveHeader(waveStream.WaveFormat); _speech.SendMessage(new ArraySegment <byte>(header, 0, header.Length)); while ((reader = waveStream.Read(buffer, 0, buffer.Length)) != 0) { AudioDataAvailable(new ArraySegment <byte>(buffer, 0, buffer.Length)); Thread.Sleep(10); } }
private WaveOut CreatePlay(string file) { string ext = Path.GetExtension(file); if (file.EndsWith(".mp3")) { using (var reader = new Mp3FileReader(file)) { using (var waveStream = WaveFormatConversionStream.CreatePcmStream(reader)) { using (var blockStream = new BlockAlignReductionStream(waveStream)) { play = new WaveOut(WaveCallbackInfo.FunctionCallback()); play.Init(blockStream); } } } } System.Media.SoundPlayer else if (file.EndsWith(".wma")) { using (var reader = new WMAFileReader(file)) { using (var waveStream = WaveFormatConversionStream.CreatePcmStream(reader)) { using (var blockStream = new BlockAlignReductionStream(waveStream)) { play = new WaveOut(WaveCallbackInfo.FunctionCallback()); play.Init(blockStream); } } } } }
public byte[] ConvertToMp3(string file, int bitrate = 128) { using (var retMs = new MemoryStream()) using (var rdr = new WMAFileReader(file)) using (var wtr = new NAudio.Lame.LameMP3FileWriter(retMs, rdr.WaveFormat, bitrate)) { rdr.CopyTo(wtr); return(retMs.ToArray()); } }
/// <summary> /// Converts a WMA file to a WAV stream /// </summary> /// <param name="outputStream">Stream to store the converted wav.</param> /// <param name="filePath">Path to a .wma file to convert</param> /// <returns>The WaveFormat object of the converted wav</returns> private static WaveFormat wmaToWav(string pathToWma, Stream outputStream, int sampleRate, int bitDepth, int numChannels) { if (!Path.GetExtension(pathToWma).ToLowerInvariant().Contains("wma")) { throw new ArgumentException("Must be a .wma file!"); } using (var reader = new WMAFileReader(pathToWma)) { var targetFormat = new NAudio.Wave.WaveFormat(sampleRate, bitDepth, numChannels); var pcmStream = new WaveFormatConversionStream(targetFormat, reader); var buffer = new byte[pcmStream.Length]; pcmStream.Read(buffer, 0, (int)pcmStream.Length); outputStream.Write(buffer, 0, buffer.Length); outputStream.Position = 0; pcmStream.Close(); return(targetFormat); } }
static public Sound Read(byte[] source) { Sound result = new Sound(); string fileName = "tmp.wma"; File.WriteAllBytes(fileName, source); WMAFileReader fileReader = new WMAFileReader(fileName); File.Delete(fileName); using (WaveStream wavStream = WaveFormatConversionStream.CreatePcmStream(fileReader)) { int bytesToRead; // using a mux, we force all sounds to be 2 channels bytesToRead = (int)wavStream.Length; byte[] rawWaveData = new byte[bytesToRead]; int bytesRead = wavStream.Read(rawWaveData, 0, bytesToRead); result.SetSound(rawWaveData, wavStream.WaveFormat); } return(result); }
/// <summary> /// Creates an input WaveChannel (Audio file reader for MP3/WAV/OGG/FLAC/WMA/AIFF/Other formats in the future) /// </summary> /// <param name="filename"></param> private void CreateInputWaveChannel(string filename) { string fileExt = Path.GetExtension(filename.ToLower()); if (fileExt == MP3Extension) { m_waveReader = new Mp3FileReader(filename); m_blockAlignedStream = new BlockAlignReductionStream(m_waveReader); // Wave channel - reads from file and returns raw wave blocks m_waveChannel = new WaveChannel32(m_blockAlignedStream); } else if (fileExt == WAVExtension) { m_waveReader = new WaveFileReader(filename); if (m_waveReader.WaveFormat.Encoding != WaveFormatEncoding.Pcm) { m_waveReader = WaveFormatConversionStream.CreatePcmStream(m_waveReader); m_waveReader = new BlockAlignReductionStream(m_waveReader); } if (m_waveReader.WaveFormat.BitsPerSample != 16) { var format = new WaveFormat(m_waveReader.WaveFormat.SampleRate, 16, m_waveReader.WaveFormat.Channels); m_waveReader = new WaveFormatConversionStream(format, m_waveReader); } m_waveChannel = new WaveChannel32(m_waveReader); } else if (fileExt == OGGVExtension) { m_waveReader = new OggVorbisFileReader(filename); if (m_waveReader.WaveFormat.Encoding != WaveFormatEncoding.Pcm) { m_waveReader = WaveFormatConversionStream.CreatePcmStream(m_waveReader); m_waveReader = new BlockAlignReductionStream(m_waveReader); } if (m_waveReader.WaveFormat.BitsPerSample != 16) { var format = new WaveFormat(m_waveReader.WaveFormat.SampleRate, 16, m_waveReader.WaveFormat.Channels); m_waveReader = new WaveFormatConversionStream(format, m_waveReader); } m_waveChannel = new WaveChannel32(m_waveReader); } else if (fileExt == FLACExtension) { m_waveReader = new FLACFileReader(filename); if (m_waveReader.WaveFormat.Encoding != WaveFormatEncoding.Pcm) { m_waveReader = WaveFormatConversionStream.CreatePcmStream(m_waveReader); m_waveReader = new BlockAlignReductionStream(m_waveReader); } if (m_waveReader.WaveFormat.BitsPerSample != 16) { var format = new WaveFormat(m_waveReader.WaveFormat.SampleRate, 16, m_waveReader.WaveFormat.Channels); m_waveReader = new WaveFormatConversionStream(format, m_waveReader); } m_waveChannel = new WaveChannel32(m_waveReader); } else if (fileExt == WMAExtension) { m_waveReader = new WMAFileReader(filename); if (m_waveReader.WaveFormat.Encoding != WaveFormatEncoding.Pcm) { m_waveReader = WaveFormatConversionStream.CreatePcmStream(m_waveReader); m_waveReader = new BlockAlignReductionStream(m_waveReader); } if (m_waveReader.WaveFormat.BitsPerSample != 16) { var format = new WaveFormat(m_waveReader.WaveFormat.SampleRate, 16, m_waveReader.WaveFormat.Channels); m_waveReader = new WaveFormatConversionStream(format, m_waveReader); } m_waveChannel = new WaveChannel32(m_waveReader); } else if (fileExt == AIFFExtension) { m_waveReader = new AiffFileReader(filename); m_waveChannel = new WaveChannel32(m_waveReader); } else { throw new ApplicationException("Cannot create Input WaveChannel - Unknown file type: " + fileExt); } }