1#ifndef AUDIO_PLAYBACK_HPP
2#define AUDIO_PLAYBACK_HPP
4#define MINIAUDIO_IMPLEMENTATION
29 uint64_t pausePosition;
30 std::thread playbackThread;
43 if (ma_engine_init(NULL, &engine) != MA_SUCCESS)
45 throw std::runtime_error(
"Failed to initialize MiniAudio engine.");
58 ma_sound_uninit(&sound);
59 ma_engine_uninit(&engine);
75 std::unique_lock<std::mutex> lock(mtx);
82 ma_sound_uninit(&sound);
84 if (ma_sound_init_from_file(&engine, filePath.c_str(), MA_SOUND_FLAG_STREAM, NULL, NULL,
85 &sound) != MA_SUCCESS)
87 throw std::runtime_error(
"Failed to load audio file: " + filePath);
103 std::unique_lock<std::mutex> lock(mtx);
106 if (ma_sound_start(&sound) != MA_SUCCESS)
108 throw std::runtime_error(
"Failed to play the sound.");
113 if (playbackThread.joinable())
115 playbackThread.join();
119 playbackThread = std::thread(
124 if (!ma_sound_is_playing(&sound))
126 std::unique_lock<std::mutex> lock(mtx);
130 std::this_thread::sleep_for(std::chrono::milliseconds(100));
146 std::unique_lock<std::mutex> lock(mtx);
150 pausePosition = ma_sound_get_time_in_pcm_frames(&sound);
152 if (ma_sound_stop(&sound) != MA_SUCCESS)
154 throw std::runtime_error(
"Failed to pause the sound.");
159 if (playbackThread.joinable())
161 playbackThread.join();
177 ma_sound_seek_to_pcm_frame(&sound, pausePosition);
191 std::unique_lock<std::mutex> lock(mtx);
194 if (ma_sound_stop(&sound) != MA_SUCCESS)
196 throw std::runtime_error(
"Failed to stop the sound.");
199 ma_sound_seek_to_pcm_frame(&sound, 0);
202 if (playbackThread.joinable())
204 playbackThread.join();
219 if (volume < 0.0f || volume > 1.0f)
221 throw std::invalid_argument(
"Volume must be between 0.0 and 1.0.");
223 ma_sound_set_volume(&sound, volume);
233 float getVolume()
const {
return ma_sound_get_volume(&sound); }
244 std::unique_lock<std::mutex> lock(mtx);
258 std::unique_lock<std::mutex> lock(mtx);
259 if (ma_sound_get_time_in_milliseconds(&sound) != MA_SUCCESS)
261 throw std::runtime_error(
"Failed to get sound duration.");
263 float duration = 0.0f;
264 ma_result result = ma_sound_get_length_in_seconds(&sound, &duration);
265 if (result != MA_SUCCESS)
267 throw std::runtime_error(
"Failed to get sound duration. Result: " + std::to_string(result));
282 std::unique_lock<std::mutex> lock(mtx);
285 ma_uint32 sampleRate = ma_engine_get_sample_rate(&engine);
288 ma_uint64 totalFrames;
289 ma_result result = ma_sound_get_length_in_pcm_frames(&sound, &totalFrames);
290 if (result != MA_SUCCESS)
292 std::cerr <<
"Failed to get total PCM frames." << std::endl;
296 ma_uint64 currentFrames = ma_sound_get_time_in_pcm_frames(&sound);
300 static_cast<ma_int64
>(currentFrames) +
static_cast<ma_int64
>(seconds) * sampleRate;
304 newFrames = seconds = 0;
305 if (
static_cast<ma_uint64
>(newFrames) > totalFrames)
306 newFrames = totalFrames;
308 result = ma_sound_seek_to_pcm_frame(&sound,
static_cast<ma_uint64
>(newFrames));
309 if (result != MA_SUCCESS)
311 std::cerr <<
"Failed to seek sound." << std::endl;
314 return (
double)seconds;
float getVolume() const
Gets the current volume of the audio playback.
Definition audio_playback.hpp:233
bool isCurrentlyPlaying()
Checks if the sound is currently playing.
Definition audio_playback.hpp:242
int loadFile(const std::string &filePath)
Loads an audio file for playback.
Definition audio_playback.hpp:73
void resume()
Resumes playback from the last paused position.
Definition audio_playback.hpp:172
void stop()
Stops the playback and resets the playback position.
Definition audio_playback.hpp:189
void pause()
Pauses the current audio playback.
Definition audio_playback.hpp:144
void setVolume(float volume)
Sets the volume of the audio playback.
Definition audio_playback.hpp:217
double seekTime(int seconds)
Seeks to a specific time in the audio (in seconds).
Definition audio_playback.hpp:280
MiniAudioPlayer()
Constructs a MiniAudioPlayer object and initializes the MiniAudio engine.
Definition audio_playback.hpp:41
~MiniAudioPlayer()
Destroys the MiniAudioPlayer object, stopping any playback and cleaning up resources.
Definition audio_playback.hpp:55
float getDuration()
Gets the total duration of the sound in seconds.
Definition audio_playback.hpp:256
void play()
Starts playback of the loaded audio file.
Definition audio_playback.hpp:101