naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.56k stars 1.1k forks source link

Problem with simultaneous recording and playback #475

Open frankfriess opened 5 years ago

frankfriess commented 5 years ago

after few days of unsucessful searching for a way to control audio recording in vb.net in a sufficient way, I found Naudio and I am really impressesed by it´s possibilities. Since it is in c# only I took this as chance to jump into C#, which I planed (and shifted) for years now anyway... So I am pretty new (few days) in C# coming from few years vb.net....this maybe part of my problem. I want to record WAV of a simultaneous Chirp, to extract the Impulse Responses of Cabinets. I managed (with your 30 days Examples), to list and enumerate devices, and to do a simple form with Play Rec, Stop. I also created a button which plays a chirp. All controls actually work - but unfortunately not together. I managed to record wav , can play chirp and files. I can even play a wavefile and simultaneous start a chirp, which perfectly bends over the wav file. Anyway when I press "Rec" to start the recording an then press "Play" or "Chirp" the program fails with

NAudio.MmException HResult=0x80131500 Nachricht = UnspecifiedError calling waveOutOpen Quelle = NAudio

How can I start a chirp on output and start a simultaneous recording of the IR response from the input? thanks Frank

markheath commented 5 years ago

hard to say what the issue is. You should be able to play and record at the same time. Maybe the playback and record are not at the same sample rate. What NAudio classes are you using for playback and record?

frankfriess commented 5 years ago

Hi Mark,

thank you for your reply. I solved the problem with the simultaneous playback and record of wav files using the asio drivers. So I can play and record waves simultaneous but did not manage to
play a sweep (generated with the generator in Naudio) and simultaneously record so far – and this is what I need since my plan is recording impulse responses of guitar cabs. Right now I am doing a little workaround by just playing a wav. file of a chirp. I tried lots of those commercial impulses and found that they are highly dependened on the soundengineers skills and mic positioning. So I built a 3 axes motorized scanner for the mic to scan a plane. I programmed the device but unfortunately there is no IR audio capable software available with ability to be triggered from external software. This is why I started digging into IR from the scratch trying to “build” my own simple app to drive the mic, adjust levels automatically, sweep, and get an IR. So far I have the motorization done, can play record, have a meter realized with Naudio to be able to adjust levels later to max s/n ratio while moving in the room. Now I am at a point where I struggle with the final IR computing. I read lots these days about convolution, FFT etc. but few things still not clear to me. As far as I understood till todays morning getting the IR is inverse convolution in time or just multiplication/division in frequency. So my (maybe naive) plan was to record the response of the speaker on a chirp. doing fft on both signals, dividing fft(cab)/fft(chirp) and iFFT the result to get the IR in time domain. Confusing for me I found lots of people posting code who seem to just convolve those signals to get the IR. For me this doesnt look logic. Then I found somewhere in functional analysis books that for teh exp. sine sweep doing the convolution of cab signal with chirp (and time flipped) would give the IR. But to be honest math lessons are that long ago, that I could hardly follow the proof... So my next step is to understand what I exactly need to do if I want to get an Impulse of a cabinet. I have a chirp at 48000Hz, 24bit and will record a mono wave 48000Hz, 24 bit. length will be around 1-2s. How will I get the transfer function?

Would be happy to get a hint from someone with obviously deep audio experience.

kind regards Frank

From: Mark Heath Sent: Friday, May 17, 2019 8:33 PM To: naudio/NAudio Cc: frankfriess ; Author Subject: Re: [naudio/NAudio] Problem with simultaneous recording and playback (#475)

hard to say what the issue is. You should be able to play and record at the same time. Maybe the playback and record are not at the same sample rate. What NAudio classes are you using for playback and record?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

frankfriess commented 5 years ago

Hi Mark,

I was able to overcome my problems in understanding asio and was able to get Naudio working with asio and simultaneous play and record. Now since I implemented my realtime filter convolution and solved lots of speed issues and latency by “optimization” of memory consumption I am pretty happy with the result – with one last “big?” problem. When I finished rewriting my filter and had it up to the speed needed, I wanted to change the in/out channels from stereo on the usb soundcard to 8Ch to fit to my actual asio device for which I was planning the whole project. But somehow I cannot get it to work. So I dont want to do any fancy mixing or something like that Ijust want to move my signal chain, which is right now

in1-asioinbuffer1-manipulate-asiooutbuffer1-out1 in2-asioinbuffer2-monipulate-asiooutbuffer2-out2

to eg. in4-asiobuffer4-manipulate-asiooutbuffer4-out4

could you please have a short look on my code? in the _Audioavailabe() method I can switch from [0] to [1] for the in and outbuffer indices but thats unfortunately it. I tried to init with onother waveformat (mor chanels) but it doesnt seem to generate more buffer How could I chance my code that I could for example use inbuffer[4] and outbuffer[4]?

private void ASIODriver_AudioAvailable(object sender, AsioAudioAvailableEventArgs e) { Marshal.Copy(e.InputBuffers[0], L_stack.buf, 0, e.SamplesPerBuffer 4); L_Loadoutputbuffer(); Marshal.Copy(L_stack.buf, 0, e.OutputBuffers[0], e.SamplesPerBuffer 4); e.WrittenToOutputBuffers = true;
}

my asio Init sequence looks like:

   var asioOut = new AsioOut(Globals.asiodevice);
       asioOut.ShowControlPanel();

       Globals.asio_instance = asioOut;        

       if (Globals.mono == true) chnr = 1;
       if (Globals.stereo == true)chnr = 2;

       WaveFormat waveFormat = new WaveFormat(48000, 24, chnr);
       buffer = new BufferedWaveProvider(waveFormat);
       asioOut.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs>(ASIODriver_AudioAvailable);

       asioOut.InitRecordAndPlayback(buffer, chnr, 48000);

       asiobuffersize = asioOut.FramesPerBuffer;
       asioOut.AutoStop = false;
        asioOut.Play()

I am already tired all permutations with chnr, tried offset parameter, but with no sucess at all. I now tried for 3 days and lots of headaches, so my last hope is to maybe get some help from you? It would be realy nice if could show me hoe I could solve this problem.

kind regards

Frank

From: Mark Heath Sent: Friday, May 17, 2019 8:33 PM To: naudio/NAudio Cc: frankfriess ; Author Subject: Re: [naudio/NAudio] Problem with simultaneous recording and playback (#475)

hard to say what the issue is. You should be able to play and record at the same time. Maybe the playback and record are not at the same sample rate. What NAudio classes are you using for playback and record?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.