2023-04-24 13:45:06 -07:00
|
|
|
#pragma once
|
2023-07-20 14:45:35 -07:00
|
|
|
#include "config.h"
|
2023-07-10 12:45:24 -07:00
|
|
|
#include "SDL_mixer.h"
|
2023-04-24 13:45:06 -07:00
|
|
|
#include <thread>
|
2023-07-15 14:52:49 -07:00
|
|
|
#include <SDL.h>
|
|
|
|
#include <SDL_audio.h>
|
2023-04-24 13:45:06 -07:00
|
|
|
#include <string>
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
2023-07-15 14:52:49 -07:00
|
|
|
#include <SoundTouch.h>
|
|
|
|
#include <span>
|
2023-09-03 11:54:07 -07:00
|
|
|
#include <optional>
|
2023-07-15 14:52:49 -07:00
|
|
|
using namespace soundtouch;
|
|
|
|
using std::span;
|
2023-09-03 11:54:07 -07:00
|
|
|
using std::optional;
|
2023-04-24 13:45:06 -07:00
|
|
|
class Playback {
|
|
|
|
private:
|
|
|
|
std::string filePath;
|
|
|
|
std::atomic_bool running;
|
|
|
|
std::atomic_bool file_changed;
|
|
|
|
std::atomic_bool seeking;
|
|
|
|
std::atomic_bool update;
|
2023-09-03 11:54:07 -07:00
|
|
|
std::atomic_bool restart;
|
|
|
|
std::atomic_bool playback_ready;
|
2023-04-24 13:45:06 -07:00
|
|
|
std::mutex flag_mutex;
|
|
|
|
std::thread thread;
|
|
|
|
double position;
|
|
|
|
double length;
|
|
|
|
bool paused;
|
2023-07-15 14:52:49 -07:00
|
|
|
Uint8* buf;
|
2023-07-10 12:45:24 -07:00
|
|
|
size_t bufsize;
|
2023-07-15 14:52:49 -07:00
|
|
|
Mix_CommonMixer_t general_mixer;
|
|
|
|
SDL_AudioDeviceID device;
|
|
|
|
SoundTouch *st;
|
|
|
|
SDL_AudioSpec spec;
|
2023-10-16 10:44:25 -07:00
|
|
|
/// @brief A fake SDL_AudioSpec used to trick SDL Mixer X into allocating a bigger buffer.
|
|
|
|
SDL_AudioSpec fakespec;
|
2023-07-15 14:52:49 -07:00
|
|
|
void SDLCallbackInner(Uint8 *stream, int len);
|
|
|
|
static void SDLCallback(void *userdata, Uint8 *stream, int len);
|
2023-07-10 12:45:24 -07:00
|
|
|
Mix_Music *Load(const char* file);
|
|
|
|
void Unload(Mix_Music* music);
|
2023-04-24 13:45:06 -07:00
|
|
|
void ThreadFunc();
|
2023-09-03 11:54:07 -07:00
|
|
|
void UpdateST();
|
|
|
|
double GetMaxSeconds();
|
2023-04-24 13:45:06 -07:00
|
|
|
public:
|
|
|
|
Playback();
|
|
|
|
~Playback();
|
|
|
|
double GetPosition();
|
|
|
|
double GetLength();
|
|
|
|
void Seek(double position);
|
|
|
|
void Start(std::string filePath);
|
|
|
|
bool IsPaused();
|
|
|
|
void Pause();
|
|
|
|
void Stop();
|
|
|
|
void Update();
|
|
|
|
bool IsStopped();
|
|
|
|
float volume;
|
|
|
|
float speed;
|
2023-07-15 14:52:49 -07:00
|
|
|
float tempo;
|
|
|
|
float pitch;
|
2023-09-03 11:54:07 -07:00
|
|
|
double MaxSeconds = 100.0;
|
|
|
|
double MaxSpeed = 4.0;
|
|
|
|
double MaxPitch = 4.0;
|
|
|
|
double MaxTempo = 4.0;
|
|
|
|
double MinSpeed = 0.25;
|
|
|
|
double MinPitch = 0.25;
|
|
|
|
double MinTempo = 0.25;
|
2023-04-24 13:45:06 -07:00
|
|
|
};
|