1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-24 13:48:49 +08:00
hybridgroup.gobot/platforms/audio/audio_adaptor.go
2023-11-15 20:51:52 +01:00

100 lines
2.4 KiB
Go

// Package audio is based on aplay audio adaptor written by @colemanserious (https://github.com/colemanserious)
package audio
import (
"errors"
"log"
"os"
"os/exec"
"path"
"gobot.io/x/gobot/v2"
)
// Adaptor is gobot Adaptor connection to audio playback
type Adaptor struct {
name string
}
// NewAdaptor returns a new audio Adaptor
func NewAdaptor() *Adaptor {
return &Adaptor{name: gobot.DefaultName("Audio")}
}
// Name returns the Adaptor Name
func (a *Adaptor) Name() string { return a.name }
// SetName sets the Adaptor Name
func (a *Adaptor) SetName(n string) { a.name = n }
// Connect establishes a connection to the Audio adaptor
func (a *Adaptor) Connect() error { return nil }
// Finalize terminates the connection to the Audio adaptor
func (a *Adaptor) Finalize() error { return nil }
// Sound plays a sound and accepts:
//
// string: The filename of the audio to start playing
func (a *Adaptor) Sound(fileName string) []error {
var errorsList []error
if fileName == "" {
log.Println("Requires filename for audio file.")
errorsList = append(errorsList, errors.New("requires filename for audio file"))
return errorsList
}
_, err := os.Stat(fileName)
if err != nil {
log.Println(err)
errorsList = append(errorsList, err)
return errorsList
}
// command to play audio file based on file type
commandName, err := CommandName(fileName)
if err != nil {
log.Println(err)
errorsList = append(errorsList, err)
return errorsList
}
err = RunCommand(commandName, fileName)
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
}
// CommandName defines the playback command for a sound and accepts:
//
// string: The filename of the audio that needs playback
func CommandName(fileName string) (string, error) {
fileType := path.Ext(fileName)
switch fileType {
case ".mp3":
return "mpg123", nil
case ".wav":
return "aplay", nil
default:
return "", errors.New("unknown filetype for audio file")
}
}
var execCommand = exec.Command
// 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
func RunCommand(audioCommand string, filename string) error {
cmd := execCommand(audioCommand, filename)
err := cmd.Start()
return err
}