Developer World
Spresense Arduino Library v3.2.0-77d75a4
RingBuff.h
1/*
2 * RingBuff.h - Ring Buffer for FFT/IIR Filter
3 * Copyright 2019 Sony Semiconductor Solutions Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef _RINGBUFF_H_
21#define _RINGBUFF_H_
22
23#include <stdlib.h>
24
26{
27public:
28 RingBuff(int sample) {
29 int size = sample * sizeof(q15_t);
30 _top = (q15_t*)malloc(size);
31 _bottom = _top + sample;
32 _wptr = _rptr = _top;
33 };
34 ~RingBuff() {
35 free(_top);
36 };
37
38 int put(q15_t *buf, int sample) {
39 if (sample < (_bottom - _wptr)) {
40 arm_copy_q15(buf, _wptr, sample);
41 _wptr += sample;
42 } else {
43 int part = _bottom - _wptr;
44 arm_copy_q15(buf, _wptr, part);
45 arm_copy_q15(&buf[part], _top, sample - part);
46 _wptr = _top + sample - part;
47 }
48 //printf("[%4d+ %4d] (w:0x%08x+ r:0x%08x)\n", stored(), remain(), (uint32_t)_wptr, (uint32_t)_rptr);
49 return sample;
50 };
51
52 int put(q15_t *buf, int sample, int chnum, int ch) {
53 int i;
54 if (sample < (_bottom - _wptr)) {
55 for (i = 0; i < sample; i++) {
56 _wptr[i] = buf[chnum * i + ch];
57 }
58 _wptr += sample;
59 } else {
60 int part = _bottom - _wptr;
61 for (i = 0; i < part; i++) {
62 _wptr[i] = buf[chnum * i + ch];
63 }
64 for (i = part; i < sample; i++) {
65 _top[i - part] = buf[chnum * i + ch];
66 }
67 _wptr = _top + sample - part;
68 }
69 //printf("[%4d+ %4d] (w:0x%08x+ r:0x%08x)\n", stored(), remain(), (uint32_t)_wptr, (uint32_t)_rptr);
70 return sample;
71 };
72
73 int get(float *buf, int sample) {
74 if ((_rptr + sample) < _bottom) {
75 arm_q15_to_float(_rptr, buf, sample);
76 _rptr += sample;
77 } else {
78 int part = _bottom - _rptr;
79 arm_q15_to_float(_rptr, buf, part);
80 arm_q15_to_float(_top, &buf[part], sample - part);
81 _rptr = _top + sample - part;
82 }
83 //printf("[%4d +%4d] (w:0x%08x +r:0x%08x)\n", stored(), remain(), (uint32_t)_wptr, (uint32_t)_rptr);
84 return sample;
85 };
86
87 int get(q15_t *buf, int sample) {
88 if ((_rptr + sample) < _bottom) {
89 arm_copy_q15(_rptr, buf,sample);
90 _rptr += sample;
91 } else {
92 int part = _bottom - _rptr;
93 arm_copy_q15(_rptr, buf,part);
94 arm_copy_q15(_top,&buf[part],sample-part);
95 _rptr = _top + sample - part;
96 }
97 //printf("[%4d +%4d] (w:0x%08x +r:0x%08x)\n", stored(), remain(), (uint32_t)_wptr, (uint32_t)_rptr);
98 return sample;
99 };
100
101 int remain() {
102 return (_bottom - _top) - stored();
103 };
104 int stored() {
105 return (_rptr <= _wptr) ?
106 _wptr - _rptr : (_bottom - _rptr) + (_wptr - _top);
107 };
108
109private:
110 q15_t *_top;
111 q15_t *_bottom;
112 q15_t *_wptr;
113 q15_t *_rptr;
114};
115
116#endif
Definition: RingBuff.h:26