-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynth.c
170 lines (147 loc) · 5.64 KB
/
synth.c
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
/*
* synth.c ~ speech synthesis toy project
*
* Copyright (c) 2016, Vlad Dumitru <[email protected]>
*
* 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 <stdio.h>
#include <math.h>
#include "audiobuffer.h"
#include "genetic.h"
#include "synth.h"
/*
* generate_base_speech_signal() -- creates a buffer of `frame_count' frames,
* containing a 25% width pulse wave of `frequency' Hz.
* @arg {float} frequency -- the frequency of the signal;
* @arg {unsigned int} frame_count -- buffer size;
* @return {struct audio_buffer *} -- the filled audio buffer structure.
*/
struct audio_buffer *generate_base_speech_signal(float frequency,
unsigned int frame_count)
{
unsigned int i;
unsigned int period = SAMPLE_RATE / frequency;
struct audio_buffer *buf = alloc_buffer(frame_count);
for (i = 0; i < frame_count; i++) {
buf->data[i] = (float)((i % period) < (period / 4)) - 0.5f;
}
return buf;
}
/*
* process_formant_filter() -- processes a given audio buffer structure,
* passing it through two two-pole low pass filters, of frequencies `f1'
* and `f2';
*
* code was partially taken from:
* http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
* Author: Robert Bristow-Johnson <[email protected]>
*
* @arg {struct audio_buffer *} buf -- the source audio buffer structure;
* @arg {float} f1 -- first formant frequency;
* @arg {float} f2 -- second formant frequency;
* @arg {unsigned int} start_frame -- first frame to be processed;
* @arg {unsigned int} end_frame -- last frame to be processed;
* @return {void}.
*/
void process_formant_filter(struct audio_buffer *buf, float f1, float f2,
unsigned int start_frame, unsigned int end_frame)
{
unsigned int i;
float y0 = 0.0f, /* current value of the filter's output */
y1 = 0.0f, /* previous value of the filter's output */
y2 = 0.0f; /* second previous value of the filter's output */
float w0 = 0.0f; /* the filter's angular frequency */
float alpha = 0.0f; /* intermediate parameter; see below */
float a0 = 0.0f, /* filter parameters */
a1 = 0.0f,
a2 = 0.0f,
b0 = 0.0f;
/* first pass -- formant f1 */
w0 = 2.0 * M_PI * (f1 / (float)SAMPLE_RATE);
alpha = sinf(w0) * 0.1f;
b0 = (1.0f - cosf(w0)) / 2.0f;
a0 = 1.0f + alpha;
a1 = -2.0 * cosf(w0);
a2 = 1.0f - alpha;
printf(" w0 = %f\n", w0);
printf("alpha = %f\n", alpha);
printf(" b0 = %f\n", b0);
printf(" a0 = %f\n", a0);
printf(" a1 = %f\n", a1);
printf(" a2 = %f\n", a2);
printf("b0/a0 = %f\n", b0 / a0);
printf("a1/a0 = %f\n", a1 / a0);
printf("a2/a0 = %f\n", a2 / a0);
printf("-----\n");
for (i = start_frame; i < end_frame; i++) {
y0 = (b0 / a0) * buf->data[i]
- (a1 / a0) * y1
- (a2 / a0) * y2;
y2 = y1;
y1 = y0;
buf->data[i] = y0;
}
/* second pass -- formant f2 */
w0 = 2.0 * M_PI * (f2 / (float)SAMPLE_RATE);
alpha = sinf(w0) * 0.001f;
b0 = (1.0f - cosf(w0)) / 2.0f;
a0 = 1.0f + alpha;
a1 = -2.0 * cosf(w0);
a2 = 1.0f - alpha;
for (i = start_frame; i < end_frame; i++) {
y0 = (b0 / a0) * buf->data[i]
- (a1 / a0) * y1
- (a2 / a0) * y2;
y2 = y1;
y1 = y0;
buf->data[i] = y0;
}
}
/*
* process_filter_from_phenotype() -- passws a given audio buffer through a
* filter whose coefficients are taken from a given phenotype;
* @arg {struct phenotype *} phenotype -- the phenotype from which to take the
* filter coefficients;
* @arg {struct audio_buffer *} buf -- the audio buffer to be processed;
* @arg {unsigned int} start_frame -- the frame index from which the
* processing begins;
* @arg {unsigned int} end_frame -- the last frame index to be processed;
* @return {void}.
*/
void process_filter_from_phenotype(struct phenotype *p,
struct audio_buffer *buf,
unsigned int start_frame,
unsigned int end_frame)
{
unsigned int i, j, k;
float memory[PHENOTYPE_CHROMOSOME_COUNT] = {0.0f};
float temp = 0.0f;
for (i = start_frame; i < end_frame; i++) {
temp = p->coefficient[1] * buf->data[i];
for (j = 2; j < PHENOTYPE_CHROMOSOME_COUNT; j++) {
temp += p->coefficient[j] * memory[j];
buf->data[i] = temp;
for (k = PHENOTYPE_CHROMOSOME_COUNT; k > 0; k--) {
memory[k] = memory[k - 1];
}
memory[0] = temp;
}
}
}