Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NAudio-录制多个设备并保存到MP3_C#_Naudio - Fatal编程技术网

C# NAudio-录制多个设备并保存到MP3

C# NAudio-录制多个设备并保存到MP3,c#,naudio,C#,Naudio,我已经为NAudio购买了PluralSight课程,我正在看很多视频 我正试图找出如何执行以下操作: 将所有麦克风和扬声器录制到一个MP3中 到目前为止,我有以下代码: using NAudio.CoreAudioApi; using NAudio.Wave; using NAudio.Wave.SampleProviders; using System; using System.Collections.Generic; using System.Collections.ObjectMode

我已经为NAudio购买了PluralSight课程,我正在看很多视频

我正试图找出如何执行以下操作: 将所有麦克风和扬声器录制到一个MP3中

到目前为止,我有以下代码:

using NAudio.CoreAudioApi;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AudioTest1 {

    public class AudioDevice {
        public MMDevice Device { get; set; }
        public bool IsSelected { get; set; }
        public string FriendlyName { get; set; }
        private IWaveIn __Capture;
        public IWaveIn Capture {
            get {
                return __Capture;
            }
            set {
                if(__Capture != null) {
                    __Capture.DataAvailable -= this.__Capture_DataAvailable;
                }

                __Capture = value;

                if (__Capture != null) {
                    __Capture.DataAvailable += this.__Capture_DataAvailable;
                }
            }
        }

        private void __Capture_DataAvailable(object sender, WaveInEventArgs e) {
            if(Buffer != null) {
                Buffer.AddSamples(e.Buffer, 0, e.BytesRecorded);
            }
        }

        public IWaveProvider Provider { get; set; }
        public BufferedWaveProvider Buffer { get; set; }

        public IWaveProvider Output { get; set; }

    }

    public class MainWindowDataModel {

        public ObservableCollection<AudioDevice> AudioDevices { get; private set; } = new ObservableCollection<AudioDevice>();

        public WaveFormat Format = new WaveFormat(16000, 8, 1);

        public MainWindowDataModel(){


            using (var Enumerator = new MMDeviceEnumerator()) {
                var Devices = Enumerator.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active).ToList();
                foreach(var Device in Devices) {

                    Device.AudioEndpointVolume.Mute = false;
                    var Capture =
                        Device.DataFlow == DataFlow.Capture
                        ? new WasapiCapture(Device)
                        : new WasapiLoopbackCapture(Device)
                        ;

                    var Provider = new WaveInProvider(Capture);
                    var Silence = new SilenceProvider(Capture.WaveFormat);
                    var Buffer = new BufferedWaveProvider(Capture.WaveFormat);

                    var Output = Device.DataFlow == DataFlow.Capture
                        ? new MixingWaveProvider32(new IWaveProvider[] { Provider })
                        : new MixingWaveProvider32(new IWaveProvider[] { Silence, Buffer })
                        ;


                    AudioDevices.Add(new AudioDevice() {
                        Device = Device,
                        IsSelected = true,
                        FriendlyName = Device.FriendlyName,
                        Capture = Capture,
                        Provider = Provider,
                        Buffer = Buffer,
                        Output = Output,
                    });

                    Capture.StartRecording();

                }

                var C = new MixingSampleProvider(from x in AudioDevices select x.Output.ToSampleProvider());
                //C.AddMixerInput(new SignalGenerator());

                WaveFileWriter.CreateWaveFile(@"C:\Test2.wav", C.Take(TimeSpan.FromSeconds(5)).ToWaveProvider());


            }



        }

    }
}
使用NAudio.CoreAudioApi;
使用NAudio.波;
使用NAudio.Wave.SampleProviders;
使用制度;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间AudioTest1{
公共级音频设备{
公用MMDevice{get;set;}
公共布尔值被选为{get;set;}
公共字符串FriendlyName{get;set;}
私人伊瓦文(IWaveIn);;
捕获公共信息{
得到{
返回-捕获;
}
设置{
如果(_捕获!=null){
__Capture.DataAvailable-=此。u Capture_DataAvailable;
}
__捕获=价值;
如果(_捕获!=null){
__Capture.DataAvailable+=这个;
}
}
}
私有void uu捕获u数据可用(对象发送方,WaveInEventArgs e){
if(缓冲区!=null){
Buffer.AddSamples(e.Buffer,0,e.bytes记录);
}
}
公共IWaveProvider提供程序{get;set;}
公共BufferedWaveProvider缓冲区{get;set;}
公共IWaveProvider输出{get;set;}
}
公共类MainWindowDataModel{
公共ObservableCollection AudioDevices{get;private set;}=new ObservableCollection();
公共波形格式=新波形格式(16000,8,1);
公共MainWindowDataModel(){
使用(var枚举器=新的MMDeviceEnumerator()){
var Devices=Enumerator.EnumerateAudioEndPoints(DataFlow.All,DeviceState.Active).ToList();
foreach(设备中的var设备){
Device.AudioEndpointVolume.Mute=false;
变量捕获=
Device.DataFlow==DataFlow.Capture
?新WASAPICATure(设备)
:新WasapiLoopbackCapture(设备)
;
var Provider=新的WaveInProvider(捕获);
var沉默=新沉默提供程序(捕获波形);
var Buffer=新的BufferedWaveProvider(Capture.WaveFormat);
var Output=Device.DataFlow==DataFlow.Capture
?新的MixingWaveProvider32(新的IWaveProvider[]{Provider})
:new mixingwaveprovider 32(new IWaveProvider[]{silene,Buffer})
;
AudioDevices.Add(新的AudioDevices(){
设备=设备,
IsSelected=true,
FriendlyName=Device.FriendlyName,
捕获=捕获,
提供者=提供者,
缓冲区=缓冲区,
输出=输出,
});
Capture.StartRecording();
}
var C=新的混音采样提供程序(从AudioDevices中的x选择x.Output.ToSampleProvider());
//C.AddMixerInput(新的信号发生器());
创建波形文件(@“C:\Test2.wav”,C.Take(TimeSpan.FromSeconds(5)).ToWaveProvider());
}
}
}
}

当我尝试创建wave文件时,函数似乎立即返回,而不是等待5秒钟过去。对我来说,一起录制此音频的最佳方式是什么?

在录制时动态混合音频实际上相当棘手,因为缓冲区从每个录制设备到达的时间不同,持续时间也可能不同。此外,如果录制未在同一时间开始,或者存在辍学,则可能需要解决同步问题

我通常推荐的最简单的解决方案是,人们将每个麦克风录制到一个临时文件中,然后将其混合


如果你真的想在飞行中混合它们,那么我会使用类似于
BufferedWaveProvider
的东西来存储来自每个录音设备的音频。每当新录制的音频缓冲区进入时,它就会被写入相应的
BufferedWaveProvider
。然后,一旦所有的
BufferedWaveProviders
中的音频超过某个阈值,就将该数量的音频混合到目标文件中。

Mark,我正在记录到不同的文件中,但我仍然不确定如何对齐混合,因为正如您所说,源不对齐。具体来说,我是从WaveIn和WASAPI录制的
WaveIn
总是在每个
data
事件上发送完全相同的数据量,但是WASAPI发送的数据量是可变的,很难看到如何排列它们。我不能等到整个录音结束,因为它可能会持续几个小时,我需要能够在一路上采取混合小节。