[phpBB Debug] PHP Warning: in file [ROOT]/ext/alfredoramos/seometadata/event/listener.php on line 126: Trying to access array offset on value of type null
IndieGameDev.net Forums • openAl sound - Page 3
Page 3 of 4

Re: openAl sound

Posted: Tue Apr 14, 2020 5:20 am
by Mckl
And if listener moving with some velocity should I account for that?

Re: openAl sound

Posted: Tue Apr 14, 2020 7:55 am
by Deckhead
Yes definitely.

Re: openAl sound

Posted: Tue Apr 14, 2020 8:00 am
by Mckl
Ahh not sure how, should I combine velocities somehow?

Re: openAl sound

Posted: Tue Apr 14, 2020 9:22 am
by Deckhead
Mckl wrote: Tue Apr 14, 2020 8:00 am Ahh not sure how, should I combine velocities somehow?
You set the position and velocity of your listener and set the position and velocity of the sources. You set these every frame, independently of the others. That's all you need to do, you don't need to calculate anything else, OpenAL will understand what the distances between the objects are.

Re: openAl sound

Posted: Tue Apr 14, 2020 9:45 am
by Mckl
Oh so instead of calculation relative position between vehicle player and some car source(where car is relatively to listener) I can just put listener position and source position.

Re: openAl sound

Posted: Tue Apr 14, 2020 8:21 pm
by Deckhead
Mckl wrote: Tue Apr 14, 2020 9:45 am Oh so instead of calculation relative position between vehicle player and some car source(where car is relatively to listener) I can just put listener position and source position.
Yes, exactly. OpenAL will calculate relative distance between listener and source for you.

Re: openAl sound

Posted: Wed Apr 15, 2020 1:38 am
by Mckl
I am running into an issue in our vehicle class I ve put ALuint aiSource. In initialization function I ve put:

for (auto vehicle : vehicles)
{
if (vehicle->type == Component::Vehicle::Type::Player) { continue; }
ALuint source;
alCall(alGenSources, 1, &source);
alCall(alSourcef, source, AL_PITCH, 1);
alCall(alSourcef, source, AL_GAIN, volume);
alCall(alSourcei, source, AL_SOURCE_RELATIVE, AL_TRUE);


vehicle -> aiSource = source;

}
then in update function
alCall(alSource3f, vehicle -> aiSource, AL_POSITION, relativePosition.x, relativePosition.y, relativePosition.z);
alCall(alSource3f, vehicle -> aiSource, AL_VELOCITY, velocity.x, velocity.y, velocity.z);

and it keeps telling me AL_INVALID_NAME: a bad name (ID) was passed to an OpenAL function
***ERROR*** (..\..\..\Engine\Systems\soundSystem\SoundSystem.cpp: right on those functions.

Re: openAl sound

Posted: Wed Apr 15, 2020 2:18 am
by Deckhead
AL_INVALID_NAME only occurs in alSource3f when the source ALuint that you're providing is not valid. Looking at your code, I believe you might want to be using an auto& in your initialisation loop. Otherwise you're assigning values to a copy of a Component::Vehicle, not the Component::Vehicle in the container.

Re: openAl sound

Posted: Wed Apr 15, 2020 5:05 am
by Mckl
It's not that It's somehow do not recognize it as source no matter what I do. My last attempt :

in initialization function

Code: Select all

auto vehicles = getEngine()->getSubSystem<EngineStore>()->getRoot().getComponentsOfType<Component::Vehicle>();
    for (auto& vehicle : vehicles)
    {
        if (vehicle->type == Component::Vehicle::Type::Player) { continue; }
        vehicle->aiSource = generateSource(); 
    }
and in update we have something like:

Code: Select all

auto vehicles = getEngine()->getSubSystem<EngineStore>()->getRoot().getComponentsOfType<Component::Vehicle>();
    
    glm::vec3 playerPosition;
    for (auto vehicle : vehicles)
    {
        if (vehicle->type == Component::Vehicle::Type::Player)
        {
            playerPosition = vehicle->position;
        }
    }

    for (auto& vehicle : vehicles)
    {
        if (!vehicle->pxVehicle)
            continue;
        
        if (vehicle->type == Component::Vehicle::Type::Player) { continue; }
     
        bool currentAcceleration;
        bool currentBreaking;
       
        glm::vec3 relativePosition = vehicle->position - playerPosition;
        auto velocity = vehicle->pxVehicle->getRigidDynamicActor()->getLinearVelocity();
       
        alCall(alSource3f, vehicle->aiSource, AL_POSITION, relativePosition.x, relativePosition.y, relativePosition.z);
        alCall(alSource3f, vehicle->aiSource, AL_VELOCITY, velocity.x, velocity.y, velocity.z);

        if (vehicle->pxVehicleInputData.getAnalogAccel() == 1)
        {
            currentAcceleration = true;
            if (currentAcceleration != vehicle->initialAccelerate)
            {
                
                alCall(alSourcei, vehicle->aiSource, AL_LOOPING, AL_TRUE);
                alCall(alSourcei, vehicle ->aiSource, AL_BUFFER, accelerationBuffer);
                playSound(vehicle ->aiSource);
                
                vehicle->initialAccelerate = true;

            }
        }
        if (vehicle->pxVehicleInputData.getAnalogAccel() == 0)
        {
            currentAcceleration = false;
            if (currentAcceleration != vehicle->initialAccelerate)
            {
                stopSound(vehicle->aiSource);
                vehicle->initialAccelerate = false;
            }
        }
        if (vehicle->pxVehicleInputData.getAnalogBrake() == 1)
        {
            currentBreaking = true;
            if (currentBreaking != vehicle->initialBreak)
            {
                alCall(alSourcei, vehicle->aiSource, AL_LOOPING, AL_FALSE);
                alCall(alSourcei, vehicle->aiSource, AL_BUFFER, breakingBuffer);
                playSound(vehicle->aiSource);
                vehicle->initialBreak = true;
            }
        }
        if (vehicle->pxVehicleInputData.getAnalogBrake() == 0)
        {
            currentBreaking = false;
            if (currentBreaking != vehicle->initialBreak)
            {
               
                stopSound(vehicle->aiSource);
                vehicle->initialBreak = false;
            }
        }
    
    }  
}

Re: openAl sound

Posted: Wed Apr 15, 2020 10:19 am
by Deckhead
Well, it's hard to say without seeing all of your code in it's entirety. I can see that you are dereferencing the vehicles so they're likely pointers to the actual vehicles, in which case your code looks like it should work.

If you're getting that AL_INVALID_NAME error on the alSource3f calls, the only way that can happen is with an invalid source. So with that in mind, have you tried doing something as simple as this:

Code: Select all

    auto vehicles = getEngine()->getSubSystem<EngineStore>()->getRoot().getComponentsOfType<Component::Vehicle>();
    for (auto& vehicle : vehicles)
    {
        if (vehicle->type == Component::Vehicle::Type::Player) { continue; }
        vehicle->aiSource = generateSource(); 
        std::cout << "vehicle al source created: " << vehicle->aiSource << std::endl;
    }
It may seem silly, but the idea is to be 100% certain that the initialisation that you think is occurring is definitely occurring. You should get a bunch of that message output in your console window and should see a bunch of sequentially numbered sources (usually you'll see from OpenALSoft that the first source is the value 1 and then the next created is 2 etc etc).