-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpreamp_audio.h
69 lines (55 loc) · 1.61 KB
/
preamp_audio.h
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
/* Copyright (c) 2018 Peter Teichman */
#ifndef PREAMP_AUDIO_H
#define PREAMP_AUDIO_H
#include <Audio.h>
#include <math.h>
// Simulate the Leslie preamp. I got this from here:
// http://www.willpirkle.com/Downloads/Rotary%20Speaker%20Sim%20App%20Note.pdf
//
// There's a link to a book for the equation, but I haven't read it.
// Preamp implements the preamp distortion of a Leslie speaker cabinet.
class Preamp : public AudioStream {
public:
Preamp() : AudioStream(1, inputQueueArray) {
}
void setK(float k) {
if (k <= 0) {
k = 0.0001;
}
// y(n) = atan(k*x(n)) / atan(k)
float invAtank = 1.0 / atan(k) * (float)(1 << 15);
for (int i = 0; i < 65536; i++) {
float kscale = k / (float)(1 << 15);
lookup[i] = atan((float)(i - 32768) * kscale) * invAtank;
}
}
void update(void) {
audio_block_t *in = receiveReadOnly(0);
if (in == NULL) {
return;
}
audio_block_t *out = allocate();
if (out == NULL) {
release(in);
return;
}
int16_t *src = in->data;
int16_t *dst = out->data;
for (int i = 0; i < AUDIO_BLOCK_SAMPLES; i++) {
int32_t index = (int32_t)(src[i]) + 32768;
if (index < 0) {
index = 0;
} else if (index > 65535) {
index = 65535;
}
dst[i] = lookup[index];
}
transmit(out, 0);
release(out);
release(in);
}
private:
int16_t lookup[65536];
audio_block_t *inputQueueArray[1];
};
#endif