From 09591a3148ca055ecb9a27289956868a53692b59 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sat, 15 Nov 2025 15:38:20 +0100 Subject: [PATCH] - added Delay - added MultiDelay - added Diffusor --- delay/delay.hpp | 67 +++++++++++++++++++++++ delay/multiDelay.hpp | 105 ++++++++++++++++++++++++++++++++++++ reverb/diffusor.hpp | 126 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 298 insertions(+) create mode 100644 delay/delay.hpp create mode 100644 delay/multiDelay.hpp create mode 100644 reverb/diffusor.hpp diff --git a/delay/delay.hpp b/delay/delay.hpp new file mode 100644 index 0000000..b1601d3 --- /dev/null +++ b/delay/delay.hpp @@ -0,0 +1,67 @@ +/* + * jayDSP, A cross-platform Digital Signal Processing library written in modern C++. + * Copyright (C) 2025 Jens Ahrensfeld, All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + * File: delay.hpp + * Author Jens Ahrensfeld on 26.10.2025. + */ + +#ifndef DSP_DELAY_H +#define DSP_DELAY_H + +#include + +template +class Delay : public dsp::Buffer +{ + int m_delay; + + public: + Delay(uint delayCapacity) + : dsp::Buffer(delayCapacity) + , m_delay(0) + { + } + + void setRandom(uint min, uint max) + { + float span = (float)max - (float)min; + float z = (float)rand()/(float)RAND_MAX; + int delay = (int)(min + z*span); + m_delay = -delay; + + } + + void setDelay(uint delay) + { + m_delay = -((int)delay); + } + + T process(T in) + { + (*this)[0] = in; + T out = (*this)[m_delay]; + ++(*this); + + return out; + } +}; + +#endif // DSP_DELAY_H \ No newline at end of file diff --git a/delay/multiDelay.hpp b/delay/multiDelay.hpp new file mode 100644 index 0000000..d352f2c --- /dev/null +++ b/delay/multiDelay.hpp @@ -0,0 +1,105 @@ +/* + * jayDSP, A cross-platform Digital Signal Processing library written in modern C++. + * Copyright (C) 2025 Jens Ahrensfeld, All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + * File: delay.hpp + * Author Jens Ahrensfeld on 26.10.2025. + */ + +#ifndef DSP_MULTIDELAY_H +#define DSP_MULTIDELAY_H + +#include + +#include + +template +class MultiDelay +{ + dsp::BufferMulti m_buf; + int m_delay_spec[numCh]; + uint m_delaySize; + + void print_delays() + { + std::cout << "delays" << std::endl; + for (int i=0; i < numCh; i++) + { + std::cout << m_delay_spec[i] << " "; + } + std::cout << std::endl; + } + public: + MultiDelay(uint delayCapacity, int delayOffset) + : m_buf(delayCapacity) + { + setDelay(delayCapacity, delayOffset); + } + + void setDelay(uint delaySize, int delayOffset) + { + float delayRagePerWindow = (float)(delaySize-delayOffset)/(float)numCh; + + if (delayRagePerWindow < 0) + { + return; + } + + // Delay spec + for (int i=0; i < numCh; i++) + { + float base_delay = i*delayRagePerWindow + delayOffset; + float z = (float)rand()/(float)RAND_MAX; + int delay = (int)(base_delay + z*delayRagePerWindow); + + m_delay_spec[i] = -delay; + } + m_delaySize = delaySize; + print_delays(); + } + + uint getDelay() + { + return m_delaySize; + } + + void process(T in, T out[numCh]) + { + m_buf = in; + m_buf.quer(out, m_delay_spec); + ++m_buf; + } + + void process(T data[numCh]) + { + m_buf = data; + m_buf.quer(data, m_delay_spec); + ++m_buf; + } + + void process(T in[numCh], T out[numCh]) + { + m_buf = in; + m_buf.quer(out, m_delay_spec); + ++m_buf; + } +}; + +#endif // DSP_MULTIDELAY_H \ No newline at end of file diff --git a/reverb/diffusor.hpp b/reverb/diffusor.hpp new file mode 100644 index 0000000..34675f0 --- /dev/null +++ b/reverb/diffusor.hpp @@ -0,0 +1,126 @@ +/* + * jayDSP, A cross-platform Digital Signal Processing library written in modern C++. + * Copyright (C) 2025 Jens Ahrensfeld, All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + * File: diffusor.hpp + * Author Jens Ahrensfeld on 26.10.2025. + */ + +#ifndef DSP_DIFFUSOR_H +#define DSP_DIFFUSOR_H + +#include "delay/multiDelay.hpp" +#include + +template +class Diffusor +{ + MultiDelay m_dly; + int m_shuffle_spec[numCh]; + + void print_shuffle() + { + std::cout << "shuffle" << std::endl; + for (int i=0; i < numCh; i++) + { + std::cout << m_shuffle_spec[i] << " "; + } + std::cout << std::endl; + } + + void shuffle_init(double pn) + { + // Shuffle spec + for (int i=0; i < numCh; i++) + { + m_shuffle_spec[i] = i; + } + + // Shuffle values + for (int i=0; i < 2*numCh; i++) + { + int i1 = rand() & (numCh-1); + int i2 = rand() & (numCh-1); + int temp = m_shuffle_spec[i1]; + m_shuffle_spec[i1] = m_shuffle_spec[i2]; + m_shuffle_spec[i2] = temp; + } + + // Shuffle signs + for (int i=0; i < numCh; i++) + { + double z = (double)rand()/(double)RAND_MAX; + if (z < pn) + { + m_shuffle_spec[i] *= (T)-1; + } + } + + print_shuffle(); + } + public: + Diffusor(uint maxDelaySize, double pn=0.5) + : m_dly(maxDelaySize, 0) + { + shuffle_init(pn); + } + + void setDelay(uint delay) + { + m_dly.setDelay(delay, 0); + } + + uint getDelay() + { + return m_dly.getDelay(); + } + + inline T process(T in) + { + T temp[numCh]; + m_dly.process(in, temp); + + Shuffle::inPlace(temp, m_shuffle_spec); + Hadamard::inPlace(temp); + Householder::inPlace(temp); + + return temp[0]; + } + + // In-place + inline void process_multi(T in_out[numCh]) + { + m_dly.process(in_out); + + Shuffle::inPlace(in_out, m_shuffle_spec); + Hadamard::inPlace(in_out); + } + + // Out-of-place + inline void process_multi(T in[numCh], T out[numCh]) + { + m_dly.process(in, out); + + Shuffle::outOfPlace(in, out, m_shuffle_spec); + Hadamard::inPlace(out); + } +}; + +#endif // DSP_DIFFUSOR_H \ No newline at end of file