Should I use threading programming for mixing 2 audio stream?
I'd like to write a program that capture audio stream from the microphone and in the same time mix this stream with a playi开发者_StackOverflow社区ng audio file.
I'm lookig for library, api, etc..etc... but my concern is about the implementation, should I use threading programming? I don't know how to use a thread yet.
The operating system is windows, the language is c++ or python.
thanks
I think you will need some kind of threading, in order to make capturing and playback run smoothly side-by-side.
Be aware that multi-threading in Python is limited by the GIL. In C++, you can use something like Boost.Thread
.
In general, getting multi-threading right is hard, and you should take your time to get acquainted with it. Your best option is probably to look for a good book.
py2.6 comes with processed-based threading as well so you don't have todo just green-threads
You can use SDL mixer. It starts a thread, so you don't have to do it. There are good tutorials and reference pages.
You can take a look at audacity source AudioIO.cpp, which uses portmixer (also worth taking a look).
You can look in portmixer to see how to start an audio io stream in windows. There are also a lot of good tutorials online like this one.
Although the windows wave API starts the audio thread for you the best practice is to have another thread to read/parse/decode the audio file you want to mix and fill up a buffer that is accessible by the audio thread callback. Portaudio/SDL/Open AL are just a layer on top of the windows sound hosts (MME/Direct Sound), so this is still true if you use one of them.
But if you're feeling really lazy or just want a fast proof of concept it is possible to do the buffer filling from the audio file in the main thread.
With regards to the mic input, when you specify the stream details you can say which device inputs you want, and these are given to you as an array of ints/floats, which you can send directly to the output array. In your case you want to mix (add) the audio file.
精彩评论