The Battle for Wesnoth  1.19.14+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 "sound_music_track.hpp"
20 #include "utils/optional_fwd.hpp"
21 
22 #include <string>
23 
24 class config;
25 
26 namespace sound {
27 
34  SOUND_FX
35 };
36 
37 std::string current_driver();
38 std::vector<std::string> enumerate_drivers();
39 
41 {
43  int frequency;
44  uint16_t format;
45  int channels;
47 
48  static driver_status query();
49 };
50 
51 bool init_sound();
52 void close_sound();
53 void reset_sound();
54 
55 void stop_music();
56 void stop_sound();
57 void stop_UI_sound();
58 void stop_bell();
59 
60 // Read config entry, alter track list accordingly.
61 void play_music_config(const config &music_node, bool allow_interrupt_current_track = false, int i = -1);
62 // Act on any track list changes from above.
64 
65 // Play this particular music file once, then silence.
66 void play_music_once(const std::string& id);
67 // Empty the playlist
68 void empty_playlist();
69 // Start playing current music.
70 void play_music();
71 
72 // Change parameters of a playing sound, given its id
73 void reposition_sound(int id, unsigned int distance);
74 #define DISTANCE_SILENT 255
75 
76 // Check if there's a sound associated with given id playing
77 bool is_sound_playing(int id);
78 
79 // Stop sound associated with a given id
80 void stop_sound(int id);
81 
82 // Play sound, or random one of comma-separated sounds.
83 void play_sound(const std::string& files, channel_group group = SOUND_FX, unsigned int repeats = 0);
84 
85 // Play sound, or random one of comma-separated sounds. Use specified
86 // distance and associate it with specified id (of a sound source).
87 void play_sound_positioned(const std::string &files, int id, int repeats, unsigned int distance);
88 
89 // Play sound, or random one of comma-separated sounds in bell channel
90 void play_bell(const std::string& files);
91 
92 // Play sound, or random one of comma-separated sounds in timer channel
93 void play_timer(const std::string& files,
94  const std::chrono::milliseconds& loop_ticks,
95  const std::chrono::milliseconds& fadein_ticks);
96 
97 // Play user-interface sound, or random one of comma-separated sounds.
98 void play_UI_sound(const std::string& files);
99 
100 // A class to periodically check for new music that needs to be played
102  void process();
103 };
104 
105 // A class to mute music when the game is in background
107 public:
108  music_muter();
109  void handle_event(const SDL_Event&) override {}
110  void handle_window_event(const SDL_Event& event) override;
111 };
112 
113 // Save music playlist for snapshot
114 void write_music_play_list(config& snapshot);
115 
116 int get_music_volume();
117 int get_sound_volume();
118 void set_music_volume(int vol);
119 void set_sound_volume(int vol);
120 void set_bell_volume(int vol);
121 void set_UI_volume(int vol);
122 
123 utils::optional<unsigned int> get_current_track_index();
124 std::shared_ptr<sound::music_track> get_current_track();
125 std::shared_ptr<sound::music_track> get_previous_music_track();
126 void set_previous_track(std::shared_ptr<music_track>);
127 unsigned int get_num_tracks();
128 void remove_track(unsigned int i);
129 void play_track(unsigned int i);
130 
131 void flush_cache();
132 
133 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
void handle_event(const SDL_Event &) override
Definition: sound.hpp:109
void handle_window_event(const SDL_Event &event) override
Definition: sound.cpp:804
std::size_t i
Definition: function.cpp:1032
Audio output for sound and music.
Definition: sound.cpp:39
void write_music_play_list(config &snapshot)
Definition: sound.cpp:846
void empty_playlist()
Definition: sound.cpp:611
int get_music_volume()
Definition: sound.cpp:1058
void set_bell_volume(int vol)
Definition: sound.cpp:1107
void play_sound(const std::string &files, channel_group group, unsigned int repeats)
Definition: sound.cpp:1020
void reset_sound()
Definition: sound.cpp:523
bool init_sound()
Definition: sound.cpp:440
void close_sound()
Definition: sound.cpp:492
void play_music_config(const config &music_node, bool allow_interrupt_current_track, int i)
Definition: sound.cpp:689
void remove_track(unsigned int i)
Definition: sound.cpp:243
void play_music()
Definition: sound.cpp:616
unsigned int get_num_tracks()
Definition: sound.cpp:218
void stop_music()
Definition: sound.cpp:554
void play_music_once(const std::string &file)
Definition: sound.cpp:600
void play_sound_positioned(const std::string &files, int id, int repeats, unsigned int distance)
Definition: sound.cpp:1027
utils::optional< unsigned int > get_current_track_index()
Definition: sound.cpp:198
void stop_UI_sound()
Definition: sound.cpp:589
std::vector< std::string > enumerate_drivers()
Definition: sound.cpp:415
void flush_cache()
Definition: sound.cpp:192
bool is_sound_playing(int id)
Definition: sound.cpp:872
void stop_bell()
Definition: sound.cpp:577
void reposition_sound(int id, unsigned int distance)
Definition: sound.cpp:856
void play_timer(const std::string &files, const std::chrono::milliseconds &loop_ticks, const std::chrono::milliseconds &fadein_ticks)
Definition: sound.cpp:1043
int get_sound_volume()
Definition: sound.cpp:1078
void commit_music_changes()
Definition: sound.cpp:817
void play_track(unsigned int i)
Definition: sound.cpp:628
void play_UI_sound(const std::string &files)
Definition: sound.cpp:1051
void set_previous_track(std::shared_ptr< music_track > track)
Definition: sound.cpp:213
std::shared_ptr< music_track > get_previous_music_track()
Definition: sound.cpp:209
void set_music_volume(int vol)
Definition: sound.cpp:1067
std::shared_ptr< music_track > get_current_track()
Definition: sound.cpp:205
void stop_sound()
Definition: sound.cpp:562
channel_group
Definition: sound.hpp:28
@ NULL_CHANNEL
Definition: sound.hpp:29
@ SOUND_UI
Definition: sound.hpp:33
@ SOUND_SOURCES
Definition: sound.hpp:30
@ SOUND_TIMER
Definition: sound.hpp:32
@ SOUND_FX
Definition: sound.hpp:34
@ SOUND_BELL
Definition: sound.hpp:31
std::string current_driver()
Definition: sound.cpp:409
void set_UI_volume(int vol)
Definition: sound.cpp:1119
void set_sound_volume(int vol)
Definition: sound.cpp:1087
void play_bell(const std::string &files)
Definition: sound.cpp:1035
static driver_status query()
Definition: sound.cpp:428
uint16_t format
Definition: sound.hpp:44