diff --git a/main.go b/main.go index 9cdb18f..7f9aea9 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "time" @@ -10,14 +11,13 @@ import ( ) var ( - count time.Duration = 15 * time.Second - str string = "" - p *widgets.Paragraph + str string = "" + p *widgets.Paragraph ) const ( playSound = true - timer = 15 * time.Second + timer = 5 * time.Second ) func prepareScreen() { @@ -37,9 +37,14 @@ func updateScreen() { } func main() { + fmt.Println("Playing only sound for 5s") + rofl := roflcopter.NewRoflcopter(timer, &str, updateScreen) + wg := rofl.OnlySound() + wg.Wait() + time.Sleep(500 * time.Millisecond) prepareScreen() - rofl := roflcopter.NewRoflcopter(timer, &str, playSound, updateScreen) - rofl.Start() - time.Sleep(timer + 500*time.Millisecond) + wg = rofl.Start(playSound) + wg.Wait() + time.Sleep(500 * time.Millisecond) ui.Close() } diff --git a/roflcopter/roflcopter.go b/roflcopter/roflcopter.go index c88ed87..5347327 100644 --- a/roflcopter/roflcopter.go +++ b/roflcopter/roflcopter.go @@ -18,7 +18,7 @@ var swah []byte var ( work bool = true duration time.Duration - wg sync.WaitGroup + inWork *sync.WaitGroup ) const ( @@ -27,16 +27,16 @@ const ( ) type Roflcopter struct { - output bool work bool count time.Duration str *string runner func() } -func NewRoflcopter(amount time.Duration, outStr *string, out bool, funcrunner func()) *Roflcopter { +// NewRoflcopter creates a new instance of roflcopter. amount sets duration, outStr is used to output frames, funcrunner will be called after frame change +func NewRoflcopter(amount time.Duration, outStr *string, funcrunner func()) *Roflcopter { + inWork = new(sync.WaitGroup) return &Roflcopter{ - output: out, work: true, str: outStr, count: amount, @@ -44,31 +44,54 @@ func NewRoflcopter(amount time.Duration, outStr *string, out bool, funcrunner fu } } -func (b *Roflcopter) Start() { +// Start starts frame changing. Soundplaying can be set by true or false. Returns WaitGroup to know when it has finished work. +func (b *Roflcopter) Start(playSound bool) *sync.WaitGroup { + var wg *sync.WaitGroup = new(sync.WaitGroup) + inWork.Add(1) *b.str = frame1 + b.count.String() work = b.work duration = b.count - if b.output { - go b.player() + if playSound { + go b.player(wg) } - go b.timecounter(500 * time.Millisecond) + go b.timeCounter(500 * time.Millisecond) go b.frameChanger(125 * time.Millisecond) + return inWork } -func (b *Roflcopter) timecounter(step time.Duration) { - for b.count >= 0 { - b.count -= step +// OnlySound only plays sound. Frames are not changing. Returns WaitGroup to know when it has finished work +func (b *Roflcopter) OnlySound() *sync.WaitGroup { + var wg *sync.WaitGroup = new(sync.WaitGroup) + inWork.Add(1) + work = b.work + duration = b.count + go b.player(wg) + go b.timeCounter(500 * time.Millisecond) + return inWork +} + +// timeCounter counts time and stops other work when time ends +func (b *Roflcopter) timeCounter(step time.Duration) { + for duration > 0 { + duration -= step time.Sleep(step) } work = false + time.Sleep(100 * time.Millisecond) } -func (b *Roflcopter) player() { - b.sound() +//player is a loop to start sound play +func (b *Roflcopter) player(wg *sync.WaitGroup) { + b.sound(wg) wg.Wait() - b.player() + if work { + b.player(wg) + } else { + inWork.Done() + } } +//frameChanger is a loop to change frames func (b *Roflcopter) frameChanger(delay time.Duration) { nextframe := 2 @@ -76,23 +99,25 @@ func (b *Roflcopter) frameChanger(delay time.Duration) { switch nextframe { case 1: { - *b.str = frame1 + "Ends in " + b.count.String() + *b.str = frame1 + "Ends in " + duration.String() time.Sleep(delay) nextframe = 2 } case 2: { - *b.str = frame2 + "Ends in " + b.count.String() + *b.str = frame2 + "Ends in " + duration.String() time.Sleep(delay) nextframe = 1 } } b.runner() } + inWork.Done() } -func (b *Roflcopter) sound() { +//sound plays sound +func (b *Roflcopter) sound(wg *sync.WaitGroup) { wg.Add(1) t := 1*time.Second + 380*time.Millisecond streamer, format, err := mp3.Decode(io.NopCloser(bytes.NewReader(swah)))