Repository navigation
Expand file tree
/
Copy pathsegmod2.cpp
More file actions
415 lines (351 loc) · 9.39 KB
/
Copy pathsegmod2.cpp
File metadata and controls
415 lines (351 loc) · 9.39 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// SEGMOD (c) 2018 by Luc Döbereiner and Martin Lorenz
// luc.doebereiner@gmail.com
// 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 <https://www.gnu.org/licenses/>.
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <math.h>
#include <cstring>
#include <unistd.h>
#include <sstream>
#include <algorithm>
#ifndef EMSCRIPTEN
#include <sndfile.hh>
#endif
using namespace std;
float freqToSampleLength (float freq, int sampleRate) {
return (float) sampleRate / freq;
}
float freqToPhaseInc (float freq, int sampleRate) {
return freq / (float) sampleRate;
}
int totalLength (std::vector<float> &freqs, int sampleRate) {
float sum = 0.0;
for (auto &f : freqs) {
sum += freqToSampleLength(f, sampleRate);
};
return (int) ceil(sum);
}
float linInterp (float x, float y1, float y2) {
return y1 + ((y2 - y1) * x);
}
float sine(float phase, float phaseOffset) {
return sin((phase + phaseOffset) * 6.283185307);
}
float triangle(float phase, float phaseOffset) {
float ph = fmod(phase + phaseOffset, 1.0);
float result;
if (ph <= 0.25) {
result = linInterp(ph / 0.25, 0.0, 1.0);
} else if (ph <= 0.75) {
result =linInterp((ph - 0.25) / 0.5, 1.0, -1.0);
} else {
result = linInterp((ph - 0.75) / 0.25, -1.0, 0.0);
}
return result;
}
float pulse(float phase, float phaseOffset) {
float ph = phase + phaseOffset;
if (ph < 0.5) {
return 1.0;
}
else {
return -1.0;
}
}
bool isNumber(const std::string &s) {
return !s.empty() && all_of(s.begin(), s.end(), ::isdigit);
}
float sawtooth(float phase, float phaseOffset) {
float ph = fmod(phase + phaseOffset, 1.0);
return linInterp(ph, 1.0, -1.0);
}
// #ifdef EMSCRIPTEN
// using sample_t = int;
// #else
// using sample_t = float;
// #endif
void synthesize(float * snd,
unsigned int sndLength,
vector<float> &freqs,
vector<int> &waves,
bool staticWave,
bool startWave,
float phaseOffset,
int sampleRate,
int breakpointPosition) {
float freq;
int tableIndex;
float tableFreq;
int wave;
float curPhase = 0.0;
unsigned int curFreq = 0;
float lastPhase = 0.0;
float curPhaseInc;
int curWave = startWave;
float thisSample = 0.0;
//printf("sndLenght %d\n",sndLength);
curPhaseInc = freqToPhaseInc(freqs[curFreq], sampleRate);
if (!staticWave) {
curWave = waves[(int) curFreq % (int) waves.size()];
};
for (unsigned int i = 0; i < sndLength; i++) {
if ((curPhase >= 1.0) || ((breakpointPosition == 0) && (curPhase >= 0.5) && (lastPhase < 0.5))) {
curFreq++;
curPhase = fmod(curPhase, 1.0);
curPhaseInc = freqToPhaseInc(freqs[curFreq], sampleRate);
lastPhase = curPhase;
if (!staticWave) {
curWave = waves[curFreq % waves.size()];
}
}
switch(curWave) {
case 0: thisSample = sine(curPhase, phaseOffset);
break;
case 1: thisSample = triangle(curPhase, phaseOffset);
break;
case 2: thisSample = pulse(curPhase, phaseOffset);
break;
case 3: thisSample = sawtooth(curPhase, phaseOffset);
break;
case 4: thisSample = 0.0;
break;
default:
printf("wrong waveform argument\n");
break;
};
snd[i] = thisSample;
curPhase += curPhaseInc;
}
return;
}
#ifndef EMSCRIPTEN
void gen_cli(int argc, char *argv[]) {
float * snd;
ifstream freqFile;
ifstream waveFile;
ifstream sampleDurFile;
ifstream tableFile;
vector<float> freqs;
vector<float> freqTable;
vector<int> freqIndices;
vector<int> waves;
SndfileHandle file;
int sampleRate = 44100;
float phaseOffset = 0.0;
int breakpointPosition = 0;
unsigned int sndLength;
bool staticWave = true;
bool tableInput = false;
string outputFile = "out.wav";
int curWave = 0;
float freq;
int wave;
int tableIndex;
float tableFreq;
int opt;
while ((opt = getopt(argc, argv, "s:w:o:f:p:b:i:")) != -1) {
switch (opt) {
case 's':
sampleRate = atoi(optarg);
break;
case 'w':
if (isNumber(optarg)) {
staticWave = true;
curWave = atoi(optarg);
} else {
waveFile.open(optarg);
staticWave = false;
if (!waveFile.good()) {
fprintf(stderr, "%s could not be opened.", optarg);
exit(EXIT_FAILURE);
};
};
break;
case 'f':
freqFile.open(optarg);
if (!freqFile.good()) {
fprintf(stderr, "%s could not be opened.", optarg);
exit(EXIT_FAILURE);
};
break;
case 'i':
tableFile.open(optarg);
if (!tableFile.good()) {
fprintf(stderr, "%s could not be opened.", optarg);
exit(EXIT_FAILURE);
};
tableInput = true;
break;
case 'p':
phaseOffset = atof(optarg);
break;
case 'b':
breakpointPosition = atoi(optarg);
break;
case 'o':
outputFile = optarg;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-s samplerate] [-w waveform file/static waveform] [-o output] [-p phase offset] [-b breakpoints per cycle (0 or 1)] [-f frequencies file] [-i index format input file]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if ((freqFile.is_open()) && (!tableInput)) {
string line;
while (getline(freqFile, line)) {
int pos = line.find('#');
if (pos != (int) string::npos) {
line.erase(pos, -1);
};
istringstream iss(line);
while (iss >> freq) {
freqs.push_back(freq);
}
}
freqFile.close();
}
if (waveFile.is_open()) {
string line;
while (getline(waveFile, line)) {
int pos = line.find('#');
if (pos != (int) string::npos) {
line.erase(pos, -1);
};
istringstream iss(line);
while (iss >> wave) {
waves.push_back(wave);
}
}
waveFile.close();
}
if (sampleDurFile.is_open()) {
string line;
while (getline(waveFile, line)) {
int pos = line.find('#');
if (pos != (int) string::npos) {
line.erase(pos, -1);
};
istringstream iss(line);
while (iss >> freq) {
freqs.push_back(freq);
}
}
}
if (tableFile.is_open()) {
string line;
bool first = true;
while (getline(tableFile, line)) {
int pos = line.find('#');
if (pos != (int) string::npos) {
line.erase(pos, -1);
};
if (line.size() > 0) {
istringstream iss(line);
if (first) {
while (iss >> tableFreq) {
freqTable.push_back(tableFreq);
}
first = false;
} else {
while (iss >> tableIndex) {
freqIndices.push_back(tableIndex);
}
}
}
}
waveFile.close();
}
if (tableInput) {
for (auto &idx : freqIndices) {
freqs.push_back(freqTable[idx-1]);
}
}
// int flent = static_cast<int>(freqs.size());
//printf("freqs.length %d\n", flent);
if (breakpointPosition == 0) {
sndLength = (totalLength(freqs, sampleRate) / 2) + 1;
} else {
sndLength = totalLength(freqs, sampleRate);
}
snd = new float [sndLength];
synthesize(snd, sndLength, freqs, waves, staticWave, curWave, phaseOffset, sampleRate, breakpointPosition);
file = SndfileHandle(outputFile, SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_FLOAT, 1, sampleRate) ;
file.write(snd, sndLength);
}
#endif
vector<int> gen_wasm(string iFreqs, string seqs, string waveforms) {
float * snd;
vector<int> snd_int;
int breakpointPosition = 0;
int sampleRate = 44100;
vector<float> freqs;
vector<float> freqTable;
vector<int> freqIndices;
vector<int> waves;
bool staticWave = false;
unsigned int sndLength;
int curWave = 0;
float phaseOffset = 0.0;
int wave;
int tableIndex;
float tableFreq;
istringstream iss(iFreqs.c_str());
while (iss >> tableFreq) {
freqTable.push_back(tableFreq);
}
istringstream isr(seqs.c_str());
while (isr >> tableIndex) {
freqIndices.push_back(tableIndex);
}
istringstream isw(waveforms.c_str());
while (isw >> wave) {
waves.push_back(wave);
}
// TODO: allow for non table input (direct list of freqs)
if (true) {
for (auto &idx : freqIndices) {
freqs.push_back(freqTable[idx-1]);
}
}
// int flent = static_cast<int>(freqs.size());
//printf("freqs.length %d\n", flent);
if (breakpointPosition == 0) {
sndLength = (totalLength(freqs, sampleRate) / 2) + 1;
} else {
sndLength = totalLength(freqs, sampleRate);
}
snd = new float [sndLength];
synthesize(snd, sndLength, freqs, waves, staticWave, curWave, phaseOffset, sampleRate, breakpointPosition);
for (int i; i < sndLength; i++) {
int thisSampleInt = (((int) (snd[i] * 32768.5)) - 0.5);
snd_int.push_back(thisSampleInt);
}
return snd_int;
}
int main(int argc, char *argv[]) {
#ifndef EMSCRIPTEN
gen_cli(argc, argv);
#else
gen_wasm("200 300 400 500 600 700", "1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6", "1 2 3");
#endif
}
#ifdef EMSCRIPTEN
#include <emscripten.h>
#include <emscripten/bind.h>
using namespace emscripten;
EMSCRIPTEN_BINDINGS(my_module) {
register_vector<int>("vector<int>");
emscripten::function("gen", &gen_wasm);
}
#endif