This project has moved and is read-only. For the latest updates, please go here.

Play and Record simultaneously problem

Jul 5, 2012 at 6:11 PM

Hi all,
I'm not an audio programming expert, but I'm trying to write a C# project using NAudio since some days.

My needs are just to play a sound and to record another sound. Simultaneously. But I met some problems.

Pressing a button application plays my simple mono-frequency sound by a WaveOut using a WaveProvider.

Then I press another button to start recording (using a WaveIn instance)... but here my problems start, because trying to record, audio playback stops.

These are some other tries I made:

1. If I put play/record code inside two classes (separated from main gui class) and try to work using a new thread for each button click (one for play and one for record), then recording doesn't start (breakpoint inside WaveIn.DataAvailable is never reached).

2. If I put play code inside a new thread, but I left recording code inside main gui class, then gui freezes (the only way to stop that is to define an upper limit to the number of buffer readings... but I wouldn't to my gui freezes for a while).

Do you have any idea to help me?
There is something I'm ignoring?
What's the best way to proceed to achieve my goal?

Any suggestion'll be really appreciated.

Thanks

Jul 16, 2012 at 10:46 AM

You ought to be able to play and record at the same time unless you are using a soundcard that doesn't support it (which is very rare but I have heard of hem). What driver callback mode are you using?

Jul 18, 2012 at 4:11 PM
markheath wrote:

You ought to be able to play and record at the same time unless you are using a soundcard that doesn't support it (which is very rare but I have heard of hem). What driver callback mode are you using?

Thank you for answering.

Meanwhile I solved the problem (but I have to study if I'm using best solution).

It seems there were some threading relative problems:
1- gui freezed just trying to acquire signal inside the main (gui) thread;
2- it isn't possible to acquire a signal in a separate thread just using default "new WaveIn()"

To solve 1. Now I'm using a thread for output and a thread for input (both started by gui thread).

To solve 2. To be able to acquire signal I used workaround described here: http://naudio.codeplex.com/workitem/16131

while (isRecording) {
                Thread.Sleep(100);
                System.Windows.Forms.Application.DoEvents();
}

When naudio next version is released, I'll try to use directly WaveInEvent.

 

PS: I'm using a PCM2902 based chip (I assembled) to work.