CoriEngine
Loading...
Searching...
No Matches
Mixer.hpp
Go to the documentation of this file.
1#pragma once
2#include "IDDefs.hpp"
3
4namespace Cori {
5 namespace Core {
6 class Application;
7 }
8
12 namespace Audio {
13 class Track;
14 class Sound;
15
20 struct PlayParams {
24 int32_t Loops{ 0 };
25
29 float MaxMilliseconds{ -1.0f };
30
34 float StartMillisecond{ 0.0f };
35
39 float LoopStartMillisecond{ 0.0f };
40
44 float FadeInMilliseconds{ 0.0f };
45
50
54 bool LoopedInSequence{ true };
55
56 friend std::ostream& operator<<(std::ostream& os, const PlayParams& params) {
57 os << "\n" <<
58 "| Loops : " << params.Loops << " |" <<
59 "| MaxMilliseconds : " << params.MaxMilliseconds << " |" <<
60 "| StartMillisecond : " << params.StartMillisecond << " |" <<
61 "| LoopStartMillisecond : " << params.LoopStartMillisecond << " |" <<
62 "| FadeInMilliseconds : " << params.FadeInMilliseconds << " |" <<
63 "| AppendSilenceMilliseconds: " << params.AppendSilenceMilliseconds << " |";
64 return os;
65 }
66
67 [[nodiscard]] std::string ToString() const {
68 std::string out = "\n" +
69 std::string("| Loops : ") + std::to_string(Loops) + " |" +
70 std::string("| MaxMilliseconds : ") + std::to_string(MaxMilliseconds) + " |" +
71 std::string("| StartMillisecond : ") + std::to_string(StartMillisecond) + " |" +
72 std::string("| LoopStartMillisecond : ") + std::to_string(LoopStartMillisecond) + " |" +
73 std::string("| FadeInMilliseconds : ") + std::to_string(FadeInMilliseconds) + " |" +
74 std::string("| AppendSilenceMilliseconds: ") + std::to_string(AppendSilenceMilliseconds) + " |";
75 return out;
76 }
77 };
78
82 class Mixer {
83 public:
84
90 static std::expected<void, Core::CoriError<>> PauseAllTracks();
91
96 static std::expected<void, Core::CoriError<>> ResumeAllTracks();
97
98
106 static std::expected<void, Core::CoriError<>> SetMasterGain(const float gain);
107
108
113 static float GetMasterGain();
114
123 static std::expected<void, Core::CoriError<>> StartTag(const char* tag, const PlayParams& params = PlayParams{});
124
125
134 static std::expected<void, Core::CoriError<>> StopTag(const char* tag, const int64_t fadeOutMS = 0);
135
136
143 static std::expected<void, Core::CoriError<>> PauseTag(const char* tag);
144
150 static std::expected<void, Core::CoriError<>> ResumeTag(const char* tag);
151
152
161 static std::expected<void, Core::CoriError<>> SetTagGain(const char* tag, const float gain);
162
163 private:
164 friend Track;
165 friend Sound;
166 friend Core::Application;
167
168 static void Init();
169 static void Shutdown();
170
171 static std::expected<void, Core::CoriError<std::filesystem::path>> LoadSound(const std::filesystem::path& path, const bool preDecode, const SoundID soundID);
172
173 static void UnloadSound(const SoundID soundID);
174
175 static std::expected<void, Core::CoriError<>> CreateTrack(Track* track);
176
177 static void DestroyTrack(const TrackID trackID);
178
179 static std::expected<void, Core::CoriError<>> SetTrackSound(const TrackID trackID, const Sound* sound);
180
181 static std::expected<void, Core::CoriError<>> PlayTrack(const TrackID trackID, const PlayParams& params = {});
182
183 static std::expected<void, Core::CoriError<>> StopTrack(const TrackID trackID, const int64_t fadeOutMS = 0);
184
185 static std::expected<void, Core::CoriError<>> PauseTrack(const TrackID trackID);
186
187 static std::expected<void, Core::CoriError<>> ResumeTrack(const TrackID trackID);
188
189 static bool IsTrackPaused(const TrackID trackID);
190
191 static bool IsTrackPlaying(const TrackID trackID);
192
193 static std::expected<void, Core::CoriError<>> SetTrackGain(const TrackID trackID, const float gain);
194
195 static float GetTrackGain(const TrackID trackID);
196
197 static std::expected<void, Core::CoriError<>> TagTrack(const TrackID trackID, const char* tag);
198
199 static void UntagTrack(const TrackID trackID, const char* tag);
200
201 static std::expected<int64_t, Core::CoriError<>> MillisecondsToFrames(const TrackID trackID, const float milliseconds);
202
203 struct Data;
204 static Data* s_Data;
205
206 };
207 }
208}
209
210
211
Mixer is responsible for mixing all the sounds, it is a global object and there can only be one Mixer...
Definition Mixer.hpp:82
static std::expected< void, Core::CoriError<> > SetTagGain(const char *tag, const float gain)
Sets the gain of all tracks with a specific tag.
Definition Mixer.cpp:107
static std::expected< void, Core::CoriError<> > SetMasterGain(const float gain)
Sets the master gain of a Mixer.
Definition Mixer.cpp:36
static std::expected< void, Core::CoriError<> > StartTag(const char *tag, const PlayParams &params=PlayParams{})
Start (ot restart) mixing all Tracks with a specific tag.
Definition Mixer.cpp:49
static float GetMasterGain()
Returns the current master gain specified for the Mixer. The default gain for a Mixer is 1....
Definition Mixer.cpp:45
static std::expected< void, Core::CoriError<> > ResumeTag(const char *tag)
Resumes all Tracks with a specific tag.
Definition Mixer.cpp:98
static std::expected< void, Core::CoriError<> > PauseAllTracks()
Pauses all Track that are currently playing on the Mixer.
Definition Mixer.cpp:18
static std::expected< void, Core::CoriError<> > PauseTag(const char *tag)
Pauses all tracks with a specific tag.
Definition Mixer.cpp:89
static std::expected< void, Core::CoriError<> > ResumeAllTracks()
Resumes all Track that are currently paused on the Mixer.
Definition Mixer.cpp:27
static std::expected< void, Core::CoriError<> > StopTag(const char *tag, const int64_t fadeOutMS=0)
Stops all Track with a specific tab, possibly fading out over time.
Definition Mixer.cpp:80
Sound asset to be played on a Track.
Definition Sound.hpp:12
You use Track to play and mix Sound objects.
Definition Track.hpp:19
Main Application object, there can only be one Application object. Basically a root of the program.
Everything connected to audio is in this namespace.
Definition IDDefs.hpp:4
uint16_t TrackID
Definition IDDefs.hpp:5
uint32_t SoundID
Definition IDDefs.hpp:6
Core systems of the engine are here.
Global engine namespace.
Parameters to be used when playing sound, you can mix your audio playback however you want with these...
Definition Mixer.hpp:20
float MaxMilliseconds
Mix at most n milliseconds. Default -1.0f, mixes all available audio.
Definition Mixer.hpp:29
friend std::ostream & operator<<(std::ostream &os, const PlayParams &params)
Definition Mixer.hpp:56
std::string ToString() const
Definition Mixer.hpp:67
bool LoopedInSequence
This value is used to define a sequence looping point when passing a sequence to Track::Play,...
Definition Mixer.hpp:54
int32_t Loops
Number of times to loop the track when it reaches the end. A value of -1 will result in an infinite l...
Definition Mixer.hpp:24
float LoopStartMillisecond
Start looping at n'th millisecond. Values <=0 will result in looping from the very beginning of the t...
Definition Mixer.hpp:39
float FadeInMilliseconds
Fade in the audio over n milliseconds. Will start from silence and reach full volume smoothly....
Definition Mixer.hpp:44
float StartMillisecond
Start mixing at n'th millisecond. Values <=0 will result in mixing from the very beginning of the tra...
Definition Mixer.hpp:34
float AppendSilenceMilliseconds
Append n milliseconds to the end of a track after all loops (specified in PlayParams::Loops) are comp...
Definition Mixer.hpp:49