From a855ce0262e17b85e8670c511acf179ebddd24fe Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 5 Feb 2018 11:41:48 -0500 Subject: Removed bare pointer from Sound class Also whitespace changes. --- src/muxer.cpp | 61 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/src/muxer.cpp b/src/muxer.cpp index d831e6d..ecce82b 100644 --- a/src/muxer.cpp +++ b/src/muxer.cpp @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include #define SAMPLE_RATE (44100) #define DELAY_IN_SECS (0.075) @@ -17,11 +20,14 @@ const int delaySize = SAMPLE_RATE * DELAY_IN_SECS; class Sound { public: Sound(const char* filename, float vol); - ~Sound(); - - float* ptr; + + inline bool isDone() const + { + return pos >= data.size(); + } + + std::vector data; unsigned long pos; - unsigned long len; float vol; }; @@ -45,29 +51,29 @@ int paMuxerCallback(const void*, void* outputBuffer, unsigned long framesPerBuff { Muxer* muxer = (Muxer*) userData; float* out = (float*) outputBuffer; - + for (unsigned long i = 0; iplaying) { - if (sound.pos < sound.len) + if (sound.pos < sound.data.size()) { - in += sound.ptr[sound.pos++] * sound.vol; + in += sound.data[sound.pos++] * sound.vol; } } - + if (in > 1) in = 1; if (in < -1) in = -1; - + float sample = muxer->delay[muxer->delayPos] * GAIN; muxer->delay[muxer->delayPos] = in + (muxer->delay[muxer->delayPos] * FEEDBACK); muxer->delayPos++; if (muxer->delayPos > delaySize) muxer->delayPos = 0; *out++ = (in * DRY) + (sample * WET); } - + return 0; } @@ -76,11 +82,11 @@ static Muxer* muxer; void initMuxer() { muxer = new Muxer(); - + dealWithPaError(Pa_Initialize()); dealWithPaError(Pa_OpenDefaultStream(&muxer->stream, 0, 1, paFloat32, SAMPLE_RATE, paFramesPerBufferUnspecified, paMuxerCallback, muxer)); dealWithPaError(Pa_StartStream(muxer->stream)); - + muxer->delay = (float*) calloc(delaySize, sizeof(float)); } @@ -89,7 +95,7 @@ void destroyMuxer() dealWithPaError(Pa_AbortStream(muxer->stream)); dealWithPaError(Pa_CloseStream(muxer->stream)); dealWithPaError(Pa_Terminate()); - + free(muxer->delay); delete muxer; muxer = 0; @@ -98,8 +104,8 @@ void destroyMuxer() void playSound(const char* filename, float vol) { // First, clear out any sounds that have finished playing - muxer->playing.remove_if([] (Sound& value) { return value.pos >= value.len; }); - + muxer->playing.remove_if([] (Sound& value) { return value.isDone(); }); + // Then, add the new sound muxer->playing.emplace_back(filename, vol); } @@ -110,21 +116,18 @@ Sound::Sound(const char* filename, float vol) SNDFILE* file = sf_open(filename, SFM_READ, &info); if (file == nullptr) { - printf("LibSndFile error: %s\n", sf_strerror(file)); - exit(-1); + std::ostringstream errmsg; + errmsg << "LibSndFile error: "; + errmsg << sf_strerror(file); + + throw std::logic_error(errmsg.str()); } - - ptr = (float*) malloc(info.frames * info.channels * sizeof(float)); - len = info.frames * info.channels; + + data.resize(info.frames * info.channels); pos = 0; this->vol = vol; - - sf_readf_float(file, ptr, info.frames); - - sf_close(file); -} -Sound::~Sound() -{ - free(ptr); + sf_readf_float(file, data.data(), info.frames); + + sf_close(file); } -- cgit 1.4.1