From 7c16b85a9682856162159040677f3007d1300299 Mon Sep 17 00:00:00 2001 From: kearwood Date: Sun, 3 Dec 2023 23:06:17 -0800 Subject: [PATCH] Moved Siren DSP functions to submodule --- .gitmodules | 3 + CMakeLists.txt | 8 ++ kraken/CMakeLists.txt | 5 - kraken/KRAudioManager.cpp | 2 +- kraken/KRAudioManager.h | 2 +- kraken/KRAudioSample.cpp | 3 +- kraken/KRDSP.h | 95 ----------------- kraken/KRDSP_slow.cpp | 209 -------------------------------------- kraken/KRDSP_vDSP.cpp | 121 ---------------------- kraken/KREngine-common.h | 1 + siren | 1 + 11 files changed, 17 insertions(+), 433 deletions(-) delete mode 100644 kraken/KRDSP.h delete mode 100644 kraken/KRDSP_slow.cpp delete mode 100644 kraken/KRDSP_vDSP.cpp create mode 160000 siren diff --git a/.gitmodules b/.gitmodules index a08bf54..a3ae783 100644 --- a/.gitmodules +++ b/.gitmodules @@ -22,3 +22,6 @@ [submodule "mimir"] path = mimir url = https://src.kearwood.com/kraken_engine/mimir +[submodule "siren"] + path = siren + url = https://github.com/KrakenEngine/siren.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 902174c..d14e9a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,9 @@ add_public_header(hydra/include/vector2i.h) add_public_header(mimir/include/mimir.h) add_public_header(mimir/include/block.h) +add_public_header(siren/include/siren.h) +add_public_header(siren/include/dsp.h) + # ---- Android ---- if(ANDROID) add_subdirectory(kraken_android) @@ -105,6 +108,11 @@ add_subdirectory(hydra) include_directories(hydra/include) list (APPEND EXTRA_LIBS hydra) +# ---- Siren ---- +add_subdirectory(siren) +include_directories(siren/include) +list (APPEND EXTRA_LIBS siren) + # ---- Mimir ---- add_subdirectory(mimir) include_directories(mimir/include) diff --git a/kraken/CMakeLists.txt b/kraken/CMakeLists.txt index b4a4540..b469dfc 100644 --- a/kraken/CMakeLists.txt +++ b/kraken/CMakeLists.txt @@ -33,11 +33,6 @@ add_sources(KRStreamerThread.cpp) add_sources(KRSwapchain.cpp) add_sources(KRContextObject.cpp) add_sources(KRDirectionalLight.cpp) -IF(APPLE) - add_sources(KRDSP_vDSP.cpp) -ELSE() - add_sources(KRDSP_slow.cpp) -ENDIF() add_sources(KRHelpers.cpp) add_sources(KRLight.cpp) add_sources(KRLocator.cpp) diff --git a/kraken/KRAudioManager.cpp b/kraken/KRAudioManager.cpp index 0aa58ca..4e5c141 100755 --- a/kraken/KRAudioManager.cpp +++ b/kraken/KRAudioManager.cpp @@ -36,7 +36,7 @@ #include "KRAudioBuffer.h" #include "KRContext.h" #include "KRCollider.h" -#include "KRDSP.h" +#include "siren.h" using namespace mimir; using namespace hydra; diff --git a/kraken/KRAudioManager.h b/kraken/KRAudioManager.h index 9112501..5781f60 100755 --- a/kraken/KRAudioManager.h +++ b/kraken/KRAudioManager.h @@ -38,7 +38,7 @@ #include "KRContextObject.h" #include "block.h" #include "KRAudioSource.h" -#include "KRDSP.h" +#include "siren.h" const int KRENGINE_AUDIO_MAX_POOL_SIZE = 60; //32; // for Circa we play a maximum of 11 mono audio streams at once + cross fading with ambient diff --git a/kraken/KRAudioSample.cpp b/kraken/KRAudioSample.cpp index e2d40b5..c78e514 100755 --- a/kraken/KRAudioSample.cpp +++ b/kraken/KRAudioSample.cpp @@ -34,9 +34,10 @@ #include "block.h" #include "KRAudioBuffer.h" #include "KRContext.h" -#include "KRDSP.h" +#include "siren.h" using namespace mimir; +using namespace siren; KRAudioSample::KRAudioSample(KRContext& context, std::string name, std::string extension) : KRResource(context, name) { diff --git a/kraken/KRDSP.h b/kraken/KRDSP.h deleted file mode 100644 index 42440a6..0000000 --- a/kraken/KRDSP.h +++ /dev/null @@ -1,95 +0,0 @@ -// -// dsp.h -// Kraken Engine -// -// Copyright 2023 Kearwood Gilbert. All rights reserved. -// -// Redistribution and use in source and binary forms, with or without modification, are -// permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list -// of conditions and the following disclaimer in the documentation and/or other materials -// provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED -// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR -// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those of the -// authors and should not be interpreted as representing official policies, either expressed -// or implied, of Kearwood Gilbert. -// - -#pragma once - -#include "KREngine-common.h" -namespace siren { -namespace dsp { - -#ifdef __APPLE__ -#define KRDSP_APPLE_VDSP -#include -#else -// Slow, but portable fallback implementation -#define KRDSP_SLOW -#endif - -#if defined(KRDSP_APPLE_VDSP) - - // Apple vDSP -typedef DSPSplitComplex SplitComplex; -struct FFTWorkspace -{ - FFTSetup setup; - - void create(size_t length); - void destroy(); - FFTWorkspace(); - ~FFTWorkspace(); -}; - -#elif defined(KRDSP_SLOW) - -typedef struct -{ - float* realp; - float* imagp; -} SplitComplex; - -struct FFTWorkspace -{ - float* sin_table; - float* cos_table; - - void create(size_t length); - void destroy(); - FFTWorkspace(); - ~FFTWorkspace(); -}; - -#else -#error Not Implemented -#endif - -void FFTForward(const FFTWorkspace& workspace, SplitComplex* src, size_t count); -void FFTInverse(const FFTWorkspace& workspace, SplitComplex* src, size_t count); -void Int16ToFloat(const short* src, size_t srcStride, float* dest, size_t destStride, size_t count); -void Scale(float* buffer, float scale, size_t count); -void ScaleCopy(const float* src, float scale, float* dest, size_t count); -void ScaleCopy(const SplitComplex* src, float scale, SplitComplex* dest, size_t count); -void ScaleRamp(float* buffer, float scaleStart, float scaleStep, size_t count); -void Accumulate(float* buffer, size_t bufferStride, const float* buffer2, size_t buffer2Stride, size_t count); -void Accumulate(SplitComplex* buffer, const SplitComplex* buffer2, size_t count); -void Multiply(const SplitComplex* a, const SplitComplex* b, SplitComplex* c, size_t count); - -} // namespace dsp -} // namespace siren diff --git a/kraken/KRDSP_slow.cpp b/kraken/KRDSP_slow.cpp deleted file mode 100644 index e0f5316..0000000 --- a/kraken/KRDSP_slow.cpp +++ /dev/null @@ -1,209 +0,0 @@ -// -// KRDSP_slow.cpp -// Kraken Engine -// -// Copyright 2023 Kearwood Gilbert. All rights reserved. -// -// Redistribution and use in source and binary forms, with or without modification, are -// permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list -// of conditions and the following disclaimer in the documentation and/or other materials -// provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED -// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR -// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those of the -// authors and should not be interpreted as representing official policies, either expressed -// or implied, of Kearwood Gilbert. -// - -#include "KRDSP.h" - -#ifdef KRDSP_SLOW - -#include "KREngine-common.h" - -namespace siren { -namespace dsp { - -FFTWorkspace::FFTWorkspace() -{ - sin_table = nullptr; - cos_table = nullptr; -} - -FFTWorkspace::~FFTWorkspace() -{ - destroy(); -} - -void FFTWorkspace::create(size_t length) -{ - size_t size = (length / 2); - cos_table = new float[size]; - sin_table = new float[size]; - for (int i = 0; i < size / 2; i++) { - float a = 2.0f * (float)M_PI * i / length; - cos_table[i] = cos(a); - sin_table[i] = sin(a); - } -} - -void FFTWorkspace::destroy() -{ - if (sin_table) { - delete sin_table; - sin_table = nullptr; - } - if (cos_table) { - delete cos_table; - cos_table = nullptr; - } -} - -void FFTForward(const FFTWorkspace& workspace, SplitComplex* src, size_t count) -{ - // Radix-2 Decimation in Time FFT Algorithm - // http://en.dsplib.org/content/fft_dec_in_time.html - - // Only power-of-two sizes supported - assert((count & (count - 1)) == 0); - - unsigned int levels = 0; - while (1 << levels <= (int)count) { - levels++; - } - - for (size_t i = 0; i < count; i++) { - size_t j = 0; - for (int k = 0; k < (int)levels; k++) { - j <<= 1; - j |= ((i >> k) & 1); - } - if (j > i) { - float temp = src->realp[i]; - src->realp[i] = src->realp[j]; - src->realp[j] = temp; - temp = src->imagp[i]; - src->imagp[i] = src->imagp[j]; - src->imagp[j] = temp; - } - } - - for (size_t size = 2; size <= count; size *= 2) { - size_t halfsize = size / 2; - size_t step = count / size; - for (size_t i = 0; i < count; i += size) { - for (size_t j = i, k = 0; j < i + halfsize; j++, k += step) { - float temp_real = src->realp[j + halfsize] * workspace.cos_table[k]; - temp_real += src->imagp[j + halfsize] * workspace.sin_table[k]; - float temp_imag = -src->realp[j + halfsize] * workspace.sin_table[k]; - temp_imag += src->imagp[j + halfsize] * workspace.cos_table[k]; - src->realp[j + halfsize] = src->realp[j] - temp_real; - src->imagp[j + halfsize] = src->imagp[j] - temp_imag; - src->realp[j] += temp_real; - src->imagp[j] += temp_imag; - } - } - } -} - -void FFTInverse(const FFTWorkspace& workspace, SplitComplex* src, size_t count) -{ - SplitComplex swapped; - swapped.imagp = src->realp; - swapped.realp = src->imagp; - FFTForward(workspace, &swapped, count); -} - -void Int16ToFloat(const short* src, size_t srcStride, float* dest, size_t destStride, size_t count) -{ - const short* r = src; - float* w = dest; - while (w < dest + destStride * count) { - *w = (float)*r; - r += srcStride; - w += destStride; - } -} - -void Scale(float* buffer, float scale, size_t count) -{ - float* w = buffer; - while (w < buffer + count) { - *w *= scale; - w++; - } -} - -void ScaleCopy(const float* src, float scale, float* dest, size_t count) -{ - const float* r = src; - float* w = dest; - while (w < dest + count) { - *w = *r * scale; - w++; - r++; - } -} - -void ScaleCopy(const SplitComplex* src, float scale, SplitComplex* dest, size_t count) -{ - ScaleCopy(src->realp, scale, dest->realp, count); - ScaleCopy(src->imagp, scale, dest->imagp, count); -} - -void ScaleRamp(float* buffer, float scaleStart, float scaleStep, size_t count) -{ - float* w = buffer; - float s = scaleStart; - while (w < buffer + count) { - *w *= s; - w++; - s += scaleStep; - } -} - -void Accumulate(float* buffer, size_t bufferStride, const float* buffer2, size_t buffer2Stride, size_t count) -{ - float* w = buffer; - const float* r = buffer2; - while (w < buffer + bufferStride * count) { - *w *= *r; - w += bufferStride; - r += buffer2Stride; - } -} - -void Accumulate(SplitComplex* buffer, const SplitComplex* buffer2, size_t count) -{ - for (size_t i = 0; i < count; i++) { - buffer->imagp[i] += buffer2->imagp[i]; - buffer->realp[i] += buffer2->realp[i]; - } -} - -void Multiply(const SplitComplex* a, const SplitComplex* b, SplitComplex* c, size_t count) -{ - for (size_t i = 0; i < count; i++) { - c->realp[i] = a->realp[i] * b->realp[i] - a->imagp[i] * b->imagp[i]; - c->imagp[i] = a->realp[i] * b->imagp[i] + a->imagp[i] * b->realp[i]; - } -} - -} // namespace dsp -} // namespace siren - -#endif // KRDSP_SLOW diff --git a/kraken/KRDSP_vDSP.cpp b/kraken/KRDSP_vDSP.cpp deleted file mode 100644 index 36b159a..0000000 --- a/kraken/KRDSP_vDSP.cpp +++ /dev/null @@ -1,121 +0,0 @@ -// -// KRDSP_vDSP.cpp -// Kraken Engine -// -// Copyright 2022 Kearwood Gilbert. All rights reserved. -// -// Redistribution and use in source and binary forms, with or without modification, are -// permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list -// of conditions and the following disclaimer in the documentation and/or other materials -// provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED -// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR -// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those of the -// authors and should not be interpreted as representing official policies, either expressed -// or implied, of Kearwood Gilbert. -// - -#include "KRDSP.h" - -#ifdef KRDSP_APPLE_VDSP - -#include - -namespace siren { -namespace dsp { - - - -FFTWorkspace::FFTWorkspace() -{ - setup = nullptr; -} - -FFTWorkspace::~FFTWorkspace() -{ - destroy(); -} - -void FFTWorkspace::create(size_t length) -{ - setup = vDSP_create_fftsetup(length, kFFTRadix2); -} - -void FFTWorkspace::destroy() -{ - if (setup) { - vDSP_destroy_fftsetup(setup); - setup = nullptr; - } -} - -void FFTForward(const FFTWorkspace& workspace, SplitComplex* src, size_t count) -{ - vDSP_fft_zip(workspace.setup, src, 1, count, kFFTDirection_Forward); -} - -void FFTInverse(const FFTWorkspace& workspace, SplitComplex* src, size_t count) -{ - vDSP_fft_zip(workspace.setup, src, 1, count, kFFTDirection_Inverse); -} - -void Int16ToFloat(const short* src, size_t srcStride, float* dest, size_t destStride, size_t count) -{ - vDSP_vflt16(src, srcStride, dest, destStride, count); -} - -void Scale(float* buffer, float scale, size_t count) -{ - vDSP_vsmul(buffer, 1, &scale, buffer, 1, count); -} - -void ScaleCopy(const float* src, float scale, float* dest, size_t count) -{ - vDSP_vsmul(src, 1, &scale, dest, 1, count); -} - -void ScaleCopy(const SplitComplex* src, float scale, SplitComplex* dest, size_t count) -{ - ScaleCopy(src->realp, scale, dest->realp, count); - ScaleCopy(src->imagp, scale, dest->imagp, count); -} - -void ScaleRamp(float* buffer, float scaleStart, float scaleStep, size_t count) -{ - vDSP_vrampmul(buffer, 1, &scaleStart, &scaleStep, buffer, 1, count); -} - -void Accumulate(float* buffer, size_t bufferStride, const float* buffer2, size_t buffer2Stride, size_t count) -{ - vDSP_vadd(buffer, bufferStride, buffer2, buffer2Stride, buffer, bufferStride, count); -} - -void Accumulate(SplitComplex* buffer, const SplitComplex* buffer2, size_t count) -{ - vDSP_zvadd(buffer2, 1, buffer, 1, buffer, 1, count); -} - - -void Multiply(const SplitComplex* a, const SplitComplex* b, SplitComplex* c, size_t count) -{ - vDSP_zvmul(a, 1, b, 1, c, 1, count, 1); -} - -} // namespace dsp -} // namespace siren - -#endif // KRDSP_APPLE_VDSP diff --git a/kraken/KREngine-common.h b/kraken/KREngine-common.h index ff5a8b8..20a688b 100755 --- a/kraken/KREngine-common.h +++ b/kraken/KREngine-common.h @@ -41,6 +41,7 @@ using namespace kraken; #include "hydra.h" +#include "siren.h" #include #include diff --git a/siren b/siren new file mode 160000 index 0000000..d5023bc --- /dev/null +++ b/siren @@ -0,0 +1 @@ +Subproject commit d5023bc5faf173eb347b2fd5231a15a354d238ee