2016-11-27 03:44:09 +08:00
|
|
|
// Package audio is based on aplay audio adaptor written by @colemanserious (https://github.com/colemanserious)
|
2016-05-14 07:09:36 +08:00
|
|
|
package audio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2016-09-25 18:17:01 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
2016-05-14 07:09:36 +08:00
|
|
|
)
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Driver is gobot software device for audio playback
|
2016-09-25 18:17:01 +08:00
|
|
|
type Driver struct {
|
2016-05-14 07:09:36 +08:00
|
|
|
name string
|
|
|
|
connection gobot.Connection
|
|
|
|
interval time.Duration
|
|
|
|
halt chan bool
|
|
|
|
gobot.Eventer
|
|
|
|
gobot.Commander
|
2016-05-25 12:01:16 +08:00
|
|
|
filename string
|
2016-05-14 07:09:36 +08:00
|
|
|
}
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// NewDriver returns a new audio Driver. It accepts:
|
|
|
|
//
|
|
|
|
// *Adaptor: The audio adaptor to use for the driver
|
|
|
|
// string: The filename of the audio to start playing
|
|
|
|
//
|
2016-09-25 18:17:01 +08:00
|
|
|
func NewDriver(a *Adaptor, filename string) *Driver {
|
2016-11-27 03:44:09 +08:00
|
|
|
return &Driver{
|
2017-02-02 22:50:49 +08:00
|
|
|
name: gobot.DefaultName("Audio"),
|
2016-05-14 07:09:36 +08:00
|
|
|
connection: a,
|
|
|
|
interval: 500 * time.Millisecond,
|
2016-05-25 12:01:16 +08:00
|
|
|
filename: filename,
|
2016-05-14 07:09:36 +08:00
|
|
|
halt: make(chan bool, 0),
|
|
|
|
Eventer: gobot.NewEventer(),
|
|
|
|
Commander: gobot.NewCommander(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Name returns the Driver Name
|
2016-09-25 18:17:01 +08:00
|
|
|
func (d *Driver) Name() string { return d.name }
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// SetName sets the Driver Name
|
2016-09-25 18:17:01 +08:00
|
|
|
func (d *Driver) SetName(n string) { d.name = n }
|
2016-05-14 07:09:36 +08:00
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Filename returns the file name for the driver to playback
|
2016-09-25 18:17:01 +08:00
|
|
|
func (d *Driver) Filename() string { return d.filename }
|
2016-05-25 12:01:16 +08:00
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Connection returns the Driver Connection
|
2016-09-25 18:17:01 +08:00
|
|
|
func (d *Driver) Connection() gobot.Connection {
|
2016-05-14 07:09:36 +08:00
|
|
|
return d.connection
|
|
|
|
}
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Sound plays back a sound file. It accepts:
|
|
|
|
//
|
|
|
|
// string: The filename of the audio to start playing
|
2016-09-25 18:17:01 +08:00
|
|
|
func (d *Driver) Sound(fileName string) []error {
|
|
|
|
return d.Connection().(*Adaptor).Sound(fileName)
|
2016-05-14 07:09:36 +08:00
|
|
|
}
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Play plays back the current sound file.
|
2016-09-25 18:17:01 +08:00
|
|
|
func (d *Driver) Play() []error {
|
2016-05-25 12:01:16 +08:00
|
|
|
return d.Sound(d.Filename())
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:17:01 +08:00
|
|
|
func (d *Driver) adaptor() *Adaptor {
|
|
|
|
return d.Connection().(*Adaptor)
|
2016-05-14 07:09:36 +08:00
|
|
|
}
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Start starts the Driver
|
2016-11-08 00:27:49 +08:00
|
|
|
func (d *Driver) Start() (err error) {
|
2016-05-14 07:09:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Halt halts the Driver
|
2016-11-08 00:27:49 +08:00
|
|
|
func (d *Driver) Halt() (err error) {
|
2016-05-14 07:09:36 +08:00
|
|
|
return
|
|
|
|
}
|