Hello online instructors,

First, thank you for the course.

I having a trouble compiling the exercise of lecture 2: sample-player-stereo, I have this error in the terminal:

Building project ...
Building render.cpp...
...done
Using library AudioFile
Building library AudioFile
Linking...
...done
Build finished
Running project ...
Loaded the audio file 'slow-drum-loop.wav' with 352846 frames (7.4 seconds) and 2 channels
Makefile:595: recipe for target 'runide' failed
Segmentation fault
make: *** [runide] Error 139
Bela stopped

I copy the solution of stereo code that Dr Andrew shows arround 23:42, and when runnig the code the error above appears. Any Idea?

Cheers,
Hernani

can you try to run this code instead?
If it works, then by comparing it with yours you can probably spot what went wrong 🙂

#include <Bela.h>
#include <libraries/AudioFile/AudioFile.h>

std::string gFilename = "slow-drum-loop.wav";	// Name of the sound file (in project folder)
std::vector<std::vector<float >> gSampleBuffer;	// Buffer that holds the sound file
int gReadPointer = 0;							// Position of the last frame we played 

bool setup(BelaContext *context, void *userData)
{
	// Load the sample from storage into a buffer
	gSampleBuffer = AudioFileUtilities::load(gFilename);
	
	// Check if the load succeeded
	if(gSampleBuffer.size() == 0) {
    	rt_printf("Error loading audio file '%s'\n", gFilename.c_str());
    	return false;
	}
	
	// Make sure we have at least as many sound file channels as audio
	// output channels, so we don't crash in render(). This check could
	// alternatively be done in render() itself.
	if(gSampleBuffer.size() < context->audioOutChannels) {
		rt_printf("Audio file '%s' has %d channels, but needs at least %d.\n", 
					gFilename.c_str(), gSampleBuffer.size(), context->audioOutChannels);
		return false;
	}

    rt_printf("Loaded the audio file '%s' with %d frames (%.1f seconds) and %d channels\n", 
    			gFilename.c_str(), gSampleBuffer[0].size(),
    			gSampleBuffer[0].size() / context->audioSampleRate,
    			gSampleBuffer.size());

	return true;
}

void render(BelaContext *context, void *userData)
{
    for(unsigned int n = 0; n < context->audioFrames; n++) {
    	// Increment and wrap the read pointer
        gReadPointer++;
        if(gReadPointer >= gSampleBuffer[0].size())
        	gReadPointer = 0;

    	for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
			// Write the sample to every audio output channel
			float out = gSampleBuffer[channel][gReadPointer];
			
    		audioWrite(context, n, channel, out);
    	}
    }
}

void cleanup(BelaContext *context, void *userData)
{

}

Hello Giulio,

Thank you for your answer. I just copy your code and paste directly in the IDE and I get different error. I also update the Bela-master. The mono example sounds. I'm using the 8 channel CTAG cap, maybe I need to adjust some part of the code that indicates 8 channels instead of 2? The error says this: "Audio file 'slow-drum-loop.wav' has 2 channels, but needs at least 8."

Cheers, Hernani

Here the entire error:

Building project ...
Building render.cpp...
...done
Using library AudioFile
Linking...
...done
Build finished
Running project ...
Error: unable to initialise audio
Audio file 'slow-drum-loop.wav' has 2 channels, but needs at least 8.
Makefile:595: recipe for target 'runide' failed
make: *** [runide] Error 1
Bela stopped

Right,the code as it is it expects the audio file to have the same number of channels as your output. You can circumvent this by removing the check in setup and then in render replace the line

float out = gSampleBuffer[channel][gReadPointer];

with

float out = gSampleBuffer[channel % gSampleBuffer.size()][gReadPointer];

This way you ensure you do not try to access an invalid element of the array. In hindsight, this could have been the issue that lead to your segmentation fault earlier on.