Moar waveforms!!!!
This commit is contained in:
parent
16a0e76145
commit
16ce15621e
@ -133,7 +133,6 @@ func (element *Button) redo () {
|
|||||||
|
|
||||||
func (element *Button) draw () {
|
func (element *Button) draw () {
|
||||||
bounds := element.Bounds()
|
bounds := element.Bounds()
|
||||||
// println(bounds.String(), element.text)
|
|
||||||
// debug.PrintStack()
|
// debug.PrintStack()
|
||||||
|
|
||||||
state := theme.PatternState {
|
state := theme.PatternState {
|
||||||
|
@ -65,8 +65,11 @@ func (element *Container) Adopt (child elements.Element, expand bool) {
|
|||||||
element.core.DamageRegion(region.Bounds())
|
element.core.DamageRegion(region.Bounds())
|
||||||
})
|
})
|
||||||
child.OnMinimumSizeChange (func () {
|
child.OnMinimumSizeChange (func () {
|
||||||
|
// TODO: this could probably stand to be more efficient. I mean
|
||||||
|
// seriously?
|
||||||
element.updateMinimumSize()
|
element.updateMinimumSize()
|
||||||
element.redoAll()
|
element.redoAll()
|
||||||
|
element.core.DamageAll()
|
||||||
})
|
})
|
||||||
if child0, ok := child.(elements.Flexible); ok {
|
if child0, ok := child.(elements.Flexible); ok {
|
||||||
child0.OnFlexibleHeightChange(element.updateMinimumSize)
|
child0.OnFlexibleHeightChange(element.updateMinimumSize)
|
||||||
@ -214,7 +217,7 @@ func (element *Container) childPosition (child elements.Element) (position image
|
|||||||
func (element *Container) redoAll () {
|
func (element *Container) redoAll () {
|
||||||
if !element.core.HasImage() { return }
|
if !element.core.HasImage() { return }
|
||||||
// do a layout
|
// do a layout
|
||||||
element.recalculate()
|
element.doLayout()
|
||||||
|
|
||||||
// draw a background
|
// draw a background
|
||||||
bounds := element.Bounds()
|
bounds := element.Bounds()
|
||||||
@ -516,7 +519,7 @@ func (element *Container) updateMinimumSize () {
|
|||||||
element.core.SetMinimumSize(width, height)
|
element.core.SetMinimumSize(width, height)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (element *Container) recalculate () {
|
func (element *Container) doLayout () {
|
||||||
element.layout.Arrange (
|
element.layout.Arrange (
|
||||||
element.children, element.config.Margin(), element.Bounds())
|
element.children, element.config.Margin(), element.Bounds())
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import "math"
|
|||||||
import "errors"
|
import "errors"
|
||||||
import "github.com/faiface/beep"
|
import "github.com/faiface/beep"
|
||||||
import "github.com/faiface/beep/speaker"
|
import "github.com/faiface/beep/speaker"
|
||||||
import "github.com/faiface/beep/generators"
|
|
||||||
import "git.tebibyte.media/sashakoshka/tomo"
|
import "git.tebibyte.media/sashakoshka/tomo"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/elements/fun"
|
import "git.tebibyte.media/sashakoshka/tomo/elements/fun"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/layouts/basic"
|
import "git.tebibyte.media/sashakoshka/tomo/layouts/basic"
|
||||||
@ -32,13 +31,15 @@ func run () {
|
|||||||
controlBar := basicElements.NewContainer(basicLayouts.Horizontal { true, false })
|
controlBar := basicElements.NewContainer(basicLayouts.Horizontal { true, false })
|
||||||
label := basicElements.NewLabel("Play a song!", false)
|
label := basicElements.NewLabel("Play a song!", false)
|
||||||
controlBar.Adopt(label, true)
|
controlBar.Adopt(label, true)
|
||||||
controlBar.Adopt(basicElements.NewLabel("Play a song!", false), true)
|
|
||||||
waveformButton := basicElements.NewButton("Sine")
|
waveformButton := basicElements.NewButton("Sine")
|
||||||
waveformButton.OnClick (func () {
|
waveformButton.OnClick (func () {
|
||||||
waveform = (waveform + 1) % 2
|
waveform = (waveform + 1) % 5
|
||||||
switch waveform {
|
switch waveform {
|
||||||
case 0: waveformButton.SetText("Sine")
|
case 0: waveformButton.SetText("Sine")
|
||||||
case 1: waveformButton.SetText("Square")
|
case 1: waveformButton.SetText("Square")
|
||||||
|
case 2: waveformButton.SetText("Saw")
|
||||||
|
case 3: waveformButton.SetText("Triangle")
|
||||||
|
case 4: waveformButton.SetText("Supersaw")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
controlBar.Adopt(waveformButton, false)
|
controlBar.Adopt(waveformButton, false)
|
||||||
@ -63,11 +64,7 @@ func stopNote (note music.Note) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func playNote (note music.Note) {
|
func playNote (note music.Note) {
|
||||||
var streamer beep.Streamer
|
streamer, _ := Tone(sampleRate, int(tuning.Tune(note)), waveform)
|
||||||
switch waveform {
|
|
||||||
case 0: streamer, _ = generators.SinTone(sampleRate, int(tuning.Tune(note)))
|
|
||||||
case 1: streamer, _ = SquareTone(sampleRate, int(tuning.Tune(note)))
|
|
||||||
}
|
|
||||||
|
|
||||||
stopNote(note)
|
stopNote(note)
|
||||||
speaker.Lock()
|
speaker.Lock()
|
||||||
@ -77,42 +74,56 @@ func playNote (note music.Note) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/faiface/beep/blob/v1.1.0/generators/toner.go
|
// https://github.com/faiface/beep/blob/v1.1.0/generators/toner.go
|
||||||
// Adapted to be a square wave instead
|
// Adapted to be a bit more versatile.
|
||||||
|
|
||||||
type toneStreamer struct {
|
type toneStreamer struct {
|
||||||
stat float64
|
position float64
|
||||||
delta float64
|
delta float64
|
||||||
|
waveform int
|
||||||
}
|
}
|
||||||
|
|
||||||
func SquareTone (sr beep.SampleRate, freq int) (beep.Streamer, error) {
|
func Tone (sr beep.SampleRate, freq int, waveform int) (beep.Streamer, error) {
|
||||||
if int(sr) / freq < 2 {
|
if int(sr) / freq < 2 {
|
||||||
return nil, errors.New (
|
return nil, errors.New (
|
||||||
"square tone generator: samplerate must be at least " +
|
"square tone generator: samplerate must be at least " +
|
||||||
"2 times greater then frequency")
|
"2 times greater then frequency")
|
||||||
}
|
}
|
||||||
tone := new(toneStreamer)
|
tone := new(toneStreamer)
|
||||||
tone.stat = 0.0
|
tone.position = 0.0
|
||||||
srf := float64(sr)
|
tone.waveform = waveform
|
||||||
ff := float64(freq)
|
steps := float64(sr) / float64(freq)
|
||||||
steps := srf / ff
|
|
||||||
tone.delta = 1.0 / steps
|
tone.delta = 1.0 / steps
|
||||||
return tone, nil
|
return tone, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tone *toneStreamer) nextSample () (sample float64) {
|
func (tone *toneStreamer) nextSample () (sample float64) {
|
||||||
if tone.stat > 0.5 {
|
switch tone.waveform {
|
||||||
sample = 1
|
case 0:
|
||||||
} else {
|
sample = math.Sin(tone.position * 2.0 * math.Pi)
|
||||||
sample = 0
|
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
|
||||||
}
|
}
|
||||||
_, tone.stat = math.Modf(tone.stat + tone.delta)
|
_, tone.position = math.Modf(tone.position + tone.delta)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tone *toneStreamer) Stream (buf [][2]float64) (int, bool) {
|
func (tone *toneStreamer) Stream (buf [][2]float64) (int, bool) {
|
||||||
for i := 0; i < len(buf); i++ {
|
for i := 0; i < len(buf); i++ {
|
||||||
s := tone.nextSample()
|
sample := tone.nextSample()
|
||||||
buf[i] = [2]float64{s, s}
|
buf[i] = [2]float64{sample, sample}
|
||||||
}
|
}
|
||||||
return len(buf), true
|
return len(buf), true
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user