-
Notifications
You must be signed in to change notification settings - Fork 36
/
nicosynth.nim
233 lines (186 loc) · 6.16 KB
/
nicosynth.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
import nico
import nico/gui
import strformat
import strutils
var data: SynthData
var editIndex = 0
var editMode = 0
var outputStr: string
var dirty = true
const minNote = 32
const maxNote = 32 + 64
const initData = synthDataFromString("0037A235C232D22FC22C82296227422422")
data = initData
proc gameInit() =
masterVol(255)
sfxVol(255)
musicVol(255)
for v in data.steps.mitems:
if v.shape == synSame:
v.shape = synSqr
proc play() =
synth(0, outputStr)
var xval,yval: int
proc gameGui() =
G.beginArea(4,4,screenWidth-8,20,gLeftToRight)
var speed = data.speed.int
if G.slider("speed", speed, 0, 15):
data.speed = speed.uint8
var loop = data.loop.int
if G.slider("loop", loop, 0, 15):
data.loop = loop.uint8
if G.button("-"):
for v in data.steps.mitems:
v.note -= 12
if G.button("+"):
for v in data.steps.mitems:
v.note += 12
G.endArea()
let playIndex = synthIndex(0)
let (mx,my) = mouse()
# pitch
let pitchHeight = 64
if G.xyarea(xval,yval,4, 24, screenWidth - 8, pitchHeight, proc(G: Gui, x,y,w,h: int, style: GuiStyle, ta,va: TextAlign) =
for i in 0..<data.steps.len:
let x0 = x + i * 4
let x1 = x + i * 4 + 2
let bottom = y + h - 1
let yrange = h
setColor(if editIndex == i: 10 elif data.steps[i].volume == 0: 1 else: 6)
let noteRel = clamp01(invLerp(minNote, maxNote, data.steps[i].note.float32))
rectfill(x0, bottom, x1, bottom - noteRel * yrange.float32)
if playIndex == i:
setColor(7)
rect(x0, bottom - noteRel * yrange.float32, x1, bottom)
if editIndex == i and G.downElement == G.element:
setColor(10)
printc(noteToNoteStr(data.steps[editIndex].note.int), x0, bottom - noteRel * yrange.float32 - 10)
setColor(7)
printr("pitch", x + w - 1, y)
setColor(10)
printr(noteToNoteStr(data.steps[editIndex].note.int), x + w - 1, y + 10)
):
let ix = xval div 4
if ix >= 0 and ix < data.steps.len:
editMode = 0
editIndex = ix
var note = clamp(minNote + pitchHeight - yval, minNote, maxNote)
if key(K_LSHIFT):
# lock to C maj, 0 2 4 7 9 11
let oct = note div 12
let noteInOct = note mod 12
let adjusted = case noteInOct:
of 0,1: 0
of 2,3: 2
of 4,5: 4
of 6,7: 7
of 8,9: 9
of 10,11: 11
else: 0
note = oct * 12 + adjusted
data.steps[ix].note = note.uint8
# volume
let volumeHeight = 32
if G.xyarea(xval,yval,4, 24 + pitchHeight + 4, screenWidth - 8, volumeHeight, proc(G: Gui, x,y,w,h: int, style: GuiStyle, ta,va: TextAlign) =
for i in 0..<data.steps.len:
let x0 = x + i * 4
let x1 = x + i * 4 + 2
let bottom = y + h - 1
let yrange = h
setColor(if editIndex == i: 10 elif data.steps[i].volume == 0: 1 else: 6)
let volumeRel = clamp01(invLerp(0, 15, data.steps[i].volume.float32))
rectfill(x0, bottom, x1, bottom - volumeRel * yrange.float32)
if playIndex == i:
setColor(7)
rect(x0, bottom - volumeRel * yrange.float32, x1, bottom)
if editIndex == i and G.downElement == G.element:
setColor(10)
printc($data.steps[editIndex].volume.int, x0, bottom - volumeRel * yrange.float32 - 10)
setColor(7)
printr("volume", x + w - 1, y)
setColor(10)
printr($data.steps[editIndex].volume.int, x + w - 1, y + 10)
):
let yrange = volumeHeight
let ix = xval div 4
if ix >= 0 and ix < data.steps.len:
editMode = 1
editIndex = ix
var vol = lerp(0'f, 15'f, (yrange - yval).float32 / yrange.float32)
data.steps[ix].volume = clamp(vol, 0, 15).uint8
# shape
let shapeHeight = 32
if G.xyarea(xval,yval,4, 24 + pitchHeight + 4 + volumeHeight + 4, screenWidth - 8, shapeHeight, proc(G: Gui, x,y,w,h: int, style: GuiStyle, ta,va: TextAlign) =
for i in 0..<data.steps.len:
let x0 = x + i * 4
let x1 = x + i * 4 + 2
let bottom = y + h - 1
let yrange = h
setColor(if editIndex == i: 10 elif data.steps[i].volume == 0: 1 else: 6)
let shapeRel = clamp01(invLerp(synSin.float32, synNoise2.float32, data.steps[i].shape.float32))
rectfill(x0, bottom, x1, bottom - shapeRel * yrange.float32)
if playIndex == i:
setColor(7)
rect(x0, bottom - shapeRel * yrange.float32, x1, bottom)
if editIndex == i and G.downElement == G.element:
setColor(10)
printc($data.steps[editIndex].shape, x0, bottom - shapeRel * yrange.float32 - 10)
setColor(7)
printr("shape", x + w - 1, y)
setColor(10)
printr($data.steps[editIndex].shape, x + w - 1, y + 10)
):
let yrange = shapeHeight
let ix = xval div 4
if ix >= 0 and ix < data.steps.len:
editMode = 2
editIndex = ix
var shape = lerp(synSin.int.float32 - 0.5'f, synNoise2.float32 + 0.5'f, (yrange - yval).float32 / yrange.float32)
data.steps[ix].shape = clamp(shape.int, synSin.int, synNoise2.int).SynthShape
G.beginArea(0,screenHeight - 20, screenWidth, 20,gLeftToRight)
if G.button("PLAY"):
play()
if G.button("COPY CODE"):
echo fmt("synth(channel, \"{outputStr}\")")
setClipboardText(outputStr)
G.endArea()
proc gameUpdate(dt: float32) =
G.update(gameGui, dt)
if btnp(pcA):
play()
if btnp(pcB):
if data.steps[editIndex].shape < SynthShape.high:
data.steps[editIndex].shape.inc()
else:
data.steps[editIndex].shape = synSin
if btnpr(pcLeft):
editIndex -= 1
if btnpr(pcRight):
editIndex += 1
if btnpr(pcUp):
case editMode:
of 0:
data.steps[editIndex].note.inc()
of 1:
data.steps[editIndex].volume.inc()
of 2:
data.steps[editIndex].shape.inc()
else: discard
if btnpr(pcDown):
case editMode:
of 0:
data.steps[editIndex].note.dec()
of 1:
data.steps[editIndex].volume.dec()
of 2:
data.steps[editIndex].shape.dec()
else: discard
editIndex = wrap(editIndex, data.steps.len)
if dirty:
outputStr = synthDataToString(data)
proc gameDraw() =
cls()
G.draw(gameGui)
nico.init("nico", "nicoSynthEditor")
nico.createWindow("nicoSynthEditor", 150, 190, 4, false)
nico.run(gameInit, gameUpdate, gameDraw)