diff --git a/audio_sink/src/main.cpp b/audio_sink/src/main.cpp index 4b48d8f6..7a7424be 100644 --- a/audio_sink/src/main.cpp +++ b/audio_sink/src/main.cpp @@ -198,13 +198,14 @@ private: AudioSink* _this = (AudioSink*)userData; int count = _this->stereoPacker.out.read(); if (count < 0) { return 0; } - if (nBufferFrames != count) { spdlog::warn("Buffer size missmatch, wanted {0}, was asked for {1}", count, nBufferFrames); } - for (int i = 0; i < count; i++) { - if (_this->stereoPacker.out.readBuf[i].l == NAN || _this->stereoPacker.out.readBuf[i].r == NAN) { spdlog::error("NAN in audio data"); } - if (_this->stereoPacker.out.readBuf[i].l == INFINITY || _this->stereoPacker.out.readBuf[i].r == INFINITY) { spdlog::error("INFINITY in audio data"); } - if (_this->stereoPacker.out.readBuf[i].l == -INFINITY || _this->stereoPacker.out.readBuf[i].r == -INFINITY) { spdlog::error("-INFINITY in audio data"); } - } + // For debug purposes only... + // if (nBufferFrames != count) { spdlog::warn("Buffer size missmatch, wanted {0}, was asked for {1}", count, nBufferFrames); } + // for (int i = 0; i < count; i++) { + // if (_this->stereoPacker.out.readBuf[i].l == NAN || _this->stereoPacker.out.readBuf[i].r == NAN) { spdlog::error("NAN in audio data"); } + // if (_this->stereoPacker.out.readBuf[i].l == INFINITY || _this->stereoPacker.out.readBuf[i].r == INFINITY) { spdlog::error("INFINITY in audio data"); } + // if (_this->stereoPacker.out.readBuf[i].l == -INFINITY || _this->stereoPacker.out.readBuf[i].r == -INFINITY) { spdlog::error("-INFINITY in audio data"); } + // } memcpy(outputBuffer, _this->stereoPacker.out.readBuf, nBufferFrames * sizeof(dsp::stereo_t)); _this->stereoPacker.out.flush(); diff --git a/core/src/dsp/buffer.h b/core/src/dsp/buffer.h index 0e3a30bc..8c199ce2 100644 --- a/core/src/dsp/buffer.h +++ b/core/src/dsp/buffer.h @@ -224,14 +224,6 @@ namespace dsp { SampleFrameBuffer(stream* in) { init(in); } - ~SampleFrameBuffer() { - generic_block>::stop(); - out.stopWriter(); - stopWorker = true; - cnd.notify_all(); - if (readWorkerThread.joinable()) { readWorkerThread.join(); } - } - void init(stream* in) { _in = in; @@ -241,8 +233,6 @@ namespace dsp { generic_block>::registerInput(in); generic_block>::registerOutput(&out); - - readWorkerThread = std::thread(&SampleFrameBuffer::worker, this); } void setInput(stream* in) { @@ -275,9 +265,9 @@ namespace dsp { writeCur++; writeCur = ((writeCur) % TEST_BUFFER_SIZE); - if (((writeCur - readCur + TEST_BUFFER_SIZE) % TEST_BUFFER_SIZE) >= (TEST_BUFFER_SIZE-2)) { - spdlog::warn("Overflow"); - } + // if (((writeCur - readCur + TEST_BUFFER_SIZE) % TEST_BUFFER_SIZE) >= (TEST_BUFFER_SIZE-2)) { + // spdlog::warn("Overflow"); + // } } cnd.notify_all(); _in->flush(); @@ -311,6 +301,25 @@ namespace dsp { bool bypass = false; private: + void doStart() { + generic_block>::workerThread = std::thread(&generic_block>::workerLoop, this); + readWorkerThread = std::thread(&SampleFrameBuffer::worker, this); + } + + void doStop() { + _in->stopReader(); + out.stopWriter(); + stopWorker = true; + cnd.notify_all(); + + if (generic_block>::workerThread.joinable()) { generic_block>::workerThread.join(); } + if (readWorkerThread.joinable()) { readWorkerThread.join(); } + + _in->clearReadStop(); + out.clearWriteStop(); + stopWorker = false; + } + stream* _in; std::thread readWorkerThread; diff --git a/core/src/gui/main_window.cpp b/core/src/gui/main_window.cpp index 2181f4ec..5718c6f6 100644 --- a/core/src/gui/main_window.cpp +++ b/core/src/gui/main_window.cpp @@ -461,7 +461,7 @@ void MainWindow::draw() { ImGui::Checkbox("Bypass buffering", &sigpath::signalPath.inputBuffer.bypass); - ImGui::Text("Buffering: %d", (sigpath::signalPath.inputBuffer.writeCur - sigpath::signalPath.inputBuffer.readCur + 20) % 20); + ImGui::Text("Buffering: %d", (sigpath::signalPath.inputBuffer.writeCur - sigpath::signalPath.inputBuffer.readCur + 32) % 32); if (ImGui::Button("Test Bug")) { spdlog::error("Will this make the software crash?"); diff --git a/core/src/gui/menus/theme.cpp b/core/src/gui/menus/theme.cpp index 0df3213f..d7366eca 100644 --- a/core/src/gui/menus/theme.cpp +++ b/core/src/gui/menus/theme.cpp @@ -1,7 +1,7 @@ #include #include #include -#include > +#include namespace thememenu { int themeId; diff --git a/core/src/signal_path/dsp.cpp b/core/src/signal_path/dsp.cpp index e8daa390..67d63df4 100644 --- a/core/src/signal_path/dsp.cpp +++ b/core/src/signal_path/dsp.cpp @@ -116,4 +116,8 @@ void SignalPath::startFFT() { void SignalPath::stopFFT() { reshape.stop(); fftHandlerSink.stop(); +} + +void SignalPath::setBuffering(bool enabled) { + inputBuffer.bypass = !enabled; } \ No newline at end of file diff --git a/core/src/signal_path/dsp.h b/core/src/signal_path/dsp.h index 99fc94b5..03c8b25f 100644 --- a/core/src/signal_path/dsp.h +++ b/core/src/signal_path/dsp.h @@ -20,6 +20,7 @@ public: void setFFTSize(int size); void startFFT(); void stopFFT(); + void setBuffering(bool enabled); dsp::SampleFrameBuffer inputBuffer; @@ -43,4 +44,5 @@ private: double fftRate; int fftSize; int inputBlockSize; + bool bufferingEnabled = false; }; \ No newline at end of file diff --git a/file_source/src/main.cpp b/file_source/src/main.cpp index 429ef707..f9f42e0b 100644 --- a/file_source/src/main.cpp +++ b/file_source/src/main.cpp @@ -63,11 +63,13 @@ private: static void menuSelected(void* ctx) { FileSourceModule* _this = (FileSourceModule*)ctx; core::setInputSampleRate(_this->sampleRate); + sigpath::signalPath.setBuffering(false); spdlog::info("FileSourceModule '{0}': Menu Select!", _this->name); } static void menuDeselected(void* ctx) { FileSourceModule* _this = (FileSourceModule*)ctx; + sigpath::signalPath.setBuffering(true); spdlog::info("FileSourceModule '{0}': Menu Deselect!", _this->name); } diff --git a/recorder/src/main.cpp b/recorder/src/main.cpp index 2d5378c8..4cffebed 100644 --- a/recorder/src/main.cpp +++ b/recorder/src/main.cpp @@ -314,8 +314,8 @@ private: static void _audioHandler(dsp::stereo_t *data, int count, void *ctx) { RecorderModule* _this = (RecorderModule*)ctx; for (int i = 0; i < count; i++) { - _this->wavSampleBuf[(2*i)] = data[i].l * 32767.0f; - _this->wavSampleBuf[(2*i) + 1] = data[i].r * 32767.0f; + _this->wavSampleBuf[(2*i)] = std::clamp(data[i].l * 32767.0f, -32768, 32767); + _this->wavSampleBuf[(2*i) + 1] = std::clamp(data[i].r * 32767.0f, -32768, 32767); } _this->audioWriter->writeSamples(_this->wavSampleBuf, count * 2 * sizeof(int16_t)); _this->samplesWritten += count;