Hi all!
It’s been two days I try to use the SDl library to play a wave file, without success. I’ve been browsing Internet looking for code, bugs, advices, documentation, but found nothing that seems to help.
Here is the code:
#include <iostream>
#include <ostream>
#include <stdio.h>
#include <SDL.h>
#include <SDL_audio.h>
#include <wiringPi.h>
using namespace std;
uint32_t audioLen;
uint8_t *audioPos;
void audioCallback(void *userdata, uint8_t *stream, int len){
if(audioLen == 0) return;
if(len > audioLen){
len = audioLen;
}
SDL_memset(stream, 0, len);
SDL_MixAudio(stream, audioPos, len, 100);
audioPos += len;
audioLen -= len;
cout << "writing " << len << " bytes, from ";
cout << (int)audioPos << " to " << (int)stream << endl;
}
void testDevices(){
for (uint8_t i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
printf("Audio driver %d: %s\n", i, SDL_GetAudioDriver(i));
}
cout << "current audio driver is " << SDL_GetCurrentAudioDriver() << endl;
int nbDevice = SDL_GetNumAudioDevices(0);
for(int i = 0; i < nbDevice; ++i){
cout << "device n°" << i << ": ";
cout << SDL_GetAudioDeviceName(i, 0) << endl;
}
}
int main(int argc, char* argv[]){
if(SDL_Init(SDL_INIT_AUDIO) != 0){
cout << "unable to init SDL" << endl;
return 1;
}
testDevices();
SDL_AudioSpec want, have;
uint32_t waveLength;
uint8_t *waveBuffer;
SDL_AudioDeviceID device;
if(SDL_LoadWAV("/home/pi/music/paint.wav", &want, &waveBuffer, &waveLength) == NULL){
cout << "could not open file" << endl;
} else {
want.callback = audioCallback;
want.userdata = NULL;
cout << "file informations:" << endl;
cout << "freq: " << want.freq << endl;
cout << "format: " << want.format << endl;
cout << "channels: " << (int)want.channels << endl;
cout << "silence: " << (int)want.silence << endl;
cout << "samples: " << want.samples << endl;
cout << "size: " << want.size << endl;
audioPos = waveBuffer;
audioLen = waveLength;
cout << "file is " << waveLength << " samples long" << endl;
cout << "buffer start is at address " << (uint32_t)waveBuffer << endl;
device = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(0, 0), 0, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
cout << "play audio on device n°" << device << endl;
SDL_PauseAudioDevice(device, 0);
SDL_PauseAudio(0);
uint32_t start = millis();
cout << "start feeding buffer" << endl;
uint32_t length = waveLength * 4;
for(;;){
if(millis() - start > 5000) break;
}
SDL_CloseAudio();
SDL_FreeWAV(waveBuffer);
}
SDL_Quit();
return 0;
}
There doesn’t seem to be any error while executing. The file is found, the values read from it are right, and the program execute till end without problem. But there is no sound.
This is intended to run on a Raspberry pi, but the same code has been tested localy on a ubuntu laptop as well. I’ve seen that sometimes you have to force the audio driver by setting the variable SDL_AUDIODRIVER to another value (say pulseaudio, alsa or dsp on Linux). I’ve seen no difference, and when I change this value (I reload the bashrc file each time) the print from the program doesn’t change, and stays on the same audio driver.
There is probably something I’m missing, and I fear it’s a really simple thing. But at this point I cannot figure out what!
Thank you in advance, hope someone here will be able to give an answer.