/// <summary> /// Converte um WaveStream para MP3Stream. /// </summary> /// <param name="wavFile"/> /// <returns></returns> public static MemoryStream ConverterWaveStreamParaMp3Stream(Wave32To16Stream wavFile) { var retMs = new MemoryStream(); using (var wtr = new LameMP3FileWriter(retMs, wavFile.WaveFormat, 128)) { wavFile.CopyTo(wtr); return retMs; } }
public WasapiProvider() { // Init Pipes this.recordingStream = new PipeStream(); this.LoopbackMp3Stream = new PipeStream(); this.LoopbackL16Stream = new PipeStream(); // Init Wave Processor thread Thread waveProcessorThread = new Thread(new ThreadStart(this.waveProcessor)) { Priority = ThreadPriority.Highest }; // Init Wasapi Capture this.loopbackWaveIn = new WasapiLoopbackCapture(); this.loopbackWaveIn.DataAvailable += new EventHandler<WaveInEventArgs>(this.loopbackWaveIn_DataAvailable); // Init Raw Wav (16bit) WaveStream rawWave16b = new Wave32To16Stream(new RawSourceWaveStream(this.recordingStream, NAudio.Wave.WaveFormat.CreateIeeeFloatWaveFormat(this.loopbackWaveIn.WaveFormat.SampleRate, this.loopbackWaveIn.WaveFormat.Channels))); // Convert Raw Wav to PCM with audio format in settings var audioFormat = AudioSettings.GetAudioFormat(); if (rawWave16b.WaveFormat.SampleRate == audioFormat.SampleRate && rawWave16b.WaveFormat.BitsPerSample == audioFormat.BitsPerSample && rawWave16b.WaveFormat.Channels == audioFormat.Channels) { // No conversion ! this.rawConvertedStream = null; this.pcmStream = WaveFormatConversionStream.CreatePcmStream(rawWave16b); } else { // Resampler this.rawConvertedStream = new WaveProviderToWaveStream(new MediaFoundationResampler(rawWave16b, audioFormat)); this.pcmStream = WaveFormatConversionStream.CreatePcmStream(rawConvertedStream); } // Init MP3 Encoder this.mp3Writer = new LameMP3FileWriter(this.LoopbackMp3Stream, pcmStream.WaveFormat, AudioSettings.GetMP3Bitrate()); // Start Recording this.loopbackWaveIn.StartRecording(); // Start Wave Processor thread waveProcessorThread.Start(); }
private byte[] RealMix(ReceivedRtp item1, ReceivedRtp item2) { if (item1 == null || item2 == null) return null; if (item1.size == 0 || item2.size == 0) return null; byte[] wavSrc1 = new byte[item1.size - headersize]; byte[] wavSrc2 = new byte[item2.size - headersize]; Array.Copy(item1.buff, headersize, wavSrc1, 0, (item1.size - headersize)); Array.Copy(item2.buff, headersize, wavSrc2, 0, (item2.size - headersize)); WaveMixerStream32 mixer = new WaveMixerStream32(); // mixer.AutoStop = true; MemoryStream memstrem = new MemoryStream(wavSrc1); RawSourceWaveStream rawsrcstream = new RawSourceWaveStream(memstrem, this.codec); WaveFormatConversionStream conversionstream = new WaveFormatConversionStream(pcmFormat16, rawsrcstream); WaveChannel32 channelstream = new WaveChannel32(conversionstream); mixer.AddInputStream(channelstream); memstrem = new MemoryStream(wavSrc2); rawsrcstream = new RawSourceWaveStream(memstrem, this.codec); conversionstream = new WaveFormatConversionStream(pcmFormat16, rawsrcstream); channelstream = new WaveChannel32(conversionstream); mixer.AddInputStream(channelstream); mixer.Position = 0; Wave32To16Stream to16 = new Wave32To16Stream(mixer); var convStm = new WaveFormatConversionStream(pcmFormat8, to16); byte[] mixedbytes = new byte[(int)convStm.Length]; int chk = convStm.Read(mixedbytes, 0, (int)convStm.Length); //Buffer.BlockCopy(tobyte, 0, writingBuffer, 0, tobyte.Length); memstrem.Close(); rawsrcstream.Close(); conversionstream.Close(); channelstream.Close(); convStm.Close(); convStm.Dispose(); convStm = null; to16.Close(); to16.Dispose(); to16 = null; mixer.Close(); mixer.Dispose(); mixer = null; return mixedbytes; }
/// <summary> /// Mescla dois arquivos Mp3, sobrepondo suas faixas. /// </summary> public static MemoryStream MesclarMp3(string arquivoTrilhaSonora, float volumeTrilha, string arquivoVoz, float volumeVoz) { // Lê os arquivos MP3 do disco Mp3FileReader mpTrilhaSonora = new Mp3FileReader(arquivoTrilhaSonora); Mp3FileReader mpVoz = new Mp3FileReader(arquivoVoz); //Decodifica os arquivos MP3 WaveStream trilhaSonora = WaveFormatConversionStream.CreatePcmStream(mpTrilhaSonora); WaveStream voz = WaveFormatConversionStream.CreatePcmStream(mpVoz); var mixer = new WaveMixerStream32 { AutoStop = true }; //var vozOffset = trilhaSonora.TotalTime; //var vozOffsetted = new WaveOffsetStream(voz, TimeSpan.FromSeconds(10), TimeSpan.Zero, voz.TotalTime.Subtract(TimeSpan.FromSeconds(30))); var trilhaSonora32 = new WaveChannel32(trilhaSonora) { PadWithZeroes = false, Volume = volumeTrilha }; mixer.AddInputStream(trilhaSonora32); var voz32 = new WaveChannel32(voz) { PadWithZeroes = false, Volume = volumeVoz }; mixer.AddInputStream(voz32); var wave32 = new Wave32To16Stream(mixer); // Codifica o Wave para MP3 return ConverterWaveStreamParaMp3Stream(wave32); }
private void ProcessMixingFinal(RcvData data, int dataSize) { string processingFn = string.Format("e:\\{0}_{1}_{2}.wav", data.seqnum, data.extension, data.peernumber); List<RecInfos> ls0 = lExtension0.FindAll( delegate(RecInfos list) { return list.rcvData.Equals(data) && list.isExtension == 0; }); List<RecInfos> ls1 = lExtension1.FindAll( delegate(RecInfos list) { return list.rcvData.Equals(data) && list.isExtension == 1; }); IsExtensionComparer isExtensionCompare = new IsExtensionComparer(); ls0.Sort(isExtensionCompare); ls1.Sort(isExtensionCompare); int count = 0; int count0 = ls0.Count(); int count1 = ls1.Count(); if (count0 - count1 < 0) count = count0; else count = count1; for (int i = 0; i < count; i++) { if (ls0[i].seq == ls1[i].seq) { // 믹싱 byte[] wavSrc0 = new byte[160]; byte[] wavSrc1 = new byte[160]; Array.Copy(ls0[i].voice, 12, wavSrc0, 0, wavSrc0.Length); Array.Copy(ls1[i].voice, 12, wavSrc1, 0, wavSrc1.Length); WaveMixerStream32 mixer = new WaveMixerStream32(); //mixer.AutoStop = true; WaveChannel32 channelStm = null; for (int j = 0; j < 2; j++) { MemoryStream memStm = null; BufferedStream bufStm = null; RawSourceWaveStream rawSrcStm = null; WaveFormatConversionStream conversionStm = null; if (j == 0) memStm = new MemoryStream(wavSrc0); else memStm = new MemoryStream(wavSrc1); bufStm = new BufferedStream(memStm); rawSrcStm = new RawSourceWaveStream(bufStm, mulawFormat); conversionStm = new WaveFormatConversionStream(pcmFormat, rawSrcStm); channelStm = new WaveChannel32(conversionStm); mixer.AddInputStream(channelStm); } mixer.Position = 0; if (File.Exists(processingFn)) { var wavefilestream = new WaveFileReader(processingFn); byte[] wavefilebyte = new byte[(int)wavefilestream.Length]; int chk0 = wavefilestream.Read(wavefilebyte, 0, wavefilebyte.Length); Wave32To16Stream to16 = new Wave32To16Stream(mixer); var conversionStm = new WaveFormatConversionStream(pcmFormat, to16); byte[] tobyte = new byte[(int)conversionStm.Length]; int chk1 = conversionStm.Read(tobyte, 0, (int)conversionStm.Length); byte[] desByte = new byte[wavefilebyte.Length + tobyte.Length]; conversionStm.Close(); wavefilestream.Close(); Buffer.BlockCopy(wavefilebyte, 0, desByte, 0, wavefilebyte.Length); Buffer.BlockCopy(tobyte, 0, desByte, wavefilebyte.Length, tobyte.Length); using (MemoryStream memStm = new MemoryStream(desByte)) using (BufferedStream buffStm = new BufferedStream(memStm)) using (RawSourceWaveStream rawSrcStm = new RawSourceWaveStream(buffStm, pcmFormat)) { WaveFileWriter.CreateWaveFile(processingFn, rawSrcStm); } } else { var mixedStm = new Wave32To16Stream(mixer); var convStm = new WaveFormatConversionStream(pcmFormat, mixedStm); WaveFileWriter.CreateWaveFile(processingFn, convStm); convStm.Close(); mixedStm.Close(); } mixer.Close(); // 삭제 lExtension0.Remove(ls0[i]); lExtension1.Remove(ls1[i]); } else if (ls0[i].seq - ls1[i].seq < 0) { // ls0 만 믹싱 // ls0 원본에 byte[] 붙임 > 원본 byte[]를 wavesream 으로 변환 > wave 파일로 저장 if (File.Exists(processingFn)) { //wavefilestream = new WaveFileReader(processingFn); } else { } // 삭제 lExtension0.Remove(ls0[i]); ls1.Insert(i + 1, ls1[i]); } else if (ls0[i].seq - ls1[i].seq > 0) { // ls1 만 믹싱 // ls1 원본에 byte[] 붙임 > 원본 byte[]를 wavesream 으로 변환 > wave 파일로 저장 if (File.Exists(processingFn)) { //wavefilestream = new WaveFileReader(processingFn); } else { } // 삭제 lExtension1.Remove(ls1[i]); ls0.Insert(i + 1, ls0[i]); } } }
private void ProcessMixing2(RcvData data, int dataSize) { string processingFn = string.Format("d:\\{0}_{1}_{2}.wav", data.seqnum, data.extension, data.peernumber); List<RecInfos> ls0 = lExtension0.FindAll( delegate(RecInfos list) { return list.rcvData.Equals(data) && list.isExtension == 0; }); List<RecInfos> ls1 = lExtension1.FindAll( delegate(RecInfos list) { return list.rcvData.Equals(data) && list.isExtension == 1; }); IsExtensionComparer isExtensionCompare = new IsExtensionComparer(); ls0.Sort(isExtensionCompare); ls1.Sort(isExtensionCompare); int count = 0; int count0 = ls0.Count(); int count1 = ls1.Count(); if (count0 - count1 < 0) count = count0; else count = count1; byte[] buffWriting = new byte[320 * count]; for (int i = 0; i < count; i++) { if (ls0[i].seq == ls1[i].seq) { // 믹싱 // 코덱 종류에 따라서 바이트 길이는 달라질 수 있다. 실제로 만들 때 경우의 수 확인하고 만들어야 한다. byte[] wavSrc0 = new byte[160]; byte[] wavSrc1 = new byte[160]; Array.Copy(ls0[i].voice, 12, wavSrc0, 0, wavSrc0.Length); Array.Copy(ls1[i].voice, 12, wavSrc1, 0, wavSrc1.Length); WaveMixerStream32 mixer = new WaveMixerStream32(); //mixer.AutoStop = true; WaveChannel32 channelStm = null; MemoryStream memStm = null; BufferedStream bufStm = null; RawSourceWaveStream rawSrcStm = null; WaveFormatConversionStream conversionStm = null; for (int j = 0; j < 2; j++) { if (j == 0) memStm = new MemoryStream(wavSrc0); else memStm = new MemoryStream(wavSrc1); bufStm = new BufferedStream(memStm); rawSrcStm = new RawSourceWaveStream(bufStm, mulawFormat); conversionStm = new WaveFormatConversionStream(pcmFormat, rawSrcStm); channelStm = new WaveChannel32(conversionStm); mixer.AddInputStream(channelStm); } mixer.Position = 0; Wave32To16Stream to16 = new Wave32To16Stream(mixer); var convStm = new WaveFormatConversionStream(pcmFormat, to16); byte[] tobyte = new byte[(int)convStm.Length]; int chk = convStm.Read(tobyte, 0, (int)convStm.Length); Buffer.BlockCopy(tobyte, 0, buffWriting, i * tobyte.Length, tobyte.Length); conversionStm.Close(); rawSrcStm.Close(); bufStm.Close(); memStm.Close(); convStm.Close(); to16.Close(); channelStm.Close(); mixer.Close(); // 삭제 lExtension0.Remove(ls0[i]); lExtension1.Remove(ls1[i]); } else if (ls0[i].seq - ls1[i].seq < 0) { // ls0 만 믹싱 // ls0 원본에 byte[] 붙임 > 원본 byte[]를 wavesream 으로 변환 > wave 파일로 저장 // 믹싱 // 코덱 종류에 따라서 바이트 길이는 달라질 수 있다. 실제로 만들 때 경우의 수 확인하고 만들어야 한다. byte[] wavSrc0 = new byte[160]; byte[] wavSrc1 = new byte[160]; Array.Copy(ls0[i].voice, 12, wavSrc0, 0, wavSrc0.Length); Array.Copy(ls1[i].voice, 12, wavSrc1, 0, wavSrc1.Length); WaveMixerStream32 mixer = new WaveMixerStream32(); //mixer.AutoStop = true; WaveChannel32 channelStm = null; MemoryStream memStm = null; BufferedStream bufStm = null; RawSourceWaveStream rawSrcStm = null; WaveFormatConversionStream conversionStm = null; for (int j = 0; j < 2; j++) { if (j == 0) memStm = new MemoryStream(wavSrc0); else memStm = new MemoryStream(wavSrc1); bufStm = new BufferedStream(memStm); rawSrcStm = new RawSourceWaveStream(bufStm, mulawFormat); conversionStm = new WaveFormatConversionStream(pcmFormat, rawSrcStm); channelStm = new WaveChannel32(conversionStm); mixer.AddInputStream(channelStm); } mixer.Position = 0; Wave32To16Stream to16 = new Wave32To16Stream(mixer); var convStm = new WaveFormatConversionStream(pcmFormat, to16); byte[] tobyte = new byte[(int)convStm.Length]; int chk = convStm.Read(tobyte, 0, (int)convStm.Length); Buffer.BlockCopy(tobyte, 0, buffWriting, i * tobyte.Length, tobyte.Length); conversionStm.Close(); rawSrcStm.Close(); bufStm.Close(); memStm.Close(); convStm.Close(); to16.Close(); channelStm.Close(); mixer.Close(); // 삭제 lExtension0.Remove(ls0[i]); ls1.Insert(i + 1, ls1[i]); } else if (ls0[i].seq - ls1[i].seq > 0) { // ls1 만 믹싱 // ls1 원본에 byte[] 붙임 > 원본 byte[]를 wavesream 으로 변환 > wave 파일로 저장 // 믹싱 // 코덱 종류에 따라서 바이트 길이는 달라질 수 있다. 실제로 만들 때 경우의 수 확인하고 만들어야 한다. byte[] wavSrc0 = new byte[160]; byte[] wavSrc1 = new byte[160]; Array.Copy(ls0[i].voice, 12, wavSrc0, 0, wavSrc0.Length); Array.Copy(ls1[i].voice, 12, wavSrc1, 0, wavSrc1.Length); WaveMixerStream32 mixer = new WaveMixerStream32(); //mixer.AutoStop = true; WaveChannel32 channelStm = null; MemoryStream memStm = null; BufferedStream bufStm = null; RawSourceWaveStream rawSrcStm = null; WaveFormatConversionStream conversionStm = null; for (int j = 0; j < 2; j++) { if (j == 0) memStm = new MemoryStream(wavSrc0); else memStm = new MemoryStream(wavSrc1); bufStm = new BufferedStream(memStm); rawSrcStm = new RawSourceWaveStream(bufStm, mulawFormat); conversionStm = new WaveFormatConversionStream(pcmFormat, rawSrcStm); channelStm = new WaveChannel32(conversionStm); mixer.AddInputStream(channelStm); } mixer.Position = 0; Wave32To16Stream to16 = new Wave32To16Stream(mixer); var convStm = new WaveFormatConversionStream(pcmFormat, to16); byte[] tobyte = new byte[(int)convStm.Length]; int chk = convStm.Read(tobyte, 0, (int)convStm.Length); Buffer.BlockCopy(tobyte, 0, buffWriting, i * tobyte.Length, tobyte.Length); conversionStm.Close(); rawSrcStm.Close(); bufStm.Close(); memStm.Close(); convStm.Close(); to16.Close(); channelStm.Close(); mixer.Close(); // 삭제 lExtension1.Remove(ls1[i]); ls0.Insert(i + 1, ls0[i]); } } // 10개의 버프를 바이트로 만들어 WaveFileWrite WaveFileWriting(buffWriting, processingFn); }