0

I'm making a version of Tetris in SDL 2 that has sounds. I have background music, which works fine. However, the sound effects (move, rotate, etc.) play static instead of what they're supposed to play.

Some code:

Definitions:

Mix_Music *music;
Mix_Chunk *move, *rotate, *clear, *difficult, *gameOver;

At the beginning:

if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 4, 2048) != 0) {
    printf("Error: Failed to initiate Mixer subsystem. SDL_Mixer Error: %s\n", Mix_GetError());
    return false;
}

music = Mix_LoadMUS("music.wav");
if (music == NULL) {
    printf("Failed to load Music. SDL_Mixer Error: %s\n", Mix_GetError());
}

move = Mix_LoadWAV("move.wav");
if (move == NULL) {
    printf("Failed to load sound \"move\". SDL_Mixer Error: %s\n", Mix_GetError());
}

rotate = Mix_LoadWAV("rotate.wav");
if (rotate == NULL) {
    printf("Failed to load sound \"rotate\". SDL_Mixer Error: %s\n", Mix_GetError());
}
...
Mix_VolumeMusic((128 * options[0].currentOption * options[1].currentOption) / 10000); // (128 * 20 * 100) / 10000 = 25;
Mix_VolumeChunk(move, (128 * options[0].currentOption * options[2].currentOption) / 10000); // (128 * 20 * 100) / 10000 = 25;
Mix_VolumeChunk(rotate, (128 * options[0].currentOption * options[2].currentOption) / 10000); // (128 * 20 * 100) / 10000 = 25;
...

Play sound code:

Mix_PlayChannel(-1, move, 0);

Close:

Mix_FreeMusic(music);
music = NULL;
Mix_FreeChunk(move);
Mix_FreeChunk(rotate);
Mix_FreeChunk(clear);
Mix_FreeChunk(difficult);
Mix_FreeChunk(gameOver);
move = NULL;
rotate = NULL;
clear = NULL;
difficult = NULL;
gameOver = NULL;

Mix_Quit();

Miscellaneous info:

  • Using C++ in VS 2013
  • Using SDL_mixer 2.0.0, downloaded yesterday
  • Sounds are ~11kB, music is 94,675kB
  • The sounds play fine in Windows Media Player

Let me know if you need any other info, and thanks in advance!

ricky3350
  • 1,710
  • 3
  • 20
  • 35
  • Have you allocated the channels you need to play multiple sounds? From what I understand of the documentation on [Mix_AllocateChannels](http://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC26) and its cousins, calling `Mix_PlayChannel` with -1 as the first argument will only work if you have enough channels allocated that a new channel can be assigned to the sound. – jaggedSpire May 21 '15 at 02:54
  • @jaggedSpire It should have 4 channels (`Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 4, 2048)`), according to http://sdl.beuc.net/sdl.wiki/Mix_OpenAudio. Does this not work? – ricky3350 May 21 '15 at 20:26
  • It looks like you're correct. Sorry about that. I can see two other (fairly unlikely) possibilities, both dealing with the formatting of the sound: Are you sure that `MIX_DEFAULT_FORMAT` is not one of the non-portable formats? Finally, I've had some mild issues with audio playback and endian-ness of the file used in the past. You could try opening up those files in Audacity and exporting them in the same format as the background music. Again: remote possibility. I don't know why SDL_Mixer wouldn't do this for you, though I'm less familiar with it than SDL_Audio. – jaggedSpire May 21 '15 at 20:44
  • You might want to try calling the channel allocation function anyway. The `Mix_OpenAudio` function's `channel` parameter might be referring to the audio format of the opened device, which discerns single track sound from stereo, and stereo sound from surround-sound. – jaggedSpire May 22 '15 at 17:47

0 Answers0