// Same as above, but with fallback to "default" variant. May still return `nullptr` const sound_effect *find_random_effect( const std::string &id, const std::string &variant ) { const auto eff = find_random_effect( id_and_variant( id, variant ) ); if( eff != nullptr ) { return eff; } return find_random_effect( id_and_variant( id, "default" ) ); }
void sfx::play_ambient_variant_sound( const std::string &id, const std::string &variant, int volume, int channel, int duration, float pitch ) { if( !check_sound( volume ) ) { return; } const sound_effect *eff = find_random_effect( id, variant ); if( eff == nullptr ) { return; } const sound_effect &selected_sound_effect = *eff; Mix_Chunk *effect_to_play = get_sfx_resource( selected_sound_effect.resource_id ); Mix_Chunk *shifted_effect = do_pitch_shift( effect_to_play, pitch ); Mix_VolumeChunk( shifted_effect, selected_sound_effect.volume * get_option<int>( "AMBIENT_SOUND_VOLUME" ) * volume / ( 100 * 100 ) ); if( duration ) { if( Mix_FadeInChannel( channel, shifted_effect, -1, duration ) == -1 ) { dbg( D_ERROR ) << "Failed to play sound effect: " << Mix_GetError(); } } else { if( Mix_PlayChannel( channel, shifted_effect, -1 ) == -1 ) { dbg( D_ERROR ) << "Failed to play sound effect: " << Mix_GetError(); } } Mix_RegisterEffect( channel, empty_effect, cleanup_when_channel_finished, shifted_effect ); }
void sfx::play_variant_sound( const std::string &id, const std::string &variant, int volume ) { if( !check_sound( volume ) ) { return; } const sound_effect *eff = find_random_effect( id, variant ); if( eff == nullptr ) { eff = find_random_effect( id, "default" ); if( eff == nullptr ) { return; } } const sound_effect &selected_sound_effect = *eff; Mix_Chunk *effect_to_play = get_sfx_resource( selected_sound_effect.resource_id ); Mix_VolumeChunk( effect_to_play, selected_sound_effect.volume * get_option<int>( "SOUND_EFFECT_VOLUME" ) * volume / ( 100 * 100 ) ); Mix_PlayChannel( -1, effect_to_play, 0 ); }
void sfx::play_ambient_variant_sound( const std::string &id, const std::string &variant, int volume, int channel, int duration ) { if( !check_sound( volume ) ) { return; } const sound_effect *eff = find_random_effect( id, variant ); if( eff == nullptr ) { return; } const sound_effect &selected_sound_effect = *eff; Mix_Chunk *effect_to_play = get_sfx_resource( selected_sound_effect.resource_id );; Mix_VolumeChunk( effect_to_play, selected_sound_effect.volume * get_option<int>( "SOUND_EFFECT_VOLUME" ) * volume / ( 100 * 100 ) ); if( Mix_FadeInChannel( channel, effect_to_play, -1, duration ) == -1 ) { dbg( D_ERROR ) << "Failed to play sound effect: " << Mix_GetError(); } }
void sfx::play_variant_sound_pitch( const std::string &id, const std::string &variant, int volume, int angle, float pitch ) { if( !check_sound( volume ) ) { return; } const sound_effect *eff = find_random_effect( id, variant ); if( eff == nullptr ) { return; } const sound_effect &selected_sound_effect = *eff; Mix_Chunk *effect_to_play = get_sfx_resource( selected_sound_effect.resource_id ); Mix_Chunk *shifted_effect = do_pitch_shift( effect_to_play, pitch ); Mix_VolumeChunk( shifted_effect, selected_sound_effect.volume * get_option<int>( "SOUND_EFFECT_VOLUME" ) * volume / ( 100 * 100 ) ); int channel = Mix_PlayChannel( -1, shifted_effect, 0 ); Mix_RegisterEffect( channel, empty_effect, cleanup_when_channel_finished, shifted_effect ); Mix_SetPosition( channel, angle, 1 ); }
bool sfx::has_variant_sound( const std::string &id, const std::string &variant ) { return find_random_effect( id, variant ) != nullptr ? true : false; }