Posted in:

Here’s my latest NAudio Q&A livestream video.

Unfortunately I was interrupted by noisy builders and children before answering all the questions I wanted to. Here’s the final one I intended to tackle…

Want to get up to speed with the the fundamentals principles of digital audio and how to got about writing audio applications with NAudio? Be sure to check out my Pluralsight courses, Digital Audio Fundamentals, and Audio Programming with NAudio.

Comments

Comment by Tim

Great website and documentation, however i cannot seem to find an answer to the question below:
I would like to know if there is a way to play a recorded file (mp3 or wav) and send the signal to a input device (WaveIn) e.g. Microphone
Can anybody help with that?

Tim
Comment by Mark Heath

hi Tim, sadly that's not something the Windows APIs make possible. You need to purchase a "virtual audio cable" or similar which lets you play audio to a virtual output that is patched to another virtual input

Mark Heath
Comment by Tim

Thanks for your quick response, not the answer i would like to hear but i understand why this isn't possible. This also seems the reason why i cannot find an application which already exist and does the trick.
Hopefully i can find some time next week to try it. If you like i can send you an update if my project is finished if i can ever finish it :s

Tim
Comment by Harold Day

I would love to have a bit of direction on modifying the wave file writer to do something Mark mentioned in a comment on StackOverflow... it was related to playing then overwriting part of the audio... Mark's comment was - " I use a customised version of WaveFileWriter that can open an existing WAV file, and lets you reposition within the data chunk."
When I set the position, checking block align, the writer still appends to the end of the wave instead of over writing it.

Harold Day
Comment by Mark Heath

hi Harold, I'm afraid my customised WaveFileWriter that can write over an existing file is not open source - I made it for a commercial project. The general approach is, first create a blank WAV file of the duration you want. Then one thread can be playing from it, while another thread is using a customised version WaveFileWriter that can overwrite rather than append. It seems to work better than trying to read from a file that is being written

Mark Heath
Comment by Harold Day

Thanks for the reply, and a quick one I might add.
That is the push along the path I needed. I had not thought about creating a second blank file. I tweaked the file permissions to the writer and the Audio file reader so that recording and playing/rewinding from the same file was easy . Overwrite capability was requested, so I was trying to do it to the same file I already had open. That didn't work so well. Thanks again for the response.

Harold Day
Comment by Mark Heath

yes, you do need to make sure that the permissions used to open both streams don't deny the other access

Mark Heath
Comment by Mark Heath

another trick I used was in debug mode I'd fill the original file with a (quiet) sine wave, which made it very obvious if the reader was playing audio that hadn't been overwritten by the writer yet

Mark Heath
Comment by Frank Post

Hello Mark,
I have a brief question regarding compatible multi-channel sounds cards that would allow proper input/output mapping using the MultiplexingWaveProvider. I have tried several Sound Blasters 7:1 and
5:1 internal cards as well as an ASUS DGX 5.1 sound card and using the ASIO4all driver with no luck. Either I
get no sound or I am getting sound throughout all the channels. For testing purposes, I used your sample code described in your article to test the card's compatibility.
Would you be so kind to let me know what Multi-Channel Audio Cards in particular are known to be compatible
with NAudio's input/output channel mapping?
Thank you for your time.

Frank Post
Comment by Mark Heath

It very much depends on the drivers of particular cards. My only real experience of working with multi-output cards is pro-audio USB devices (e.g. Focusrite Scarlett series) which have ASIO drivers to support the multiple outputs

Mark Heath
Comment by Frank Post

Thank you Mark. I purchased the Focusrite Scarlett 2i4 2n Gen to test. Is it required to use AsioOut or can the WaveOutEvent be used for mapping multichannel input-to-outputs? I have a large code set that heavily uses the
WaveOutEvent and when attempting to implement the AsioOut it continues to throw the exception "Unable to instantiate ASIO .. Check if STAThread is set". When using the WaveOutEvent it only realizes 1 input channel and 2 output channels instead to 4 available channels. I'd prefer to use the WaveOutEvent to avoid any major code modifications if possible. If the WaveOutEvent can be used is there any way for it to realize all the available input/output channels?
Thank you Mark for your time.

Frank Post
Comment by Mark Heath

Yes, AsioOut is likely the only one you can use for multichannel ins and outs. What sort of application are you using it from - is it a console app?

Mark Heath
Comment by Frank Post

Hi Mark ... it is a Windows Form Application ... it is an application that continues to grow over the past 5 years. The new feature that is being implemented is the ability to play multiple tracks at the same time through selected channels. There is a main form that acts as a controller "container" that invokes an instance of the music application and displays it as a tab. Each instance essentially plays a provided playlist of tracks mapped to one of the available outputs. Currently the application can play two instances using the WaveOutEvent by splitting the raw audio to right and left front. Now we would like to expand this to play at least up to 4 channels (4 playlist zones).

Frank Post
Comment by Mark Heath

Don't you have STAThread set on your main thread? The ASIOOut object should be created on the UI thread

Mark Heath