ToyMaker Game Engine 0.0.2
ToyMaker is a game engine developed and maintained by Zoheb Shujauddin.
Loading...
Searching...
No Matches
types.hpp
1#ifndef TOYMAKER_SOUNDTYPES_H
2#define TOYMAKER_SOUNDTYPES_H
3
4#include <SDL3_mixer/SDL_mixer.h>
5#include <glm/glm.hpp>
6
8
9namespace ToyMaker {
10
11 class SoundChannel;
12 class SoundMixer;
13
19 class Sound: public Resource<Sound> {
20 public:
25 inline static std::string getResourceTypeName() { return "Sound"; }
26
31 Sound(MIX_Audio* sound);
32
34 Sound(Sound&& other);
36 Sound(const Sound& other) = delete;
38 Sound& operator=(Sound&& other);
40 Sound& operator=(const Sound& other) = delete;
41
46 int32_t getDuration() const;
47
48 private:
49
54 std::unique_ptr<MIX_Audio, decltype(&MIX_DestroyAudio)> mAudio;
55
56 friend class SoundChannel;
57 };
58
73 public:
78 SoundChannel(const SoundMixer& mixer);
79
84 inline ~SoundChannel() {
85 SDL_DestroyProperties(mProperties);
86 }
87
91 SoundChannel(const SoundChannel& other) = delete;
95 SoundChannel& operator=(const SoundChannel& other) = delete;
96
101 void play();
102
107 void restart();
108
113 void pause();
114
119 void stop(uint32_t millis);
120
125 bool isPlaying() const;
126
131 bool isPaused() const;
132
137 void setPosition(const glm::vec3& position);
138
144 void unsetPosition();
145
150 void setSound(const Sound& sound);
151
156 void unsetSound();
157
163 bool isChannelPositional() const;
164
169 void setPlayStart(uint32_t millis);
170
175 void setPlayEnd(uint32_t millis);
176
181 void setLoopCount(int32_t count);
182
188 void setLoopStart(uint32_t millis);
189
195 void setFadeInDuration(uint32_t millis);
196
202 void setGain(float gain);
203
208 float getGain() const;
209
215 void addTag(const std::string& tag);
216
221 void removeTag(const std::string& tag);
222
227 void removeAllTags();
228
233 std::vector<std::string> getTags() const;
234
235 private:
240 std::unique_ptr<MIX_Track, decltype(&MIX_DestroyTrack)> mTrack;
241
246 SDL_PropertiesID mProperties;
247 };
248
257 public:
262 SoundMixer();
263
268 float getGainMaster() const;
269
274 void setGainMaster(float gain);
275
282 void setGainTagged(const std::string& tag, float gain);
283
288 void stopTagged(const std::string& tag, uint32_t millis);
289
294 void pauseTagged(const std::string& tag);
295
300 void resumeTagged(const std::string& tag);
301
302 private:
307 std::unique_ptr<MIX_Mixer, decltype(&MIX_DestroyMixer)> mMixer;
308
309 friend class SoundChannel;
310 };
311
318 class SoundFromFile: public ResourceConstructor<Sound, SoundFromFile> {
319 public:
324
331 inline static std::string getResourceConstructorName() { return "fromFile"; }
332 private:
333
342 std::shared_ptr<IResource> createResource(const nlohmann::json& methodParameters) override;
343 };
344}
345
346#endif
347
ResourceConstructor(int explicitlyInitializeMe)
Definition resource_database.hpp:488
Resource(int explicitlyInitializeMe)
Definition resource_database.hpp:386
Class representing a single voice to be mixed with other voices that may play simultaneiously to prod...
Definition types.hpp:72
SoundChannel(const SoundChannel &other)=delete
Copy constructor.
void unsetSound()
Unsets whichever sound this channel was reading from earlier.
Definition types.cpp:195
void removeAllTags()
Removes all tags associated with this channel.
Definition types.cpp:244
SoundChannel(const SoundMixer &mixer)
Constructs a channel owned and used by mixer.
Definition types.cpp:78
SoundChannel & operator=(const SoundChannel &other)=delete
Copy assignment.
void pause()
Pauses sound attached to this channel.
Definition types.cpp:128
std::vector< std::string > getTags() const
Returns all the tags associated with this channel.
Definition types.cpp:248
void setPlayEnd(uint32_t millis)
When this channel plays, what frame on the track is considered the end of the sound clip.
Definition types.cpp:179
void setLoopStart(uint32_t millis)
The timestamp in milliseconds at which the sound is restarted after the end of the first iteration,...
Definition types.cpp:211
~SoundChannel()
Destroys sound channel and any related properties it owns.
Definition types.hpp:84
void setSound(const Sound &sound)
Sets which sound this channel may play.
Definition types.cpp:186
bool isPlaying() const
Whether or not this channel is currently playing any sound.
Definition types.cpp:150
void setPlayStart(uint32_t millis)
When this channel plays, at which point in the input sound clip to start from.
Definition types.cpp:174
void setPosition(const glm::vec3 &position)
Sets the 3D position associated with this channel.
Definition types.cpp:158
void setGain(float gain)
Sets the factor by which the sound from this channel will be multiplied.
Definition types.cpp:225
void play()
Starts playing sound attached to this channel.
Definition types.cpp:110
SDL_PropertiesID mProperties
The SDL properties controlling the playback for this track.
Definition types.hpp:246
void removeTag(const std::string &tag)
Removes a string tag from this channel.
Definition types.cpp:240
float getGain() const
Gets the factor by which the sound from this channel is multiplied.
Definition types.cpp:231
bool isChannelPositional() const
Whether this channel will have direction related effects applied when mixed into the final sound outp...
Definition types.cpp:167
void unsetPosition()
Unsets the 3D position associated with this channel, making audio independent of sound source.
Definition types.cpp:163
void addTag(const std::string &tag)
Adds a string tag to this channel, allowing channels sharing the same tag to be controlled together.
Definition types.cpp:235
void restart()
Start playback with newly configured properties right away.
Definition types.cpp:123
void setFadeInDuration(uint32_t millis)
At the first playback iteration, the period over which the sound goes from quiet to its full volume.
Definition types.cpp:218
void stop(uint32_t millis)
Stops playing sound attached to this channel, fading over the duration specified.
Definition types.cpp:133
SoundChannel & operator=(SoundChannel &&other)
Move assignment.
Definition types.cpp:97
std::unique_ptr< MIX_Track, decltype(&MIX_DestroyTrack)> mTrack
The underlying SDL3_mixer track object object.
Definition types.hpp:240
bool isPaused() const
Whether or not this channel is paused.
Definition types.cpp:154
void setLoopCount(int32_t count)
The number of times the sound should loop before ending, with -1 indicating infinity.
Definition types.cpp:204
std::shared_ptr< IResource > createResource(const nlohmann::json &methodParameters) override
Creates a Sound resource based on its parameters.
Definition types.cpp:266
static std::string getResourceConstructorName()
Gets the resource constructor type string for this constructor.
Definition types.hpp:331
SoundFromFile()
Creates a SoundFromFile object.
Definition types.cpp:36
Class managing a group of sound channels, as many as permitted by the underlying hardware,...
Definition types.hpp:256
void pauseTagged(const std::string &tag)
Pauses playback of all channels marked with a specific tag.
Definition types.cpp:68
SoundMixer()
Creates a mixer for the default audio playback device.
Definition types.cpp:38
void setGainTagged(const std::string &tag, float gain)
Sets the gain applied to all channels marked with a particular tag.
Definition types.cpp:57
void resumeTagged(const std::string &tag)
Resumes playback of all channels marked with a specific tag.
Definition types.cpp:73
float getGainMaster() const
Returns the gain applied to the mixed sound output applied by this mixer.
Definition types.cpp:47
void setGainMaster(float gain)
Sets the factor by which the sound returned by this mixer is multiplied.
Definition types.cpp:51
void stopTagged(const std::string &tag, uint32_t millis)
Stops the playback of all channels marked with a specific tag.
Definition types.cpp:63
std::unique_ptr< MIX_Mixer, decltype(&MIX_DestroyMixer)> mMixer
The SDL3_mixer object this class is a wrapper over.
Definition types.hpp:307
Class representing a single playable piece of audio.
Definition types.hpp:19
int32_t getDuration() const
Returns the duration of this sound in millis, with -1 indicating the audio is infinite.
Definition types.cpp:31
Sound & operator=(Sound &&other)
Move assignment.
Definition types.cpp:21
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> mAudio
The underlying SDL3_mixer audio object.
Definition types.hpp:54
Sound(const Sound &other)=delete
Copy constructor.
Sound(MIX_Audio *sound)
Initializer for this sound.
Definition types.cpp:11
Sound & operator=(const Sound &other)=delete
Copy assignment.
static std::string getResourceTypeName()
The id string representing this kind of resource.
Definition types.hpp:25
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition application.hpp:26
Headers relating to resources and their management for a given project.