Sandro4912
Sandro4912

Reputation: 351

SDL_LoadWAV not loading file

I want to load a wav file and play it with the following code:

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
    std::cerr << "Could not init SDL:" << SDL_GetError() << '\n';
}
SDL_AudioSpec wavSpec;
Uint32 wavLength;
Uint8 *wavBuffer;

if(SDL_LoadWAV("sound.wav", &wavSpec, &wavBuffer, &wavLength) == NULL) {
    std::cerr << "Could not load wave:" << SDL_GetError() << '\n';
    return;
}

SDL_AudioDeviceID deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0);

int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength);
SDL_PauseAudioDevice(deviceId, 0);

SDL_CloseAudioDevice(deviceId);
SDL_FreeWAV(wavBuffer);
SDL_Quit();

However SDL_LoadWav returns NULL. SDL_GetError just returns:

Couldn't open sounds.wav

I already checked that the file exists but still no luck. Is there anything wrong with the code?

For debug purpose i also tryed to open the wav with ifstream like this:

std::ifstream ifs{"hitBrick.wav"};
if(!ifs) {
    std::cerr << "cannot open plattform fstream \n";
}

and here it works no error.

What could be the issue?

EDIT:

I could find the reason for the wav not getting loaded by SDL_LoadWAV.

According to the docu of SDL_LoadWav only supported wave formats are MS-ADPCM and IMA-ADPCM.

But my file is PCM format. So I converted the files under linux with the tool SOX: https://wiki.ubuntuusers.de/SoX/

Now I don't get any errors but also I don't hear any sounds. What could be that issue?

Upvotes: 1

Views: 391

Answers (1)

Sandro4912
Sandro4912

Reputation: 351

I found the Issue while I don't hear sound.

SDL_PauseAudioDevice(deviceId, 0);
SDL_CloseAudioDevice(deviceId);

Here I get ready to play the sound and close it directly.

With a delay in between the sound can be heared:

SDL_PauseAudioDevice(deviceId, 0);
SDL_Delay(1000);
SDL_CloseAudioDevice(deviceId);

Upvotes: 1

Related Questions