naudio - Record audio from microphone and speaker in C# -
i'm building application record audio microphone , speaker file (.wav). want application write data file microphone , speaker same merged file. i'm having trouble save merged file both. saves microphone.
this code:
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 recorder recorder = new wavein(); //recorder = new wasapiloopbackcapture(); //recorder = new wavein(); recorder.dataavailable += recorderondataavailable; // set our signal chain bufferedwaveprovider = new bufferedwaveprovider(recorder.waveformat); savingwaveprovider = new savingwaveprovider(bufferedwaveprovider, guid.newguid() + ".wav"); // set 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 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 { { return sourcewaveprovider.waveformat; } } public void dispose() { if (!iswriterdisposed) { iswriterdisposed = true; writer.dispose(); } } } }
how can both speaker , microphone same file?
probably easiest approach save both separate files, , use mixingsampleprovider
mix them after recording. easiest if both recorded in same wave format (sample rate, bit depth, channel count).
Comments
Post a Comment