add different sound channel for end turn bell, thx to Pietro

This commit is contained in:
Jérémy Rosen 2006-09-06 20:07:48 +00:00
parent d433bb211d
commit 3d42ad18b7
9 changed files with 148 additions and 15 deletions

View file

@ -15,6 +15,8 @@ Version 1.3-svn:
* unit Art
* added a recruit animation to the skeleton
* user interface
* turn bell moved to sound tab in prefereces
* new slider - bell volume
* disable inactive sliders instead of hiding them
* sliders can be adjusted with keyboard left and right
* sidebar reports related to gold, units, villages and time get greyed out

View file

@ -557,6 +557,7 @@ Paul Smedley (Creeping)
Andrea Palmatè (afxgroup)
EdB
Jim Carroll (Jimm)
Petr Sobotka (Pietro)
"
[/about]

View file

@ -231,10 +231,11 @@ game_controller::game_controller(int argc, char** argv)
}
}
if (preferences::sound_on() || preferences::music_on()) {
if (preferences::sound_on() || preferences::music_on() || preferences::turn_bell()) {
if(!sound::init_sound()) {
preferences::set_sound(false);
preferences::set_music(false);
preferences::set_turn_bell(false);
}
}
}

View file

@ -444,7 +444,7 @@ void playsingle_controller::before_human_turn(bool save)
}
if(preferences::turn_bell()) {
sound::play_sound(game_config::sounds::turn_bell);
sound::play_bell(game_config::sounds::turn_bell);
}
if(preferences::turn_dialog()) {

View file

@ -198,6 +198,17 @@ void set_sound_volume(int vol)
sound::set_sound_volume(sound_volume());
}
int bell_volume()
{
return lexical_cast_default<int>(prefs["bell_volume"], 100);
}
void set_bell_volume(int vol)
{
prefs["bell_volume"] = lexical_cast_default<std::string>(vol, "100");
sound::set_bell_volume(bell_volume());
}
bool adjust_gamma()
{
return prefs["adjust_gamma"] == "yes";
@ -343,7 +354,22 @@ bool turn_bell()
void set_turn_bell(bool ison)
{
prefs["turn_bell"] = (ison ? "yes" : "no");
if(!turn_bell() && ison) {
prefs["turn_bell"] = "yes";
if(!music_on() && !sound_on()) {
if(!sound::init_sound()) {
prefs["turn_bell"] = "no";
return;
}
}
} else if(turn_bell() && !ison) {
prefs["turn_bell"] = "no";
sound::stop_bell();
if(!music_on() && !sound_on())
sound::close_sound();
}
return;
}
const std::string& turn_cmd()
@ -373,7 +399,7 @@ bool sound_on() {
bool set_sound(bool ison) {
if(!sound_on() && ison) {
prefs["sound"] = "yes";
if(!music_on()) {
if(!music_on() && !turn_bell()) {
if(!sound::init_sound()) {
prefs["sound"] = "no";
return false;
@ -382,7 +408,7 @@ bool set_sound(bool ison) {
} else if(sound_on() && !ison) {
prefs["sound"] = "no";
sound::stop_sound();
if(!music_on())
if(!music_on() && !turn_bell())
sound::close_sound();
}
return true;
@ -395,7 +421,7 @@ bool music_on() {
bool set_music(bool ison) {
if(!music_on() && ison) {
prefs["music"] = "yes";
if(!sound_on()) {
if(!sound_on() && !turn_bell()) {
if(!sound::init_sound()) {
prefs["music"] = "no";
return false;
@ -405,7 +431,7 @@ bool set_music(bool ison) {
sound::play_music();
} else if(music_on() && !ison) {
prefs["music"] = "no";
if(!sound_on())
if(!sound_on() && !turn_bell())
sound::close_sound();
else
sound::stop_music();

View file

@ -67,6 +67,9 @@ namespace preferences {
int sound_volume();
void set_sound_volume(int vol);
int bell_volume();
void set_bell_volume(int vol);
bool music_on();
bool set_music(bool ison);

View file

@ -191,13 +191,14 @@ private:
const config* get_advanced_pref() const;
void set_advanced_menu();
gui::slider music_slider_, sound_slider_, scroll_slider_, gamma_slider_, chat_lines_slider_;
// change
gui::slider music_slider_, sound_slider_, bell_slider_, scroll_slider_, gamma_slider_, chat_lines_slider_;
gui::button fullscreen_button_, turbo_button_, show_ai_moves_button_,
show_grid_button_, show_lobby_joins_button_, show_floating_labels_button_, turn_dialog_button_,
turn_bell_button_, show_team_colours_button_, show_colour_cursors_button_,
show_haloing_button_, video_mode_button_, theme_button_, hotkeys_button_, gamma_button_,
flip_time_button_, advanced_button_, sound_button_, music_button_, chat_timestamp_button_;
gui::label music_label_, sound_label_, scroll_label_, gamma_label_, chat_lines_label_;
gui::label music_label_, sound_label_, bell_label_, scroll_label_, gamma_label_, chat_lines_label_;
unsigned slider_label_width_;
gui::menu advanced_;
@ -209,9 +210,10 @@ private:
const config& game_cfg_;
};
//change
preferences_dialog::preferences_dialog(display& disp, const config& game_cfg)
: gui::preview_pane(disp.video()),
music_slider_(disp.video()), sound_slider_(disp.video()),
music_slider_(disp.video()), sound_slider_(disp.video()), bell_slider_(disp.video()),
scroll_slider_(disp.video()), gamma_slider_(disp.video()), chat_lines_slider_(disp.video()),
fullscreen_button_(disp.video(), _("Toggle Full Screen"), gui::button::TYPE_CHECK),
turbo_button_(disp.video(), _("Accelerated Speed"), gui::button::TYPE_CHECK),
@ -234,6 +236,7 @@ preferences_dialog::preferences_dialog(display& disp, const config& game_cfg)
music_button_(disp.video(), _("Music"), gui::button::TYPE_CHECK),
chat_timestamp_button_(disp.video(), _("Chat Timestamping"), gui::button::TYPE_CHECK),
music_label_(disp.video(), _("Music Volume:")), sound_label_(disp.video(), _("SFX Volume:")),
bell_label_(disp.video(), _("Bell Volume:")),
scroll_label_(disp.video(), _("Scroll Speed:")), gamma_label_(disp.video(), _("Gamma:")), chat_lines_label_(disp.video(), ""),
slider_label_width_(0), advanced_(disp.video(),std::vector<std::string>(),false,-1,-1,NULL,&gui::menu::bluebg_style), advanced_selection_(-1),
tab_(GENERAL_TAB), disp_(disp), game_cfg_(game_cfg)
@ -260,6 +263,11 @@ preferences_dialog::preferences_dialog(display& disp, const config& game_cfg)
music_slider_.set_value(music_volume());
music_slider_.set_help_string(_("Change the music volume"));
// bell volume slider
bell_slider_.set_min(0);
bell_slider_.set_max(128);
bell_slider_.set_value(bell_volume());
bell_slider_.set_help_string(_("Change the bell volume"));
scroll_slider_.set_min(1);
scroll_slider_.set_max(100);
@ -333,6 +341,7 @@ handler_vector preferences_dialog::handler_members()
handler_vector h;
h.push_back(&music_slider_);
h.push_back(&sound_slider_);
h.push_back(&bell_slider_); //change
h.push_back(&scroll_slider_);
h.push_back(&gamma_slider_);
h.push_back(&chat_lines_slider_);
@ -358,6 +367,7 @@ handler_vector preferences_dialog::handler_members()
h.push_back(&chat_timestamp_button_);
h.push_back(&music_label_);
h.push_back(&sound_label_);
h.push_back(&bell_label_);
h.push_back(&scroll_label_);
h.push_back(&gamma_label_);
h.push_back(&chat_lines_label_);
@ -388,7 +398,6 @@ void preferences_dialog::update_location(SDL_Rect const &rect)
ypos += item_interline; turbo_button_.set_location(rect.x, ypos);
ypos += item_interline; show_ai_moves_button_.set_location(rect.x, ypos);
ypos += item_interline; turn_dialog_button_.set_location(rect.x, ypos);
ypos += item_interline; turn_bell_button_.set_location(rect.x, ypos);
ypos += item_interline; show_team_colours_button_.set_location(rect.x, ypos);
ypos += item_interline; show_grid_button_.set_location(rect.x, ypos);
ypos += item_interline; hotkeys_button_.set_location(rect.x, ypos);
@ -410,7 +419,7 @@ void preferences_dialog::update_location(SDL_Rect const &rect)
theme_button_.set_location(rect.x+video_mode_button_.width()+10, ypos);
// Sound tab
slider_label_width_ = maximum<unsigned>(music_label_.width(), sound_label_.width());
slider_label_width_ = maximum<unsigned>(maximum<unsigned>(music_label_.width(), sound_label_.width()), bell_label_.width());
ypos = rect.y + top_border;
sound_button_.set_location(rect.x, ypos);
@ -429,6 +438,17 @@ void preferences_dialog::update_location(SDL_Rect const &rect)
rect.w - slider_label_width_ - right_border, 0 };
music_slider_.set_location(music_rect);
// Bell slider
ypos += item_interline;
turn_bell_button_.set_location(rect.x, ypos);
ypos += item_interline;
bell_label_.set_location(rect.x, ypos);
const SDL_Rect bell_rect = { rect.x + slider_label_width_, ypos,
rect.w - slider_label_width_ - right_border, 0 };
bell_slider_.set_location(bell_rect);
// Multiplayer tab
ypos = rect.y + top_border;
chat_lines_label_.set_location(rect.x, ypos);
@ -494,6 +514,7 @@ void preferences_dialog::process_event()
sound_button_.set_check(false);
}
set_sound_volume(sound_slider_.value());
set_bell_volume(bell_slider_.value());
if (music_button_.pressed()) {
if(!set_music(music_button_.checked()))
@ -589,7 +610,6 @@ void preferences_dialog::set_selection(int index)
turbo_button_.hide(hide_general);
show_ai_moves_button_.hide(hide_general);
turn_dialog_button_.hide(hide_general);
turn_bell_button_.hide(hide_general);
hotkeys_button_.hide(hide_general);
show_team_colours_button_.hide(hide_general);
show_grid_button_.hide(hide_general);
@ -615,6 +635,9 @@ void preferences_dialog::set_selection(int index)
sound_button_.hide(hide_sound);
sound_label_.hide(hide_sound);
sound_slider_.hide(hide_sound);
turn_bell_button_.hide(hide_sound);
bell_label_.hide(hide_sound);
bell_slider_.hide(hide_sound);
const bool hide_multiplayer = tab_ != MULTIPLAYER_TAB;
chat_lines_label_.hide(hide_multiplayer);

View file

@ -35,6 +35,7 @@ namespace {
bool mix_ok = false;
unsigned music_start_time = 0;
std::map<std::string,Mix_Chunk*> sound_cache;
std::map<std::string,Mix_Chunk*> bell_cache; // Bell sound use separate channel
std::map<std::string,Mix_Music*> music_cache;
struct music_track
@ -220,6 +221,7 @@ bool init_sound() {
Mix_AllocateChannels(16);
set_sound_volume(preferences::sound_volume());
set_music_volume(preferences::music_volume());
set_bell_volume(preferences::bell_volume());
LOG_AUDIO << "Audio initialized.\n";
@ -232,6 +234,7 @@ void close_sound() {
int numtimesopened, frequency, channels;
Uint16 format;
if(mix_ok) {
stop_bell();
stop_sound();
stop_music();
mix_ok = false;
@ -264,7 +267,8 @@ void stop_music() {
void stop_sound() {
if(mix_ok) {
Mix_HaltChannel(-1);
for (int i = 0; i < 15; i++)
Mix_HaltChannel(i);
std::map<std::string,Mix_Chunk*>::iterator i;
for(i = sound_cache.begin(); i != sound_cache.end(); ++i)
@ -273,6 +277,17 @@ void stop_sound() {
}
}
void stop_bell() {
if(mix_ok) {
Mix_HaltChannel(15);
std::map<std::string,Mix_Chunk*>::iterator i;
for(i = bell_cache.begin(); i != bell_cache.end(); ++i)
Mix_FreeChunk(i->second);
bell_cache.clear();
}
}
void think_about_music(void)
{
if (!music_start_time) {
@ -426,6 +441,7 @@ void write_music_play_list(config& snapshot)
void play_sound(const std::string& files)
{
if(files.empty()) return;
if(preferences::sound_on() && mix_ok) {
std::string file = pick_one(files);
@ -455,6 +471,8 @@ void play_sound(const std::string& files)
}
//play on the first available channel
//FIXME: in worst case it can play on bell channel(15), nothing happend
// only sound can have another volume than others sounds
const int res = Mix_PlayChannel(-1, cache, 0);
if(res < 0) {
ERR_AUDIO << "error playing sound effect: " << Mix_GetError() << "\n";
@ -462,6 +480,47 @@ void play_sound(const std::string& files)
}
}
// Play bell on separate volume than sound
void play_bell(const std::string& files)
{
if(files.empty()) return;
if(preferences::turn_bell() && mix_ok) {
std::string file = pick_one(files);
// the insertion will fail if there is already an element in the cache
std::pair< std::map< std::string, Mix_Chunk * >::iterator, bool >
it = bell_cache.insert(std::make_pair(file, (Mix_Chunk *)0));
Mix_Chunk *&cache = it.first->second;
if (it.second) {
std::string const &filename = get_binary_file_location("sounds", file); if (!filename.empty()) {
#ifdef USE_ZIPIOS
std::string const &s = read_file(filename);
if (!s.empty()) {
SDL_RWops* ops = SDL_RWFromMem((void*)s.c_str(), s.size());
cache = Mix_LoadWAV_RW(ops,0);
}
#else
cache = Mix_LoadWAV(filename.c_str());
#endif
}
if (cache == NULL) {
ERR_AUDIO << "Could not load sound file '" << filename << "': "
<< Mix_GetError() << "\n";
return;
}
}
//play on the last (bell) channel
const int res = Mix_PlayChannel(15, cache, 0);
if(res < 0) {
ERR_AUDIO << "error playing sound effect: " << Mix_GetError() << "\n";
}
}
}
void set_music_volume(int vol)
{
if(mix_ok && vol >= 0) {
@ -476,8 +535,21 @@ void set_sound_volume(int vol)
if(mix_ok && vol >= 0) {
if(vol > MIX_MAX_VOLUME)
vol = MIX_MAX_VOLUME;
Mix_Volume(-1, vol);
// Bell has separate channel which we can't set up from this
for (int i = 0; i < 15; i++){
Mix_Volume(i, vol);
}
}
}
void set_bell_volume(int vol)
{
if(mix_ok && vol >= 0) {
if(vol > MIX_MAX_VOLUME)
vol = MIX_MAX_VOLUME;
Mix_Volume(15, vol);
}
}
}

View file

@ -29,6 +29,7 @@ void close_sound();
void stop_music();
void stop_sound();
void stop_bell();
// Read config entry, alter track list accordingly.
void play_music_config(const config &music);
@ -47,6 +48,9 @@ void play_music();
// Play sound, or random one of comma-separated sounds.
void play_sound(const std::string& files);
// Play sound, or random one of comma-separated sounds in bell channel
void play_bell(const std::string& files);
// Called from event loop to see if we need new music track.
void think_about_music(void);
@ -55,6 +59,7 @@ void write_music_play_list(config& snapshot);
void set_music_volume(int vol);
void set_sound_volume(int vol);
void set_bell_volume(int vol);
}