I recently managed to get past the errors of using SDL for sound.
Now that it's running and I'm not running into errors, my program is only playing beeping noises instead of the file I've provided.
I want the program to play the .wav file I'm passing to the SDL_LoadWAV.
I've tried with two different .wav files of different length and size, and checked the header files to find comments and tips on what format is required for the SDL to play the .wav file, haven't gotten anywhere with either of it.
The myAudioCallback function is responsible for handling the SDL callback.
void myAudioCallback(void* userdata, Uint8* stream, int len)
{
AudioData* audio = (AudioData*)userdata;
if (audio->length == 0)
return;
Uint32 length = (Uint32)len;
length = (length > audio->length ? audio->length : length); // if length is more than the audio length, then set length to be the audio.length, if not, set it to be the length passed to the function
std::cout << "Audio Length " << audio->length << std::endl;
std::cout << "Audio Position " << audio->position << std::endl;
SDL_memcpy(stream, audio->position, length); // audio callback is called by SDL, this ensures that the stream and data that is sent, is copied over to our struct, so we can use it and manipulate it
audio->position += length;
audio->length -= length;
}
My loadAudio function is responsible for loading the audio file and saving information about the audio file to the various variables I've declared in the .h (see further down for my .h)
void mainEngineCW4::loadAudio() // this function is for the sole purpose of loading the .wav file
{
SDL_Init(SDL_INIT_AUDIO); // loads the SDL to initialise audio
char* audioFile = "backgroundmusic.wav"; // a char pointer for the file path
// LoadWAV loads the wav file, and by putting it in an if statement like this, we can simutaneously check if the result is null, meaning an error occured while loading it.
if (SDL_LoadWAV(audioFile, &wavSpec, &wavStart, &wavLength) == NULL)
std::cerr << "Error: file could not be loaded as an audio file." << std::endl;
else
std::cout << audioFile << " loaded" << std::endl;
}
The playAudio function is responsible for loading the audio device and playing the audio through the Audio device
void mainEngineCW4::playAudio() // this function is for loading an audio device, and playing the audio through that device
{
audio.position = wavStart; // define where we start in the audio file
audio.length = wavLength; // define the length of the audio file
wavSpec.callback = myAudioCallback; // the callback variable needs a function that its going to run to be able to call back the audio that is played. assigning the function name to the variable allows it to call that function when needed
wavSpec.userdata = &audio; // the userdata is the audio itself
audioDevice = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, SDL_AUDIO_ALLOW_ANY_CHANGE); // opens the audio device, also having it play the audio information found at memory address for wavspec
if (audioDevice == 0) {
std::cerr << SDL_GetError() << std::endl;
return; }
SDL_PauseAudioDevice(audioDevice, 0); // mildly confused by why they decided to call the function for starting to play audio for "PauseAudioDevice" but yeah. this plays audio.
}
Here's my .h. I've defined myAudioCallback outside of the class, since SDL doesn't like the additional hidden parameter of a member function
struct AudioData
{
Uint8* position;
Uint32 length;
};
void myAudioCallback(void* userdata, Uint8* stream, int len);
class mainEngineCW4 :
public BaseEngine
{
public:
void loadAudio();
void playAudio();
void endAudio();
private:
// variables and pointers for audio information
AudioData audio;
SDL_AudioSpec wavSpec;
SDL_AudioDeviceID audioDevice;
Uint8* wavStart;
Uint32 wavLength;
};
I've removed the other functions and variables that are irrelevant to the issue I'm having
My problem is that I want my program to play the audio file I pass in, not just beeping noises. Any help is greatly appreciated
EDIT: I realised I'm crap at providing info and explanation to things, so I edited in more information, explanation and the header file. If there is anything else I can provide, please let me know