2023-04-24 13:45:06 -07:00
|
|
|
#include "playback.h"
|
2023-07-08 14:50:37 -07:00
|
|
|
extern "C" {
|
|
|
|
#include "raudio.h"
|
|
|
|
}
|
2023-04-24 13:45:06 -07:00
|
|
|
#include <thread>
|
|
|
|
using namespace std::chrono;
|
|
|
|
void Playback::ThreadFunc() {
|
|
|
|
InitAudioDevice();
|
|
|
|
Music music = LoadMusicStream(filePath.c_str());
|
|
|
|
PlayMusicStream(music);
|
|
|
|
length = GetMusicTimeLength(music);
|
|
|
|
while (running) {
|
|
|
|
if (file_changed.exchange(false)) {
|
|
|
|
StopMusicStream(music);
|
|
|
|
UnloadMusicStream(music);
|
|
|
|
music = LoadMusicStream(filePath.c_str());
|
|
|
|
PlayMusicStream(music);
|
|
|
|
length = GetMusicTimeLength(music);
|
|
|
|
}
|
|
|
|
UpdateMusicStream(music);
|
|
|
|
if (flag_mutex.try_lock()) {
|
|
|
|
if (seeking.exchange(false)) {
|
|
|
|
SeekMusicStream(music, position);
|
|
|
|
}
|
|
|
|
if (paused) {
|
|
|
|
PauseMusicStream(music);
|
|
|
|
} else {
|
|
|
|
ResumeMusicStream(music);
|
|
|
|
}
|
|
|
|
if (update.exchange(false)) {
|
|
|
|
SetMasterVolume(volume / 100.0);
|
|
|
|
SetMusicPitch(music, speed);
|
|
|
|
}
|
|
|
|
flag_mutex.unlock();
|
|
|
|
}
|
|
|
|
position = GetMusicTimePlayed(music);
|
|
|
|
std::this_thread::sleep_for(5ms);
|
|
|
|
}
|
|
|
|
UnloadMusicStream(music);
|
|
|
|
CloseAudioDevice();
|
|
|
|
}
|
|
|
|
|
|
|
|
Playback::Playback() {
|
|
|
|
running = false;
|
|
|
|
paused = true;
|
|
|
|
position = 0;
|
|
|
|
length = 0;
|
|
|
|
volume = 100.0;
|
|
|
|
speed = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Playback::~Playback() {
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Playback::Start(std::string filePath) {
|
|
|
|
this->filePath = filePath;
|
|
|
|
printf("Playing %s...\n", filePath.c_str());
|
|
|
|
flag_mutex.lock();
|
|
|
|
paused = false;
|
|
|
|
Update();
|
|
|
|
if (running.exchange(true)) {
|
|
|
|
file_changed.store(true);
|
|
|
|
} else {
|
|
|
|
thread = std::thread(&Playback::ThreadFunc, this);
|
|
|
|
}
|
|
|
|
flag_mutex.unlock();
|
|
|
|
}
|
|
|
|
double Playback::GetPosition() {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
double Playback::GetLength() {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Playback::Seek(double position) {
|
|
|
|
flag_mutex.lock();
|
|
|
|
this->position = position;
|
|
|
|
seeking.store(true);
|
|
|
|
flag_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Playback::Pause() {
|
|
|
|
flag_mutex.lock();
|
|
|
|
paused = !paused;
|
|
|
|
flag_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Playback::IsPaused() {
|
|
|
|
return paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Playback::Stop() {
|
|
|
|
if (running.exchange(false)) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Playback::Update() {
|
|
|
|
update.store(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Playback::IsStopped() {
|
|
|
|
return !running;
|
|
|
|
}
|