diff --git a/xybrid/audio/audio.h b/xybrid/audio/audio.h new file mode 100644 index 0000000..8377a0f --- /dev/null +++ b/xybrid/audio/audio.h @@ -0,0 +1,5 @@ +#pragma once + +namespace Xybrid::Audio { + typedef float bufferType; +} diff --git a/xybrid/audio/audioengine.cpp b/xybrid/audio/audioengine.cpp index a4ec127..c26d7a5 100644 --- a/xybrid/audio/audioengine.cpp +++ b/xybrid/audio/audioengine.cpp @@ -406,8 +406,8 @@ qint64 AudioEngine::readData(char *data, qint64 maxlen) { // convert non-interleaved floating point into interleaved int16 int16_t* l = reinterpret_cast(data); int16_t* r = reinterpret_cast(data+smp); - *l = static_cast(std::clamp(buffer[0][bufPos] * 32768.0f, -32767.0f, 32767.0f)); - *r = static_cast(std::clamp(buffer[1][bufPos] * 32768.0f, -32767.0f, 32767.0f)); + *l = static_cast(std::clamp(buffer[0][bufPos] * 32768.0, -32767.0, 32767.0)); + *r = static_cast(std::clamp(buffer[1][bufPos] * 32768.0, -32767.0, 32767.0)); bufPos++; data += stride; @@ -470,7 +470,7 @@ void AudioEngine::nextTick() { processNodes(); if (auto p = std::static_pointer_cast(project->rootGraph->port(Port::Output, Port::Audio, 0)); p) { p->pull(); - size_t bufs = ts * sizeof(float); + size_t bufs = ts * sizeof(bufferType); memcpy(buffer[0].data(), p->bufL, bufs); memcpy(buffer[1].data(), p->bufR, bufs); } @@ -654,7 +654,7 @@ void AudioEngine::nextTick() { processNodes(); if (auto p = std::static_pointer_cast(project->rootGraph->port(Port::Output, Port::Audio, 0)); p) { p->pull(); - size_t bufs = ts * sizeof(float); + size_t bufs = ts * sizeof(bufferType); memcpy(buffer[0].data(), p->bufL, bufs); memcpy(buffer[1].data(), p->bufR, bufs); } diff --git a/xybrid/audio/audioengine.h b/xybrid/audio/audioengine.h index 51393dd..d89bc00 100644 --- a/xybrid/audio/audioengine.h +++ b/xybrid/audio/audioengine.h @@ -12,6 +12,8 @@ #include #include +#include "audio/audio.h" + class QThread; namespace Xybrid::Data { class Project; @@ -57,11 +59,12 @@ namespace Xybrid::Audio { int sampleRate = 48000; int bufferMs = 64; - std::vector buffer[2]; + std::vector buffer[2]; size_t bufPos = 0; //std::vector outBuf; - static const constexpr size_t tickBufSize = (1024*1024*5); // 5mb should be enough + // 32MiB really isn't much to take up for being a far higher ceiling than we should ever need + static const constexpr size_t tickBufSize = (1024*1024*32); std::unique_ptr tickBuf; std::atomic tickBufPtr; size_t* tickBufEnd; diff --git a/xybrid/data/porttypes.cpp b/xybrid/data/porttypes.cpp index 5ef90dc..26526a7 100644 --- a/xybrid/data/porttypes.cpp +++ b/xybrid/data/porttypes.cpp @@ -14,7 +14,7 @@ void AudioPort::pull() { size_t ts = audioEngine->curTickSize(); size = ts; - size_t s = sizeof(float) * ts; + size_t s = sizeof(bufferType) * ts; if (type == Input) { if (connections.size() == 1) { @@ -26,7 +26,7 @@ void AudioPort::pull() { goto done;//return; } } - bufL = static_cast(audioEngine->tickAlloc(s*2)); + bufL = static_cast(audioEngine->tickAlloc(s*2)); bufR = &bufL[ts]; // for some reason just adding the size wonks out memset(bufL, 0, s*2); // clear buffers @@ -45,7 +45,7 @@ void AudioPort::pull() { bufL = pt->bufL; bufR = pt->bufR; } else { // output without valid passthrough, just clear and prepare a blank buffer - bufL = static_cast(audioEngine->tickAlloc(s*2)); + bufL = static_cast(audioEngine->tickAlloc(s*2)); bufR = &bufL[ts]; memset(bufL, 0, s*2); // clear buffers } diff --git a/xybrid/data/porttypes.h b/xybrid/data/porttypes.h index 754754e..da628c3 100644 --- a/xybrid/data/porttypes.h +++ b/xybrid/data/porttypes.h @@ -2,6 +2,7 @@ #include "data/node.h" #include "data/audioframe.h" +#include "audio/audio.h" namespace Xybrid::Data { class AudioPort : public Port { @@ -13,21 +14,21 @@ namespace Xybrid::Data { FrameRef(AudioPort* port, size_t at) : port(port), at(at) { } public: FrameRef& operator=(AudioFrame f) { - port->bufL[at] = static_cast(f.l); - port->bufR[at] = static_cast(f.r); + port->bufL[at] = static_cast(f.l); + port->bufR[at] = static_cast(f.r); return *this; } FrameRef& operator+=(AudioFrame f) { - port->bufL[at] += static_cast(f.l); - port->bufR[at] += static_cast(f.r); + port->bufL[at] += static_cast(f.l); + port->bufR[at] += static_cast(f.r); return *this; } operator AudioFrame() const { return { port->bufL[at], port->bufR[at] }; } AudioFrame operator*(AudioFrame o) const { return static_cast(*this) * o; } }; - float* bufL; - float* bufR; + Audio::bufferType* bufL; + Audio::bufferType* bufR; size_t size; AudioPort() = default;