sdl3_impl 0.1.0
Vendor SDL3 3.4 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SdlSoundBuffer.hpp
Go to the documentation of this file.
1
11#ifndef SDLSOUNDBUFFER_HPP_
12#define SDLSOUNDBUFFER_HPP_
13
14//Sdl
15#include <SDL3/SDL.h>
16#include <SDL3_mixer/SDL_mixer.h>
17
18//Interface
19#include "ISoundBuffer.hpp"
20
21#include <iostream>
22#include <string>
23
35class SdlSoundBuffer : public audio::ISoundBuffer {
36
37 public:
38 SdlSoundBuffer(MIX_Mixer *mixer, std::string path) {
39 _audio = MIX_LoadAudio(mixer, path.c_str(), true);
40 if (!_audio)
41 std::cerr << "Failed to load sound: " << SDL_GetError() << std::endl;
42 }
43
45 if (_audio)
46 MIX_DestroyAudio(_audio);
47 }
48
49 bool isReady() const override { return _audio != nullptr; }
50
57 float getLength() const override {
58 if (!_audio)
59 return 0.f;
60
61 const Sint64 frames = MIX_GetAudioDuration(_audio);
62
63 if (frames <= 0)
64 return 0.f;
65 return static_cast<float>(MIX_AudioFramesToMS(_audio, frames)) / 1000.f;
66 }
67
68 MIX_Audio *handle() const { return _audio; }
69
70 private:
71 MIX_Audio *_audio = nullptr;
72};
73
76#endif /* !SDLSOUNDBUFFER_HPP_ */
Un echantillon decode d'avance, partageable par plusieurs sons.
Definition SdlSoundBuffer.hpp:35
~SdlSoundBuffer()
Definition SdlSoundBuffer.hpp:44
float getLength() const override
La duree, en secondes.
Definition SdlSoundBuffer.hpp:57
bool isReady() const override
Definition SdlSoundBuffer.hpp:49
SdlSoundBuffer(MIX_Mixer *mixer, std::string path)
Definition SdlSoundBuffer.hpp:38
MIX_Audio * handle() const
Definition SdlSoundBuffer.hpp:68