- added Delay
- added MultiDelay - added Diffusor
This commit is contained in:
@@ -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 <delay/buffer.hpp>
|
||||
|
||||
template<typename T>
|
||||
class Delay : public dsp::Buffer<T>
|
||||
{
|
||||
int m_delay;
|
||||
|
||||
public:
|
||||
Delay(uint delayCapacity)
|
||||
: dsp::Buffer<T>(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
|
||||
@@ -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 <iostream>
|
||||
|
||||
#include <delay/buffer_multi.hpp>
|
||||
|
||||
template<typename T, size_t numCh>
|
||||
class MultiDelay
|
||||
{
|
||||
dsp::BufferMulti<T, numCh> 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
|
||||
@@ -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 <common/mix.hpp>
|
||||
|
||||
template<typename T, size_t numCh>
|
||||
class Diffusor
|
||||
{
|
||||
MultiDelay<T, numCh> 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<T, numCh>::inPlace(temp, m_shuffle_spec);
|
||||
Hadamard<T, numCh>::inPlace(temp);
|
||||
Householder<T, numCh>::inPlace(temp);
|
||||
|
||||
return temp[0];
|
||||
}
|
||||
|
||||
// In-place
|
||||
inline void process_multi(T in_out[numCh])
|
||||
{
|
||||
m_dly.process(in_out);
|
||||
|
||||
Shuffle<T, numCh>::inPlace(in_out, m_shuffle_spec);
|
||||
Hadamard<T, numCh>::inPlace(in_out);
|
||||
}
|
||||
|
||||
// Out-of-place
|
||||
inline void process_multi(T in[numCh], T out[numCh])
|
||||
{
|
||||
m_dly.process(in, out);
|
||||
|
||||
Shuffle<T, numCh>::outOfPlace(in, out, m_shuffle_spec);
|
||||
Hadamard<T, numCh>::inPlace(out);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // DSP_DIFFUSOR_H
|
||||
Reference in New Issue
Block a user