2023-02-08 19:05:36 -07:00
|
|
|
package main
|
|
|
|
|
2023-02-08 22:01:39 -07:00
|
|
|
import "math"
|
2023-02-09 21:52:27 -07:00
|
|
|
import "time"
|
2023-02-08 22:01:39 -07:00
|
|
|
import "errors"
|
2023-02-08 21:41:31 -07:00
|
|
|
import "github.com/faiface/beep"
|
|
|
|
import "github.com/faiface/beep/speaker"
|
2023-02-08 19:05:36 -07:00
|
|
|
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"
|
2023-02-08 19:05:36 -07:00
|
|
|
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"
|
2023-02-08 19:05:36 -07:00
|
|
|
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-10 13:08:20 -07:00
|
|
|
var playing = map[music.Note] *toneStreamer { }
|
2023-02-10 19:55:59 -07:00
|
|
|
var adsr = ADSR {
|
|
|
|
Attack: 5 * time.Millisecond,
|
|
|
|
Decay: 400 * time.Millisecond,
|
|
|
|
Sustain: 0.7,
|
|
|
|
Release: 500 * time.Millisecond,
|
2023-02-10 23:46:12 -07:00
|
|
|
}
|
|
|
|
var gain = 0.3
|
2023-02-08 21:41:31 -07:00
|
|
|
|
2023-02-08 19:05:36 -07:00
|
|
|
func main () {
|
2023-02-08 21:41:31 -07:00
|
|
|
speaker.Init(sampleRate, bufferSize)
|
2023-02-08 19:05:36 -07:00
|
|
|
tomo.Run(run)
|
|
|
|
}
|
|
|
|
|
|
|
|
func run () {
|
|
|
|
window, _ := tomo.NewWindow(2, 2)
|
|
|
|
window.SetTitle("Piano")
|
|
|
|
container := basicElements.NewContainer(basicLayouts.Vertical { true, true })
|
2023-02-08 22:01:39 -07:00
|
|
|
controlBar := basicElements.NewContainer(basicLayouts.Horizontal { true, false })
|
2023-02-10 20:26:34 -07:00
|
|
|
|
2023-02-10 23:27:28 -07:00
|
|
|
waveformColumn := basicElements.NewContainer(basicLayouts.Vertical { true, false })
|
2023-02-10 20:26:34 -07:00
|
|
|
waveformList := basicElements.NewList (
|
|
|
|
basicElements.NewListEntry("Sine", func(){ waveform = 0 }),
|
|
|
|
basicElements.NewListEntry("Triangle", func(){ waveform = 3 }),
|
|
|
|
basicElements.NewListEntry("Square", func(){ waveform = 1 }),
|
|
|
|
basicElements.NewListEntry("Saw", func(){ waveform = 2 }),
|
|
|
|
basicElements.NewListEntry("Supersaw", func(){ waveform = 4 }),
|
|
|
|
)
|
2023-02-10 22:18:21 -07:00
|
|
|
waveformList.OnNoEntrySelected (func(){waveformList.Select(0)})
|
2023-02-10 20:26:34 -07:00
|
|
|
waveformList.Select(0)
|
2023-02-10 19:55:59 -07:00
|
|
|
|
2023-02-10 23:06:47 -07:00
|
|
|
adsrColumn := basicElements.NewContainer(basicLayouts.Vertical { true, false })
|
|
|
|
adsrGroup := basicElements.NewContainer(basicLayouts.Horizontal { true, false })
|
2023-02-10 19:55:59 -07:00
|
|
|
attackSlider := basicElements.NewLerpSlider(0, 3 * time.Second, adsr.Attack, true)
|
|
|
|
decaySlider := basicElements.NewLerpSlider(0, 3 * time.Second, adsr.Decay, true)
|
|
|
|
sustainSlider := basicElements.NewSlider(adsr.Sustain, true)
|
|
|
|
releaseSlider := basicElements.NewLerpSlider(0, 3 * time.Second, adsr.Release, true)
|
2023-02-10 23:46:12 -07:00
|
|
|
gainSlider := basicElements.NewSlider(math.Sqrt(gain), false)
|
2023-02-10 19:55:59 -07:00
|
|
|
|
|
|
|
attackSlider.OnRelease (func () {
|
|
|
|
adsr.Attack = attackSlider.Value()
|
|
|
|
})
|
|
|
|
decaySlider.OnRelease (func () {
|
|
|
|
adsr.Decay = decaySlider.Value()
|
|
|
|
})
|
|
|
|
sustainSlider.OnRelease (func () {
|
|
|
|
adsr.Sustain = sustainSlider.Value()
|
|
|
|
})
|
|
|
|
releaseSlider.OnRelease (func () {
|
|
|
|
adsr.Release = releaseSlider.Value()
|
|
|
|
})
|
2023-02-10 23:46:12 -07:00
|
|
|
gainSlider.OnRelease (func () {
|
|
|
|
gain = math.Pow(gainSlider.Value(), 2)
|
|
|
|
})
|
2023-02-10 22:58:54 -07:00
|
|
|
|
2023-02-10 23:27:28 -07:00
|
|
|
patchColumn := basicElements.NewContainer(basicLayouts.Vertical { true, false })
|
2023-02-10 22:58:54 -07:00
|
|
|
patch := func (w int, a, d time.Duration, s float64, r time.Duration) func () {
|
|
|
|
return func () {
|
|
|
|
waveform = w
|
|
|
|
adsr = ADSR {
|
|
|
|
a * time.Millisecond,
|
|
|
|
d * time.Millisecond,
|
|
|
|
s,
|
|
|
|
r * time.Millisecond,
|
|
|
|
}
|
|
|
|
waveformList.Select(w)
|
|
|
|
attackSlider .SetValue(adsr.Attack)
|
|
|
|
decaySlider .SetValue(adsr.Decay)
|
|
|
|
sustainSlider.SetValue(adsr.Sustain)
|
|
|
|
releaseSlider.SetValue(adsr.Release)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
patchList := basicElements.NewList (
|
|
|
|
basicElements.NewListEntry ("Bones", patch (
|
|
|
|
0, 0, 100, 0.0, 0)),
|
|
|
|
basicElements.NewListEntry ("Staccato", patch (
|
2023-02-10 23:27:28 -07:00
|
|
|
4, 70, 500, 0, 0)),
|
2023-02-10 22:58:54 -07:00
|
|
|
basicElements.NewListEntry ("Sustain", patch (
|
|
|
|
4, 70, 200, 0.8, 500)),
|
|
|
|
basicElements.NewListEntry ("Upright", patch (
|
|
|
|
1, 0, 500, 0.4, 70)),
|
|
|
|
basicElements.NewListEntry ("Space Pad", patch (
|
|
|
|
4, 1500, 0, 1.0, 3000)),
|
|
|
|
basicElements.NewListEntry ("Popcorn", patch (
|
|
|
|
2, 0, 40, 0.0, 0)),
|
|
|
|
basicElements.NewListEntry ("Racer", patch (
|
|
|
|
3, 70, 0, 0.7, 400)),
|
2023-02-10 23:27:28 -07:00
|
|
|
basicElements.NewListEntry ("Reverse", patch (
|
|
|
|
2, 3000, 60, 0, 0)),
|
2023-02-10 22:58:54 -07:00
|
|
|
)
|
|
|
|
patchList.Collapse(0, 32)
|
|
|
|
patchScrollBox := basicElements.NewScrollContainer(false, true)
|
2023-02-08 22:01:39 -07:00
|
|
|
|
2023-02-09 09:38:01 -07:00
|
|
|
piano := fun.NewPiano(2, 5)
|
2023-02-08 21:41:31 -07:00
|
|
|
piano.OnPress(playNote)
|
|
|
|
piano.OnRelease(stopNote)
|
2023-02-10 23:27:28 -07:00
|
|
|
|
|
|
|
// honestly, if you were doing something like this for real, i'd
|
|
|
|
// encourage you to build a custom layout because this is a bit cursed.
|
|
|
|
// i need to add more layouts...
|
|
|
|
|
2023-02-10 19:55:59 -07:00
|
|
|
window.Adopt(container)
|
2023-02-10 23:27:28 -07:00
|
|
|
|
|
|
|
controlBar.Adopt(patchColumn, true)
|
|
|
|
patchColumn.Adopt(basicElements.NewLabel("Presets", false), false)
|
|
|
|
patchColumn.Adopt(patchScrollBox, true)
|
2023-02-10 22:58:54 -07:00
|
|
|
patchScrollBox.Adopt(patchList)
|
2023-02-10 23:27:28 -07:00
|
|
|
|
2023-02-10 19:55:59 -07:00
|
|
|
controlBar.Adopt(basicElements.NewSpacer(true), false)
|
2023-02-10 23:27:28 -07:00
|
|
|
|
|
|
|
controlBar.Adopt(waveformColumn, false)
|
|
|
|
waveformColumn.Adopt(basicElements.NewLabel("Waveform", false), false)
|
|
|
|
waveformColumn.Adopt(waveformList, true)
|
|
|
|
|
|
|
|
controlBar.Adopt(basicElements.NewSpacer(true), false)
|
|
|
|
|
2023-02-10 23:06:47 -07:00
|
|
|
adsrColumn.Adopt(basicElements.NewLabel("ADSR", false), false)
|
|
|
|
adsrGroup.Adopt(attackSlider, false)
|
|
|
|
adsrGroup.Adopt(decaySlider, false)
|
|
|
|
adsrGroup.Adopt(sustainSlider, false)
|
|
|
|
adsrGroup.Adopt(releaseSlider, false)
|
|
|
|
adsrColumn.Adopt(adsrGroup, true)
|
2023-02-10 23:46:12 -07:00
|
|
|
adsrColumn.Adopt(gainSlider, false)
|
2023-02-10 23:27:28 -07:00
|
|
|
|
2023-02-10 23:06:47 -07:00
|
|
|
controlBar.Adopt(adsrColumn, false)
|
2023-02-10 19:55:59 -07:00
|
|
|
container.Adopt(controlBar, true)
|
|
|
|
container.Adopt(piano, false)
|
|
|
|
|
2023-02-10 23:27:28 -07:00
|
|
|
piano.Focus()
|
2023-02-08 19:05:36 -07:00
|
|
|
window.OnClose(tomo.Stop)
|
|
|
|
window.Show()
|
|
|
|
}
|
2023-02-08 21:41:31 -07:00
|
|
|
|
2023-02-10 22:58:54 -07:00
|
|
|
type Patch struct {
|
|
|
|
ADSR
|
|
|
|
Waveform int
|
|
|
|
}
|
|
|
|
|
2023-02-08 21:41:31 -07:00
|
|
|
func stopNote (note music.Note) {
|
|
|
|
if _, is := playing[note]; !is { return }
|
|
|
|
|
|
|
|
speaker.Lock()
|
2023-02-09 21:52:27 -07:00
|
|
|
playing[note].Release()
|
2023-02-08 21:41:31 -07:00
|
|
|
delete(playing, note)
|
|
|
|
speaker.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func playNote (note music.Note) {
|
2023-02-09 21:52:27 -07:00
|
|
|
streamer, _ := Tone (
|
|
|
|
sampleRate,
|
|
|
|
int(tuning.Tune(note)),
|
|
|
|
waveform,
|
2023-02-10 23:46:12 -07:00
|
|
|
gain,
|
2023-02-10 19:55:59 -07:00
|
|
|
adsr)
|
2023-02-08 21:41:31 -07:00
|
|
|
|
|
|
|
stopNote(note)
|
|
|
|
speaker.Lock()
|
2023-02-09 21:52:27 -07:00
|
|
|
playing[note] = streamer
|
2023-02-08 21:41:31 -07:00
|
|
|
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
|
2023-02-10 13:08:20 -07:00
|
|
|
cycles uint64
|
2023-02-09 00:04:58 -07:00
|
|
|
delta float64
|
2023-02-09 21:52:27 -07:00
|
|
|
|
2023-02-09 00:04:58 -07:00
|
|
|
waveform int
|
2023-02-09 14:15:02 -07:00
|
|
|
gain float64
|
2023-02-09 21:52:27 -07:00
|
|
|
|
|
|
|
adsr ADSR
|
|
|
|
released bool
|
|
|
|
complete bool
|
|
|
|
|
|
|
|
adsrPhase int
|
|
|
|
adsrPosition float64
|
|
|
|
adsrDeltas [4]float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type ADSR struct {
|
|
|
|
Attack time.Duration
|
|
|
|
Decay time.Duration
|
|
|
|
Sustain float64
|
|
|
|
Release time.Duration
|
2023-02-08 22:01:39 -07:00
|
|
|
}
|
|
|
|
|
2023-02-09 14:15:02 -07:00
|
|
|
func Tone (
|
2023-02-09 21:52:27 -07:00
|
|
|
sampleRate beep.SampleRate,
|
|
|
|
frequency int,
|
2023-02-09 14:15:02 -07:00
|
|
|
waveform int,
|
|
|
|
gain float64,
|
2023-02-09 21:52:27 -07:00
|
|
|
adsr ADSR,
|
2023-02-09 14:15:02 -07:00
|
|
|
) (
|
2023-02-09 21:52:27 -07:00
|
|
|
*toneStreamer,
|
2023-02-09 14:15:02 -07:00
|
|
|
error,
|
|
|
|
) {
|
2023-02-09 21:52:27 -07:00
|
|
|
if int(sampleRate) / frequency < 2 {
|
2023-02-08 22:01:39 -07:00
|
|
|
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")
|
|
|
|
}
|
2023-02-09 21:52:27 -07:00
|
|
|
|
2023-02-08 22:01:39 -07:00
|
|
|
tone := new(toneStreamer)
|
2023-02-09 00:04:58 -07:00
|
|
|
tone.waveform = waveform
|
2023-02-09 21:52:27 -07:00
|
|
|
tone.position = 0.0
|
|
|
|
steps := float64(sampleRate) / float64(frequency)
|
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-09 21:52:27 -07:00
|
|
|
|
|
|
|
if adsr.Attack < time.Millisecond { adsr.Attack = time.Millisecond }
|
|
|
|
if adsr.Decay < time.Millisecond { adsr.Decay = time.Millisecond }
|
|
|
|
if adsr.Release < time.Millisecond { adsr.Release = time.Millisecond }
|
|
|
|
tone.adsr = adsr
|
|
|
|
|
|
|
|
attackSteps := adsr.Attack.Seconds() * float64(sampleRate)
|
|
|
|
decaySteps := adsr.Decay.Seconds() * float64(sampleRate)
|
|
|
|
releaseSteps := adsr.Release.Seconds() * float64(sampleRate)
|
|
|
|
tone.adsrDeltas[0] = 1 / attackSteps
|
|
|
|
tone.adsrDeltas[1] = 1 / decaySteps
|
|
|
|
tone.adsrDeltas[2] = 0
|
|
|
|
tone.adsrDeltas[3] = 1 / releaseSteps
|
|
|
|
|
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:
|
2023-02-10 13:08:20 -07:00
|
|
|
unison := 5
|
|
|
|
detuneDelta := 0.00005
|
|
|
|
|
|
|
|
detune := 0.0 - (float64(unison) / 2) * detuneDelta
|
|
|
|
for i := 0; i < unison; i ++ {
|
|
|
|
_, offset := math.Modf(detune * float64(tone.cycles) + tone.position)
|
|
|
|
sample += (offset - 0.5) * 2
|
|
|
|
detune += detuneDelta
|
|
|
|
}
|
|
|
|
|
|
|
|
sample /= float64(unison)
|
2023-02-08 22:01:39 -07:00
|
|
|
}
|
2023-02-09 21:52:27 -07:00
|
|
|
|
|
|
|
adsrGain := 0.0
|
|
|
|
switch tone.adsrPhase {
|
|
|
|
case 0: adsrGain = tone.adsrPosition
|
|
|
|
if tone.adsrPosition > 1 {
|
|
|
|
tone.adsrPosition = 0
|
|
|
|
tone.adsrPhase = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
case 1: adsrGain = 1 + tone.adsrPosition * (tone.adsr.Sustain - 1)
|
|
|
|
if tone.adsrPosition > 1 {
|
|
|
|
tone.adsrPosition = 0
|
|
|
|
tone.adsrPhase = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
case 2: adsrGain = tone.adsr.Sustain
|
|
|
|
if tone.released {
|
|
|
|
tone.adsrPhase = 3
|
|
|
|
}
|
|
|
|
|
|
|
|
case 3: adsrGain = (1 - tone.adsrPosition) * tone.adsr.Sustain
|
|
|
|
if tone.adsrPosition > 1 {
|
|
|
|
tone.adsrPosition = 0
|
|
|
|
tone.complete = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sample *= adsrGain * adsrGain
|
|
|
|
|
|
|
|
tone.adsrPosition += tone.adsrDeltas[tone.adsrPhase]
|
2023-02-09 00:04:58 -07:00
|
|
|
_, tone.position = math.Modf(tone.position + tone.delta)
|
2023-02-10 13:08:20 -07:00
|
|
|
tone.cycles ++
|
2023-02-08 22:01:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tone *toneStreamer) Stream (buf [][2]float64) (int, bool) {
|
2023-02-09 21:52:27 -07:00
|
|
|
if tone.complete {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2023-02-08 22:01:39 -07:00
|
|
|
for i := 0; i < len(buf); i++ {
|
2023-02-09 21:52:27 -07:00
|
|
|
sample := 0.0
|
|
|
|
if !tone.complete {
|
|
|
|
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
|
|
|
|
}
|
2023-02-09 21:52:27 -07:00
|
|
|
|
2023-02-08 22:01:39 -07:00
|
|
|
func (tone *toneStreamer) Err () error {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-09 21:52:27 -07:00
|
|
|
|
|
|
|
func (tone *toneStreamer) Release () {
|
|
|
|
tone.released = true
|
|
|
|
}
|