AudioTransportSource::setGain () question

My application plays wav streams built into the binary - a very simple usage of audio sources, transports etc. I need looped and non-looped sound, all of which works fine, except:

I’d like to control the volume using setGain() between playbacks of the sound. But JUCE applies the gain change at the beginning of the stream using a gain ramp, resulting in an annoying click at the beginning of the first playback, especially if the gain change is too big (of course it remembers the gain if played again). How can I control or turn off the ramping stuff? I guess that this is nice for smoothing the gain change while playing, but I really need to let the user set volume between plays.

    // ....
    AudioFormatReader* reader = formatManager.createReaderFor (new MemoryInputStream(BinaryData::sound_wav, BinaryData::sound_wavSize, false) );

    if (reader != 0)
        audioSource = new AudioFormatReaderSource (reader, true);
        transport.setSource (
        transport.setGain (0.1f);
        sampleRate = reader->sampleRate;
        blockSamples = defaultBlockSamples;
        // ...
    } else {
        // ...


Well, you could leave the transport running and just add/remove your source from it. That’s how I tend to use them.

Eeer… thanks, but to be honest, I haven’t the slightest idea what you’re saying :slight_smile: I couldn’t say I’m too familiar with the audio stuff…

But :slight_smile: I managed to make it work by simply applying the setGain to the AudioSourcePlayer (on top of the audio foodchain, as I understand) instead of the transport. I hope I’m not too far off the map…

I meant that you’d leave your transport source connected to the audio device and running all the time, but then add/remove your audio readers from the transport source, rather than starting/stopping the device and the entire chain.

Just stumbled upon this thread due to having the same ‘clicking’ problem when calling AudioTransportSource::setGain() before playing an audio file.
For now I’ve just quick-fixed it by changing the way in which the AudioTransportSource class changes the gain by changing AudioSampleBuffer::applyGainRamp() to just AudioSampleBuffer::applyGain().

Would it be possible to add a new function to AudioTransportSource called setGainWithoutRamp() that would set both the gain and lastGain variables, essentially removing the ramp, or is there some logic in that that doesn’t make sense?

I looked into doing what you suggested to spiderman, however I don’t think I’m completely clear on what you meant.