The Battle for Wesnoth  1.19.26+dev
sound.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "events.hpp"
19 #include "map/location.hpp"
20 #include "sound_tracks.hpp"
21 #include "sound_music_track.hpp"
22 #include "utils/optional_fwd.hpp"
23 
24 #include <map>
25 #include <string>
26 
27 class config;
28 
29 namespace sound {
30 
31 std::string current_driver();
32 std::vector<std::string> enumerate_drivers();
33 
35 {
37  int frequency;
38  SDL_AudioFormat format;
39  int channels;
41 
42  static driver_status query();
43 };
44 
45 bool init_sound();
46 void close_sound();
47 void reset_sound();
48 
49 void stop_music();
50 void stop_sound();
51 void stop_UI_sound();
52 void stop_bell();
53 
54 // Read config entry, alter track list accordingly.
55 void play_music_config(const config &music_node, bool allow_interrupt_current_track = false, int i = -1);
56 // Act on any track list changes from above.
58 
59 // Play this particular music file once, then silence.
60 void play_music_once(const std::string& id);
61 // Empty the playlist
62 void empty_playlist();
63 // Start playing current music.
64 void play_music();
65 
66 // Change parameters of a playing sound, given its id
67 void reposition_sound(unsigned id, unsigned int distance);
68 #define DISTANCE_SILENT 255
69 #define DISTANCE_NONE 0
70 
71 // Check if there's a sound associated with given id playing
72 bool is_sound_playing(int id);
73 
74 // Stop sound associated with a given id
75 void stop_sound(unsigned id);
76 
77 // Play sound, or random one of comma-separated sounds.
78 void play_sound(const std::string& files, sound_tracks::type group = sound_tracks::type::sound_fx, unsigned int repeats = 0);
79 
80 // Play sound, or random one of comma-separated sounds. Use specified
81 // distance and associate it with specified id (of a sound source).
82 void play_sound_positioned(const std::string &files, int repeats, unsigned int distance, unsigned int id);
83 
84 // Play sound, or random one of comma-separated sounds in bell channel
85 void play_bell(const std::string& files);
86 
87 // Play sound, or random one of comma-separated sounds in timer channel
88 void play_timer(const std::string& files, const std::chrono::milliseconds& loop_ticks, const std::chrono::milliseconds& fadein_ticks);
89 
90 // Play user-interface sound, or random one of comma-separated sounds.
91 void play_UI_sound(const std::string& files);
92 
93 // A class to periodically check for new music that needs to be played
95  void process();
96 };
97 
98 // A class to mute music when the game is in background
100 public:
101  music_muter();
102  void handle_event(const SDL_Event&) override {}
103  void handle_window_event(const SDL_Event& event) override;
104 };
105 
106 // Save music playlist for snapshot
107 void write_music_play_list(config& snapshot);
108 
109 class volume
110 {
111 public:
112  constexpr explicit volume(float factor)
113  : gain(factor)
114  {
115  }
116 
117  constexpr static volume from_percent(float percentage)
118  { return volume{percentage / 100.f}; }
119 
120  constexpr float as_percent() const
121  { return gain * 100.f; }
122 
123  /** Implicit conversion for use with SDL. */
124  constexpr operator float() const
125  { return gain; }
126 
127  constexpr auto operator*(const volume& other) const
128  { return volume{gain * other.gain}; }
129 
130  constexpr auto operator/(const volume& other) const
131  { return volume{gain / other.gain}; }
132 
133  constexpr auto operator+(const volume& other) const
134  { return volume{gain + other.gain}; }
135 
136  constexpr auto operator-(const volume& other) const
137  { return volume{gain - other.gain}; }
138 
139 private:
140  float gain{1.0};
141 };
142 
143 constexpr inline volume silence{0.f};
144 constexpr inline volume full_volume{1.f};
145 constexpr inline volume max_volume{1.28f};
146 
147 volume get_music_volume();
148 volume get_sound_volume();
149 
150 void set_music_volume(volume vol);
151 void set_sound_volume(volume vol);
152 void set_bell_volume(volume vol);
153 void set_UI_volume(volume vol);
154 
155 utils::optional<unsigned int> get_current_track_index();
156 std::shared_ptr<sound::music_track> get_current_track();
157 std::shared_ptr<sound::music_track> get_previous_music_track();
158 void set_previous_track(std::shared_ptr<music_track>);
159 unsigned int get_num_tracks();
160 void remove_track(unsigned int i);
161 void play_track(unsigned int i);
162 
163 void flush_cache();
164 
165 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:157
void handle_event(const SDL_Event &) override
Definition: sound.hpp:102
void handle_window_event(const SDL_Event &event) override
Definition: sound.cpp:711
constexpr float as_percent() const
Definition: sound.hpp:120
constexpr volume(float factor)
Definition: sound.hpp:112
float gain
Definition: sound.hpp:140
constexpr auto operator*(const volume &other) const
Definition: sound.hpp:127
constexpr static volume from_percent(float percentage)
Definition: sound.hpp:117
constexpr auto operator/(const volume &other) const
Definition: sound.hpp:130
constexpr auto operator+(const volume &other) const
Definition: sound.hpp:133
constexpr auto operator-(const volume &other) const
Definition: sound.hpp:136
std::size_t i
Definition: function.cpp:1031
Audio output for sound and music.
Definition: preferences.hpp:69
void write_music_play_list(config &snapshot)
Definition: sound.cpp:755
void empty_playlist()
Definition: sound.cpp:518
void reposition_sound(unsigned id, unsigned int distance)
Definition: sound.cpp:765
void set_UI_volume(volume vol)
Definition: sound.cpp:1029
void reset_sound()
Definition: sound.cpp:443
constexpr volume full_volume
Definition: sound.hpp:144
void set_music_volume(volume vol)
Definition: sound.cpp:983
bool init_sound()
Definition: sound.cpp:346
void close_sound()
Definition: sound.cpp:413
constexpr volume silence
Definition: sound.hpp:143
volume get_music_volume()
Definition: sound.cpp:974
void play_music_config(const config &music_node, bool allow_interrupt_current_track, int i)
Definition: sound.cpp:610
void remove_track(unsigned int i)
Definition: sound.cpp:168
void play_music()
Definition: sound.cpp:523
unsigned int get_num_tracks()
Definition: sound.cpp:143
void stop_music()
Definition: sound.cpp:472
void set_sound_volume(volume vol)
Definition: sound.cpp:1000
void play_music_once(const std::string &file)
Definition: sound.cpp:507
utils::optional< unsigned int > get_current_track_index()
Definition: sound.cpp:123
void stop_UI_sound()
Definition: sound.cpp:500
std::vector< std::string > enumerate_drivers()
Definition: sound.cpp:316
void play_sound(const std::string &files, sound_tracks::type group, unsigned int repeats)
Definition: sound.cpp:936
void flush_cache()
Definition: sound.cpp:1040
void set_bell_volume(volume vol)
Definition: sound.cpp:1019
bool is_sound_playing(int id)
Definition: sound.cpp:780
void stop_bell()
Definition: sound.cpp:492
volume get_sound_volume()
Definition: sound.cpp:990
void play_timer(const std::string &files, const std::chrono::milliseconds &loop_ticks, const std::chrono::milliseconds &fadein_ticks)
Definition: sound.cpp:959
void play_sound_positioned(const std::string &files, int repeats, unsigned int distance, unsigned int id)
Definition: sound.cpp:943
void commit_music_changes()
Definition: sound.cpp:726
void play_track(unsigned int i)
Definition: sound.cpp:535
constexpr volume max_volume
Definition: sound.hpp:145
void play_UI_sound(const std::string &files)
Definition: sound.cpp:967
void set_previous_track(std::shared_ptr< music_track > track)
Definition: sound.cpp:138
std::shared_ptr< music_track > get_previous_music_track()
Definition: sound.cpp:134
std::shared_ptr< music_track > get_current_track()
Definition: sound.cpp:130
void stop_sound()
Definition: sound.cpp:481
std::string current_driver()
Definition: sound.cpp:310
void play_bell(const std::string &files)
Definition: sound.cpp:951
static driver_status query()
Definition: sound.cpp:329
SDL_AudioFormat format
Definition: sound.hpp:38