[Q] How to play more than one sound at the same time?

Discussion in '3DS - Homebrew Development and Emulators' started by LeifEricson, Oct 4, 2015.

  1. LeifEricson
    OP

    LeifEricson Coming Soon™

    Member
    212
    165
    Jun 22, 2012
    United States
    New York, USA
    I'm writing a new app and I'm trying to get sound effects to play over my music. However, whenever I call csndPlaySound more than once, even after playing two sounds on separate channels, the second sound always plays instead of the first sound.

    Is there some sort of flag or something that I'm missing that will allow me to play multiple sounds at once?
     
  2. TheCruel

    TheCruel Developer

    Banned
    1,351
    2,882
    Dec 6, 2013
    United States
    You're probably doing something wrong. There are 24 channels usable by CSND. Just keep cycling through them. Something like this:

    Code:
    void Sound::play()
    {
        if (!m_buffer || m_buffer->getSampleCount() == 0)
            return;
    
        // 24 possible CSND channels to cycle through (0-7) reserved for DSP
        static int channel;
        channel++;
        channel %= 24;
    
        u32 flags = SOUND_FORMAT_16BIT;
        if (m_loop)
            flags |= SOUND_REPEAT;
        u32 size = sizeof(Int16) * m_buffer->getSampleCount();
        GSPGPU_FlushDataCache(NULL, (u8*)m_buffer->getSamples(), size);
        csndPlaySound(channel+8, flags, m_buffer->getSampleRate(), 1.0, 0.0, (u32*)m_buffer->getSamples(), (u32*)m_buffer->getSamples(), size);
    }
     
    Last edited by TheCruel, Oct 4, 2015
    marc00077 likes this.