forked from chipaudette/OpenAudio_ArduinoLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynth_sin_cos_f32.cpp
More file actions
103 lines (94 loc) · 4.24 KB
/
synth_sin_cos_f32.cpp
File metadata and controls
103 lines (94 loc) · 4.24 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* synth_sin_cos_f32.cpp
*
* SynthSinCos_F32 Bob Larkin April 17, 2020
*
* Based on Chip Audette's OpenAudio sine(), that was
* Modeled on: AudioSynthWaveformSine from Teensy Audio Library
*
* Purpose: Create sine and cosine wave of given amplitude, frequency
* and phase. Outputs in float32_t floating point.
* Routines are from the arm CMSIS library and use a 512 point lookup
* table with linear interpolation to achieve float accuracy limits.
*
* Copyright (c) 2020 Bob Larkin
*
* 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 NONINFRINGEMENT. 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.
*/
#include "synth_sin_cos_f32.h"
// 513 values of the sine wave in a float array:
#include "sinTable512_f32.h"
void AudioSynthSineCosine_F32::update(void) {
audio_block_f32_t *blockS, *blockC;
uint16_t index, i;
float32_t a, b, deltaPhase, phaseC;
blockS = AudioStream_F32::allocate_f32(); // Output blocks
if (!blockS) return;
blockC = AudioStream_F32::allocate_f32();
if (!blockC) {
AudioStream_F32::release(blockS);
return;
}
// doSimple has amplitude (-1, 1) and sin/cos differ by 90.00 degrees.
if (doSimple) {
for (i=0; i < block_length; i++) {
phaseS += phaseIncrement;
if (phaseS > 512.0f)
phaseS -= 512.0f;
index = (uint16_t) phaseS;
deltaPhase = phaseS -(float32_t) index;
/* Read two nearest values of input value from the sin table */
a = sinTable512_f32[index];
b = sinTable512_f32[index+1];
blockS->data[i] = a + 0.001953125*(b-a)*deltaPhase; /* Linear interpolation process */
/* Repeat for cosine by adding 90 degrees phase */
index = (index + 128) & 0x01ff;
/* Read two nearest values of input value from the sin table */
a = sinTable512_f32[index];
b = sinTable512_f32[index+1];
/* deltaPhase will be the same as used for sin */
blockC->data[i] = a + 0.001953125*(b-a)*deltaPhase; /* Linear interpolation process */
}
}
else { // Do a more flexible update, i.e., not doSimple
for (i=0; i < block_length; i++) {
phaseS += phaseIncrement;
if (phaseS > 512.0f) phaseS -= 512.0f;
index = (uint16_t) phaseS;
deltaPhase = phaseS -(float32_t) index;
/* Read two nearest values of input value from the sin table */
a = sinTable512_f32[index];
b = sinTable512_f32[index+1];
blockS->data[i] = amplitude_pk * (a + 0.001953125*(b-a)*deltaPhase); /* Linear interpolation process */
/* Shift forward phaseS_C and get cos. First, the calculation of index of the table */
phaseC = phaseS + phaseS_C;
if (phaseC > 512.0f) phaseC -= 512.0f;
index = (uint16_t) phaseC;
deltaPhase = phaseC -(float32_t) index;
/* Read two nearest values of input value from the sin table */
a = sinTable512_f32[index];
b = sinTable512_f32[index+1];
blockC->data[i] = amplitude_pk * (a + 0.001953125*(b-a)*deltaPhase); /* Linear interpolation process */
}
}
AudioStream_F32::transmit(blockS, 0);
AudioStream_F32::release (blockS);
AudioStream_F32::transmit(blockC, 1);
AudioStream_F32::release (blockC);
return;
}