From 9fedd040b328cb71cd16b8d81e75de36e64a567f Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Mon, 27 Oct 2025 17:58:03 +0100 Subject: [PATCH] added BufferView --- delay/buffer_view.hpp | 65 +++++++++++++++++++++++++++++++++++++ delay/tests/test_buffer.cpp | 27 ++++++++++----- 2 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 delay/buffer_view.hpp diff --git a/delay/buffer_view.hpp b/delay/buffer_view.hpp new file mode 100644 index 0000000..13839ac --- /dev/null +++ b/delay/buffer_view.hpp @@ -0,0 +1,65 @@ +/* + * 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: dsp_ringbuffer.hpp + * Author Jens Ahrensfeld on 26.10.2025. + */ + +#ifndef DSP_BUFFER_VIEW_H +#define DSP_BUFFER_VIEW_H + +#include "buffer.hpp" + + +namespace dsp { + +template +class BufferView +{ + Buffer &m_buffer; + uint m_offset; + + public: + BufferView(Buffer &buffer, int offset) + : m_buffer(buffer) + , m_offset(offset) + { + } + + T & operator[](int offset) + { + return m_buffer[m_offset + offset]; + } + + const T & operator[](int offset) const + { + return m_buffer[m_offset + offset]; + } + + size_t size() + { + return m_buffer.size(); + } + +}; +} + +#endif // DSP_BUFFER_VIEW_H diff --git a/delay/tests/test_buffer.cpp b/delay/tests/test_buffer.cpp index bb5347d..20a42f6 100644 --- a/delay/tests/test_buffer.cpp +++ b/delay/tests/test_buffer.cpp @@ -1,16 +1,22 @@ -#include #include +#include #include -#include #include -void print(std::ranges::input_range auto&& range) +template +void print(dsp::BufferView &v, size_t size) { - for(const auto&i : range) +// for(const auto&i : v) +// { +// std::cout << i << std::endl; +// } + std::cout << "----------------------------------------------" << std::endl; + for (int i=0; i < size; i++) { - std::cout << i << std::endl; + std::cout << v[i] << std::endl; } + } int main() @@ -29,9 +35,14 @@ int main() printf("i=%d: v=%d\n", i, v); } - std::vector numbers{1, 2, 3, 4, 5}; - auto v{std::views::take(numbers, 3)}; - print<>(v); + auto v1 = dsp::BufferView(buffer, 0); + auto v2 = dsp::BufferView(buffer, 5); + + buffer.reset(); + v1[5] = 55; + print(v1, 10); + v2[2] = 77; + print(v2,10); return 0; } \ No newline at end of file