Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello
I want to play many sounds at the same time every sound is independant from others i know that i have to use Thread but i dont know how ?
thanks
Posted
Comments
Chandrasekharan P 22-Jun-11 6:27am    
Can you explain a little more on this. Are you using a GUI for this? how are you opening the files? What have you done so far?
kinani 22-Jun-11 6:36am    
yes im using GUI and i have 3 classes generating three kind of sound ( three classes are independant )a call them and i have to use the play function of each one the problem is that the three play functions must be at the same time
thanks
TRK3 22-Jun-11 14:55pm    
This is going to depend more on the audio driver and interface then on the use of threads. If the audio interface supports multiple voices/sounds/channels at the same time you should be able to do it.

Note: Most audio interfaces have a play function which queues the sound up to be played and returns immediately. If that's the case, you can play multiple sounds "at the same time" by playing each one on a different audio channel -- you don't even need threads. They won't start simultaneously -- but they won't do that even if you start them on separate threads. If you need them to start simultaneously then see SAKryukov's solution below.
kinani 24-Jun-11 11:20am    
im using Maudio delta 44 interface but the problem maybe is that sounds are independant means don t share any buffer so for playing them i will have (with c++)
(play(mp3,device3)
play(wav,device1)
play(selfGenerated,device2)
for exemple and so the sounds will not be played simultaneously what mean pre-mixing them i dont understand what's this mean as SAKryulov said thanks
TRK3 27-Jun-11 13:55pm    
Did you try that:

play(mp3,device3);
play(wav,device1);
play(selfGenerated,device2);

???

What happens? Do they play one after the other, or do they play pretty much at the same time?

If you try to play different sounds in different threads, in the very best case the sounds will come our with random timing each time. You're not working with real time system. To a human ear, lack if audio synchronization will be clear and create very bad impression (unless you want to generate randomized noises).

Same thing with asynchronous operations. You should really avoid asynchronous operations as you can use threads, which is easier to implement and more robust.

If you don' want to create such noise, you have to pre-mix you audio and play a mixed audio. Use some audio-mixing software or do you by yourself.

—SA
 
Share this answer
 
Comments
TRK3 22-Jun-11 14:50pm    
OP states that the sounds are independent. And based on earlier questions from OP, the sound sources are MP3, WAV and "auto-generated" -- which suggests background music + sound effects which don't necessarily have a timing relationship.
Sergey Alexandrovich Kryukov 22-Jun-11 18:24pm    
How do you know? Maybe but this is rather unusual. Anyway, the principal lack of synchronization is important to know anyway, don't you think so?
--SA
TRK3 23-Jun-11 13:06pm    
How do I know? (a) I recently answered other questions from the same person in which they described the audio in more detail, and (b) what they were describing sounded exactly like the audio in a (iPhone) game that I was programming recently that had background music + sound effects -- in that sort of scenario, it's not unusual to have independent sounds that have no timing relationship.

But, you are right, the lack of synchronization is important for OP to be aware of. If OP needs tight synchronization he/she isn't going to get it using separate threads. Some audio frameworks do permit you to synchronize separate tracks (the iOS core media stuff on the iPhone comes to mind, I'm not familiar with PC audio frameworks, but if it exists on one platform it probably does on another) -- but you don't need separate threads when doing that.

Sergey Alexandrovich Kryukov 24-Jun-11 19:13pm    
Well, you never know exactly despite of any your experience as inquirers do or want most weird things sometimes.
Thank you for your note. I agree -- you really don't need separate threads for this purpose.
--SA
kinani 24-Jun-11 10:47am    
thanks what you mean by mixing audio is this allows to play different sounds simultaneously in different devices ? i have found a program here http://www.codeproject.com/KB/audio-video/admixer.aspx is it helpful here ? thanks
I think we can use Asyn call to play different different sounds at a same time.
 
Share this answer
 
Comments
kinani 22-Jun-11 6:37am    
Asyn call is this a thread and is it easy to use? thanks
[no name] 22-Jun-11 7:05am    
we can create as many thread , as many sound we want to play. and start the thread in an async manner. it will play all file.

yes. it is easy to use.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900