fix(tracker/sequencer): add a way to exit the sequencer loop

This commit is contained in:
Matias Lahti 2020-11-08 04:27:52 +02:00
parent d30388a09a
commit b1ac141ea5
3 changed files with 14 additions and 3 deletions

View File

@ -21,7 +21,9 @@ func main() {
app.Size(unit.Dp(800), unit.Dp(600)),
app.Title("Sointu Tracker"),
)
if err := tracker.New(plr).Run(w); err != nil {
t := tracker.New(plr)
defer t.Close()
if err := t.Run(w); err != nil {
fmt.Println(err)
os.Exit(1)
}

View File

@ -12,7 +12,7 @@ func (t *Tracker) TogglePlay() {
}
// sequencerLoop is the main goroutine that handles the playing logic
func (t *Tracker) sequencerLoop() {
func (t *Tracker) sequencerLoop(closer chan struct{}) {
playing := false
rowTime := (time.Second * 60) / time.Duration(4*t.song.BPM)
tick := make(<-chan time.Time)
@ -46,6 +46,8 @@ func (t *Tracker) sequencerLoop() {
atomic.StoreInt32(&t.PlayRow, int32(rowJump))
case patternJump := <-t.patternJump:
atomic.StoreInt32(&t.PlayPattern, int32(patternJump))
case <-closer:
return
case playState := <-t.setPlaying:
playing = playState
if playing {

View File

@ -26,6 +26,7 @@ type Tracker struct {
player audio.Player
synth go4k.Synth
playBuffer []float32
closer chan struct{}
}
func (t *Tracker) LoadSong(song go4k.Song) error {
@ -42,6 +43,11 @@ func (t *Tracker) LoadSong(song go4k.Song) error {
return nil
}
func (t *Tracker) Close() {
t.player.Close()
t.closer <- struct{}{}
}
func New(player audio.Player) *Tracker {
t := &Tracker{
QuitButton: new(widget.Clickable),
@ -51,8 +57,9 @@ func New(player audio.Player) *Tracker {
rowJump: make(chan int),
patternJump: make(chan int),
ticked: make(chan struct{}),
closer: make(chan struct{}),
}
go t.sequencerLoop()
go t.sequencerLoop(t.closer)
if err := t.LoadSong(defaultSong); err != nil {
panic(fmt.Errorf("cannot load default song: %w", err))
}