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 {
36  bool initialized{false};
37  int frequency{0};
38  SDL_AudioFormat format{SDL_AUDIO_UNKNOWN};
39  int channels{0};
40 
41  static driver_status query();
42 };
43 
44 bool init_sound();
45 void close_sound();
46 
47 void stop_music();
48 void stop_sound();
49 void stop_UI_sound();
50 void stop_bell();
51 
52 void restart_music();
53 void restart_sound();
54 void restart_bell();
55 void restart_UI_sound();
56 
57 // Read config entry, alter track list accordingly.
58 void play_music_config(const config &music_node, bool allow_interrupt_current_track = false, int i = -1);
59 // Act on any track list changes from above.
61 
62 // Play this particular music file once, then silence.
63 void play_music_once(const std::string& id);
64 // Empty the playlist
65 void empty_playlist();
66 // Start playing current music.
67 void play_music();
68 
69 // Change parameters of a playing sound, given its id
70 void reposition_sound(unsigned id, unsigned int distance);
71 
72 constexpr inline int distance_silent = 255;
73 constexpr inline int distance_none = 0;
74 
75 // Check if there's a sound associated with given id playing
76 bool is_sound_playing(int id);
77 
78 // Stop sound associated with a given id
79 void stop_sound(unsigned id);
80 
81 // Play sound, or random one of comma-separated sounds.
82 void play_sound(const std::string& files, sound_tracks::type group = sound_tracks::type::sound_fx, unsigned int repeats = 0);
83 
84 // Play sound, or random one of comma-separated sounds. Use specified
85 // distance and associate it with specified id (of a sound source).
86 void play_sound_positioned(const std::string &files, int repeats, unsigned int distance, unsigned int id);
87 
88 // Play sound, or random one of comma-separated sounds in bell channel
89 void play_bell(const std::string& files);
90 
91 // Play sound, or random one of comma-separated sounds in timer channel
92 void play_timer(const std::string& files, const std::chrono::milliseconds& loop_ticks, const std::chrono::milliseconds& fadein_ticks);
93 
94 // Play user-interface sound, or random one of comma-separated sounds.
95 void play_UI_sound(const std::string& files);
96 
97 // A class to periodically check for new music that needs to be played
99  void process();
100 };
101 
102 // A class to mute music when the game is in background
104 public:
105  music_muter();
106  void handle_event(const SDL_Event&) override {}
107  void handle_window_event(const SDL_Event& event) override;
108 };
109 
110 // Save music playlist for snapshot
111 void write_music_play_list(config& snapshot);
112 
113 class volume
114 {
115 public:
116  constexpr explicit volume(float factor)
117  : gain(factor)
118  {
119  }
120 
121  constexpr static volume from_percent(float percentage)
122  { return volume{percentage / 100.f}; }
123 
124  constexpr float as_percent() const
125  { return gain * 100.f; }
126 
127  /** Implicit conversion for use with SDL. */
128  constexpr operator float() const
129  { return gain; }
130 
131  constexpr auto operator*(const volume& other) const
132  { return volume{gain * other.gain}; }
133 
134  constexpr auto operator/(const volume& other) const
135  { return volume{gain / other.gain}; }
136 
137  constexpr auto operator+(const volume& other) const
138  { return volume{gain + other.gain}; }
139 
140  constexpr auto operator-(const volume& other) const
141  { return volume{gain - other.gain}; }
142 
143 private:
144  float gain{1.0};
145 };
146 
147 constexpr inline volume silence{0.f};
148 constexpr inline volume full_volume{1.f};
149 constexpr inline volume max_volume{1.28f};
150 
151 volume get_music_volume();
152 volume get_sound_volume();
153 
154 void set_music_volume(volume vol);
155 void set_sound_volume(volume vol);
156 void set_bell_volume(volume vol);
157 void set_UI_volume(volume vol);
158 
159 utils::optional<unsigned int> get_current_track_index();
160 std::shared_ptr<sound::music_track> get_current_track();
161 std::shared_ptr<sound::music_track> get_previous_music_track();
162 unsigned int get_num_tracks();
163 void remove_track(unsigned int i);
164 void play_track(unsigned int i);
165 
166 void flush_cache();
167 
168 }
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:106
void handle_window_event(const SDL_Event &event) override
Definition: sound.cpp:767
constexpr float as_percent() const
Definition: sound.hpp:124
constexpr volume(float factor)
Definition: sound.hpp:116
float gain
Definition: sound.hpp:144
constexpr auto operator*(const volume &other) const
Definition: sound.hpp:131
constexpr static volume from_percent(float percentage)
Definition: sound.hpp:121
constexpr auto operator/(const volume &other) const
Definition: sound.hpp:134
constexpr auto operator+(const volume &other) const
Definition: sound.hpp:137
constexpr auto operator-(const volume &other) const
Definition: sound.hpp:140
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:810
void empty_playlist()
Definition: sound.cpp:584
void reposition_sound(unsigned id, unsigned int distance)
Definition: sound.cpp:820
void set_UI_volume(volume vol)
Definition: sound.cpp:1053
constexpr volume full_volume
Definition: sound.hpp:148
void set_music_volume(volume vol)
Definition: sound.cpp:1013
bool init_sound()
Definition: sound.cpp:422
void close_sound()
Definition: sound.cpp:485
constexpr volume silence
Definition: sound.hpp:147
volume get_music_volume()
Definition: sound.cpp:1004
void play_music_config(const config &music_node, bool allow_interrupt_current_track, int i)
Definition: sound.cpp:664
void remove_track(unsigned int i)
Definition: sound.cpp:244
void play_music()
Definition: sound.cpp:589
unsigned int get_num_tracks()
Definition: sound.cpp:219
constexpr int distance_none
Definition: sound.hpp:73
void stop_music()
Definition: sound.cpp:511
void restart_UI_sound()
Definition: sound.cpp:567
void set_sound_volume(volume vol)
Definition: sound.cpp:1030
void play_music_once(const std::string &file)
Definition: sound.cpp:574
constexpr int distance_silent
Definition: sound.hpp:72
void restart_bell()
Definition: sound.cpp:559
utils::optional< unsigned int > get_current_track_index()
Definition: sound.cpp:199
void stop_UI_sound()
Definition: sound.cpp:537
std::vector< std::string > enumerate_drivers()
Definition: sound.cpp:392
void play_sound(const std::string &files, sound_tracks::type group, unsigned int repeats)
Definition: sound.cpp:966
void flush_cache()
Definition: sound.cpp:1060
void set_bell_volume(volume vol)
Definition: sound.cpp:1043
bool is_sound_playing(int id)
Definition: sound.cpp:835
void restart_sound()
Definition: sound.cpp:551
void stop_bell()
Definition: sound.cpp:529
volume get_sound_volume()
Definition: sound.cpp:1020
void play_timer(const std::string &files, const std::chrono::milliseconds &loop_ticks, const std::chrono::milliseconds &fadein_ticks)
Definition: sound.cpp:989
void play_sound_positioned(const std::string &files, int repeats, unsigned int distance, unsigned int id)
Definition: sound.cpp:973
void commit_music_changes()
Definition: sound.cpp:782
void play_track(unsigned int i)
Definition: sound.cpp:601
constexpr volume max_volume
Definition: sound.hpp:149
void play_UI_sound(const std::string &files)
Definition: sound.cpp:997
std::shared_ptr< music_track > get_previous_music_track()
Definition: sound.cpp:214
void restart_music()
Definition: sound.cpp:544
std::shared_ptr< music_track > get_current_track()
Definition: sound.cpp:206
void stop_sound()
Definition: sound.cpp:518
std::string current_driver()
Definition: sound.cpp:386
void play_bell(const std::string &files)
Definition: sound.cpp:981
static driver_status query()
Definition: sound.cpp:405
SDL_AudioFormat format
Definition: sound.hpp:38