I have a MIDI file for background music and a WAV file for sound effect. The volume of the MIDI file is too high that it masks out the WAV file when playing together. So I hope to lower down the volume of the MIDI file while turn up the volume of the WAV file. Following are the program snippet I tried:
//Initialize SDL_mixer
if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0 )
{
printf( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
return -1;
}
//The music that will be played
Mix_Music *gMusic = NULL;
//The sound effects that will be used
Mix_Chunk *gScratch = NULL;
//Load music
gMusic = Mix_LoadMUS( "salamander.mid" );
if( gMusic == NULL )
{
printf( "Failed to load background music! SDL_mixer Error: %s\n", Mix_GetError() );
return -1;
}
//Load sound effects
gScratch = Mix_LoadWAV( "Fire.wav" );
if( gScratch == NULL )
{
printf( "Failed to load sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
return -1;
}
//Play the music
Mix_PlayMusic(gMusic, -1);
Mix_VolumeMusic(1);
//Play sound effect
Mix_PlayChannel(-1, gScratch, -1);
//Mix_VolumeChunk(gScratch, 1/*MIX_MAX_VOLUME*/);
Mix_Volume(-1, MIX_MAX_VOLUME);
Sleep(5000);
//Free the sound effects
Mix_FreeChunk( gScratch );
//Free the music
Mix_FreeMusic( gMusic );
The problem is that both the music and sound effect are turned down by Mix_VolumeMusic(1);. I have tried many combinations to control the volume of two sounds separately, but none of them works. Could you please tell me how I should use SDL_mixer to control the volume of two sound files separately – lower down the MIDI while turn up the WAV? Thanks a lot.
PS: I was trying on Windows 10 with SDL 2.0.4 and SDL_mixer 2.0.4.
PS2: The two sound files has been uploaded as attachment of this thread for your testing.
PS3: The above code works fine on Ubuntu. The problem only arises on Windows.
salamander.mid (161.8 KB)