我正在构建一个应用程序,它将麦克风和扬声器的音频录制到文件(.wav)。我希望我的应用程序将数据从麦克风和扬声器写入文件到同一个合并文件。我无法为两者保存合并文件。它仅为麦克风保存。
这是我的代码:
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Test_Recorder
{
public partial class Form2 : Form
{
private WaveIn recorder;
private BufferedWaveProvider bufferedWaveProvider;
private SavingWaveProvider savingWaveProvider;
private WaveOut player;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StartRecording();
}
private void button2_Click(object sender, EventArgs e)
{
StopRecording();
}
public void StartRecording()
{
// set up the recorder
recorder = new WaveIn();
//recorder = new WasapiLoopbackCapture();
//recorder = new WaveIn();
recorder.DataAvailable += RecorderOnDataAvailable;
// set up our signal chain
bufferedWaveProvider = new BufferedWaveProvider(recorder.WaveFormat);
savingWaveProvider = new SavingWaveProvider(bufferedWaveProvider, Guid.NewGuid() + ".wav");
// set up playback
player = new WaveOut();
player.Init(savingWaveProvider);
player.Volume = 1;
// begin playback & record
player.Play();
recorder.StartRecording();
}
public void StopRecording()
{
// stop recording
recorder.StopRecording();
// stop playback
player.Stop();
// finalise the WAV file
savingWaveProvider.Dispose();
}
private void RecorderOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
bufferedWaveProvider.AddSamples(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
}
private void button3_Click(object sender, EventArgs e)
{
//rec.BeginRecording();
Form3 frm = new Form3();
this.Hide();
frm.Show();
}
}
class SavingWaveProvider : IWaveProvider, IDisposable
{
private readonly IWaveProvider sourceWaveProvider;
private readonly WaveFileWriter writer;
private bool isWriterDisposed;
public SavingWaveProvider(IWaveProvider sourceWaveProvider, string wavFilePath)
{
this.sourceWaveProvider = sourceWaveProvider;
writer = new WaveFileWriter(wavFilePath, sourceWaveProvider.WaveFormat);
}
public int Read(byte[] buffer, int offset, int count)
{
var read = sourceWaveProvider.Read(buffer, offset, count);
if (count > 0 && !isWriterDisposed)
{
writer.Write(buffer, offset, read);
}
if (count == 0)
{
Dispose(); // auto-dispose in case users forget
}
return read;
}
public WaveFormat WaveFormat { get { return sourceWaveProvider.WaveFormat; } }
public void Dispose()
{
if (!isWriterDisposed)
{
isWriterDisposed = true;
writer.Dispose();
}
}
}
}
我如何才能将扬声器和麦克风都用于同一个文件?
答案 0 :(得分:1)
首先,您不能将2个数据流保存到一个已打开的文件中。你需要创建2个独立的Memory Streams并将它们加入一些奇特的魔法dll :)然后你可以用StreamWriter或其他文件写入方法将这一个内存流保存到文件中
为了保存谷歌搜索,我发现这一小段代码可以帮助你解决问题。 http://www.codeproject.com/Articles/31356/C-MP3-Sound-Capturing-Recording-Component以及此How to record audio from microphone (Line-In) and playback device (speakers Line-Out) to one MP3 file
还有一个提示。更好的做法是使用"使用"然后调用Dispose。
答案 1 :(得分:1)
可能最简单的方法是将两者保存到单独的文件中,然后在录制后使用MixingSampleProvider
将它们混合在一起。如果两者都以相同的波形格式(采样率,位深度,通道数)记录,也将是最简单的。