The Battle for Wesnoth  1.19.4+dev
sound.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 over and over and over.
66 void play_music_repeatedly(const std::string& id);
67 // Play this particular music file once, then silence.
68 void play_music_once(const std::string& id);
69 // Empty the playlist
70 void empty_playlist();
71 // Start playing current music.
72 void play_music();
73 
74 // Change parameters of a playing sound, given its id
75 void reposition_sound(int id, unsigned int distance);
76 #define DISTANCE_SILENT 255
77 
78 // Check if there's a sound associated with given id playing
79 bool is_sound_playing(int id);
80 
81 // Stop sound associated with a given id
82 void stop_sound(int id);
83 
84 // Play sound, or random one of comma-separated sounds.
85 void play_sound(const std::string& files, channel_group group = SOUND_FX, unsigned int repeats = 0);
86 
87 // Play sound, or random one of comma-separated sounds. Use specified
88 // distance and associate it with specified id (of a sound source).
89 void play_sound_positioned(const std::string &files, int id, int repeats, unsigned int distance);
90 
91 // Play sound, or random one of comma-separated sounds in bell channel
92 void play_bell(const std::string& files);
93 
94 // Play sound, or random one of comma-separated sounds in timer channel
95 void play_timer(const std::string& files, int loop_ticks, int 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
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:172
void handle_event(const SDL_Event &) override
Definition: sound.hpp:109
void handle_window_event(const SDL_Event &event) override
Definition: sound.cpp:824
void process(events::pump_info &info)
Definition: sound.cpp:776
std::size_t i
Definition: function.cpp:1023
logger & info()
Definition: log.cpp:316
Audio output for sound and music.
Definition: sound.cpp:40
void write_music_play_list(config &snapshot)
Definition: sound.cpp:866
void empty_playlist()
Definition: sound.cpp:610
int get_music_volume()
Definition: sound.cpp:1073
void set_bell_volume(int vol)
Definition: sound.cpp:1122
void play_sound(const std::string &files, channel_group group, unsigned int repeats)
Definition: sound.cpp:1035
void reset_sound()
Definition: sound.cpp:524
bool init_sound()
Definition: sound.cpp:441
void close_sound()
Definition: sound.cpp:493
void play_music_config(const config &music_node, bool allow_interrupt_current_track, int i)
Definition: sound.cpp:713
void remove_track(unsigned int i)
Definition: sound.cpp:244
void play_music_repeatedly(const std::string &id)
Definition: sound.cpp:691
void play_music()
Definition: sound.cpp:615
unsigned int get_num_tracks()
Definition: sound.cpp:219
void stop_music()
Definition: sound.cpp:555
void play_music_once(const std::string &file)
Definition: sound.cpp:601
void play_sound_positioned(const std::string &files, int id, int repeats, unsigned int distance)
Definition: sound.cpp:1042
utils::optional< unsigned int > get_current_track_index()
Definition: sound.cpp:199
void stop_UI_sound()
Definition: sound.cpp:590
std::vector< std::string > enumerate_drivers()
Definition: sound.cpp:416
void flush_cache()
Definition: sound.cpp:193
bool is_sound_playing(int id)
Definition: sound.cpp:892
void play_timer(const std::string &files, int loop_ticks, int fadein_ticks)
Definition: sound.cpp:1058
void stop_bell()
Definition: sound.cpp:578
void reposition_sound(int id, unsigned int distance)
Definition: sound.cpp:876
int get_sound_volume()
Definition: sound.cpp:1093
void commit_music_changes()
Definition: sound.cpp:837
void play_track(unsigned int i)
Definition: sound.cpp:627
void play_UI_sound(const std::string &files)
Definition: sound.cpp:1066
void set_previous_track(std::shared_ptr< music_track > track)
Definition: sound.cpp:214
std::shared_ptr< music_track > get_previous_music_track()
Definition: sound.cpp:210
void set_music_volume(int vol)
Definition: sound.cpp:1082
std::shared_ptr< music_track > get_current_track()
Definition: sound.cpp:206
void stop_sound()
Definition: sound.cpp:563
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:410
void set_UI_volume(int vol)
Definition: sound.cpp:1134
void set_sound_volume(int vol)
Definition: sound.cpp:1102
void play_bell(const std::string &files)
Definition: sound.cpp:1050
static driver_status query()
Definition: sound.cpp:429
uint16_t format
Definition: sound.hpp:44