AudioSourcePlayer with AudioTransportSource plays back with unwanted distortion


#1

I am using AudioSourcePlayer with AudioTransportSource to play wav files of drum samples when a user hits a midi note on the keyboard. When the sound plays, it sounds kind of like its clipping - this is especially evident on the kick drum samples, but affects all of the files I have tried to play. I’ve played around with setting readAheadThread thinking that would help, but it sounds exactly the same even with a buffer. When I play the same files using the SoundPlayer class the playback is perfect, so I don’t think its anything directly related to my AudioDeviceManager initialization. My understand, though, is that the SoundPlayer class will just play the sound and won’t actually pass it into the DAW when the track is bounced. Not to mention it doesn’t seem to allow setting the gain of the playback which is something I need.

This seems like a simple enough thing to do so I feel like I’m probably overlooking something obvious. It’s probably also worth noting that I’ve been testing using the Plugin Host app. When I load the plugin into Cubase, the first note i hit crashes the VST bridge and doesn’t actually play at all. This is not the case when using the SoundPlayer class though.

Here is some of the code from my Processor class’s constructor:

formatManager.registerBasicFormats();
//deviceManager.addAudioCallback(&player);
audioSourcePlayer.setSource(&transportSource);
deviceManager.addAudioCallback(&audioSourcePlayer);

deviceManager.initialise(2, 2, 0, true, String::empty, 0);

Here is the code I am using inside the handleNoteOn method - any ideas on what could be happening?:

    if (file.exists()) {

			AudioFormatReader* reader = formatManager.createReaderFor(file); 

			
			if (reader != nullptr)
			{

				ScopedPointer<AudioFormatReaderSource> newSource = new AudioFormatReaderSource(reader, true);
				transportSource.setSource(newSource, 0, nullptr, reader->sampleRate);

				transportSource.start();
				readerSource = newSource.release();

				//player.play(file);
			}

	}

Thanks in advance to anyone taking the time to look at this.


#2

Ok, so I figured out the distorted sound was actually my sample playing back at double speed. I was calling transportSource.getNextAudioBlock() inside the getNextAudioBlock function and so, as best I can tell, it was processing the blocks 2x as fast as it should. When I omit that function, I get the proper playback, but exporting the recorded track with Export > Audio Mixdown from Cubase is silent…

Should I be calling this function from somewhere else?

void DrumsAudioProcessor::getNextAudioBlock(const AudioSourceChannelInfo& bufferToFill) 
{
    if (readerSource == nullptr)
    {
	bufferToFill.clearActiveBufferRegion();
	return;
    }
    //this causes distortion (plays 2x as fast) but makes it export to the DAW
    //transportSource.getNextAudioBlock(bufferToFill);

}

#3

What sample rate is your source file and what sample rate are you using on your sound card? It sounds like there could be a mismatch.


#4

other thing to check, the AudioTransportSource can use a BufferingAudioSource internally. When you render offline, you need to synchronise the buffering thread, otherwise the BufferingAudioSource will only provide silence.
It is determined in the AudioTransportSource::setSource()

But that would need changes on the AudioTransportSource, so better avoid the background buffering in that setup.