The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
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"
23
24#include <map>
25#include <string>
26
27class config;
28
29namespace sound {
30
31std::string current_driver();
32std::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
44bool init_sound();
45void close_sound();
46
47void stop_music();
48void stop_sound();
49void stop_UI_sound();
50void stop_bell();
51
52void restart_music();
53void restart_sound();
54void restart_bell();
55void restart_UI_sound();
56
57// Read config entry, alter track list accordingly.
58void 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.
63void play_music_once(const std::string& id);
64// Empty the playlist
65void empty_playlist();
66// Start playing current music.
67void play_music();
68
69// Change parameters of a playing sound, given its id
70void reposition_sound(unsigned id, unsigned int distance);
71
72constexpr inline int distance_silent = 255;
73constexpr inline int distance_none = 0;
74
75// Check if there's a sound associated with given id playing
76bool is_sound_playing(int id);
77
78// Stop sound associated with a given id
79void stop_sound(unsigned id);
80
81// Play sound, or random one of comma-separated sounds.
82void 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).
86void 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
89void play_bell(const std::string& files);
90
91// Play sound, or random one of comma-separated sounds in timer channel
92void 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.
95void 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
104public:
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
111void write_music_play_list(config& snapshot);
112
114{
115public:
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
143private:
144 float gain{1.0};
145};
146
147constexpr inline volume silence{0.f};
148constexpr inline volume full_volume{1.f};
149constexpr inline volume max_volume{1.28f};
150
151volume get_music_volume();
152volume get_sound_volume();
153
154void set_music_volume(volume vol);
155void set_sound_volume(volume vol);
156void set_bell_volume(volume vol);
157void set_UI_volume(volume vol);
158
159utils::optional<std::size_t> get_current_track_index();
160std::shared_ptr<sound::music_track> get_current_track();
161std::shared_ptr<sound::music_track> get_previous_music_track();
162unsigned int get_num_tracks();
163void remove_track(unsigned int i);
164void play_track(unsigned int i);
165
166void 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:777
constexpr float as_percent() const
Definition sound.hpp:124
static constexpr volume from_percent(float percentage)
Definition sound.hpp:121
constexpr volume(float factor)
Definition sound.hpp:116
constexpr auto operator*(const volume &other) const
Definition sound.hpp:131
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
Audio output for sound and music.
void write_music_play_list(config &snapshot)
Definition sound.cpp:820
void empty_playlist()
Definition sound.cpp:605
void reposition_sound(unsigned id, unsigned int distance)
Definition sound.cpp:830
void set_UI_volume(volume vol)
Definition sound.cpp:1062
constexpr volume full_volume
Definition sound.hpp:148
void set_music_volume(volume vol)
Definition sound.cpp:1022
bool init_sound()
Definition sound.cpp:444
void close_sound()
Definition sound.cpp:507
constexpr volume silence
Definition sound.hpp:147
volume get_music_volume()
Definition sound.cpp:1013
void play_music_config(const config &music_node, bool allow_interrupt_current_track, int i)
Definition sound.cpp:684
void remove_track(unsigned int i)
Definition sound.cpp:251
utils::optional< std::size_t > get_current_track_index()
Definition sound.cpp:199
void play_music()
Definition sound.cpp:610
unsigned int get_num_tracks()
Definition sound.cpp:226
constexpr int distance_none
Definition sound.hpp:73
void stop_music()
Definition sound.cpp:533
void restart_UI_sound()
Definition sound.cpp:589
void set_sound_volume(volume vol)
Definition sound.cpp:1039
void play_music_once(const std::string &file)
Definition sound.cpp:596
constexpr int distance_silent
Definition sound.hpp:72
void restart_bell()
Definition sound.cpp:581
void stop_UI_sound()
Definition sound.cpp:559
std::vector< std::string > enumerate_drivers()
Definition sound.cpp:414
void play_sound(const std::string &files, sound_tracks::type group, unsigned int repeats)
Definition sound.cpp:975
void flush_cache()
Definition sound.cpp:1069
void set_bell_volume(volume vol)
Definition sound.cpp:1052
bool is_sound_playing(int id)
Definition sound.cpp:845
void restart_sound()
Definition sound.cpp:573
void stop_bell()
Definition sound.cpp:551
volume get_sound_volume()
Definition sound.cpp:1029
void play_timer(const std::string &files, const std::chrono::milliseconds &loop_ticks, const std::chrono::milliseconds &fadein_ticks)
Definition sound.cpp:998
void play_sound_positioned(const std::string &files, int repeats, unsigned int distance, unsigned int id)
Definition sound.cpp:982
void commit_music_changes()
Definition sound.cpp:792
void play_track(unsigned int i)
Definition sound.cpp:622
constexpr volume max_volume
Definition sound.hpp:149
void play_UI_sound(const std::string &files)
Definition sound.cpp:1006
std::shared_ptr< music_track > get_previous_music_track()
Definition sound.cpp:221
void restart_music()
Definition sound.cpp:566
std::shared_ptr< music_track > get_current_track()
Definition sound.cpp:213
void stop_sound()
Definition sound.cpp:540
std::string current_driver()
Definition sound.cpp:408
void play_bell(const std::string &files)
Definition sound.cpp:990
static driver_status query()
Definition sound.cpp:427
SDL_AudioFormat format
Definition sound.hpp:38
#define i