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
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
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
|
|
|
|
//
|
2023-05-20 20:25:21 +08:00
|
|
|
// 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,
|
2023-05-19 20:16:22 +08:00
|
|
|
halt: make(chan bool),
|
2016-05-14 07:09:36 +08:00
|
|
|
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:
|
|
|
|
//
|
2023-05-20 20:25:21 +08:00
|
|
|
// string: The filename of the audio to start playing
|
2016-09-25 18:17:01 +08:00
|
|
|
func (d *Driver) Sound(fileName string) []error {
|
2023-11-16 03:51:52 +08:00
|
|
|
//nolint:forcetypeassert // ok here
|
2016-09-25 18:17:01 +08:00
|
|
|
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-11-27 03:44:09 +08:00
|
|
|
// Start starts the Driver
|
2023-11-16 03:51:52 +08:00
|
|
|
func (d *Driver) Start() error {
|
|
|
|
return nil
|
2016-05-14 07:09:36 +08:00
|
|
|
}
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Halt halts the Driver
|
2023-11-16 03:51:52 +08:00
|
|
|
func (d *Driver) Halt() error {
|
|
|
|
return nil
|
2016-05-14 07:09:36 +08:00
|
|
|
}
|