-
Notifications
You must be signed in to change notification settings - Fork 0
/
nssnf.go
588 lines (522 loc) · 14.4 KB
/
nssnf.go
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package main
import (
"fmt"
"io/ioutil"
"math"
// "os"
"math/rand"
// "github.com/coral/fluidsynth2"
"fluidsynth2"
// "github.com/xthexder/go-jack"
"github.com/rakyll/launchpad"
"log"
"os"
"time"
"os/signal"
"buttons"
"inputs"
"screen"
)
var main_screen *screen.Screen
var err error
var displayChan chan bool
var ui []screen.Msg
var ui_loading []screen.Msg
var ui_bar []screen.Msg
var available_presets []fluidsynth2.PresetName
var available_soundfonts []string
var currentVoices []fluidsynth2.Voice
var sf_id int
var programIndex int
var menuIndex int
var sfIndex int
var genTypeIndex fluidsynth2.FluidGenType
const audio_driver string = "jack"
const midi_driver string = "alsa_seq"
const default_soundfont string = "Super_Italo_DiscoFont_Director_s_Cut.sf2"
const RAND_MAX int = 126
var synth fluidsynth2.Synth
var s fluidsynth2.Settings
const CENTER int = 0
const POT1 int = 1
const POT2 int = 2
const POT3 int = 3
const POT4 int = 4
const POT5 int = 5
const POT6 int = 6
const TOUCH int = 7
const BUTTON1 int = 8
const BUTTON2 int = 9
const BUTTON3 int = 10
const BUTTON4 int = 11
const MENU_LINE1 int = 12
const MENU_LINE2 int = 13
const MENU_LINE3 int = 14
func resetUi() []screen.Msg {
var reseted_ui []screen.Msg
for i := 0; i <= MENU_LINE3; i++ {
message := screen.Msg{}
message.SetMsg(0, 0, false, "")
reseted_ui = append(reseted_ui, message)
}
return reseted_ui
}
func main() {
// os.Clearenv()
main_screen, err = screen.NewScreen()
if err != nil {
log.Fatal(err)
}
ui = resetUi()
ui_loading = resetUi()
ui_bar = resetUi()
go forgeUiLoading(CENTER, 10, 30, "NsynthSuperHard")
initFluidSynth()
// go initLaunchpad()
menuIndex = 1
genTypeIndex = 0
displayChan = make(chan bool)
// go main_screen.DisplayGif("images/golang.gif", displayChan)
go buttons.NewButton("GPIO5", handlerButton1)
go buttons.NewButton("GPIO6", handlerButton2)
go buttons.NewButton("GPIO26", handlerButton3)
go buttons.NewButton("GPIO13", handlerButton4)
go inputs.StartInputs(touchHandler, encodersHandler, potentiometersHandler)
// for i := 0; i<10 ; i++ {
// for j := 0; j<10 ; j++ {
// forgeUi(TOUCH, int(i*4)+80, int(j*4), ".")
// }
// }
// go main_screen.DisplayGif("images/sexy-weiner.gif", displayChan)
// time.Sleep(1000* time.Millisecond)
// displayChan <- true
// go playMidi()
for {
// time.Sleep(1000* time.Millisecond)
playMidi()
}
}
func forgeUi(index int, x int, y int, invert bool, msg string, v ...interface{}) {
message := screen.Msg{}
msg = fmt.Sprintf(msg, v...)
message.SetMsg(x, y, invert, msg)
ui[index] = message
main_screen.DisplayMessage(ui)
}
func forgeUiLoading(index int, x int, y int, msg string, v ...interface{}) {
message := screen.Msg{}
msg = fmt.Sprintf(msg, v...)
message.SetMsg(x, y, true, msg)
ui_loading[index] = message
main_screen.DisplayLoadingMessage(ui_loading, false)
}
func forgeUiBarX(index int, x int, y int, percent int, msg string, v ...interface{}) {
message := screen.Msg{}
msg = fmt.Sprintf(msg, v...)
message.SetMsg(x, y, true, msg)
ui_bar[index] = message
main_screen.DisplayBarXMessage(ui_bar, percent)
}
func handlerButton1(buttonChannel chan bool) {
for {
select {
case pushed := <-buttonChannel:
if pushed {
log.Printf("Pushed 1")
// forgeUi(BUTTON1, 0, 10, true, "B1")
} else {
log.Printf("Released 1")
if menuIndex == 0 {
sfIndex = sfIndex -1
if sfIndex < 0 {
sfIndex = len(available_soundfonts)-1
}
}else if menuIndex == 1 {
sfIndex = sfIndex
}else if menuIndex == 2 {
sfIndex = sfIndex +1
if sfIndex > len(available_soundfonts)-1 {
sfIndex = 0
}
}
selectSF()
// forgeUi(BUTTON1, 0, 10, true, " ")
}
}
}
}
func handlerButton2(buttonChannel chan bool) {
for {
select {
case pushed := <-buttonChannel:
if pushed {
log.Printf("Pushed 2")
// forgeUi(BUTTON2, 10, 10, false, "B2")
} else {
log.Printf("Released 2")
index := programIndex
if menuIndex == 0 {
index = index -1
if index < 0 {
index = len(available_presets)-1
}
}else if menuIndex == 1 {
index = index
}else if menuIndex == 2 {
index = index +1
if index > len(available_presets)-1 {
index = 0
}
}
synth.ProgramSelect(0,
sf_id,
available_presets[index].Bank,
available_presets[index].Num)
currentVoices = synth.GetVoicelist()
log.Printf("Soundfont ID : %v", sf_id)
log.Printf("Program change to %v", index)
log.Println(available_presets[index].Name)
log.Printf("Voices %v", currentVoices)
// forgeUi(BUTTON2, 10, 10, false, " ")
}
}
}
}
func handlerButton3(buttonChannel chan bool) {
for {
select {
case pushed := <-buttonChannel:
if pushed {
log.Printf("Pushed 3")
forgeUi(BUTTON3, 20, 10, false, "B3")
} else {
log.Printf("Released 3")
forgeUi(BUTTON3, 20, 10, false, " ")
}
}
}
}
func handlerButton4(buttonChannel chan bool) {
for {
select {
case pushed := <-buttonChannel:
if pushed {
log.Printf("Pushed 4")
forgeUi(BUTTON4, 30, 10, false, "B4")
} else {
log.Printf("Released 4")
forgeUi(BUTTON4, 30, 10, false, " ")
}
}
}
}
func touchHandler(touchChannel chan []byte) {
for data := range touchChannel {
log.Printf("Handler touch: %v\n", data)
// for i := 0; i<10 ; i++ {
// for j := 0; j<10 ; j++ {
// forgeUi(TOUCH, int(i*4)+80, int(j*4), ".")
// }
// }
forgeUi(TOUCH, int(data[0]*4)+80, int(data[1]*4), false, "*")
}
}
func displayMenu(names []string, index int) {
name_0 := ""
name := names[index]
name_1 := ""
if index == 0 {
name_0 = names[len(names)-1]
name_1 = names[index+1]
} else if index == len(names)-1 {
name_0 = names[index-1]
name_1 = names[0]
}else {
name_0 = names[index-1]
name_1 = names[index+1]
}
if menuIndex == 0 {
forgeUi(MENU_LINE1, 0, 15, true, "%v", name_0)
}else {
forgeUi(MENU_LINE1, 0, 15, false, "%v", name_0)
}
if menuIndex == 1 {
forgeUi(MENU_LINE2, 0, 30, true, "%v", name)
}else {
forgeUi(MENU_LINE2, 0, 30, false, "%v", name)
}
if menuIndex == 2 {
forgeUi(MENU_LINE3, 0, 45, true, "%v", name_1)
}else {
forgeUi(MENU_LINE3, 0, 45, false, "%v", name_1)
}
}
func selectSF() {
sf_id = sfIndex + 1
available_presets = synth.SFGetPresetsName(sf_id)
if programIndex > len(available_presets)-1 {
programIndex = len(available_presets)-1
}
log.Printf("SFINDEX : %v\n", sfIndex)
log.Printf("Soundfont : %v\n", available_soundfonts[sfIndex])
displayMenu(fluidsynth2.GetPresetsName(available_presets), programIndex)
}
func encodersHandler(encsChannel chan []byte) {
previous_encs := make([]byte, 4)
for encs := range encsChannel {
// Change program with encoder 0
if previous_encs[0] != encs[0] {
if encs[0] > previous_encs[0] {
if menuIndex < 2 {
menuIndex++
}else {
sfIndex++
}
} else {
if menuIndex != 0 {
menuIndex--
}else {
sfIndex--
}
}
if sfIndex > len(available_soundfonts)-1 {
sfIndex = 0
}
if sfIndex < 0 {
sfIndex = len(available_soundfonts) - 1
}
displayMenu(available_soundfonts, sfIndex)
previous_encs[0] = encs[0]
}
// Change program with encoder 1
if previous_encs[1] != encs[1] {
if encs[1] > previous_encs[1] {
if menuIndex < 2 {
menuIndex++
}else {
programIndex++
}
} else {
if menuIndex != 0 {
menuIndex--
}else {
programIndex--
}
}
if programIndex > len(available_presets)-1 {
programIndex = 0
}
if programIndex < 0 {
programIndex = len(available_presets) - 1
}
displayMenu(fluidsynth2.GetPresetsName(available_presets), programIndex)
previous_encs[1] = encs[1]
}
// Change program with encoder 2
if previous_encs[2] != encs[2] && encs[2]/2 < 126 {
go synth.SetPoly(int(encs[2]))
log.Println("Polyphony : ", encs[3])
previous_encs[2] = encs[2]
}
// Change program with encoder 3
if previous_encs[3] != encs[3] && encs[3]/2 < 126 {
if encs[3] > previous_encs[3] {
genTypeIndex++
} else {
genTypeIndex--
}
if int(genTypeIndex) > len(fluidsynth2.FluidGenTypeMappings)-1 {
genTypeIndex = 0
}
if int(genTypeIndex) < 0 {
genTypeIndex = fluidsynth2.FluidGenType(len(fluidsynth2.FluidGenTypeMappings) - 1)
}
forgeUi(POT1, 0, 60, false, fluidsynth2.FluidGenTypeMappings[genTypeIndex])
previous_encs[3] = encs[3]
// forgeUi(CENTER, 0, 30, false, "%d", int(encs[3]))
}
}
}
func potentiometersHandler(potsChannel chan []byte) {
previous_pots := make([]byte, 6)
for pots := range potsChannel {
if previous_pots[0] != pots[0] && (previous_pots[0]+2 < pots[0] || previous_pots[0]+2 > pots[0]) {
go synth.SetGain(math.Round((float64(pots[0])/126.0)*100) / 100)
// go s.SetNum("synth.gain", math.Round((float64(pots[0])/126.0)*100)/100)
forgeUiBarX(POT1, 0, 64, int(pots[0]/2), "Volume")
previous_pots[0] = pots[0]
}
if previous_pots[1] != pots[1] && (previous_pots[1]+2 < pots[1] || previous_pots[1]+2 > pots[1]) {
// forgeUi(POT2, 20, 54, false, "%0.2f", float64(pots[1])/255.0)
// if pots[1] == 0 {
// // result := synth.EnableReverb(-1, 0)
// go s.SetInt("synth.reverb.active", 0)
// // forgeUi(POT2, 20, 54, "%d", result)
// }else{
// // result := synth.EnableReverb(-1, 1)
// // forgeUi(POT2, 20, 54, "%d", result)
// go s.SetInt("synth.reverb.active", 1)
// }
go synth.SetReverbLevel(1, float64(pots[2])/10.0)
// currentVoices[0].VoiceReverbSendSet(float64(pots[5])/255.0)
// for _, voice := range currentVoices {
// voice.VoiceReverbSendSet((float64(pots[5]) / 255.0) * 100)
// }
// synth.MidiReverbSendSet(float64(pots[5])/255.0)
forgeUiBarX(POT1, 0, 64, int(pots[1]/2), "ReverbLevel")
previous_pots[1] = pots[1]
}
if previous_pots[2] != pots[2] && (previous_pots[2]+2 < pots[2] || previous_pots[2]+2 > pots[2]) {
// forgeUi(POT3, 40, 64, false, "%0.2f", float64(pots[2])/255.0)
previous_pots[2] = pots[2]
// go synth.SetReverbLevel(1, float64(pots[2])/10.0)
// go s.SetNum("synth.reverb.level", float64(pots[2])/255.0)
forgeUiBarX(POT1, 0, 64, int(pots[2]/2), "Filter Cutoff")
go synth.MidiFilterCutoffSet(float64(pots[2]/2))
}
if previous_pots[3] != pots[3] && (previous_pots[3]+2 < pots[3] || previous_pots[3]+2 > pots[3]) {
// forgeUi(POT4, 60, 54, false, "%0.2f", float64(pots[3])/10.0)
previous_pots[3] = pots[3]
forgeUiBarX(POT1, 0, 64, int(pots[3]/2), "Filter Res")
go synth.MidiFilterResoSet(float64(pots[3]/2))
// go synth.SetReverbWidth(1, float64(pots[3])/10.0)
// go s.SetNum("synth.reverb.width", float64(pots[3])/255.0)
}
if previous_pots[4] != pots[4] && (previous_pots[4]+2 < pots[4] || previous_pots[4]+2 > pots[4]) {
// forgeUi(POT5, 80, 64, false, "%d", float64(pots[4])/255.0)
go synth.SetReverbDamp(1, float64(pots[4])/255.0)
// go s.SetNum("synth.reverb.damp", float64(pots[4])/255.0)
previous_pots[4] = pots[4]
}
if previous_pots[5] != pots[5] && (previous_pots[5]+2 < pots[5] || previous_pots[5]+2 > pots[5]) {
// forgeUi(POT6, 100, 54, false, "%d", int(pots[5])/2)
go synth.MidiGenSet(genTypeIndex, float64(pots[5]/2))
forgeUiBarX(POT1, 0, 64, int(pots[5]/2), fluidsynth2.FluidGenTypeMappings[genTypeIndex])
previous_pots[5] = pots[5]
}
}
}
func load_sfont(soundfont string) {
sf_id = synth.SFLoad("soundfonts/"+soundfont, true)
fmt.Println("Soundfont id : ", sf_id)
fmt.Println("Soundfont name : ", soundfont)
presets := synth.SFGetPresetsName(sf_id)
for _, preset_name := range presets {
if preset_name.Name != "" {
available_presets = append(available_presets, preset_name)
}
}
fmt.Println("Available presets : ", len(available_presets))
}
func load_available_preset() {
presets := synth.SFGetPresetsName(sf_id)
for _, preset_name := range presets {
if preset_name.Name != "" {
available_presets = append(available_presets, preset_name)
}
}
}
func initFluidSynth() {
s = fluidsynth2.NewSettings()
fmt.Println("\nAvaliable audio drivers:")
for _, value := range s.GetOptions("audio.driver") {
fmt.Println(value)
}
fmt.Println("\nAvaliable midi drivers:")
for _, value := range s.GetOptions("midi.driver") {
fmt.Println(value)
}
// Set settings
s.SetString("audio.driver", audio_driver)
s.SetInt("audio.jack.autoconnect", 1)
s.SetString("midi.driver", midi_driver)
s.SetInt("midi.autoconnect", 1)
s.SetString("midi.portname", "FluidSynth")
// Create the synth
synth = fluidsynth2.NewSynth(s)
// Set Gain
s.SetNum("synth.gain", 0.20)
// Set polyphony
synth.SetPoly(200)
// Create midi driver
midi_driver := fluidsynth2.NewFluidMidiDriver(s, synth)
midi_router := fluidsynth2.NewFluidMidiRouter(s, synth)
// Create audio driver
audio_driver := fluidsynth2.NewAudioDriver(s, synth)
// Get all available soundfont files in the soundfonts folder
files, err := ioutil.ReadDir("./soundfonts")
if err != nil {
log.Fatal(err)
}
for index, f := range files {
fmt.Println("Load : ", int((100/len(files))*index))
load_sfont(f.Name())
available_soundfonts = append(available_soundfonts, f.Name())
forgeUiBarX(POT1, 0, 60, int((100/len(files))*index+(100/len(files))), "Loading SF ...")
}
sf_id = 1
sfIndex = sf_id - 1
load_available_preset()
programIndex = 0
synth.ProgramSelect(0,
sf_id,
available_presets[programIndex].Bank,
available_presets[programIndex].Num)
log.Println("Effect channels : ", synth.CountEffectsChannels())
log.Println("Effect groups : ", synth.CountEffectsGroups())
// Example of how to play from memory
// player := fluidsynth2.NewPlayer(synth)
// dat, err := ioutil.ReadFile("Super Mario 64 - Medley.mid")
// if err != nil {
// panic(err)
// }
// player.AddMem(dat)
// player.SetBPM(300)
// player.SetTempo(300)
// player.Play()
// player.Join()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func(){
for sig := range c {
audio_driver.Close()
midi_driver.Close()
midi_router.Close()
log.Println(sig)
os.Exit(0)
}
}()
}
func playMidi() {
// for {
for j := 0; j < 10; j++ {
/* Generate a random key */
rand.Seed(time.Now().UnixNano())
min := 30
max := 60
// fmt.Println(rand.Intn(max - min + 1) + min)
note := rand.Intn(max-min+1) + min
/* Play a note */
synth.NoteOn(0, uint8(note), 80)
/* Sleep for 1 second */
time.Sleep(175 * time.Millisecond)
/* Stop the note */
synth.NoteOff(0, uint8(note))
}
// }
}
func initLaunchpad() {
pad, err := launchpad.Open()
if err != nil {
log.Fatal(err)
}
defer pad.Close()
pad.Clear()
ch := pad.Listen()
for {
select {
case hit := <-ch:
pad.Light(hit.X, hit.Y, 3, 5)
}
}
}