This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/examples/piano/main.go

144 lines
3.4 KiB
Go
Raw Normal View History

package main
2023-02-08 22:01:39 -07:00
import "math"
import "errors"
2023-02-08 21:41:31 -07:00
import "github.com/faiface/beep"
import "github.com/faiface/beep/speaker"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/elements/fun"
2023-02-08 21:41:31 -07:00
import "git.tebibyte.media/sashakoshka/tomo/layouts/basic"
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
2023-02-08 21:41:31 -07:00
import "git.tebibyte.media/sashakoshka/tomo/elements/fun/music"
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
2023-02-08 21:41:31 -07:00
const sampleRate = 44100
const bufferSize = 256
var tuning = music.EqualTemparment { A4: 440 }
2023-02-08 22:01:39 -07:00
var waveform = 0
2023-02-08 21:41:31 -07:00
var playing = map[music.Note] *beep.Ctrl { }
func main () {
2023-02-08 21:41:31 -07:00
speaker.Init(sampleRate, bufferSize)
tomo.Run(run)
}
func run () {
window, _ := tomo.NewWindow(2, 2)
window.SetTitle("Piano")
container := basicElements.NewContainer(basicLayouts.Vertical { true, true })
window.Adopt(container)
2023-02-08 22:01:39 -07:00
controlBar := basicElements.NewContainer(basicLayouts.Horizontal { true, false })
label := basicElements.NewLabel("Play a song!", false)
2023-02-08 22:01:39 -07:00
controlBar.Adopt(label, true)
waveformButton := basicElements.NewButton("Sine")
waveformButton.OnClick (func () {
2023-02-09 00:04:58 -07:00
waveform = (waveform + 1) % 5
2023-02-08 22:01:39 -07:00
switch waveform {
case 0: waveformButton.SetText("Sine")
case 1: waveformButton.SetText("Square")
2023-02-09 00:04:58 -07:00
case 2: waveformButton.SetText("Saw")
case 3: waveformButton.SetText("Triangle")
case 4: waveformButton.SetText("Supersaw")
2023-02-08 22:01:39 -07:00
}
})
controlBar.Adopt(waveformButton, false)
container.Adopt(controlBar, false)
2023-02-09 09:38:01 -07:00
piano := fun.NewPiano(2, 5)
container.Adopt(piano, true)
2023-02-08 21:41:31 -07:00
piano.OnPress(playNote)
piano.OnRelease(stopNote)
2023-02-09 14:36:38 -07:00
piano.Focus()
window.OnClose(tomo.Stop)
window.Show()
}
2023-02-08 21:41:31 -07:00
func stopNote (note music.Note) {
if _, is := playing[note]; !is { return }
speaker.Lock()
playing[note].Streamer = nil
delete(playing, note)
speaker.Unlock()
}
func playNote (note music.Note) {
2023-02-09 14:15:02 -07:00
streamer, _ := Tone(sampleRate, int(tuning.Tune(note)), waveform, 0.3)
2023-02-08 21:41:31 -07:00
stopNote(note)
speaker.Lock()
playing[note] = &beep.Ctrl { Streamer: streamer }
speaker.Unlock()
speaker.Play(playing[note])
}
2023-02-08 22:01:39 -07:00
// https://github.com/faiface/beep/blob/v1.1.0/generators/toner.go
2023-02-09 00:04:58 -07:00
// Adapted to be a bit more versatile.
2023-02-08 22:01:39 -07:00
type toneStreamer struct {
2023-02-09 00:04:58 -07:00
position float64
delta float64
waveform int
2023-02-09 14:15:02 -07:00
gain float64
2023-02-08 22:01:39 -07:00
}
2023-02-09 14:15:02 -07:00
func Tone (
sr beep.SampleRate,
freq int,
waveform int,
gain float64,
) (
beep.Streamer,
error,
) {
2023-02-08 22:01:39 -07:00
if int(sr) / freq < 2 {
return nil, errors.New (
2023-02-09 14:15:02 -07:00
"tone generator: samplerate must be at least " +
2023-02-08 22:01:39 -07:00
"2 times greater then frequency")
}
tone := new(toneStreamer)
2023-02-09 00:04:58 -07:00
tone.position = 0.0
tone.waveform = waveform
steps := float64(sr) / float64(freq)
2023-02-08 22:01:39 -07:00
tone.delta = 1.0 / steps
2023-02-09 14:15:02 -07:00
tone.gain = gain
2023-02-08 22:01:39 -07:00
return tone, nil
}
func (tone *toneStreamer) nextSample () (sample float64) {
2023-02-09 00:04:58 -07:00
switch tone.waveform {
case 0:
sample = math.Sin(tone.position * 2.0 * math.Pi)
case 1:
if tone.position > 0.5 {
sample = 1
} else {
sample = -1
}
case 2:
sample = (tone.position - 0.5) * 2
case 3:
sample = 1 - math.Abs(tone.position - 0.5) * 4
case 4:
sample =
-1 + 13.7 * tone.position +
28.32 * tone.position * tone.position +
15.62 * tone.position * tone.position * tone.position
2023-02-08 22:01:39 -07:00
}
2023-02-09 00:04:58 -07:00
_, tone.position = math.Modf(tone.position + tone.delta)
2023-02-08 22:01:39 -07:00
return
}
func (tone *toneStreamer) Stream (buf [][2]float64) (int, bool) {
for i := 0; i < len(buf); i++ {
2023-02-09 14:15:02 -07:00
sample := tone.nextSample() * tone.gain
2023-02-09 00:04:58 -07:00
buf[i] = [2]float64{sample, sample}
2023-02-08 22:01:39 -07:00
}
return len(buf), true
}
func (tone *toneStreamer) Err () error {
return nil
}