uplink/plugins/test/sine/instance.go

80 lines
1.7 KiB
Go
Raw Normal View History

package sine
import (
"io"
"log"
"time"
"git.icedream.tech/icedream/uplink/app/channels"
humanize "github.com/dustin/go-humanize"
"github.com/viert/lame"
)
type pluginInstance struct {
2018-04-11 15:55:15 +00:00
channelManager *channels.ChannelManager
}
func (instance *pluginInstance) SetChannelManager(channelManager *channels.ChannelManager) {
2018-04-11 15:55:15 +00:00
instance.channelManager = channelManager
}
func (instance *pluginInstance) Init() {
channelManager := instance.channelManager
go func() {
2018-04-11 15:55:15 +00:00
time.Sleep(2 * time.Second) // give burst cache a chance to realize
c, err := channelManager.Open("sine")
if err != nil {
log.Println("ERROR: sine channel could not be opened:", err)
log.Println("Skipping sine channel creation")
return
}
go func() {
lastTime := time.Now()
for {
lastTime = lastTime.Add(time.Second)
time.Sleep(time.Until(lastTime))
c.SetMetadata(map[string]string{
"StreamTitle": "beep - time: " + time.Now().String(),
})
}
}()
outputStream := c.AddOutputStream("mp3")
defer outputStream.Close()
outputContainer := c.AddOutputContainer("mp3")
defer outputContainer.Close()
w := io.MultiWriter(
outputContainer,
outputStream,
)
wr := lame.NewWriter(w)
wr.Encoder.SetBitrate(192)
wr.Encoder.SetQuality(1)
wr.Encoder.SetInSamplerate(44100)
wr.Encoder.SetNumChannels(2)
wr.Encoder.InitParams()
log.Println("Sine stream goroutine started")
sine := new(SineStream)
sine.Samplerate = 44100
sine.Frequency = 990
sine.Beep = true
sine.Timestamp = time.Now()
log.Println("Will now broadcast sine stream")
n, err := io.Copy(wr, sine)
if err != nil {
log.Fatal("Sine stream copy failed:", err)
}
log.Println("Sine stream finished, written", humanize.Bytes(uint64(n)), "bytes")
}()
}