Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Play several sounds synchronously

Discussion in 'Audio & Video' started by robertohuertasm, Jul 4, 2015.

  1. robertohuertasm

    robertohuertasm

    Joined:
    Aug 29, 2014
    Posts:
    44
    Hi to everyone,

    is there a way to play several sounds and start all of them at the same time? Maybe dynamically create an audiocilp in runtime?

    I'm porting my piano app to unity3d from Windows Phone and there I used DirectX XAudio2 in order to accomplish that. In this case I mix several voices at runtime and play the mixed voice. Is this possible in Unity3d? What would be the recommended approach?

    I've seen some sequencer Unity apps that seem to accomplish this kind of synchronicity but I don't have a clue how they managed to start all the sounds at the same time.

    In my case, I use a loop to iterate through all the AudioSources and play them. It works when I have 2 or three sounds (even it's not perfect) but it's completely unacceptable when I have more than 7 sounds playing as the delay between each play call is clearly noticeable and the chord is played arpeggiated not as a unique sound.

    Any insight on these matters would be really appreciated. Maybe you know a sound library (similar to XAudio2) that is capable of this in the Unity world.

    Thanks for your help.

    Best regards,

    Roberto.
     
  2. robertohuertasm

    robertohuertasm

    Joined:
    Aug 29, 2014
    Posts:
    44
    Hi, I've tried with audioclip's PlayScheduled method but I've found that it doesn't seem to be working ok. Whenever I call this method the sound plays, then stops and then plays again at the specified time. It seems to be a known bug that hasn't been resolved yet.

    Any other ideas?

    Best regards,

    Roberto.
     
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    Are your audiosources set to PlayOnAwake? Make sure they aren't.

    Otherwise, you should probably post the code. Sounds like you might be doing something wrong.
     
    Last edited: Jul 5, 2015
  4. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,555
    If you are playing mp3's and not wav files, that would account for the delay. Starting an mp3 playing takes about 120 times as long as starting a wave file. And it's absolutely terrible for performance which is why they should never be used for sound effects.
     
  5. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    I havent done it myself, but i watched this talk recently, and it was really informative. Cant really tell, if things have changed with U5, but i believe combining samples / tracks at runtime have at least same principles:

     
  6. robertohuertasm

    robertohuertasm

    Joined:
    Aug 29, 2014
    Posts:
    44
  7. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    I see what you mean.

    If I add a debug log (which is known to be intensive) when I schedule audio in a test project I get what you describe.

    It looks like it's still a bug. You should file a bug report.
     
  8. robertohuertasm

    robertohuertasm

    Joined:
    Aug 29, 2014
    Posts:
    44
    Thanks Naraku,

    For the record, I've tried using a coroutine in the loop instead of calling the playScheduled method directly and it seems to be working in some scenarios.

    In my specific case it doesn't seem to be working.

    Cheers!
     
  9. WoozyBytes

    WoozyBytes

    Joined:
    Mar 16, 2015
    Posts:
    8
    @Newton_W
    Hi !
    I'm not sure if you found a solution to your problem but i leave my input just in case.

    "Whenever I call this method the sound plays, then stops and then plays again at the specified time"
    You need an extra Audio Source , basically let's say 1 second before the loop ends you get the time samples left in the Audio Source that's playing and use that value to play another audio source with the PlayScheduled() Method, that way the audio will keep playing and switch seamlessly (at least with wav files, definitely not with mp3's).

    You can also create audio clips at runtime and and play them in perfect sync to do it is a bit tricky though and it will depend on how you app works.

    Here's an example of a (one track) step sequencer you can easily modify this to work with multiple tracks.
    http://github.com/WoozyBytes/UnityStepSequencer
    Feel free to let me know if you have any questions.
    Regards WB.
     
  10. robertohuertasm

    robertohuertasm

    Joined:
    Aug 29, 2014
    Posts:
    44
    Hi @WoozyBytes,

    first of all, thank you very much for your answer. It seems that in the latest versions of Unity the issue was solved, so I can use PlayScheduled with no troubles.

    Nonetheless, I've not solved the problem of synchronizing several sounds to play exactly at the same time. I've looked at your sample but I'm not really sure on how to proceed in order to sync several sounds.

    It seems that you divide the audiclip in several steps and then you dynamically create another one but I don't get the point of it as I don't have this kind of audio background.

    Thanks again for your help! :D

    Best regards,

    Roberto.
     
  11. robertohuertasm

    robertohuertasm

    Joined:
    Aug 29, 2014
    Posts:
    44
    Hi @WoozyBytes,

    just to be clear on the sync issue. Now that PlayScheduled works I can play sounds at the same time. Consider this thread closed.

    Best regards,

    Roberto.
     
  12. WoozyBytes

    WoozyBytes

    Joined:
    Mar 16, 2015
    Posts:
    8
    Hi Roberto !
    I'm Glad you fixed the issue.
    "but I'm not really sure on how to proceed in order to sync several sounds."
    On that sample code i showed how to create a one track step sequencer.
    But if you use that code to create multiple tracks and play them at the same time they will play perfect in sync.
    Regards
     
  13. WoozyBytes

    WoozyBytes

    Joined:
    Mar 16, 2015
    Posts:
    8
    "it seems that you divide the audiclip in several steps"
    Yes i divide one bar in 16 steps that is the normal 4/4 time signature on most electronic music but there's others.

    "and then you dynamically create another one but I don't get the point of it as I don't have this kind of audio background."
    No i only create one audio clip the dividing part is just for reference, so i can place the individuals sounds at the right time (in samples) in the main loop i created.

    Try running the script on a test project it has a basic UI so you can try the sequencer, it might give you some ideas.
    If you want i can explain this better just let me know.

    Regards :)
     
  14. robertohuertasm

    robertohuertasm

    Joined:
    Aug 29, 2014
    Posts:
    44
    @WoozyBytes thank you very much for the insights! I'll take a further look at your sequencer.

    Best regards,
    Roberto.
     
  15. WoozyBytes

    WoozyBytes

    Joined:
    Mar 16, 2015
    Posts:
    8
    No problem :)
    Regards