sdl3_impl 0.1.0
Vendor SDL3 3.4 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SdlAudioModule.hpp
Go to the documentation of this file.
1
13#ifndef SDLAUDIO_MODULE_HPP
14#define SDLAUDIO_MODULE_HPP
15
16#include "IAudioModule.hpp"
17
18#include "audio/SdlMusic.hpp"
19#include "audio/SdlSound.hpp"
21
22#include <SDL3/SDL.h>
23#include <SDL3_mixer/SDL_mixer.h>
24
35class SdlAudioModule : public IAudioModule {
36
37public:
38 SdlAudioModule() = default;
39 ~SdlAudioModule() { stop(); }
40
41 const char *type() const override { return IAudioModule::contract; }
42 const char *name() const override { return "sdl3"; }
43
44 // music
45 audio::IMusic *createMusic(std::string path) override {
46 if (!start())
47 return nullptr;
48 _opened++;
49 return new SdlMusic(_mixer, path);
50 }
51 void deleteMusic(audio::IMusic *music) override { drop(music); }
52
53 // sound buffer
54 audio::ISoundBuffer *createSoundBuffer(std::string path) override {
55 if (!start())
56 return nullptr;
57 _opened++;
58 return new SdlSoundBuffer(_mixer, path);
59 }
60 void deleteSoundBuffer(audio::ISoundBuffer *buffer) override { drop(buffer); }
61
62 // sound
63 audio::ISound *createSound(audio::ISoundBuffer *buffer) override {
64 if (!buffer || !start())
65 return nullptr;
66 _opened++;
67 return new SdlSound(_mixer, *static_cast<SdlSoundBuffer *>(buffer));
68 }
69 void deleteSound(audio::ISound *sound) override { drop(sound); }
70
71private:
79 bool start() {
80 if (_started)
81 return true;
82 if (!SDL_InitSubSystem(SDL_INIT_AUDIO))
83 return false;
84 if (!MIX_Init()) {
85 SDL_QuitSubSystem(SDL_INIT_AUDIO);
86 return false;
87 }
88 /* MIX_CreateMixerDevice et pas MIX_CreateMixer : le second genere
89 * vers un tampon memoire qu'il faut pousser soi-meme, et exige un
90 * format. C'est celui-ci qui ouvre le peripherique de sortie.
91 *
92 * DEFAULT_PLAYBACK plutot qu'un peripherique nomme : SDL suit alors
93 * le defaut du systeme, casque branche en cours de route compris.
94 * nullptr pour le format : SDL_mixer convertit de toute facon. */
95 _mixer = MIX_CreateMixerDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, nullptr);
96 if (!_mixer) {
97 MIX_Quit();
98 SDL_QuitSubSystem(SDL_INIT_AUDIO);
99 return false;
100 }
101 _started = true;
102 return true;
103 }
104
105 void stop() {
106 if (!_started)
107 return;
108 MIX_DestroyMixer(_mixer);
109 _mixer = nullptr;
110 MIX_Quit();
111 SDL_QuitSubSystem(SDL_INIT_AUDIO);
112 _started = false;
113 }
114
116 template <typename T>
117 void drop(T *object) {
118 if (!object)
119 return;
120 delete object;
121 if (_opened > 0)
122 _opened--;
123 if (_opened == 0)
124 stop();
125 }
126
127 MIX_Mixer *_mixer = nullptr;
128 unsigned _opened = 0;
129 bool _started = false;
130};
131
134#endif /* !SDLAUDIO_MODULE_HPP */
SDL3_mixer derriere IAudioModule.
Definition SdlAudioModule.hpp:35
~SdlAudioModule()
Definition SdlAudioModule.hpp:39
audio::IMusic * createMusic(std::string path) override
Definition SdlAudioModule.hpp:45
void deleteSoundBuffer(audio::ISoundBuffer *buffer) override
Definition SdlAudioModule.hpp:60
SdlAudioModule()=default
audio::ISoundBuffer * createSoundBuffer(std::string path) override
Definition SdlAudioModule.hpp:54
void deleteSound(audio::ISound *sound) override
Definition SdlAudioModule.hpp:69
const char * type() const override
Definition SdlAudioModule.hpp:41
audio::ISound * createSound(audio::ISoundBuffer *buffer) override
Definition SdlAudioModule.hpp:63
void deleteMusic(audio::IMusic *music) override
Definition SdlAudioModule.hpp:51
const char * name() const override
Definition SdlAudioModule.hpp:42
Un flux long, decode au fil de la lecture, sur sa propre piste.
Definition SdlMusic.hpp:36
Un echantillon decode d'avance, partageable par plusieurs sons.
Definition SdlSoundBuffer.hpp:35
Une lecture d'un tampon, sur sa propre piste.
Definition SdlSound.hpp:32