-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsndtest.nt
307 lines (301 loc) · 8.96 KB
/
sndtest.nt
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
module sndtest;
import std.sort, std.math, std.sound, std.file, std.string;
void cfgNote(Note res, float freq, float len) {
res.freq = freq;
using res {
maxVolume = 0.2;
sustain = 0.1;
decayStart = len * 0.05;
sustainStart = len * 0.3;
releaseStart = len * 0.7;
end = len;
}
}
void main(string[] args) {
SoundOutput snd;
if (args.length && args[0] == "-oss") {
args = args[1 .. $];
snd = new OSSOutput("/dev/dsp");
} else if (args.length && args[0] == "-wav") {
args = args[1 .. $];
snd = new WAVCreator "out.wav";
} else
snd = new AlsaOutput("default");
snd.open();
float delegate(float) dg;
(Note, float)[] notelist;
void addNoteAt(float start, float freq, float length, bool useSine = false) {
// writeln "addNoteAt($start, $freq, $length)";
(Note, float)[auto~] res;
for int i <- 0..notelist.length res ~= notelist[i];
Note n;
if (useSine) n = new SineNote;
else n = new KarplusStrongNote;
cfgNote(n, freq, length);
res ~= (n, start);
notelist = res[];
}
void addNoteAtRef(float* pp, float freq, float len, bool useSine = false) {
alias pos = *pp;
// writeln "$pos: $freq for $len";
addNoteAt(pos, freq, len, useSine);
pos += len;
}
void clearList(float pos) {
(Note, float)[auto~] res;
for int i <- 0..notelist.length {
alias n = notelist[i];
if pos < (n[1] + n[0].end) res ~= n;
}
notelist = res[];
}
void sortList() { qsort(\((Note, float) a, b) -> a[1] < b[1], notelist); }
void addTrack(string notes) {
float offs = 0;
// With a frequency around 261.626 Hz, middle C is designated C4
float baseFreq = 261.626;
float len = 0.3705;
int octaves; // int so it doesn't get fucked up by repeated mul/div
int lenscale = 4;
bool useSine;
int note;
float note-len;
bool note-set;
bool note-sharp, note-flat;
int note-octave;
void resetNote() {
note-set = false;
note-len = 1f/lenscale;
note-sharp = false;
note-flat = false;
note-octave = octaves;
}
float x 14 scale;
for int i <- 0..14
scale[i] = pow(pow(2, 1/12f), i - 1);
void flushNote() {
if (!note-set) return;
if (note == -1) {
offs += note-len;
} else {
// writeln "add $baseFreq * 2^$(note-octave) = $(pow(2, note-octave)) * 12root2 ^ $(note + note-sharp - note-flat) = $(scale[note+note-sharp-note-flat+1]) for $note-len";
addNoteAtRef(&offs, baseFreq * pow(2, note-octave) * scale[note + note-sharp - note-flat + 1], note-len, useSine);
}
resetNote;
}
resetNote;
while true {
notes.strip;
if (!notes.length) break;
if auto rest = notes.startsWith "t" {
(string tempo, notes) = rest.slice " ";
len = (60 / tempo.atof()) / 4;
continue;
}
if auto rest = notes.startsWith "l" {
(string len, notes) = rest.slice " ";
lenscale = len.atoi();
continue;
}
if auto rest = notes.startsWith "o" {
(string oct, notes) = rest.slice " ";
octaves = oct.atoi() - 4;
continue;
}
bool hit;
for auto tup <- [("r", -1), ("c", 0), ("d", 2), ("e", 4), ("f", 5), ("g", 7), ("a", 9), ("b", 10), ("h", 11)] {
if auto rest = notes.startsWith tup[0] {
notes = rest;
flushNote;
note = tup[1];
note-set = true;
note-octave = octaves;
hit = true;
}
}
if (hit) continue;
alias notes-start-with-digit = notes[0] >= "0" && notes[0] <= "9";
if (notes-start-with-digit) {
int num = notes[0] - "0";
notes = notes[1..$];
while notes.length && notes-start-with-digit {
num = num * 10 + (notes[0] - "0");
notes = notes[1..$];
}
note-len = 1f / num;
continue;
}
auto cur = notes[0];
notes = notes[1..$];
if (cur == ".") {
note-len *= 1.5;
continue;
}
if (cur == "#" || cur == "+") {
note-sharp = true;
continue;
}
if (cur == "-") {
note-flat = true;
continue;
}
if (cur == ">") {
octaves ++;
continue;
}
if (cur == "<") {
octaves --;
continue;
}
if (cur == " ") continue;
writeln "unknown cmd '$cur' at $notes";
exit(1);
}
flushNote;
}
void addMidiTrack(string fn) using fn.readAll() {
void step(int i) {
that = that[i .. $];
}
short getShort() { onSuccess step 2; return short:$ _0 << 8 + _1; }
if (that[0..4] != "MThd")
raise new Error "Not a midi file: invalid header code: $(that[0..4])";
step 8;
auto fileFmt = getShort;
if fileFmt != 0 && fileFmt != 1
raise new Error "Can only parse mode-0 or mode-1 files, not $fileFmt. ";
writeln "Format $fileFmt";
auto tracks = getShort;
writeln "$tracks tracks.";
auto tickrate = getShort;
writeln "tick rate $tickrate";
int track_id;
string info;
while length {
if (that[0..4] != "MTrk")
raise new Error "Not a midi track: invalid track code: $(that[0..4])";
step 4;
int trklen = _0 << 24 + _1 << 16 + _2 << 8 + _3; step 4;
writeln "$track_id: $trklen b";
track_id ++;
auto track = that[0 .. trklen]; step trklen;
ubyte[] takeTrack(int i) {
(ubyte[] res, track) = track[(0..i, i..$)];
return res;
}
int pos;
int x 128 pressedOn;
byte lastCmd;
float speedfactor = 0.5;
while track.length > 0 {
int readVarLen() {
int res;
while track[0] >= 0x80 {
res = res * 128 + track[0] & 0x7f;
track = track[1 .. $];
}
res = res * 128 + track[0];
track = track[1 .. $];
return res;
}
int delta = readVarLen();
/*if (delta < 1024)*/ pos += delta;
ubyte cmd;
if (track[0] & 0x80) {
cmd = takeTrack(1)[0];
lastCmd = cmd;
} else cmd = lastCmd;
if (cmd == 0xff) {
byte subcmd = takeTrack(1)[0];
int len = readVarLen();
ubyte[] subdata = takeTrack(len);
// writeln "@$pos: SPECIAL $subcmd, $(subdata.length)";
if (subcmd == 81) {
int spd = subdata[0] << 16 + subdata[1] << 8 + subdata[2];
writeln "speed = $spd";
speedfactor = spd / 1000000f;
}
if (subcmd == 3) {
writeln "'$(string:subdata)'";
info = string:subdata;
}
} else {
auto cmdcode = (cmd & 0xf0) >> 4, channel = cmd & 0x0f;
if (cmdcode == 0x9 && track[1]) {
// writeln "@$pos: NOTE ON $(track[0])";
auto note = takeTrack(2)[0];
pressedOn[note] = pos;
} else if (cmdcode == 0x8 || cmdcode == 0x9 && !track[1]) {
// writeln "@$pos: NOTE OFF $(track[0])";
auto note = takeTrack(2)[0];
auto from = pressedOn[note];
auto len = pos - from;
auto quarters = len * speedfactor / tickrate;
quarters *= 1.5;
if (len > 1)
addNoteAt(from * speedfactor / tickrate, 8 * pow(pow(2, 1/12f), note), quarters,
eval info == "Recorder" || info == "Pan flute" || info == "Horns" || info == "Trombone" || info.find("String") != -1);
} else if (cmdcode == 0xa) { // aftertouch, whev
takeTrack(2);
} else if (cmdcode == 0xb) {
takeTrack(2);
} else if (cmdcode == 0xc) {
takeTrack(1);
} else if (cmdcode == 0xe) {
takeTrack(2); // pitch change, ignored
} else {
writeln "@$pos: $cmd - $cmdcode on $channel";
writeln "left: $(track.length), $track";
track = null;
}
}
}
}
//writeln "$that";
}
if args.length {
for auto arg <- args {
if arg.endsWith(".mid")
addMidiTrack arg;
else
addTrack arg;
}
}
sortList();
writeln "added $(notelist.length) notes";
int lastDoneAt; bool lastHadNotes;
dg = delegate float(float f) {
float res = 0f;
bool done, hadNotes;
int i, active, doneAt;
while !done && i < notelist.length {
alias n = notelist[i];
if (f >= n[1]) {
if f < n[1] + n[0].end { res += n[0].calcValue (f - n[1]); active ++; hadNotes = true; }
} else {
done = true;
doneAt = i;
}
i++;
}
if (doneAt != lastDoneAt || hadNotes != lastHadNotes) {
auto prev = notelist.length;
clearList f;
lastDoneAt = doneAt;
}
lastHadNotes = hadNotes;
res = atan(res) / (PI / 2);
return res;
};
int base;
float volume = 1;
int length = 1024;
while notelist.length {
snd.dump(delegate Sample(int i) {
auto res = Sample:short:int:(dg((base + i) / 48000f) * 32767f * volume);
return res;
}, length, 1f);
base += length;
}
snd.close();
}