-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (161 loc) · 4.66 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"fmt"
"image/color"
"image/png"
"math"
"math/rand"
"os"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/faiface/beep"
"github.com/faiface/beep/speaker"
)
type SweepingSineWave struct {
startFreq, endFreq float64
duration time.Duration
sampleRate beep.SampleRate
samplesPlayed int
channel int
}
func NewSweepingSineWave(startFreq, endFreq float64, duration time.Duration, sampleRate beep.SampleRate, channel int) *SweepingSineWave {
return &SweepingSineWave{
startFreq: startFreq,
endFreq: endFreq,
duration: duration,
sampleRate: sampleRate,
samplesPlayed: 0,
channel: channel,
}
}
func (s *SweepingSineWave) Stream(samples [][2]float64) (n int, ok bool) {
for i := range samples {
freq := s.startFreq + (s.endFreq-s.startFreq)*float64(s.samplesPlayed)/(float64(s.sampleRate.N(s.duration)))
samples[i][s.channel] = math.Sin(2 * math.Pi * freq * float64(s.samplesPlayed) / float64(s.sampleRate))
s.samplesPlayed++
}
return len(samples), true
}
func (s *SweepingSineWave) Err() error {
return nil
}
var stop chan bool
func main() {
duration := 30 * time.Minute
sampleRate := beep.SampleRate(44100)
speaker.Init(sampleRate, sampleRate.N(time.Second/10))
stop = make(chan bool)
a := app.New()
w := a.NewWindow("albino")
presetDropdown := widget.NewSelect(
getPresetNames(),
nil,
)
presetDropdown.PlaceHolder = "Select a preset"
presetDropdown.SetSelected(selectRandomPreset())
playingImageFile, err := os.Open("./img/playing2.png")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open playing image file: %v\n", err)
return
}
defer playingImageFile.Close()
playingImage, err := png.Decode(playingImageFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to decode playing image file: %v\n", err)
return
}
playingCanvas := canvas.NewImageFromImage(playingImage)
playingCanvas.SetMinSize(fyne.NewSize(580, 260))
playingCanvas.Hide()
playButton := widget.NewButton(" Play ", func() {
if presetDropdown.Selected == "" {
return
}
if !playingCanvas.Visible() {
go playPreset(presetDropdown.Selected, duration, sampleRate)
playingCanvas.Show()
playingCanvas.Refresh()
}
})
stopButton := widget.NewButton(" Stop ", func() {
if playingCanvas.Visible() {
speaker.Clear()
stop <- true
playingCanvas.Hide()
}
})
buttons := container.NewHBox(layout.NewSpacer(), playButton, stopButton, layout.NewSpacer())
imageFile, err := os.Open("./img/background.png")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open image file: %v\n", err)
return
}
defer imageFile.Close()
image, err := png.Decode(imageFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to decode image file: %v\n", err)
return
}
background := canvas.NewImageFromImage(image)
background.FillMode = canvas.ImageFillStretch
background.Refresh()
content := container.NewVBox(
container.NewHBox(layout.NewSpacer(), presetDropdown, layout.NewSpacer()),
layout.NewSpacer(),
buttons,
layout.NewSpacer(),
layout.NewSpacer(),
layout.NewSpacer(),
layout.NewSpacer(),
container.NewGridWithColumns(1,
container.NewHBox(layout.NewSpacer(), playingCanvas, layout.NewSpacer()),
),
layout.NewSpacer(),
)
marginTop := canvas.NewRectangle(color.Transparent)
marginTop.SetMinSize(fyne.NewSize(0, 35))
marginContent := container.NewVBox(marginTop, content)
contentWithBackground := container.New(layout.NewBorderLayout(nil, nil, nil, nil),
background, marginContent)
w.SetContent(contentWithBackground)
w.Resize(fyne.NewSize(600, 400))
w.ShowAndRun()
}
func getPresetNames() []string {
names := make([]string, 0, len(presets))
for name := range presets {
names = append(names, name)
}
return names
}
func playPreset(preset string, duration time.Duration, sampleRate beep.SampleRate) {
frequencies, ok := presets[preset]
if !ok {
fmt.Fprintf(os.Stderr, "Invalid preset name. Available presets: %v\n", getPresetNames())
return
}
leftSine := NewSweepingSineWave(frequencies[0][0], frequencies[0][1], duration, sampleRate, 0)
rightSine := NewSweepingSineWave(frequencies[1][0], frequencies[1][1], duration, sampleRate, 1)
mergedStream := beep.Mix(rightSine, leftSine)
done := make(chan bool)
speaker.Play(beep.Seq(mergedStream, beep.Callback(func() {
done <- true
})))
select {
case <-done:
case <-stop:
speaker.Lock()
speaker.Unlock()
}
}
func selectRandomPreset() string {
presetNames := getPresetNames()
src := rand.NewSource(time.Now().UnixNano())
r := rand.New(src)
return presetNames[r.Intn(len(presetNames))]
}