Adding Music Playback

This commit is contained in:
IlyaShurupov 2023-11-29 19:42:17 +03:00
parent 7b325244c5
commit 231b30b2cb
12 changed files with 1840 additions and 149 deletions

View file

@ -1,12 +1,22 @@
#include "Allocators.hpp"
#include "HeapAllocatorGlobal.hpp"
#include <cstdlib>
#include <stdio.h>
static bool init(const tp::ModuleManifest* self) {
tp::HeapAllocGlobal::stopIgnore();
if (tp::HeapAllocGlobal::getNAllocations()) {
printf("Warning : Operator new was called outside module initialization!!\n\n");
}
return true;
}
static void deinit(const tp::ModuleManifest* self) { tp::HeapAllocGlobal::checkLeaks(); }
static tp::ModuleManifest* sModuleDependencies[] = { &tp::gModuleUtils, nullptr };
tp::ModuleManifest tp::gModuleAllocators = ModuleManifest("Allocators", nullptr, deinit, sModuleDependencies);
tp::ModuleManifest tp::gModuleAllocators = ModuleManifest("Allocators", init, deinit, sModuleDependencies);
void* operator new(size_t aSize) { return tp::HeapAllocGlobal::allocate(aSize); }
void* operator new[](size_t aSize) { return tp::HeapAllocGlobal::allocate(aSize); }

View file

@ -21,13 +21,14 @@ HeapAllocGlobal::~HeapAllocGlobal() = default;
bool HeapAllocGlobal::checkLeaks() { return false; }
void HeapAllocGlobal::startIgnore() {}
void HeapAllocGlobal::stopIgnore() {}
ualni HeapAllocGlobal::getNAllocations() { return 0; }
#else
tp::MemHead* tp::HeapAllocGlobal::mEntry = nullptr;
tp::ualni tp::HeapAllocGlobal::mNumAllocations = 0;
tp::Mutex tp::HeapAllocGlobal::mMutex;
bool tp::HeapAllocGlobal::mIgnore;
bool tp::HeapAllocGlobal::mIgnore = true;
// ----------------------- Debug Implementation ---------------------------- //
// |----------------|
@ -100,7 +101,9 @@ ALLOCATE:
// 3) Trace the stack
#ifdef MEM_STACK_TRACE
head->mCallStack = gCSCapture->getSnapshot();
// check if somewhat decides to call new within static variable initialization
if (gCSCapture) head->mCallStack = gCSCapture->getSnapshot();
else head->mCallStack = nullptr;
#endif
mMutex.unlock();
@ -194,6 +197,10 @@ void HeapAllocGlobal::stopIgnore() {
mMutex.unlock();
}
ualni HeapAllocGlobal::getNAllocations() {
return mNumAllocations;
}
HeapAllocGlobal::~HeapAllocGlobal() = default;
#endif

View file

@ -25,6 +25,7 @@ namespace tp {
static bool checkLeaks();
static void startIgnore();
static void stopIgnore();
static ualni getNAllocations();
public:
[[nodiscard]] bool checkWrap() const { return false; }

View file

@ -5,7 +5,7 @@ set(CMAKE_CXX_STANDARD 20)
# set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/./tmp/install)
# set(CMAKE_CXX_FLAGS "-fuse-ld=lld-15 ${CMAKE_CXX_FLAGS} -gdwarf-4 ")
# add_compile_definitions(MEM_DEBUG)
add_compile_definitions(MEM_DEBUG)
project(ModulesRoot)

View file

@ -1,10 +1,11 @@
project(LibraryViewer)
find_package(ALSA REQUIRED)
# find_package(PortAudio REQUIRED)
### ---------------------- Externals --------------------- ###
set(BINDINGS_INCLUDE ../Externals/glfw/include ../Externals/glew/include)
set(BINDINGS_LIBS glfw glew_s Imgui Nanovg)
set(BINDINGS_LIBS glfw glew_s Imgui Nanovg portaudio)
### ---------------------- Static Library --------------------- ###
file(GLOB SOURCES "./private/*.cpp" "./private/*/*.cpp")

View file

@ -1,5 +1,5 @@
// #include "NewPlacement.hpp"
#include "NewPlacement.hpp"
#include "WavPlayer.hpp"
#include "LibraryGui.hpp"
@ -8,6 +8,11 @@
void runApp() {
TrackPlayer player;
player.startStreamTrack(0);
player.playSong();
Library library;
library.loadJson("library/Library.json");
@ -32,7 +37,6 @@ void runApp() {
}
int main() {
// example();
tp::ModuleManifest* deps[] = { &tp::gModuleLibraryViewer, nullptr };
tp::ModuleManifest binModule("LibViewEntry", nullptr, nullptr, deps);

Binary file not shown.

View file

@ -1,138 +1,217 @@
#include "WavPlayer.hpp"
#include <chrono>
#include <fstream>
#include <iostream>
#include <math.h>
#include <stdio.h>
WavPlayer::WavPlayer() :
handle(nullptr),
playbackThread(nullptr),
isPlaying(false),
progress(0.0f) {
if (snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
std::cerr << "Error: Couldn't open ALSA PCM device." << std::endl;
#include "portaudio.h"
#include "Buffer.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 {
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;
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;
}
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;
};
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));
}
}
WavPlayer::~WavPlayer() {
stopSong();
if (handle) {
snd_pcm_close(handle);
void load() {
if (connected) {
stop();
sine.close();
}
exampleLoad();
connected = sine.open(Pa_GetDefaultOutputDevice());
}
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();
}
}
void WavPlayer::loadSong(const char* filePath) {
stopSong();
void exampleLoad() {
AudioFile<double> audioFile;
audioFile.load("library/Example.wav");
if (snd_pcm_prepare(handle) < 0) {
std::cerr << "Error: Couldn't prepare PCM." << std::endl;
return;
}
audioFile.printSummary();
// TODO: Load and parse the WAV file
// For simplicity, assume a 16-bit, mono WAV file with a 44.1 kHz sample rate
// You need to handle loading and parsing of the WAV file here
sine.setSampleRate(audioFile.getSampleRate());
// Example:
// std::ifstream file(filePath, std::ios::binary);
// if (!file) {
// std::cerr << "Error: Could not open WAV file." << std::endl;
// return;
// }
// ... (load and parse WAV file)
int numSamples = audioFile.getNumSamplesPerChannel();
snd_pcm_set_params(handle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 44100, 1, 500000);
}
auto left = sine.geLeftBuffer();
auto right = sine.geRightBuffer();
void WavPlayer::playSong() {
if (!isPlaying) {
isPlaying = true;
playbackThread = new std::thread(&WavPlayer::playbackThreadFunction, this);
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];
}
}
void WavPlayer::stopSong() {
if (isPlaying) {
isPlaying = false;
if (playbackThread && playbackThread->joinable()) {
playbackThread->join();
}
delete playbackThread;
playbackThread = nullptr;
snd_pcm_drop(handle);
}
private:
Sine sine;
PaError initStatus;
bool connected = false;
};
TrackPlayer::TrackPlayer() {
mContext = new MusicPlayerContext();
}
void WavPlayer::setVolume(float volume) {
// TODO: Adjust volume using ALSA API
// You might need to use functions like snd_pcm_volume() or other ALSA functions
TrackPlayer::~TrackPlayer() {
delete mContext;
}
float WavPlayer::getProgress() const { return progress; }
float WavPlayer::getDurationSec() const {
// TODO: Return the duration of the loaded song
// You need information from the loaded WAV file for this
return 0.0f;
void TrackPlayer::startStreamTrack(SongId id) {
mContext->load();
}
void WavPlayer::setProgress(float newProgress) {
if (newProgress >= 0.0f && newProgress <= 1.0f) {
progress = newProgress;
// TODO: Adjust playback position based on the progress using ALSA API
// You might need to use functions like snd_pcm_writei() or other ALSA functions
}
void TrackPlayer::playSong() {
mContext->start();
}
void WavPlayer::playbackThreadFunction() {
const int buffer_size = 1024; // Adjust the buffer size according to your requirements
short buffer[buffer_size];
while (isPlaying) {
// TODO: Implement actual playback logic using ALSA API
// You might need to use functions like snd_pcm_writei() or other ALSA functions
// For simplicity, we'll fill the buffer with dummy data
for (int i = 0; i < buffer_size; ++i) {
buffer[i] = static_cast<short>(32767 * std::sin(2.0 * M_PI * 440.0 * progress));
void TrackPlayer::stopSong() {
mContext->stop();
}
// Write the buffer to the PCM device
int err = snd_pcm_writei(handle, buffer, buffer_size);
if (err < 0) {
std::cerr << "Error writing to PCM device: " << snd_strerror(err) << std::endl;
stopSong();
return;
}
void TrackPlayer::setVolume(float volume) {}
// Update progress
progress += 0.1f;
if (progress >= 1.0f) {
stopSong();
}
float TrackPlayer::getProgress() const { return 0; }
// Sleep for a short duration (adjust as needed)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
float TrackPlayer::getDurationSec() const { return 0; }
void example() {
WavPlayer player;
// Load a WAV file
player.loadSong("path/to/your/file.wav");
// Start playing the song asynchronously
player.playSong();
// Simulate a main application loop (replace this with your actual application logic)
for (int i = 0; i < 30; ++i) {
std::cout << "Progress: " << player.getProgress() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
// Stop the song playback
player.stopSong();
}
void TrackPlayer::setProgress(float newProgress) {}

View file

@ -1,29 +1,29 @@
#pragma once
#include <iostream>
#include <thread>
#include <alsa/asoundlib.h>
#include "Common.hpp"
class WavPlayer {
class MusicPlayerContext;
typedef tp::ualni SongId;
class TrackPlayer {
public:
WavPlayer();
~WavPlayer();
TrackPlayer();
~TrackPlayer();
void startStreamTrack(SongId id);
void loadSong(const char* filePath);
void playSong();
void stopSong();
void setVolume(float volume);
float getProgress() const;
float getDurationSec() const;
void setProgress(float newProgress);
private:
void playbackThreadFunction();
snd_pcm_t* handle;
std::thread* playbackThread;
bool isPlaying;
float progress;
MusicPlayerContext* mContext;
};
void example();

File diff suppressed because it is too large Load diff

View file

@ -208,10 +208,12 @@ void CallStackCapture::platformWriteDebugSymbols(FramePointer frame, DebugSymbol
void CallStackCapture::printSnapshot(const CallStack* snapshot) {
printf("CallStack: \n");
if (snapshot) {
for (auto frame : *snapshot) {
auto symbols = gCSCapture->getSymbols(frame.getFrame());
printf(" %s ----- %s:%llu\n", symbols->getFunc(), symbols->getFile(), symbols->getLine());
}
}
printf("\n");
}

View file

@ -31,7 +31,7 @@ namespace tp {
ModuleManifest gModuleUtils = ModuleManifest("Utils", initialize, deinitialize, sModuleUtilsDeps);
void memSetVal(void* p, uhalni byteSize, uint1 val) {
MODULE_SANITY_CHECK(gModuleBase)
// MODULE_SANITY_CHECK(gModuleBase)
alni alignedVal = val;
alignedVal = (alignedVal << 8) | alignedVal;