sdl2_impl 0.1.0
Vendor SDL2 2.32 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SdlSound.hpp
Go to the documentation of this file.
1
11#ifndef SDLSOUND_HPP_
12#define SDLSOUND_HPP_
13
14//Sdl
15#include <SDL_mixer.h>
16
17//Interface
18#include "ISound.hpp"
19
20//encapsulation
21#include "SdlSoundBuffer.hpp"
22
29class SdlSound : public audio::ISound {
30
31 public:
32 SdlSound(SdlSoundBuffer &buffer) : _buffer(buffer) {}
33
34 ~SdlSound() override {
35 if (mine())
36 Mix_HaltChannel(_channel);
37 }
38
39 bool isReady() const override { return _buffer.isReady(); }
40
41 void play() override {
42 if (!_buffer.isReady())
43 return;
44 if (mine() && Mix_Paused(_channel)) {
45 Mix_Resume(_channel);
46 return;
47 }
48 _channel = Mix_PlayChannel(-1, _buffer.handle(), 0);
49 if (mine())
50 Mix_Volume(_channel, static_cast<int>(_volume * MIX_MAX_VOLUME));
51 }
52
53 void pause() override {
54 if (mine())
55 Mix_Pause(_channel);
56 }
57
58 void stop() override {
59 if (mine())
60 Mix_HaltChannel(_channel);
61 _channel = -1;
62 }
63
65 void setVolume(float volume) override {
66 _volume = volume;
67 if (mine())
68 Mix_Volume(_channel, static_cast<int>(volume * MIX_MAX_VOLUME));
69 }
70
71 float getVolume() const override { return _volume; }
72
73 /* SDL_mixer ne fait pas de spatialisation 3D : il n'a qu'une
74 * panoramique gauche/droite. On retient donc la position pour la
75 * rendre telle quelle, et on projette x sur la panoramique - ce que
76 * le contrat permet, puisqu'il demande de POUVOIR positionner, sans
77 * promettre un modele acoustique. */
78 void setPosition(Vector3f position) override {
79 _position = position;
80 if (!mine())
81 return;
82
83 const float clamped = (position.x < -1.f) ? -1.f : (position.x > 1.f ? 1.f : position.x);
84 const uint8_t right = static_cast<uint8_t>((clamped + 1.f) * 0.5f * 255.f);
85
86 Mix_SetPanning(_channel, static_cast<uint8_t>(255 - right), right);
87 }
88
89 Vector3f getPosition() const override { return _position; }
90
91 void setVelocity(Vector3f velocity) override { _velocity = velocity; }
92 Vector3f getVelocity() const override { return _velocity; }
93
94 private:
96 bool mine() const {
97 return _channel >= 0 && Mix_GetChunk(_channel) == _buffer.handle();
98 }
99
100 SdlSoundBuffer &_buffer;
101 int _channel = -1;
102 float _volume = 1.f;
103 Vector3f _position{0, 0, 0};
104 Vector3f _velocity{0, 0, 0};
105};
106
109#endif /* !SDLSOUND_HPP_ */
Un echantillon decode en memoire, partageable par plusieurs sons.
Definition SdlSoundBuffer.hpp:30
bool isReady() const override
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
SdlSound(SdlSoundBuffer &buffer)
Definition SdlSound.hpp:32
void setVelocity(Vector3f velocity) override
Definition SdlSound.hpp:91
Vector3f getPosition() const override
Definition SdlSound.hpp:89
~SdlSound() override
Definition SdlSound.hpp:34
void pause() override
Definition SdlSound.hpp:53
void stop() override
Definition SdlSound.hpp:58
bool isReady() const override
Definition SdlSound.hpp:39
Vector3f getVelocity() const override
Definition SdlSound.hpp:92
void setVolume(float volume) override
dans [0, 1] comme le contrat, MIX_MAX_VOLUME est un detail de SDL
Definition SdlSound.hpp:65
void play() override
Definition SdlSound.hpp:41
void setPosition(Vector3f position) override
Definition SdlSound.hpp:78
float getVolume() const override
Definition SdlSound.hpp:71