我正在使用多输入声卡,我希望实现多个输入的实时混音。所有输入都是立体声,所以我需要先将它们分开,混合选择的通道并将它们作为单声道流提供。
目标将是这样的混合频道1 [左] +频道3 [右] +频道4 [右] - >单流。
我已经实现了这样的流程链:
1) WaveIn - >为每个频道创建 BufferedWaveProvider - >使用 wavein.DataAvailable + = {buffwavprovider [channel] .AddSamples(...)将样本(只是当前频道的样本)添加到每个BufferedWaveProvider ... 这给了我一个很好的BufferdWaveProvider列表。这里的分割音频部分正确实现。
2)选择多个BufferedWaveProviders并将它们提供给 MixingWaveProvider32 。然后创建一个WaveStream(使用WaveMixerStream32 and IWaveProvider)。
3)MultiChannelToMonoStream接受WaveStream并生成混合。这也有效。
但结果是,音频被切断了。就像缓冲区有些麻烦......
这是解决此问题的正确方法,还是有更好的解决方法?
编辑 - 已添加代码:
public class AudioSplitter
{
public List<NamedBufferedWaveProvider> WaveProviders { private set; get; }
public string Name { private set; get; }
private WaveIn _wavIn;
private int bytes_per_sample = 4;
/// <summary>
/// Splits up one WaveIn into one BufferedWaveProvider for each channel
/// </summary>
/// <param name="wavein"></param>
/// <returns></returns>
public AudioSplitter(WaveIn wavein, string name)
{
if (wavein.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat)
throw new Exception("Format must be IEEE float");
WaveProviders = new List<NamedBufferedWaveProvider>(wavein.WaveFormat.Channels);
Name = name;
_wavIn = wavein;
_wavIn.StartRecording();
var outFormat = NAudio.Wave.WaveFormat.CreateIeeeFloatWaveFormat(wavein.WaveFormat.SampleRate, 1);
for (int i = 0; i < wavein.WaveFormat.Channels; i++)
{
WaveProviders.Add(new NamedBufferedWaveProvider(outFormat) { DiscardOnBufferOverflow = true, Name = Name + "_" + i });
}
bytes_per_sample = _wavIn.WaveFormat.BitsPerSample / 8;
wavein.DataAvailable += Wavein_DataAvailable;
}
/// <summary>
/// add samples for each channel to bufferedwaveprovider
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Wavein_DataAvailable(object sender, WaveInEventArgs e)
{
int channel = 0;
byte[] buffer = e.Buffer;
for (int i = 0; i < e.BytesRecorded - bytes_per_sample; i = i + bytes_per_sample)
{
byte[] channel_buffer = new byte[bytes_per_sample];
for (int j = 0; j < bytes_per_sample; j++)
{
channel_buffer[j] = buffer[i + j];
}
WaveProviders[channel].AddSamples(channel_buffer, 0, channel_buffer.Length);
channel++;
if (channel >= _wavIn.WaveFormat.Channels)
channel = 0;
}
}
}
每个通道使用Audiosplitter可提供缓冲波提供商列表(单声道32位浮点数)。
var mix = new MixingWaveProvider32(_waveProviders);
var wps = new WaveProviderToWaveStream(mix);
MultiChannelToMonoStream mms = new MultiChannelToMonoStream(wps);
new Thread(() =>
{
byte[] buffer = new byte[4096];
while (mms.Read(buffer, 0, buffer.Length) > 0 && isrunning)
{
using (FileStream fs = new FileStream("C:\\temp\\audio\\mono_32.wav", FileMode.Append, FileAccess.Write))
{
fs.Write(buffer, 0, buffer.Length);
}
}
}).Start();
答案 0 :(得分:0)
还有一些空间可供优化,但基本上这可以完成工作:
myletter = mycell.Value2 ' get the Value2 (without the cell format)
With mycell.Offset(1, 1)
.NumberFormat = "@" ' first: format the destination cell as text
.Value2 = myletter ' second: get the Value2 (just in case)
End With
我们需要为每个频道提供WaveProvider(WaveProviders [j])。