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 load_soundset() { const std::string default_path = FILENAMES["defaultsounddir"]; const std::string default_soundpack = "basic"; std::string current_soundpack = get_option<std::string>( "SOUNDPACKS" ); std::string soundpack_path; // Get current soundpack and it's directory path. if( current_soundpack.empty() ) { dbg( D_ERROR ) << "Soundpack not set in options or empty."; soundpack_path = default_path; current_soundpack = default_soundpack; } else { dbg( D_INFO ) << "Current soundpack is: " << current_soundpack; soundpack_path = SOUNDPACKS[current_soundpack]; } if( soundpack_path.empty() ) { dbg( D_ERROR ) << "Soundpack with name " << current_soundpack << " can't be found or empty string"; soundpack_path = default_path; current_soundpack = default_soundpack; } else { dbg( D_INFO ) << '"' << current_soundpack << '"' << " soundpack: found path: " << soundpack_path; } current_soundpack_path = soundpack_path; try { loading_ui ui( false ); DynamicDataLoader::get_instance().load_data_from_path( soundpack_path, "core", ui ); } catch( const std::exception &err ) { dbg( D_ERROR ) << "failed to load sounds: " << err.what(); } // Preload sound effects for( const auto &preload : sfx_preload ) { const auto find_result = sfx_resources.sound_effects.find( preload ); if( find_result != sfx_resources.sound_effects.end() ) { for( const auto &sfx : find_result->second ) { get_sfx_resource( sfx.resource_id ); } } } // Memory of unique_paths no longer required, swap with locally scoped unordered_map // to force deallocation of resources. { unique_paths.clear(); std::unordered_map<std::string, int> t_swap; unique_paths.swap( t_swap ); } // Memory of sfx_preload no longer required, swap with locally scoped vector // to force deallocation of resources. { sfx_preload.clear(); std::vector<id_and_variant> t_swap; sfx_preload.swap( t_swap ); } }
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 ); }