openframeworks / openframeworks/openFrameworks

audioOutputExample broken if buffer.getNumFrames is larger than the buffer size

Open
#6,200 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
10.4k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
9

Description

Hi OF team,

I'm testing OF in a Toradex Apalis iMX6 (based in NXP's iMX6 SoC) and apart from this ofFbo issue #6199 , it seems that there is an error in the AudioOutputExample:

buffer.getNumFrames() returns more than 1700 (in my case).
bufferSize is by default 512.

This returns an allocation/stack backtrace error when trying to launch the application.

To test this, I simply made bufferSize global (should be possible to do the same by measuring the channels buffers, but can't be bothered, as I have little idea of OF), and add a conditional clause. Just as a Proof of Concept, I added this in audioOut (but since this is called everytime, it is suboptimal, so should be calculated once and elsewhere). Changed buffer.GetNumFrames for the conditional variable.

void ofApp::audioOut(ofSoundBuffer & buffer){
	
	//pan = 0.5f;
	float leftScale = 1 - pan;
	float rightScale = pan;
	int usedBufferSize = (buffer.getNumFrames() > bufferSize) ? bufferSize : buffer.getNumFrames();
	// sin (n) seems to have trouble when n is very large, so we
	// keep phase in the range of 0-TWO_PI like this:
	while (phase > TWO_PI){
		phase -= TWO_PI;
	}

	if ( bNoise == true){
		// ---------------------- noise --------------

		//for (size_t i = 0; i < buffer.getNumFrames(); i++){
		for (size_t i = 0; i < usedBufferSize; i++){
			lAudio[i] = buffer[i*buffer.getNumChannels()    ] = ofRandom(0, 1) * volume * leftScale;
			rAudio[i] = buffer[i*buffer.getNumChannels() + 1] = ofRandom(0, 1) * volume * rightScale;
		}
	} else {
		phaseAdder = 0.95f * phaseAdder + 0.05f * phaseAdderTarget;
		//for (size_t i = 0; i < buffer.getNumFrames(); i++){
		for (size_t i = 0; i < usedBufferSize; i++){

			phase += phaseAdder;
			float sample = sin(phase);
			lAudio[i] = buffer[i*buffer.getNumChannels()    ] = sample * volume * leftScale;
			rAudio[i] = buffer[i*buffer.getNumChannels() + 1] = sample * volume * rightScale;
		}
	}
}

I do not know if this happens in other OF versions as I only tested this in the ARMv7 version and in the Toradex Apalis iMX6.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the AudioOutputExample audioOut callback and inspect how buffer.getNumFrames(), bufferSize, lAudio, and rAudio are used when the reported frame count exceeds the configured size. Reproduce the allocation or stack backtrace with a buffer larger than 512 frames, then verify the example handles that case without writing beyond its audio buffers.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
audio-video-rtc
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.