Enes Altuncu
Enes Altuncu

Reputation: 439

How to play multiple sounds in openal?

I'm trying to make a c++ program that plays 7 sounds together. To do this, I have 7 play button formed by Qt Designer 5.3.1 and whenever I clicked on a pushbutton, it loads the sound to the corresponding buffer. I'm using OpenAL to play the sound. After I checked the soundfile if it's wav file or not, I'm using the following code to load data to the buffer :

//now we put our data into the openAL buffer and
    //check for success
    if (ui->groupBox_2->isHidden() == true)  //play button is in the groupbox, so I'm using groupbox.
    {
        alGetError();
        alBufferData(buffer[0], *format, data, *size, *freq);
    }
    else if (ui->groupBox_5->isHidden() == true)
    {
        alGetError();
        alBufferData(buffer[1], *format, data, *size, *freq);
    }
    else if (ui->groupBox_14->isHidden() == true)
    {
        alGetError();
        alBufferData(buffer[2], *format, data, *size, *freq);
    }
    else if (ui->groupBox_23->isHidden() == true)
    {
        alGetError();
        alBufferData(buffer[3], *format, data, *size, *freq);
    }
    else if (ui->groupBox_26->isHidden() == true)
    {
        alGetError();
        alBufferData(buffer[4], *format, data, *size, *freq);
    }
    else if (ui->groupBox_29->isHidden() == true)
    {
        alGetError();
        alBufferData(buffer[5], *format, data, *size, *freq);
    }
    else if (ui->groupBox_32->isHidden() == true)
    {
        alGetError();
        alBufferData(buffer[6], *format, data, *size, *freq);
    }

    if (alGetError() != AL_NO_ERROR)
    {
        return false;
    }

And for playing the sound :

alGenSources(7, source);
// Attach the buffer to an available source.
QObject* button = QObject::sender();
if (button == ui->pushButton_2)
{
    alSourcei(source[0], AL_BUFFER, buffer[0]);
    alGetSourcei(source[0], AL_SOURCE_STATE, &state);
    if (state != AL_PLAYING)
    {
        alSourcePlay(source[0]);
        alGetSourcei(source[0], AL_SOURCE_STATE, &state);
        if (state == AL_PLAYING)
        {
            ui->groupBox_2->setTitle(name[0] + " is Playing...");
        }
    }
    if (alGetError() != AL_NO_ERROR)
    {
        return false;
    }
}
else if (button == ui->pushButton_35)
{
    alSourcei(source[1], AL_BUFFER, buffer[1]);
    alGetSourcei(source[1], AL_SOURCE_STATE, &state);
    if (state != AL_PLAYING)
    {
        alSourcePlay(source[1]);
        alGetSourcei(source[1], AL_SOURCE_STATE, &state);
        if (state == AL_PLAYING)
        {
            ui->groupBox_5->setTitle(name[1] + " is Playing...");
        }
    }
    if (alGetError() != AL_NO_ERROR)
    {
        return false;
    }
}
else if (button == ui->pushButton_39)
{
    alSourcei(source[2], AL_BUFFER, buffer[2]);
    alGetSourcei(source[2], AL_SOURCE_STATE, &state);
    if (state != AL_PLAYING)
    {
        alSourcePlay(source[2]);
        alGetSourcei(source[2], AL_SOURCE_STATE, &state);
        if (state == AL_PLAYING)
        {
            ui->groupBox_14->setTitle(name[2] + " is Playing...");
        }
    }
    if (alGetError() != AL_NO_ERROR)
    {
        return false;
    }
}
else if (button == ui->pushButton_43)
{
    alSourcei(source[3], AL_BUFFER, buffer[3]);
    alGetSourcei(source[3], AL_SOURCE_STATE, &state);
    if (state != AL_PLAYING)
    {
        alSourcePlay(source[3]);
        alGetSourcei(source[3], AL_SOURCE_STATE, &state);
        if (state == AL_PLAYING)
        {
            ui->groupBox_23->setTitle(name[3] + " is Playing...");
        }
    }
    if (alGetError() != AL_NO_ERROR)
    {
        return false;
    }
}
else if (button == ui->pushButton_47)
{
    alSourcei(source[4], AL_BUFFER, buffer[4]);
    alGetSourcei(source[4], AL_SOURCE_STATE, &state);
    if (state != AL_PLAYING)
    {
        alSourcePlay(source[4]);
        alGetSourcei(source[4], AL_SOURCE_STATE, &state);
        if (state == AL_PLAYING)
        {
            ui->groupBox_26->setTitle(name[4] + " is Playing...");
        }
    }
    if (alGetError() != AL_NO_ERROR)
    {
        return false;
    }
}
else if (button == ui->pushButton_51)
{
    alSourcei(source[5], AL_BUFFER, buffer[5]);
    alGetSourcei(source[5], AL_SOURCE_STATE, &state);
    if (state != AL_PLAYING)
    {
        alSourcePlay(source[5]);
        alGetSourcei(source[5], AL_SOURCE_STATE, &state);
        if (state == AL_PLAYING)
        {
            ui->groupBox_29->setTitle(name[5] + " is Playing...");
        }
    }
    if (alGetError() != AL_NO_ERROR)
    {
        return false;
    }
}
else if (button == ui->pushButton_55)
{
    alSourcei(source[6], AL_BUFFER, buffer[6]);
    alGetSourcei(source[6], AL_SOURCE_STATE, &state);
    if (state != AL_PLAYING)
    {
        alSourcePlay(source[6]);
        alGetSourcei(source[6], AL_SOURCE_STATE, &state);
        if (state == AL_PLAYING)
        {
            ui->groupBox_32->setTitle(name[6] + " is Playing...");
        }
    }
    if (alGetError() != AL_NO_ERROR)
    {
        return false;
    }
}

But when I execute this code, just the last loaded sound is playing. When I remove if and elses from "alBufferData" functions, then the last loaded sound plays 7 times simultaneously. So, how can I play 7 different sound simultaneously in this case?

Upvotes: 4

Views: 687

Answers (0)

Related Questions