P-E-R-R-Y
Utilitaires, contrats et vendors du moteur, en une seule doc
Loading...
Searching...
No Matches
SdlMusic.hpp
Go to the documentation of this file.
1
10
11#ifndef SDLMUSIC_HPP_
12#define SDLMUSIC_HPP_
13
14//Sdl
15#include <SDL3/SDL.h>
16#include <SDL3_mixer/SDL_mixer.h>
17
18//Interface
19#include "IMusic.hpp"
20
21#include <iostream>
22#include <string>
23
36class SdlMusic : public audio::IMusic {
37
38 public:
39 SdlMusic(MIX_Mixer *mixer, std::string path) {
40 _audio = MIX_LoadAudio(mixer, path.c_str(), false);
41 if (!_audio) {
42 std::cerr << "Failed to load music: " << SDL_GetError() << std::endl;
43 return;
44 }
45 _track = MIX_CreateTrack(mixer);
46 if (_track)
47 MIX_SetTrackAudio(_track, _audio);
48 }
49
50 ~SdlMusic() override {
51 if (_track)
52 MIX_DestroyTrack(_track);
53 if (_audio)
54 MIX_DestroyAudio(_audio);
55 }
56
57 bool isReady() const override { return _audio != nullptr && _track != nullptr; }
58
59 void play() override {
60 if (!isReady())
61 return;
62 if (MIX_TrackPaused(_track)) {
63 MIX_ResumeTrack(_track);
64 return;
65 }
66 MIX_SetTrackLoops(_track, _loop ? -1 : 0);
67 MIX_PlayTrack(_track, 0);
68 }
69
70 void pause() override {
71 if (_track)
72 MIX_PauseTrack(_track);
73 }
74
75 void stop() override {
76 if (_track)
77 MIX_StopTrack(_track, 0);
78 }
79
81 void update() override {}
82
83 void setVolume(float volume) override {
84 _volume = volume;
85 if (_track)
86 MIX_SetTrackGain(_track, volume);
87 }
88
89 float getVolume() const override { return _volume; }
90
91 /* La boucle se pose sur la piste, donc elle peut changer en cours de
92 * lecture - la ou SDL2 la figeait au lancement. */
93 void setLoop(bool loop) override {
94 _loop = loop;
95 if (_track)
96 MIX_SetTrackLoops(_track, loop ? -1 : 0);
97 }
98
99 bool getLoop() const override { return _loop; }
100
101 void setTime(float position) override {
102 if (_track)
103 MIX_SetTrackPlaybackPosition(_track, MIX_TrackMSToFrames(_track, static_cast<Sint64>(position * 1000.f)));
104 }
105
107 float getTime() const override {
108 if (!_track)
109 return 0.f;
110 return static_cast<float>(MIX_TrackFramesToMS(_track, MIX_GetTrackPlaybackPosition(_track))) / 1000.f;
111 }
112
113 float getLength() const override {
114 if (!_audio)
115 return 0.f;
116
117 const Sint64 frames = MIX_GetAudioDuration(_audio);
118
119 if (frames <= 0)
120 return 0.f;
121 return static_cast<float>(MIX_AudioFramesToMS(_audio, frames)) / 1000.f;
122 }
123
124 void setPosition(Vector3f position) override {
125 _position = position;
126 if (!_track)
127 return;
128
129 const MIX_Point3D point{static_cast<float>(position.x),
130 static_cast<float>(position.y),
131 static_cast<float>(position.z)};
132
133 MIX_SetTrack3DPosition(_track, &point);
134 }
135
136 Vector3f getPosition() const override { return _position; }
137
138 void setVelocity(Vector3f velocity) override { _velocity = velocity; }
139 Vector3f getVelocity() const override { return _velocity; }
140
141 private:
142 MIX_Audio *_audio = nullptr;
143 MIX_Track *_track = nullptr;
144
145 float _volume = 1.f;
146 bool _loop = false;
147 Vector3f _position{0, 0, 0};
148 Vector3f _velocity{0, 0, 0};
149};
150
152
153#endif /* !SDLMUSIC_HPP_ */
Un flux long, decode au fil de la lecture.
Definition SdlMusic.hpp:32
void update() override
SDL_mixer decode sur son propre fil : rien a pousser par frame.
Definition SdlMusic.hpp:81
float getVolume() const override
get the volume of the music
Definition SdlMusic.hpp:89
void stop() override
Stops playback AND rewinds to zero.
Definition SdlMusic.hpp:75
void setVelocity(Vector3f velocity) override
set the velocity of the music, used by backends that apply a doppler effect
Definition SdlMusic.hpp:138
void pause() override
Suspends playback without touching the position.
Definition SdlMusic.hpp:70
~SdlMusic() override
Definition SdlMusic.hpp:50
void play() override
Starts playback.
Definition SdlMusic.hpp:59
void setTime(float position) override
Set the time of the music in seconds.
Definition SdlMusic.hpp:101
void setPosition(Vector3f position) override
set the 3D position of the music, for backends that spatialize against a listener....
Definition SdlMusic.hpp:124
bool isReady() const override
Definition SdlMusic.hpp:57
Vector3f getPosition() const override
get the 3D position of the music
Definition SdlMusic.hpp:136
Vector3f getVelocity() const override
get the velocity of the music
Definition SdlMusic.hpp:139
SdlMusic(MIX_Mixer *mixer, std::string path)
Definition SdlMusic.hpp:39
void setLoop(bool loop) override
set the music to loop or not
Definition SdlMusic.hpp:93
float getLength() const override
Get the Length of the music.
Definition SdlMusic.hpp:113
float getTime() const override
la vraie position de lecture, que SDL2 ne savait pas donner
Definition SdlMusic.hpp:107
void setVolume(float volume) override
set the volume of the music
Definition SdlMusic.hpp:83
bool getLoop() const override
get if the music is looping or not
Definition SdlMusic.hpp:99
Definition IMusic.hpp:19
Vector3< double > Vector3f
Definition Vector3.hpp:121
T x
Definition Vector3.hpp:21
T y
Definition Vector3.hpp:21
T z
Definition Vector3.hpp:21