sdl2_impl 0.1.0
Vendor SDL2 2.32 : 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 <SDL_mixer.h>
16
17//Interface
18#include "ISoundBuffer.hpp"
19
20#include <iostream>
21#include <string>
22
30class SdlSoundBuffer : public audio::ISoundBuffer {
31
32 public:
33 SdlSoundBuffer(std::string path) {
34 _chunk = Mix_LoadWAV(path.c_str());
35 if (!_chunk)
36 std::cerr << "Failed to load sound: " << Mix_GetError() << std::endl;
37 }
38
40 if (_chunk)
41 Mix_FreeChunk(_chunk);
42 }
43
44 bool isReady() const override { return _chunk != nullptr; }
45
54 float getLength() const override {
55 int frequency = 0;
56 uint16_t format = 0;
57 int channels = 0;
58
59 if (!_chunk || !Mix_QuerySpec(&frequency, &format, &channels) || frequency == 0)
60 return 0.f;
61
62 const int bytes = (SDL_AUDIO_BITSIZE(format) / 8) * channels;
63
64 if (bytes == 0)
65 return 0.f;
66 return static_cast<float>(_chunk->alen) / static_cast<float>(bytes * frequency);
67 }
68
69 Mix_Chunk *handle() const { return _chunk; }
70
71 private:
72 Mix_Chunk *_chunk = nullptr;
73};
74
77#endif /* !SDLSOUNDBUFFER_HPP_ */
Un echantillon decode en memoire, partageable par plusieurs sons.
Definition SdlSoundBuffer.hpp:30
~SdlSoundBuffer()
Definition SdlSoundBuffer.hpp:39
float getLength() const override
La duree, en secondes.
Definition SdlSoundBuffer.hpp:54
SdlSoundBuffer(std::string path)
Definition SdlSoundBuffer.hpp:33
bool isReady() const override
Definition SdlSoundBuffer.hpp:44
Mix_Chunk * handle() const
Definition SdlSoundBuffer.hpp:69