-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfloat_waveform.c
More file actions
180 lines (156 loc) · 5.07 KB
/
float_waveform.c
File metadata and controls
180 lines (156 loc) · 5.07 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Pithesiser - a software synthesiser for Raspberry Pi
// Copyright (C) 2015 Nicholas Tuckett
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
* float_waveform.c
*
* Created on: 17 Feb 2013
* Author: ntuckett
*/
#include "float_waveform.h"
#include <math.h>
#include <stdlib.h>
void float_generate_sine(float_waveform_t *waveform, float sample_rate, float frequency)
{
static float max_phase = 1.0f;
float phase_step_float = max_phase * frequency / sample_rate;
waveform->frequency = frequency;
waveform->phase_step = DOUBLE_TO_FIXED(phase_step_float);
waveform->sample_count = roundf(max_phase / phase_step_float);
waveform->samples = malloc(waveform->sample_count * sizeof(waveform->samples[0]));
float phase;
for (int i = 0; i < waveform->sample_count; i++)
{
phase = i * phase_step_float;
waveform->samples[i] = sinf(phase * M_PI * 2.0f);
}
}
void waveform_float_procedural_sine(float_oscillator_t *osc, float *sample_buffer, int sample_count)
{
float phase_step = osc->frequency / SYSTEM_SAMPLE_RATE;
float amplitude_step = (osc->level - osc->last_level) / sample_count;
float amplitude_scale = osc->last_level;
while (sample_count > 0)
{
while (osc->phase < 0.5f && sample_count > 0)
{
float sample = 1.0f - osc->phase;
sample = 1.0f - (sample * sample);
sample *= amplitude_scale;
*sample_buffer++ = sample;
*sample_buffer++ = sample;
osc->phase += phase_step;
amplitude_scale += amplitude_step;
sample_count--;
}
while (osc->phase < 1.0f && sample_count > 0)
{
float sample = 1.0f - (osc->phase - 0.5f);
sample = (sample * sample) - 1.0f;
sample *= amplitude_scale;
*sample_buffer++ = sample;
*sample_buffer++ = sample;
osc->phase += phase_step;
amplitude_scale += amplitude_step;
sample_count--;
}
if (osc->phase >= 1.0f)
{
osc->phase -= 1.0f;
}
}
}
void waveform_float_procedural_sine_mix(float_oscillator_t *osc, float *sample_buffer, int sample_count)
{
float phase_step = osc->frequency / SYSTEM_SAMPLE_RATE;
float amplitude_step = (osc->level - osc->last_level) / sample_count;
float amplitude_scale = osc->last_level;
while (sample_count > 0)
{
while (osc->phase < 0.5f && sample_count > 0)
{
float sample = 1.0f - osc->phase;
sample = 1.0f - (sample * sample);
sample *= amplitude_scale;
sample += *sample_buffer;
*sample_buffer++ = sample;
*sample_buffer++ = sample;
osc->phase += phase_step;
amplitude_scale += amplitude_step;
sample_count--;
}
while (osc->phase < 1.0f && sample_count > 0)
{
float sample = 1.0f - (osc->phase - 0.5f);
sample = (sample * sample) - 1.0f;
sample *= amplitude_scale;
sample += *sample_buffer;
*sample_buffer++ = sample;
*sample_buffer++ = sample;
osc->phase += phase_step;
amplitude_scale += amplitude_step;
sample_count--;
}
if (osc->phase >= 1.0f)
{
osc->phase -= 1.0f;
}
}
}
void waveform_float_wavetable_sine(float_waveform_t *waveform, float_oscillator_t *osc, float *sample_buffer, int sample_count)
{
fixed_t frequency_ratio = DOUBLE_TO_FIXED(osc->frequency / waveform->frequency);
fixed_t phase_step = fixed_mul(frequency_ratio, waveform->phase_step);
float amplitude_step = (osc->level - osc->last_level) / sample_count;
float amplitude_scale = osc->last_level;
while (sample_count > 0)
{
int sample_index = fixed_mul(osc->phase_fixed, waveform->sample_count);
float sample = waveform->samples[sample_index];
sample *= amplitude_scale;
*sample_buffer++ = sample;
*sample_buffer++ = sample;
osc->phase_fixed += phase_step;
if (osc->phase_fixed >= FIXED_ONE)
{
osc->phase_fixed -= FIXED_ONE;
}
amplitude_scale += amplitude_step;
sample_count--;
}
}
void waveform_float_wavetable_sine_mix(float_waveform_t *waveform, float_oscillator_t *osc, float *sample_buffer, int sample_count)
{
fixed_t frequency_ratio = DOUBLE_TO_FIXED(osc->frequency / waveform->frequency);
fixed_t phase_step = fixed_mul(frequency_ratio, waveform->phase_step);
float amplitude_step = (osc->level - osc->last_level) / sample_count;
float amplitude_scale = osc->last_level;
while (sample_count > 0)
{
int sample_index = fixed_mul(osc->phase_fixed, waveform->sample_count);
float sample = waveform->samples[sample_index];
sample *= amplitude_scale;
sample += *sample_buffer;
*sample_buffer++ = sample;
*sample_buffer++ = sample;
osc->phase_fixed += phase_step;
if (osc->phase_fixed >= FIXED_ONE)
{
osc->phase_fixed -= FIXED_ONE;
}
amplitude_scale += amplitude_step;
sample_count--;
}
}