Refactor!
This commit is contained in:
parent
5d7a343a20
commit
a5f4d7149a
14 changed files with 13024 additions and 1920 deletions
|
|
@ -1,8 +1,9 @@
|
|||
|
||||
#include "LibraryViewer.hpp"
|
||||
#include "Library.hpp"
|
||||
#include "LocalConnection.hpp"
|
||||
|
||||
#include "picojson.h"
|
||||
#include <filesystem>
|
||||
|
||||
namespace tp {
|
||||
static ModuleManifest* deps[] = { &gModuleGraphics, &gModuleConnection, nullptr };
|
||||
|
|
@ -28,6 +29,25 @@ tp::String getHome() {
|
|||
}
|
||||
}
|
||||
|
||||
tp::String getSongLocalPath(SongId id) {
|
||||
return getHome() + "tracks/" + tp::String((tp::alni)id);
|
||||
}
|
||||
|
||||
SONG_FORMAT getSongFormat(const String& localPath) {
|
||||
std::filesystem::path wavFormat((localPath + ".wav").read());
|
||||
std::filesystem::path flacFormat((localPath + ".flac").read());
|
||||
if (std::filesystem::exists(flacFormat)) return SONG_FORMAT::FLAC;
|
||||
if (std::filesystem::exists(wavFormat)) return SONG_FORMAT::WAV;
|
||||
return SONG_FORMAT::NONE;
|
||||
}
|
||||
|
||||
void Library::checkExisting() {
|
||||
for (auto track : mTraks) {
|
||||
auto const path = getSongLocalPath(track->mId);
|
||||
track->mExists = getSongFormat(path) != SONG_FORMAT::NONE;
|
||||
}
|
||||
}
|
||||
|
||||
bool Library::loadJson(const String& path) {
|
||||
LocalConnection libraryFile;
|
||||
Buffer<int1> libraryFileMem;
|
||||
203
LibraryViewer/private/Player.cpp
Normal file
203
LibraryViewer/private/Player.cpp
Normal file
|
|
@ -0,0 +1,203 @@
|
|||
#include "Player.hpp"
|
||||
#include "Buffer.hpp"
|
||||
#include "Multithreading.hpp"
|
||||
|
||||
#include "portaudio.h"
|
||||
|
||||
class DeviceStream {
|
||||
friend class Player;
|
||||
|
||||
public:
|
||||
DeviceStream() {
|
||||
auto paStatus = Pa_Initialize() ;
|
||||
mInitializationStatus = paStatus == paNoError;
|
||||
if (!mInitializationStatus) {
|
||||
fprintf(stderr, "An error occurred while using the portaudio stream\n");
|
||||
fprintf(stderr, "Error number: %d\n", paStatus);
|
||||
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(paStatus));
|
||||
}
|
||||
}
|
||||
|
||||
~DeviceStream() {
|
||||
if (mInitializationStatus) {
|
||||
Pa_Terminate();
|
||||
}
|
||||
}
|
||||
|
||||
bool openStream(const tp::halnf* buffer, const tp::ualni* pointerMax, tp::ualni sampleRate) {
|
||||
if (!mInitializationStatus) return false;
|
||||
|
||||
mPointerMax = pointerMax;
|
||||
mBuffer = buffer;
|
||||
|
||||
PaStreamParameters outputParameters;
|
||||
|
||||
outputParameters.device = Pa_GetDefaultOutputDevice();
|
||||
if (outputParameters.device == paNoDevice) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const PaDeviceInfo* pInfo = Pa_GetDeviceInfo(outputParameters.device);
|
||||
if (pInfo != 0) {
|
||||
printf("Output device name: '%s'\r", pInfo->name);
|
||||
}
|
||||
|
||||
outputParameters.channelCount = 2;
|
||||
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
|
||||
outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
|
||||
outputParameters.hostApiSpecificStreamInfo = NULL;
|
||||
|
||||
PaError err = Pa_OpenStream(&mPaStream, NULL, &outputParameters, sampleRate, 44100 * 0.2f, 0, &DeviceStream::paCallback, this);
|
||||
|
||||
if (err != paNoError) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = Pa_SetStreamFinishedCallback(mPaStream, &DeviceStream::paStreamFinished);
|
||||
|
||||
if (err != paNoError) {
|
||||
Pa_CloseStream(mPaStream);
|
||||
mPaStream = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool closeStream() {
|
||||
if (mPaStream == 0) return false;
|
||||
PaError err = Pa_CloseStream(mPaStream);
|
||||
|
||||
mPaStream = 0;
|
||||
mBuffer = nullptr;
|
||||
mPointerMax = 0;
|
||||
mPointer = 0;
|
||||
|
||||
return (err == paNoError);
|
||||
}
|
||||
|
||||
bool continueStream() {
|
||||
if (mPaStream == 0) return false;
|
||||
PaError err = Pa_StartStream(mPaStream);
|
||||
return (err == paNoError);
|
||||
}
|
||||
|
||||
bool freezeStream() {
|
||||
if (mPaStream == 0) return false;
|
||||
PaError err = Pa_StopStream(mPaStream);
|
||||
return (err == paNoError);
|
||||
}
|
||||
|
||||
tp::ualni getCurrentFrame() const {
|
||||
return mPointer;
|
||||
}
|
||||
|
||||
void setCurrentFrame(tp::ualni ptr) {
|
||||
ptr = tp::clamp(ptr, (tp::ualni) 0, *mPointerMax);
|
||||
mMutex.lock();
|
||||
mPointer = ptr;
|
||||
mMutex.unlock();
|
||||
}
|
||||
|
||||
private:
|
||||
int paCallbackMethod(const void* inputBuffer, void* outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags) {
|
||||
if (mPointer + framesPerBuffer >= *mPointerMax) return paContinue;
|
||||
|
||||
float* out = (float*) outputBuffer;
|
||||
mMutex.lock();
|
||||
for (unsigned long i = 0; i < framesPerBuffer; i++) {
|
||||
*out++ = (mBuffer)[mPointer * 2] * mVolume;
|
||||
*out++ = (mBuffer)[mPointer * 2 + 1] * mVolume;
|
||||
mPointer++;
|
||||
}
|
||||
mMutex.unlock();
|
||||
return paContinue;
|
||||
}
|
||||
|
||||
void paStreamFinishedMethod() { printf("Stream Completed\n"); }
|
||||
|
||||
static int paCallback(const void* inputBuffer, void* outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void* userData) {
|
||||
return ((DeviceStream*) userData)->paCallbackMethod(inputBuffer, outputBuffer, framesPerBuffer, timeInfo, statusFlags);
|
||||
}
|
||||
|
||||
static void paStreamFinished(void* userData) { return ((DeviceStream*) userData)->paStreamFinishedMethod(); }
|
||||
|
||||
private:
|
||||
bool mInitializationStatus = false;
|
||||
PaStream* mPaStream = nullptr;
|
||||
tp::Mutex mMutex;
|
||||
|
||||
const tp::halnf* mBuffer = nullptr;
|
||||
const tp::ualni* mPointerMax = nullptr;
|
||||
|
||||
tp::ualni mPointer = 0;
|
||||
tp::halnf mVolume = 1.f;
|
||||
};
|
||||
|
||||
Player::Player() {
|
||||
mDeviceStream = new DeviceStream();
|
||||
}
|
||||
|
||||
Player::~Player() {
|
||||
delete mDeviceStream;
|
||||
}
|
||||
|
||||
bool Player::startStreamTrack(SongId id) {
|
||||
mDeviceStream->closeStream();
|
||||
|
||||
if (!mMusicStream.openStream(id)) {
|
||||
connected = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!mDeviceStream->openStream(mMusicStream.getBuffer(), mMusicStream.getProgressLive(), mMusicStream.getRate())) {
|
||||
connected = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
mMusicStream.continueStream();
|
||||
|
||||
connected = id;
|
||||
return true;
|
||||
}
|
||||
|
||||
SongId Player::getPlayingId() const {
|
||||
return connected;
|
||||
}
|
||||
|
||||
void Player::continuePlayback() {
|
||||
if (!connected) return;
|
||||
mDeviceStream->continueStream();
|
||||
}
|
||||
|
||||
void Player::freezePlayback() {
|
||||
if (!connected) return;
|
||||
mDeviceStream->freezeStream();
|
||||
}
|
||||
|
||||
tp::halnf Player::getPlaybackProgress() const {
|
||||
if (!connected) return 0;
|
||||
return (tp::halnf) mDeviceStream->getCurrentFrame() / (tp::halnf) mMusicStream.getFramesLength();
|
||||
}
|
||||
|
||||
tp::halnf Player::getDurationSec() const { return 0; }
|
||||
|
||||
void Player::setPlaybackProgress(tp::halnf val) {
|
||||
if (!connected) return;
|
||||
mDeviceStream->setCurrentFrame(val * mMusicStream.getFramesLength());
|
||||
}
|
||||
|
||||
void Player::setVolume(tp::halnf vol) {
|
||||
if (!connected) return;
|
||||
mDeviceStream->mVolume = vol;
|
||||
}
|
||||
|
||||
tp::halnf Player::getLoadProgress() const {
|
||||
if (!connected) return 0;
|
||||
return (tp::halnf) mMusicStream.getFramesLoaded() / (tp::halnf) mMusicStream.getFramesLength();
|
||||
}
|
||||
|
||||
tp::halnf Player::getVolume() {
|
||||
if (!connected) return 0;
|
||||
return mDeviceStream->mVolume;
|
||||
}
|
||||
125
LibraryViewer/private/Streamer.cpp
Normal file
125
LibraryViewer/private/Streamer.cpp
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
#include "Streamer.hpp"
|
||||
|
||||
typedef u_int8_t uint8_t;
|
||||
typedef u_int16_t uint16_t;
|
||||
typedef u_int32_t uint32_t;
|
||||
|
||||
#define DR_FLAC_IMPLEMENTATION
|
||||
#include "dr_flac.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
class Context {
|
||||
public:
|
||||
Context() = default;
|
||||
|
||||
bool preloadInfo(const tp::String& path, MusicStream* owner) {
|
||||
mOwner = owner;
|
||||
|
||||
pFlac = drflac_open_file((path + ".flac").read(), NULL);
|
||||
|
||||
if (pFlac == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pFlac->channels != 2) {
|
||||
drflac_close(pFlac);
|
||||
return false;
|
||||
}
|
||||
|
||||
mOwner->mLengthFrames = pFlac->totalPCMFrameCount;
|
||||
mOwner->mChanels = pFlac->channels;
|
||||
mOwner->mRate = pFlac->sampleRate;
|
||||
|
||||
mOwner->mBuffer.reserve(mOwner->mLengthFrames * 2);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void runThread() {
|
||||
if (threadRunning) return;
|
||||
threadRunning = true;
|
||||
threadObj = std::thread(&Context::threadWorkStatic, this);
|
||||
}
|
||||
|
||||
void stop() {
|
||||
if (threadRunning) {
|
||||
exitThread = true;
|
||||
threadObj.join();
|
||||
threadRunning = false;
|
||||
exitThread = false;
|
||||
}
|
||||
|
||||
if (pFlac) drflac_close(pFlac);
|
||||
pFlac = nullptr;
|
||||
}
|
||||
|
||||
~Context() {
|
||||
stop();
|
||||
}
|
||||
|
||||
private:
|
||||
std::thread threadObj;
|
||||
bool threadRunning = false;
|
||||
bool exitThread = false;
|
||||
|
||||
void threadWork() {
|
||||
int framesRead = 0;
|
||||
while (!exitThread && (framesRead = drflac_read_pcm_frames_f32(pFlac, mOwner->mRate, mOwner->mBuffer.getBuff() + mOwner->mProgress * 2)) > 0) {
|
||||
mOwner->mProgress += framesRead;
|
||||
// drflac_seek_to_pcm_frame(pFlac, mOwner->mProgress);
|
||||
// tp::sleep(500);
|
||||
}
|
||||
}
|
||||
|
||||
static void threadWorkStatic(void* p) { ((Context*) p)->threadWork(); }
|
||||
|
||||
public:
|
||||
drflac* pFlac = nullptr;
|
||||
MusicStream* mOwner;
|
||||
};
|
||||
|
||||
MusicStream::MusicStream() {
|
||||
mContext = new Context();
|
||||
}
|
||||
|
||||
MusicStream::~MusicStream() {
|
||||
delete mContext;
|
||||
}
|
||||
|
||||
bool MusicStream::openStream(SongId id) {
|
||||
closeStream();
|
||||
auto const path = getSongLocalPath(id);
|
||||
auto format = getSongFormat(path);
|
||||
if (format != SONG_FORMAT::FLAC) return false;
|
||||
return mContext->preloadInfo(path, this);
|
||||
}
|
||||
|
||||
void MusicStream::closeStream() {
|
||||
mContext->stop();
|
||||
mProgress = 0;
|
||||
}
|
||||
|
||||
tp::halnf* MusicStream::getBuffer() {
|
||||
return mBuffer.getBuff();
|
||||
}
|
||||
|
||||
tp::ualni* MusicStream::getProgressLive() {
|
||||
return &mProgress;
|
||||
}
|
||||
|
||||
tp::uhalni MusicStream::getRate() const {
|
||||
return mRate;
|
||||
}
|
||||
|
||||
void MusicStream::continueStream() {
|
||||
mContext->runThread();
|
||||
}
|
||||
|
||||
tp::ualni MusicStream::getFramesLength() const {
|
||||
return mLengthFrames;
|
||||
}
|
||||
|
||||
tp::ualni MusicStream::getFramesLoaded() const {
|
||||
return mProgress;
|
||||
}
|
||||
|
|
@ -1,248 +0,0 @@
|
|||
#include "WavPlayer.hpp"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "portaudio.h"
|
||||
#include "Buffer.hpp"
|
||||
#include "Multithreading.hpp"
|
||||
|
||||
typedef u_int8_t uint8_t;
|
||||
typedef u_int16_t uint16_t;
|
||||
typedef u_int32_t uint32_t;
|
||||
|
||||
#include "wav_file_reader.h"
|
||||
|
||||
class Sine {
|
||||
friend class MusicPlayerContext;
|
||||
public:
|
||||
Sine() {}
|
||||
|
||||
void setSampleRate(int aSampleRate) {
|
||||
sampleRate = aSampleRate;
|
||||
}
|
||||
|
||||
tp::Buffer<float>* geLeftBuffer() { return &leftBuffer; }
|
||||
tp::Buffer<float>* geRightBuffer() { return &rightBuffer; }
|
||||
|
||||
bool open(PaDeviceIndex index) {
|
||||
PaStreamParameters outputParameters;
|
||||
|
||||
outputParameters.device = index;
|
||||
if (outputParameters.device == paNoDevice) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const PaDeviceInfo* pInfo = Pa_GetDeviceInfo(index);
|
||||
if (pInfo != 0) {
|
||||
printf("Output device name: '%s'\r", pInfo->name);
|
||||
}
|
||||
|
||||
outputParameters.channelCount = 2;
|
||||
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
|
||||
outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
|
||||
outputParameters.hostApiSpecificStreamInfo = NULL;
|
||||
|
||||
PaError err = Pa_OpenStream(&stream, NULL, &outputParameters, sampleRate, paFramesPerBufferUnspecified, paClipOff, &Sine::paCallback, this);
|
||||
|
||||
/* Failed to open stream to device !!! */
|
||||
if (err != paNoError) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = Pa_SetStreamFinishedCallback(stream, &Sine::paStreamFinished);
|
||||
|
||||
if (err != paNoError) {
|
||||
Pa_CloseStream(stream);
|
||||
stream = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool close() {
|
||||
if (stream == 0) return false;
|
||||
PaError err = Pa_CloseStream(stream);
|
||||
stream = 0;
|
||||
return (err == paNoError);
|
||||
}
|
||||
|
||||
bool start() {
|
||||
if (stream == 0) return false;
|
||||
PaError err = Pa_StartStream(stream);
|
||||
return (err == paNoError);
|
||||
}
|
||||
|
||||
bool stop() {
|
||||
if (stream == 0) return false;
|
||||
PaError err = Pa_StopStream(stream);
|
||||
return (err == paNoError);
|
||||
}
|
||||
|
||||
private:
|
||||
int paCallbackMethod(const void* inputBuffer, void* outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags) {
|
||||
float* out = (float*) outputBuffer;
|
||||
|
||||
mMutex.lock();
|
||||
for (unsigned long i = 0; i < framesPerBuffer; i++) {
|
||||
*out++ = leftBuffer[leftSampleIdx];
|
||||
*out++ = rightBuffer[rightSampleIdx];
|
||||
|
||||
leftSampleIdx += 1;
|
||||
rightSampleIdx += 1;
|
||||
|
||||
if (leftSampleIdx >= leftBuffer.size())
|
||||
leftSampleIdx -= 1;
|
||||
if (rightSampleIdx >= rightBuffer.size())
|
||||
rightSampleIdx -= 1;
|
||||
}
|
||||
mMutex.unlock();
|
||||
|
||||
return paContinue;
|
||||
}
|
||||
|
||||
void paStreamFinishedMethod() { printf("Stream Completed\n"); }
|
||||
|
||||
static int paCallback(const void* inputBuffer, void* outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void* userData) {
|
||||
return ((Sine*) userData)->paCallbackMethod(inputBuffer, outputBuffer, framesPerBuffer, timeInfo, statusFlags);
|
||||
}
|
||||
|
||||
static void paStreamFinished(void* userData) { return ((Sine*) userData)->paStreamFinishedMethod(); }
|
||||
|
||||
PaStream* stream = 0;
|
||||
|
||||
tp::Buffer<float> leftBuffer;
|
||||
tp::Buffer<float> rightBuffer;
|
||||
|
||||
int leftSampleIdx = 0;
|
||||
int rightSampleIdx = 0;
|
||||
|
||||
int sampleRate = 44100;
|
||||
|
||||
tp::Mutex mMutex;
|
||||
};
|
||||
|
||||
class MusicPlayerContext {
|
||||
public:
|
||||
MusicPlayerContext() {
|
||||
initStatus = Pa_Initialize();
|
||||
if (initStatus != paNoError) {
|
||||
fprintf(stderr, "An error occurred while using the portaudio stream\n");
|
||||
fprintf(stderr, "Error number: %d\n", initStatus);
|
||||
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(initStatus));
|
||||
}
|
||||
}
|
||||
|
||||
bool load(SongId id) {
|
||||
if (connected) {
|
||||
stop();
|
||||
sine.close();
|
||||
}
|
||||
|
||||
if (loadWav(id)) {
|
||||
connected = sine.open(Pa_GetDefaultOutputDevice());
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void stop() {
|
||||
if (!connected) return;
|
||||
sine.stop();
|
||||
}
|
||||
|
||||
void start() {
|
||||
if (!connected) return;
|
||||
sine.start();
|
||||
}
|
||||
|
||||
~MusicPlayerContext() {
|
||||
if (connected) {
|
||||
stop();
|
||||
sine.close();
|
||||
}
|
||||
|
||||
if (initStatus == paNoError) {
|
||||
Pa_Terminate();
|
||||
}
|
||||
}
|
||||
|
||||
bool loadWav(SongId id) {
|
||||
AudioFile<double> audioFile;
|
||||
|
||||
if (!audioFile.load(std::string(getHome().read()) + std::string("traks/") + std::to_string(id) + ".wav")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
audioFile.printSummary();
|
||||
|
||||
sine.setSampleRate(audioFile.getSampleRate());
|
||||
|
||||
int numSamples = audioFile.getNumSamplesPerChannel();
|
||||
|
||||
auto left = sine.geLeftBuffer();
|
||||
auto right = sine.geRightBuffer();
|
||||
|
||||
left->reserve(numSamples);
|
||||
right->reserve(numSamples);
|
||||
|
||||
int channel = 0;
|
||||
for (int i = 0; i < numSamples; i++) {
|
||||
left->getBuff()[i] = audioFile.samples[0][i];
|
||||
right->getBuff()[i] = audioFile.samples[1][i];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
float getProgress() const {
|
||||
return float(sine.leftSampleIdx) / sine.leftBuffer.size();
|
||||
}
|
||||
|
||||
void setProgress(float newProgress) {
|
||||
newProgress = tp::clamp(newProgress, 0.f, 1.f);
|
||||
sine.mMutex.lock();
|
||||
sine.leftSampleIdx = sine.leftBuffer.size() * newProgress;
|
||||
sine.rightSampleIdx = sine.leftBuffer.size() * newProgress;
|
||||
sine.mMutex.unlock();
|
||||
}
|
||||
|
||||
private:
|
||||
Sine sine;
|
||||
PaError initStatus;
|
||||
bool connected = false;
|
||||
};
|
||||
|
||||
TrackPlayer::TrackPlayer() {
|
||||
mContext = new MusicPlayerContext();
|
||||
}
|
||||
|
||||
TrackPlayer::~TrackPlayer() {
|
||||
delete mContext;
|
||||
}
|
||||
|
||||
bool TrackPlayer::startStreamTrack(SongId id) {
|
||||
return mContext->load(id);
|
||||
}
|
||||
|
||||
void TrackPlayer::playSong() {
|
||||
mContext->start();
|
||||
}
|
||||
|
||||
void TrackPlayer::stopSong() {
|
||||
mContext->stop();
|
||||
}
|
||||
|
||||
void TrackPlayer::setVolume(float volume) {}
|
||||
|
||||
float TrackPlayer::getProgress() const {
|
||||
return mContext->getProgress();
|
||||
}
|
||||
|
||||
float TrackPlayer::getDurationSec() const { return 0; }
|
||||
|
||||
void TrackPlayer::setProgress(float newProgress) {
|
||||
mContext->setProgress(newProgress);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue