-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiveReverse.hpp
More file actions
69 lines (57 loc) · 1.56 KB
/
LiveReverse.hpp
File metadata and controls
69 lines (57 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// LiveReverse.hpp
// LiveReverse
//
// Created by Ragnar Hrafnkelsson on 01/12/2016.
// Copyright © 2016 Reactify. All rights reserved.
//
#pragma once
#include <stdlib.h>
#include <assert.h>
#include <vector>
static const int MAX_BUFFER_SIZE = 352800;
template <typename T>
class LiveReverse {
public:
//!
LiveReverse( int channels, int windowSize ) : channels( channels ) {
bInternal = (T **)malloc( channels * sizeof(T*) );
for ( int i = 0; i < channels; ++i ) {
bInternal[i] = (T *)calloc( MAX_BUFFER_SIZE, sizeof(T) );
}
setWindowSize( windowSize );
writePos = windowSize; // Start writing one window ahead
}
//!
~LiveReverse() {
for ( int i = 0; i < channels; ++i ) {
free( bInternal[i] );
}
free( bInternal );
}
//!
void setWindowSize( int windowSize ) {
bufferSize = windowSize * 2; // We use two windows in one buffer
assert( bufferSize <= MAX_BUFFER_SIZE );
}
//!
inline void process( T **const bIn, T **const bOut, int size ) {
for ( int j = 0; j < size; ++j ) {
// Read position is one window behind write position
// and we read from back to front ( reverse )
int readPos = ( bufferSize - writePos ) - 1;
// Fill buffers
for ( int i = 0; i < channels; ++i ) {
bInternal[i][writePos] = bIn[i][j];
bOut[i][j] = bInternal[i][ readPos ];
}
// Advance write position
writePos = ( writePos + 1 ) % bufferSize;
}
}
private:
int channels;
int writePos;
int bufferSize;
T** bInternal;
};