-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathaudio.nim
342 lines (317 loc) · 9.98 KB
/
audio.nim
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
import nico
import strutils
var n = 69
var shape = 0
type Instrument = tuple[shape: SynthShape, init: range[0..15], change: range[-7..7]]
type Row = tuple[note: uint8, inst: uint8, command: uint8, arg: uint8]
type Pattern = array[16, Row]
type SongRow = array[4, uint8]
type Song = array[64, SongRow]
var patterns: array[64, Pattern]
var song: Song
var instruments: array[64, Instrument]
type View = enum
songView = "Song"
patternView = "Pattern"
instrumentView = "Inst"
var view: View
var tick = 0
var currentPattern = 0
var currentSongRow = 0
var currentChannel = 0
var currentPatternRow = 0
var currentPatternCol: range[0..3] = 0
var currentInstrument = 0
var currentInstrumentSetting: range[0..3] = 0
var songChannelCursors: array[4, uint8]
proc musicUpdate() =
for channel in 0..3:
let pat = song[songChannelCursors[channel]][channel]
if pat != 0:
let n = patterns[pat][tick mod 16].note
if n != 0:
# todo get instr settings
let i = patterns[pat][tick mod 16].inst
let inst = instruments[i]
if i == 0:
pitch(channel, note(n.int))
else:
synth(channel, inst.shape, note(n.int), inst.init, inst.change)
tick += 1
if tick mod 16 == 0:
# move cursors
for channel in 0..3:
# check if the next row in the song has an entry
if song[songChannelCursors[channel] + 1][channel] != 0:
songChannelCursors[channel] += 1
else:
# seek up to the top of the island
while true:
if songChannelCursors[channel] == 0 or song[songChannelCursors[channel] - 1][channel] == 0:
break
else:
songChannelCursors[channel] -= 1
proc gameInit() =
bpm(90)
tpb(4)
n = 69
shape = 0
tick = 0
view = songView
currentPattern = 0
currentSongRow = 0
currentChannel = 0
currentPatternRow = 0
currentPatternCol = 0
instruments[1].shape = synP25
instruments[1].init = 7
instruments[1].change = -1
instruments[2].shape = synP12
instruments[2].init = 5
instruments[2].change = -2
instruments[3].shape = synSaw
instruments[3].init = 3
instruments[3].change = -3
instruments[4].shape = synNoise
instruments[4].init = 5
instruments[4].change = -7
instruments[5].shape = synTri
instruments[5].init = 0
instruments[5].change = 1
setAudioTickCallback(musicUpdate)
proc gameUpdate(dt: float32) =
if btn(pcX):
if btnp(pcLeft):
if view > typeof(view).low:
view = (view.int - 1).View
return
if btnp(pcRight):
if view < typeof(view).high:
view = (view.int + 1).View
return
case view:
of songView:
if btnp(pcY):
for i in 0..3:
songChannelCursors[i] = currentSongRow.uint8
tick = 0
if btn(pcA):
if btnp(pcLeft):
if song[currentSongRow][currentChannel].int > 0:
song[currentSongRow][currentChannel] -= 1
currentPattern = song[currentSongRow][currentChannel].int
if btnp(pcRight):
if song[currentSongRow][currentChannel].int < 63:
song[currentSongRow][currentChannel] += 1
currentPattern = song[currentSongRow][currentChannel].int
if btnp(pcUp):
if song[currentSongRow][currentChannel].int < 48:
song[currentSongRow][currentChannel] += 16
currentPattern = song[currentSongRow][currentChannel].int
if btnp(pcDown):
if song[currentSongRow][currentChannel].int > 15:
song[currentSongRow][currentChannel] -= 16
currentPattern = song[currentSongRow][currentChannel].int
elif btnp(pcB):
song[currentSongRow][currentChannel] = 0
else:
if btnp(pcLeft):
currentChannel -= 1
if currentChannel < 0:
currentChannel = 0
currentPattern = song[currentSongRow][currentChannel].int
if btnp(pcRight):
currentChannel += 1
if currentChannel > 3:
currentChannel = 3
currentPattern = song[currentSongRow][currentChannel].int
if btnp(pcUp):
currentSongRow -= 1
if currentSongRow < 0:
currentSongRow = 0
currentPattern = song[currentSongRow][currentChannel].int
if btnp(pcDown):
currentSongRow += 1
if currentSongRow > 63:
currentSongRow = 63
currentPattern = song[currentSongRow][currentChannel].int
of patternView:
if btn(pcA):
case currentPatternCol:
of 0:
if patterns[currentPattern][currentPatternRow].note != 0:
n = patterns[currentPattern][currentPatternRow].note.int
if btnp(pcUp):
n += 12
if btnp(pcDown):
n -= 12
if btnp(pcLeft):
n -= 1
if btnp(pcRight):
n += 1
patterns[currentPattern][currentPatternRow].note = n.uint8
patterns[currentPattern][currentPatternRow].inst = currentInstrument.uint8
of 1:
currentInstrument = patterns[currentPattern][currentPatternRow].inst.int
if btnp(pcUp):
if currentInstrument < 63:
currentInstrument += 16
else:
currentInstrument = 63
if btnp(pcDown):
if currentInstrument > 16:
currentInstrument -= 16
else:
currentInstrument = 0
if btnp(pcLeft):
if currentInstrument > 0:
currentInstrument -= 1
if btnp(pcRight):
if currentInstrument < 15:
currentInstrument += 1
patterns[currentPattern][currentPatternRow].inst = currentInstrument.uint8
of 2:
discard
of 3:
discard
elif btnp(pcB):
case currentPatternCol:
of 0:
patterns[currentPattern][currentPatternRow].note = 0
of 1:
patterns[currentPattern][currentPatternRow].inst = 0
of 2:
patterns[currentPattern][currentPatternRow].command = 0
of 3:
patterns[currentPattern][currentPatternRow].arg = 0
else:
if btnp(pcUp):
if currentPatternRow > 0:
currentPatternRow -= 1
if btnp(pcDown):
if currentPatternRow < 15:
currentPatternRow += 1
if btnp(pcLeft):
if currentPatternCol > 0:
currentPatternCol -= 1
if btnp(pcRight):
if currentPatternCol < 3:
currentPatternCol += 1
of instrumentView:
if btn(pcA):
case currentInstrumentSetting:
of 0:
if btnp(pcLeft):
if instruments[currentInstrument].shape < SynthShape.high:
instruments[currentInstrument].shape.inc()
if btnp(pcRight):
if instruments[currentInstrument].shape > SynthShape.low:
instruments[currentInstrument].shape.dec()
of 1:
if btnp(pcLeft):
if instruments[currentInstrument].init > 0:
instruments[currentInstrument].init -= 1
if btnp(pcRight):
if instruments[currentInstrument].init < 15:
instruments[currentInstrument].init += 1
of 2:
if btnp(pcLeft):
if instruments[currentInstrument].change > -7:
instruments[currentInstrument].change -= 1
if btnp(pcRight):
if instruments[currentInstrument].change < 7:
instruments[currentInstrument].change += 1
of 3:
discard
else:
if btnp(pcUp):
if currentInstrumentSetting > 0:
currentInstrumentSetting -= 1
if btnp(pcDown):
if currentInstrumentSetting < 15:
currentInstrumentSetting += 1
proc noteString(n: int): string =
if n == 0:
return "..."
let oct = n div 12
let note = n mod 12
case note:
of 0:
return "C-" & $oct
of 1:
return "C#" & $oct
of 2:
return "D-" & $oct
of 3:
return "D#" & $oct
of 4:
return "E-" & $oct
of 5:
return "F-" & $oct
of 6:
return "F#" & $oct
of 7:
return "G-" & $oct
of 8:
return "G#" & $oct
of 9:
return "A-" & $oct
of 10:
return "A#" & $oct
of 11:
return "B-" & $oct
else:
return "???"
proc gameDraw() =
cls()
case view:
of songView:
setColor(2)
printr($view, screenWidth - 1, 1)
for row in 0..<song.len:
setColor(5)
print(toHex(row, 2), 1, 1 + row * 8)
for col in 0..3:
if songChannelCursors[col].int == row:
setColor(2)
print(">", 16 + col * 16 - 4, 1 + row * 8)
setColor(if currentSongRow == row and currentChannel == col: 7 else: 5)
if song[row][col] == 0:
print("..", 16 + col * 16, 1 + row * 8)
else:
print(toHex(song[row][col]), 16 + col * 16, 1 + row * 8)
of patternView:
setColor(2)
printr($view & " " & toHex(currentPattern, 2), screenWidth - 1, 1)
var p = patterns[currentPattern]
for row in 0..<p.len:
setColor(if tick mod 16 == row: 2 elif row mod 4 == 0: 3 else: 5)
print(toHex(row, 2), 1, 1 + row * 8)
# note
setColor(if row == currentPatternRow and currentPatternCol == 0: 7 else: 5)
print(noteString(p[row].note.int), 16, 1 + row * 8)
# inst
setColor(if row == currentPatternRow and currentPatternCol == 1: 7 else: 5)
print(if p[row].inst == 0: " .." else: "I" & toHex(p[row].inst), 32, 1 + row * 8)
# cmd
setColor(if row == currentPatternRow and currentPatternCol == 2: 7 else: 5)
print(toHex(p[row].command), 64, 1 + row * 8)
# cmd
setColor(if row == currentPatternRow and currentPatternCol == 3: 7 else: 5)
print(toHex(p[row].arg), 72, 1 + row * 8)
of instrumentView:
setColor(2)
printr($view & " " & toHex(currentInstrument, 2), screenWidth - 1, 1)
setColor(if currentInstrumentSetting == 0: 7 else: 5)
print("SHAPE: " & $instruments[currentInstrument].shape.SynthShape, 1, 10)
setColor(if currentInstrumentSetting == 1: 7 else: 5)
print("INIT: " & $instruments[currentInstrument].init, 1, 30)
setColor(if currentInstrumentSetting == 2: 7 else: 5)
print("CHANGE: " & $instruments[currentInstrument].change, 1, 40)
nico.init("nico", "audio")
nico.createWindow("audio", 128, 128, 4)
integerScale(true)
fixedSize(true)
loadFont(0, "font.png")
setFont(0)
nico.run(gameInit, gameUpdate, gameDraw)