P-E-R-R-Y
Utilitaires, contrats et vendors du moteur, en une seule doc
Loading...
Searching...
No Matches
SdlSound.hpp
Go to the documentation of this file.
1
10
11#ifndef SDLSOUND_HPP_
12#define SDLSOUND_HPP_
13
14//Sdl
15#include <SDL3/SDL.h>
16#include <SDL3_mixer/SDL_mixer.h>
17
18//Interface
19#include "ISound.hpp"
20
21//encapsulation
22#include "SdlSoundBuffer.hpp"
23
32class SdlSound : public audio::ISound {
33
34 public:
35 SdlSound(MIX_Mixer *mixer, SdlSoundBuffer &buffer) : _buffer(buffer) {
36 _track = MIX_CreateTrack(mixer);
37 if (_track && buffer.isReady())
38 MIX_SetTrackAudio(_track, buffer.handle());
39 }
40
41 ~SdlSound() override {
42 if (_track)
43 MIX_DestroyTrack(_track);
44 }
45
46 bool isReady() const override { return _track != nullptr && _buffer.isReady(); }
47
48 void play() override {
49 if (!isReady())
50 return;
51 if (MIX_TrackPaused(_track)) {
52 MIX_ResumeTrack(_track);
53 return;
54 }
55 MIX_PlayTrack(_track, 0); //0 : aucune option, la piste dit deja tout
56 }
57
58 void pause() override {
59 if (_track)
60 MIX_PauseTrack(_track);
61 }
62
63 void stop() override {
64 if (_track)
65 MIX_StopTrack(_track, 0); //0 : coupe net, sans fondu
66 }
67
69 void setVolume(float volume) override {
70 _volume = volume;
71 if (_track)
72 MIX_SetTrackGain(_track, volume);
73 }
74
75 float getVolume() const override { return _volume; }
76
77 /* Vraie spatialisation, contrairement a SDL2 qui n'avait qu'une
78 * panoramique gauche/droite : SDL3_mixer place la source dans
79 * l'espace et calcule lui-meme ce que chaque oreille entend. */
80 void setPosition(Vector3f position) override {
81 _position = position;
82 if (!_track)
83 return;
84
85 const MIX_Point3D point{static_cast<float>(position.x),
86 static_cast<float>(position.y),
87 static_cast<float>(position.z)};
88
89 MIX_SetTrack3DPosition(_track, &point);
90 }
91
92 Vector3f getPosition() const override { return _position; }
93
94 //aucun effet Doppler dans SDL3_mixer : retenue, jamais appliquee
95 void setVelocity(Vector3f velocity) override { _velocity = velocity; }
96 Vector3f getVelocity() const override { return _velocity; }
97
98 private:
99 SdlSoundBuffer &_buffer;
100 MIX_Track *_track = nullptr;
101 float _volume = 1.f;
102 Vector3f _position{0, 0, 0};
103 Vector3f _velocity{0, 0, 0};
104};
105
107
108#endif /* !SDLSOUND_HPP_ */
Un echantillon decode en memoire, partageable par plusieurs sons.
Definition SdlSoundBuffer.hpp:30
bool isReady() const override
notice if the buffer is ready
Definition SdlSoundBuffer.hpp:44
Mix_Chunk * handle() const
Definition SdlSoundBuffer.hpp:69
Une lecture d'un tampon, sur son propre canal.
Definition SdlSound.hpp:29
void setVelocity(Vector3f velocity) override
set the velocity of the sound, used by backends that apply a doppler effect
Definition SdlSound.hpp:95
Vector3f getPosition() const override
get the 3D position of the sound
Definition SdlSound.hpp:92
~SdlSound() override
Definition SdlSound.hpp:41
void pause() override
Suspends the sound without touching the position.
Definition SdlSound.hpp:58
void stop() override
Stops the sound AND rewinds to zero.
Definition SdlSound.hpp:63
bool isReady() const override
Definition SdlSound.hpp:46
Vector3f getVelocity() const override
get the velocity of the sound
Definition SdlSound.hpp:96
SdlSound(MIX_Mixer *mixer, SdlSoundBuffer &buffer)
Definition SdlSound.hpp:35
void setVolume(float volume) override
dans [0, 1] comme le contrat ; MIX_SetTrackGain accepte au-dela
Definition SdlSound.hpp:69
void play() override
Fires the sound from the beginning.
Definition SdlSound.hpp:48
void setPosition(Vector3f position) override
set the 3D position of the sound, for backends that spatialize against a listener....
Definition SdlSound.hpp:80
float getVolume() const override
get the volume of the sound
Definition SdlSound.hpp:75
Definition ISound.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