The Battle for Wesnoth  1.19.5+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,
96  const std::chrono::milliseconds& loop_ticks,
97  const std::chrono::milliseconds& fadein_ticks);
98 
99 // Play user-interface sound, or random one of comma-separated sounds.
100 void play_UI_sound(const std::string& files);
101 
102 // A class to periodically check for new music that needs to be played
104  void process();
105 };
106 
107 // A class to mute music when the game is in background
109 public:
110  music_muter();
111  void handle_event(const SDL_Event&) override {}
112  void handle_window_event(const SDL_Event& event) override;
113 };
114 
115 // Save music playlist for snapshot
116 void write_music_play_list(config& snapshot);
117 
118 int get_music_volume();
119 int get_sound_volume();
120 void set_music_volume(int vol);
121 void set_sound_volume(int vol);
122 void set_bell_volume(int vol);
123 void set_UI_volume(int vol);
124 
125 utils::optional<unsigned int> get_current_track_index();
126 std::shared_ptr<sound::music_track> get_current_track();
127 std::shared_ptr<sound::music_track> get_previous_music_track();
128 void set_previous_track(std::shared_ptr<music_track>);
129 unsigned int get_num_tracks();
130 void remove_track(unsigned int i);
131 void play_track(unsigned int i);
132 
133 void flush_cache();
134 
135 }
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:111
void handle_window_event(const SDL_Event &event) override
Definition: sound.cpp:829
std::size_t i
Definition: function.cpp:1028
Audio output for sound and music.
Definition: sound.cpp:41
void write_music_play_list(config &snapshot)
Definition: sound.cpp:871
void empty_playlist()
Definition: sound.cpp:612
int get_music_volume()
Definition: sound.cpp:1084
void set_bell_volume(int vol)
Definition: sound.cpp:1133
void play_sound(const std::string &files, channel_group group, unsigned int repeats)
Definition: sound.cpp:1046
void reset_sound()
Definition: sound.cpp:526
bool init_sound()
Definition: sound.cpp:443
void close_sound()
Definition: sound.cpp:495
void play_music_config(const config &music_node, bool allow_interrupt_current_track, int i)
Definition: sound.cpp:715
void remove_track(unsigned int i)
Definition: sound.cpp:246
void play_music_repeatedly(const std::string &id)
Definition: sound.cpp:693
void play_music()
Definition: sound.cpp:617
unsigned int get_num_tracks()
Definition: sound.cpp:221
void stop_music()
Definition: sound.cpp:557
void play_music_once(const std::string &file)
Definition: sound.cpp:603
void play_sound_positioned(const std::string &files, int id, int repeats, unsigned int distance)
Definition: sound.cpp:1053
utils::optional< unsigned int > get_current_track_index()
Definition: sound.cpp:201
void stop_UI_sound()
Definition: sound.cpp:592
std::vector< std::string > enumerate_drivers()
Definition: sound.cpp:418
void flush_cache()
Definition: sound.cpp:195
bool is_sound_playing(int id)
Definition: sound.cpp:897
void stop_bell()
Definition: sound.cpp:580
void reposition_sound(int id, unsigned int distance)
Definition: sound.cpp:881
void play_timer(const std::string &files, const std::chrono::milliseconds &loop_ticks, const std::chrono::milliseconds &fadein_ticks)
Definition: sound.cpp:1069
int get_sound_volume()
Definition: sound.cpp:1104
void commit_music_changes()
Definition: sound.cpp:842
void play_track(unsigned int i)
Definition: sound.cpp:629
void play_UI_sound(const std::string &files)
Definition: sound.cpp:1077
void set_previous_track(std::shared_ptr< music_track > track)
Definition: sound.cpp:216
std::shared_ptr< music_track > get_previous_music_track()
Definition: sound.cpp:212
void set_music_volume(int vol)
Definition: sound.cpp:1093
std::shared_ptr< music_track > get_current_track()
Definition: sound.cpp:208
void stop_sound()
Definition: sound.cpp:565
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:412
void set_UI_volume(int vol)
Definition: sound.cpp:1145
void set_sound_volume(int vol)
Definition: sound.cpp:1113
void play_bell(const std::string &files)
Definition: sound.cpp:1061
static driver_status query()
Definition: sound.cpp:431
uint16_t format
Definition: sound.hpp:44