-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsampler.pixi
245 lines (214 loc) · 6.5 KB
/
sampler.pixi
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
osctab = new(100, 1, INT)
drumtab = new(42, 1, INT)
oscdata = load("Wave/Wave.dat")
if (get_xsize(oscdata) != 25600) { halt }
master_gain = 1
$i = 0
while ($i < 100) {
osctab[$i] = new(512, 1, INT16)
$j = 0
while ($j < 256) {
osctab[$i][$j] = oscdata[256 * $i + $j] * 256
osctab[$i][$j + 256] = osctab[$i][$j]
$j + 1
}
$i + 1
}
// Raw PCM, 22050 Hz, signed 8-bit, mono
$path = ""
$i = 0
while ($i < 42) {
$real = clone(drum_names[$i])
$len = get_xsize($real)
if ($real[$len - 1] == '*') {
$real[$len - 1] = '\0'
}
sprintf($path, "Wave/%s.wav", $real)
remove($real)
$drumdata = load($path)
$j = 0
$n = get_xsize($drumdata)
drumtab[$i] = new($n, 1, INT16)
while ($j < $n) {
drumtab[$i][$j] = $drumdata[$j] * 256
$j + 1
}
$i + 1
}
remove($path)
// For little oscillographs only
fn osc_sample($instr, $phase) {
ret(osctab[$instr][floor($phase * 256)] div 256)
}
fn drum_sample_phase($instr, $phase) {
ret(drumtab[$instr][floor($phase * get_xsize(drumtab[$instr]))] div 256)
}
// Tracks 16 and 17 are for preview
track_step = new(18, 1, INT32) // in samples; divide by 2^16; 0 means note off
track_vol = new(18, 1, INT16) // [0, 255]; INT8 introduces complexity
track_pan = new(18, 1, INT8) // [0, 12]
track_phase = new(18, 1, INT32) // in samples; divide by 2^16
override_instr = -1
clean(track_step)
SMP_LEN = 256 * 65536
// -1 on any parameter means no change
fn note_on($tid, $note, $vol, $pan) {
if ($note != -1) {
// Reset phase at onset
track_phase[$tid] = 0
if ($tid < 8 || $tid == 16) {
// Note 45 plays at 440 Hz
// Thus the magic number is log_2(440) - 45/12 = 5.03136
$freq = pow(2, ($note + tunings[$tid] / 1000 - 1) / 12 + 5.03136)
$step = 256 / 44100 * $freq
} else {
// note / 28 / (44100 / 22050)
// where 28 is the note at which the sample is played at 1x speed
// 44100 is the mixing sample frequency
// and 22050 is the source format sample frequency
$step = $note / 56
}
track_step[$tid] = round($step * 65536)
}
if ($vol != -1) { track_vol[$tid] = $vol }
if ($pan != -1) { track_pan[$tid] = $pan }
}
fn note_preview_instr($tid, $instr) {
instruments[$tid] = $instr
if ($tid < 8 || $tid == 16) {
note_on($tid, 45, 0x80, 6)
} else {
note_on($tid, 28, 0x80, 6)
}
}
fn note_off($tid) {
if ($tid <= 8 || $tid == 16) {
track_step[$tid] = 0
}
}
fn all_silence() {
clean(track_step)
}
aud_buf0 = -1
aud_buf1 = -1
aud_buf2 = -1
aud_buf3 = -1
smpconf = new(SMP_INFO_SIZE, 1, INT32)
smpconf[SMP_DEST_OFF] = 0
smpconf[SMP_SRC_SIZE] = 0
smpconf[SMP_LOOP] = 0
smpconf[SMP_FLAGS] = SMP_FLAG_INTERP4
fn lanczos_resmp($table, $sz, $x) {
$i = $x >> 16
$x / 65536
$A = 4
$r = 0
$j = $i - $A + 1
$den = 0
while ($j <= $i + $A) {
$y = $x - $j
if ($y >= -0.00001 && $y <= 0.00001) {
$L = 1
} else { if (abs($y) < $A) {
$y * M_PI
$L = sin($y) * sin($y / $A) / ($y * $y / $A)
} else { $L = 0 } }
$den + $L
if ($j >= 0 && $j < $sz) {
$r + $table[$j] * $L
}
$j + 1
}
ret($r / $den)
}
fn sat_add($x, $y) {
$z = $x + $y
if ($z >= 32767) { ret(32767) }
if ($z < -32767) { ret(-32767) }
ret($z)
}
fn audio_callback($stream, $userdata, $channels, $frames,
$output_time_in_system_ticks, $in_channels, $latency_in_frames)
{
if (aud_buf0 == -1) {
aud_buf0 = new($frames, 1, INT16)
aud_buf1 = new($frames, 1, INT16)
aud_buf2 = new($frames, 1, INT16)
aud_buf3 = new($frames, 1, INT16)
}
clean(aud_buf0)
clean(aud_buf1)
smpconf[SMP_DEST_LEN] = $frames
$i = 0
while ($i < 18) {
if (track_step[$i] == 0) { $i + 1 continue }
$vol = track_vol[$i]
$pan = track_pan[$i]
$delta = track_step[$i]
$instr = override_instr
if (override_instr == -1) { $instr = instruments[$i] }
$lvol = round($vol * 128 * (12 - $pan) / 12)
$rvol = round($vol * 128 * $pan / 12)
if ($i < 8 || $i == 16) {
$src = osctab[$instr]
$loop = 256 // Whole sample
} else {
$src = drumtab[$instr]
$loop = 0 // No loop
}
if ($i < 8 || $i == 16 || $delta >= 65536) {
smpconf[SMP_DEST] = aud_buf2
smpconf[SMP_SRC] = $src
smpconf[SMP_LOOP_LEN] = $loop
smpconf[SMP_SRC_OFF_H] = track_phase[$i] >> 16
smpconf[SMP_SRC_OFF_L] = track_phase[$i] & 0xffff
smpconf[SMP_VOL1] = $lvol * master_gain
smpconf[SMP_VOL2] = smpconf[SMP_VOL1]
smpconf[SMP_DELTA] = $delta
sampler(smpconf)
op_cc(OP_SADD, aud_buf0, aud_buf2)
smpconf[SMP_DEST] = aud_buf3
smpconf[SMP_SRC_OFF_H] = track_phase[$i] >> 16
smpconf[SMP_SRC_OFF_L] = track_phase[$i] & 0xffff
smpconf[SMP_VOL1] = $rvol * master_gain
smpconf[SMP_VOL2] = smpconf[SMP_VOL1]
sampler(smpconf)
op_cc(OP_SADD, aud_buf1, aud_buf3)
} else {
$lvol = $lvol / 32768 * master_gain
$rvol = $rvol / 32768 * master_gain
$offs = track_phase[$i]
$p = 0
$sz = get_xsize($src)
while ($p < $frames) {
$sample = lanczos_resmp($src, $sz, $offs)
aud_buf0[$p] = sat_add(aud_buf0[$p], round($sample * $lvol))
aud_buf1[$p] = sat_add(aud_buf1[$p], round($sample * $rvol))
$offs + $delta
if ($loop != 0 && $offs >= $loop) {
$offs - $loop
}
$p + 1
}
}
if ($i < 8 || $i == 16) {
$increment = $frames * $delta
track_phase[$i] =
(track_phase[$i] + $increment) % SMP_LEN
} else {
$increment = $frames * $delta
track_phase[$i] = track_phase[$i] + $increment
if (track_phase[$i] >= get_xsize(drumtab[$instr]) * 65536) {
track_step[$i] = 0 // Turn off note
}
}
$i + 1
}
copy($channels[0], aud_buf0)
copy($channels[1], aud_buf1)
ret(1)
}
set_audio_callback(audio_callback, 0, 44100, INT16, 2, 0)
fn sampler_get_position() {
ret(floor(get_timer(0) / tick_len))
}