-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
180 lines (158 loc) · 4.01 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"github.com/boomlinde/acidforth/collection"
"github.com/boomlinde/acidforth/machine"
"github.com/boomlinde/acidforth/midi"
"github.com/boomlinde/acidforth/synth"
"github.com/gordonklaus/portaudio"
"github.com/rakyll/portmidi"
"io/ioutil"
"log"
"math"
"os"
"path/filepath"
"regexp"
"strconv"
"sync"
)
var sfreq float64
func main() {
var listMidi bool
var midiInterface int
var address string
var m *midi.Midi
var prompt float64
var samples string
var sfreqi int
flag.BoolVar(&listMidi, "l", false, "List MIDI interfaces")
flag.IntVar(&midiInterface, "m", -1, "Connect to MIDI interface ID")
flag.StringVar(&address, "s", "", "HTTP server address. Leave unset to disable")
flag.StringVar(&samples, "w", "", "Sample directory")
flag.IntVar(&sfreqi, "r", 44100, "Sample rate")
flag.Parse()
args := flag.Args()
sfreq = float64(sfreqi)
if listMidi {
portmidi.Initialize()
defer portmidi.Terminate()
deviceCount := portmidi.CountDevices()
for i := 0; i < deviceCount; i++ {
fmt.Println(i, portmidi.Info(portmidi.DeviceID(i)))
}
os.Exit(0)
} else if midiInterface != -1 {
portmidi.Initialize()
defer portmidi.Terminate()
in, err := portmidi.NewInputStream(portmidi.DeviceID(midiInterface), 1024)
if err != nil {
log.Fatal(err)
}
defer in.Close()
m = midi.NewMidi(in.Listen())
} else {
m = midi.NewMidi(make(chan portmidi.Event))
}
pl := &sync.Mutex{}
col := collection.NewCollection()
m.Register(col)
go m.Listen()
addComponents(sfreq, col, samples)
col.Machine.Register("prompt", func(s *machine.Stack) {
pl.Lock()
s.Push(prompt)
pl.Unlock()
})
if len(args) > 0 {
prg, err := ioutil.ReadFile(args[len(args)-1])
if err != nil {
log.Println("ERROR:", err)
}
log.Println("Compiling program")
err = col.Machine.Build(prg)
if err != nil {
log.Println("ERROR:", err)
}
}
log.Println("Running")
if address != "" {
go server(col, address)
}
portaudio.Initialize()
defer portaudio.Terminate()
stream, err := portaudio.OpenDefaultStream(0, 2, sfreq, 0, col.Callback)
chk(err)
defer stream.Close()
stream.Start()
reader := bufio.NewReader(os.Stdin)
numberRe, err := regexp.Compile("[0-9]+\\.?[0-9]*")
chk(err)
for {
text, err := reader.ReadString('\n')
chk(err)
found := numberRe.FindString(text)
if found == "" {
if col.Playing {
log.Print("Stopping sequencer")
} else {
log.Println("Starting sequencer")
}
col.Playing = !col.Playing
} else {
tpr, err := strconv.ParseFloat(found, 64)
if err != nil {
log.Println(err)
continue
}
pl.Lock()
prompt = tpr
pl.Unlock()
}
}
}
func addComponents(srate float64, c *collection.Collection, samples string) {
dseqs := make([]synth.Triggable, 0, 16)
for i := 1; i <= 8; i++ {
synth.NewOperator(fmt.Sprintf("op%d", i), c, srate)
synth.NewEnvelope(fmt.Sprintf("env%d", i), c, srate)
synth.NewPulser(fmt.Sprintf("pulser%d", i), c, srate)
synth.NewVactrol(fmt.Sprintf("vactrol%d", i), c, srate)
dseqs = append(dseqs, synth.NewDSeq(fmt.Sprintf("dseq%d", i), c))
dseqs = append(dseqs, synth.NewVSeq(fmt.Sprintf("vseq%d", i), c))
}
for _, r := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" {
synth.NewRegister(string(r), c)
}
for i := 1; i <= 4; i++ {
synth.NewAccumulator(fmt.Sprintf("mix%d", i), c)
synth.NewDelay(fmt.Sprintf("delay%d", i), c, srate)
synth.NewITable(fmt.Sprintf("itab%d", i), c)
}
if samples != "" {
files, err := filepath.Glob(filepath.Join(samples, "*.wav"))
if err != nil {
log.Fatal(err)
}
for _, f := range files {
synth.NewSampler(f, c, srate)
}
}
synth.NewSeq("seq", c, srate, dseqs)
synth.NewWaveTables(c)
synth.NewShaper(c)
c.Machine.Register("srate", func(s *machine.Stack) { s.Push(srate) })
c.Machine.Register("m2f", func(s *machine.Stack) {
s.Push(440 * math.Pow(2, (s.Pop()-69)/12))
})
c.Machine.Register("TET", func(s *machine.Stack) {
scale := s.Pop()
s.Push(440 * math.Pow(2, (s.Pop()-69)/scale))
})
}
func chk(err error) {
if err != nil {
log.Fatal(err)
}
}