diff --git a/src/LibBasicFunctions.cpp b/src/LibBasicFunctions.cpp index 25e3178..43bc0c2 100644 --- a/src/LibBasicFunctions.cpp +++ b/src/LibBasicFunctions.cpp @@ -9,6 +9,55 @@ namespace BAGuitar { +AudioDelay::AudioDelay(MemType type, size_t maxSamples) +{ + m_type = type; + if (type == MemType::INTERNAL) { + // INTERNAL memory consisting of audio_block_t data structures. + QueuePosition pos = calcQueuePosition(maxSamples); + m_ringBuffer = new RingBuffer(pos.index+2); // If the delay is in queue x, we need to overflow into x+1, thus x+2 total buffers. + } else { + // TODO EXTERNAL memory + + } +} + +AudioDelay::AudioDelay(MemType type, float delayTimeMs) +: AudioDelay(type, calcAudioSamples(delayTimeMs)) +{ + +} + +AudioDelay::~AudioDelay() +{ + if (m_ringBuffer) delete m_ringBuffer; +} + +bool AudioDelay::addBlock(audio_block_t *block) +{ + if (m_type != MemType::INTERNAL) return false; // ERROR + + // purposefully don't check if block is valid, the ringBuffer can support nullptrs + if ( m_ringBuffer->size() < m_ringBuffer->max_size() ) { + // pop before adding + release(m_ringBuffer->front()); + m_ringBuffer->pop_front(); + } + + // add the new buffer + m_ringBuffer->push_back(block); + +} + +void AudioDelay::getSamples(audio_block_t *dest, size_t offset, size_t numSamples = AUDIO_BLOCK_SAMPLES) +{ + QueuePosition pos = calcQueuePosition(offset); + size_t readOffset = pos.offset; + size_t index = pos.index; + + // Audio is stored in reverse order. That means the first sample (in time) goes in the last location in the audio block. + +} } diff --git a/src/LibBasicFunctions.h b/src/LibBasicFunctions.h index 386fabf..7ba0f5c 100644 --- a/src/LibBasicFunctions.h +++ b/src/LibBasicFunctions.h @@ -9,15 +9,39 @@ #include #include "Arduino.h" +#include "Audio.H" +#include "LibMemoryManagement.h" #ifndef SRC_LIBBASICFUNCTIONS_H_ #define SRC_LIBBASICFUNCTIONS_H_ namespace BAGuitar { +class RingBuffer; // forward declare + +enum MemType : unsigned { + INTERNAL, + EXTERNAL +}; + struct INTERNAL_MEMORY {}; struct EXTERNAL_MEMORY {}; +class AudioDelay { + + AudioDelay() = delete; + AudioDelay(MemType type, size_t maxSamples); + AudioDelay(MemType type, float delayTimeMs); + ~AudioDelay(); + + void addBlock(audio_block_t *block); + + void getSamples(size_t offset, size_t numSamples); +private: + MemType m_type; + RingBuffer *m_ringBuffer = nullptr; +}; + template class RingBuffer { public: @@ -74,6 +98,10 @@ public: return size; } + size_t max_size() const { + return m_maxSize; + } + T& operator[] (size_t index) { return m_buffer[index]; } diff --git a/src/LibMemoryManagement.h b/src/LibMemoryManagement.h index 118fbfd..e2ab3bc 100644 --- a/src/LibMemoryManagement.h +++ b/src/LibMemoryManagement.h @@ -15,7 +15,7 @@ #include "BAHardware.h" #include "BASpiMemory.h" -#include "LibBasicFunctions.h" +//#include "LibBasicFunctions.h" namespace BAGuitar {