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 (
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-05-16 01:46:35 +08:00
|
|
|
"path"
|
2017-02-02 22:50:49 +08:00
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
2016-05-14 07:09:36 +08:00
|
|
|
)
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Adaptor is gobot Adaptor connection to audio playback
|
2016-09-25 18:17:01 +08:00
|
|
|
type Adaptor struct {
|
2016-05-14 07:09:36 +08:00
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// NewAdaptor returns a new audio Adaptor
|
|
|
|
//
|
2016-09-25 18:17:01 +08:00
|
|
|
func NewAdaptor() *Adaptor {
|
2017-02-02 22:50:49 +08:00
|
|
|
return &Adaptor{name: gobot.DefaultName("Audio")}
|
2016-05-14 07:09:36 +08:00
|
|
|
}
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Name returns the Adaptor Name
|
2016-09-25 18:17:01 +08:00
|
|
|
func (a *Adaptor) Name() string { return a.name }
|
2016-05-14 07:09:36 +08:00
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// SetName sets the Adaptor Name
|
2016-09-25 18:17:01 +08:00
|
|
|
func (a *Adaptor) SetName(n string) { a.name = n }
|
2016-05-14 07:09:36 +08:00
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Connect establishes a connection to the Audio adaptor
|
2016-11-08 00:27:49 +08:00
|
|
|
func (a *Adaptor) Connect() error { return nil }
|
2016-05-14 07:09:36 +08:00
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Finalize terminates the connection to the Audio adaptor
|
2016-11-08 00:27:49 +08:00
|
|
|
func (a *Adaptor) Finalize() error { return nil }
|
2016-09-25 18:17:01 +08:00
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// Sound plays a sound and accepts:
|
|
|
|
//
|
|
|
|
// string: The filename of the audio to start playing
|
2016-09-25 18:17:01 +08:00
|
|
|
func (a *Adaptor) Sound(fileName string) []error {
|
2016-05-14 07:09:36 +08:00
|
|
|
var errorsList []error
|
|
|
|
|
|
|
|
if fileName == "" {
|
2016-05-25 09:49:58 +08:00
|
|
|
log.Println("Requires filename for audio file.")
|
2016-05-16 01:46:35 +08:00
|
|
|
errorsList = append(errorsList, errors.New("Requires filename for audio file."))
|
2016-05-14 07:09:36 +08:00
|
|
|
return errorsList
|
|
|
|
}
|
|
|
|
|
2016-05-16 01:46:35 +08:00
|
|
|
_, err := os.Stat(fileName)
|
2016-05-14 07:09:36 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
errorsList = append(errorsList, err)
|
|
|
|
return errorsList
|
|
|
|
}
|
|
|
|
|
2016-05-16 01:46:35 +08:00
|
|
|
// command to play audio file based on file type
|
2016-05-25 08:57:59 +08:00
|
|
|
commandName, err := CommandName(fileName)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
errorsList = append(errorsList, err)
|
2016-05-16 01:46:35 +08:00
|
|
|
return errorsList
|
|
|
|
}
|
2016-05-14 07:09:36 +08:00
|
|
|
|
2016-05-25 11:04:51 +08:00
|
|
|
err = RunCommand(commandName, fileName)
|
2016-05-14 07:09:36 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
errorsList = append(errorsList, err)
|
|
|
|
return errorsList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to return to fulfill function sig, even though returning an empty
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-25 08:57:59 +08:00
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// CommandName defines the playback command for a sound and accepts:
|
|
|
|
//
|
|
|
|
// string: The filename of the audio that needs playback
|
2016-05-25 08:57:59 +08:00
|
|
|
func CommandName(fileName string) (commandName string, err error) {
|
|
|
|
fileType := path.Ext(fileName)
|
|
|
|
if fileType == ".mp3" {
|
|
|
|
return "mpg123", nil
|
|
|
|
} else if fileType == ".wav" {
|
|
|
|
return "aplay", nil
|
|
|
|
} else {
|
|
|
|
return "", errors.New("Unknown filetype for audio file.")
|
|
|
|
}
|
|
|
|
}
|
2016-05-25 11:04:51 +08:00
|
|
|
|
|
|
|
var execCommand = exec.Command
|
|
|
|
|
2016-11-27 03:44:09 +08:00
|
|
|
// RunCommand executes the playback command for a sound file and accepts:
|
|
|
|
//
|
|
|
|
// string: The audio command to be use for playback
|
|
|
|
// string: The filename of the audio that needs playback
|
2016-05-25 11:04:51 +08:00
|
|
|
func RunCommand(audioCommand string, filename string) error {
|
|
|
|
cmd := execCommand(audioCommand, filename)
|
|
|
|
err := cmd.Start()
|
|
|
|
return err
|
|
|
|
}
|