-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
214 lines (180 loc) · 4.6 KB
/
app.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
package main
import (
"context"
"fmt"
"os"
"sync"
"github.com/jfhamlin/muscrat/pkg/conf"
"github.com/jfhamlin/muscrat/pkg/mrat"
"github.com/jfhamlin/muscrat/pkg/pubsub"
"github.com/jfhamlin/muscrat/pkg/ugen"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type (
App struct {
ctx context.Context
srv *mrat.Server
cancelPlayFile func()
playFileStop chan struct{}
channelBuffers [][]float64
mtx sync.Mutex
}
OpenFileDialogResponse struct {
FileName string
Content string
}
)
// NewApp creates a new App application struct
func NewApp() *App {
return &App{
playFileStop: make(chan struct{}),
srv: mrat.NewServer(),
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
a.srv.Start(context.Background())
// send at ~15 times per second, in multiples of conf.BufferSize
maxBuffersSamples := conf.SampleRate / 15
// round to nearest multiple of conf.BufferSize
maxBuffersSamples = (maxBuffersSamples/conf.BufferSize + 1) * conf.BufferSize
pubsub.Subscribe("samples", func(event string, data any) {
if samples, ok := data.([][]float64); ok {
a.mtx.Lock()
if len(a.channelBuffers) != len(samples) {
a.channelBuffers = make([][]float64, len(samples))
}
for i := range samples {
a.channelBuffers[i] = append(a.channelBuffers[i], samples[i]...)
}
if len(a.channelBuffers[0]) >= maxBuffersSamples {
cpy := make([][]float64, len(a.channelBuffers))
for i := range a.channelBuffers {
cpy[i] = make([]float64, len(a.channelBuffers[i]))
copy(cpy[i], a.channelBuffers[i])
}
go runtime.EventsEmit(ctx, "samples", cpy)
for i := range a.channelBuffers {
a.channelBuffers[i] = a.channelBuffers[i][:0]
}
}
a.mtx.Unlock()
}
})
pubsub.Subscribe(ugen.KnobsChangedEvent, func(event string, data any) {
// send the new knobs to the UI
go func() {
runtime.EventsEmit(ctx, "knobs-changed", ugen.GetKnobs())
}()
})
// forward knob value changes from the UI to the pubsub
runtime.EventsOn(ctx, "knob-value-change", func(data ...any) {
id := data[0].(float64)
value := data[1].(float64)
update := ugen.KnobUpdate{
ID: uint64(id),
Value: value,
}
pubsub.Publish(ugen.KnobValueChangeEvent, update)
})
pubsub.Subscribe("console.log", func(event string, data any) {
go runtime.EventsEmit(ctx, "console.log", data)
})
}
func (a *App) GetSampleRate() int {
return conf.SampleRate
}
// OpenFileDialog opens a file dialog.
func (a *App) OpenFileDialog() (*OpenFileDialogResponse, error) {
fileName, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Open File",
Filters: []runtime.FileFilter{
{
DisplayName: "Glojure Files (*.glj)",
Pattern: "*.glj",
},
},
})
if err != nil {
return nil, err
}
buf, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
return &OpenFileDialogResponse{
FileName: fileName,
Content: string(buf),
}, nil
}
// SaveFile saves a file. If the fileName is empty, a file dialog is
// shown. Returns the filename and an error.
func (a *App) SaveFile(fileName string, content string) (string, error) {
if fileName == "" {
var err error
fileName, err = runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "Save File",
Filters: []runtime.FileFilter{
{
DisplayName: "Glojure Files (*.glj)",
Pattern: "*.glj",
},
},
CanCreateDirectories: true,
})
if err != nil {
return "", err
}
if fileName == "" {
return "", fmt.Errorf("no file selected")
}
}
err := os.WriteFile(fileName, []byte(content), 0644)
if err != nil {
return "", err
}
return fileName, nil
}
// PlayFile plays a file. The file is re-evaluated whenever it
// changes.
func (a *App) PlayFile(fileName string) error {
a.mtx.Lock()
defer a.mtx.Unlock()
a.stopFile()
ctx, cancel := context.WithCancel(context.Background())
a.cancelPlayFile = cancel
go func() {
defer func() {
a.playFileStop <- struct{}{}
}()
if err := mrat.WatchScriptFile(ctx, fileName, a.srv); err != nil {
fmt.Printf("error watching script file: %v\n", err)
// TODO: send error to UI
return
}
}()
return nil
}
func (a *App) Silence() {
a.mtx.Lock()
defer a.mtx.Unlock()
a.stopFile()
go a.srv.PlayGraph(mrat.ZeroGraph())
}
func (a *App) stopFile() {
if a.cancelPlayFile == nil {
return
}
a.cancelPlayFile()
a.cancelPlayFile = nil
<-a.playFileStop
}
func (a *App) GetNSPublics() []mrat.Symbol {
return mrat.GetNSPublics()
}
func (a *App) GetKnobs() []*ugen.Knob {
return ugen.GetKnobs()
}