gobot.io/x/gobot@v1.16.0/platforms/audio/audio_adaptor.go (about)

     1  // Package audio is based on aplay audio adaptor written by @colemanserious (https://github.com/colemanserious)
     2  package audio
     3  
     4  import (
     5  	"errors"
     6  	"log"
     7  	"os"
     8  	"os/exec"
     9  	"path"
    10  
    11  	"gobot.io/x/gobot"
    12  )
    13  
    14  // Adaptor is gobot Adaptor connection to audio playback
    15  type Adaptor struct {
    16  	name string
    17  }
    18  
    19  // NewAdaptor returns a new audio Adaptor
    20  //
    21  func NewAdaptor() *Adaptor {
    22  	return &Adaptor{name: gobot.DefaultName("Audio")}
    23  }
    24  
    25  // Name returns the Adaptor Name
    26  func (a *Adaptor) Name() string { return a.name }
    27  
    28  // SetName sets the Adaptor Name
    29  func (a *Adaptor) SetName(n string) { a.name = n }
    30  
    31  // Connect establishes a connection to the Audio adaptor
    32  func (a *Adaptor) Connect() error { return nil }
    33  
    34  // Finalize terminates the connection to the Audio adaptor
    35  func (a *Adaptor) Finalize() error { return nil }
    36  
    37  // Sound plays a sound and accepts:
    38  //
    39  //  string: The filename of the audio to start playing
    40  func (a *Adaptor) Sound(fileName string) []error {
    41  	var errorsList []error
    42  
    43  	if fileName == "" {
    44  		log.Println("Requires filename for audio file.")
    45  		errorsList = append(errorsList, errors.New("Requires filename for audio file."))
    46  		return errorsList
    47  	}
    48  
    49  	_, err := os.Stat(fileName)
    50  	if err != nil {
    51  		log.Println(err)
    52  		errorsList = append(errorsList, err)
    53  		return errorsList
    54  	}
    55  
    56  	// command to play audio file based on file type
    57  	commandName, err := CommandName(fileName)
    58  	if err != nil {
    59  		log.Println(err)
    60  		errorsList = append(errorsList, err)
    61  		return errorsList
    62  	}
    63  
    64  	err = RunCommand(commandName, fileName)
    65  	if err != nil {
    66  		log.Println(err)
    67  		errorsList = append(errorsList, err)
    68  		return errorsList
    69  	}
    70  
    71  	// Need to return to fulfill function sig, even though returning an empty
    72  	return nil
    73  }
    74  
    75  // CommandName defines the playback command for a sound and accepts:
    76  //
    77  //  string: The filename of the audio that needs playback
    78  func CommandName(fileName string) (commandName string, err error) {
    79  	fileType := path.Ext(fileName)
    80  	if fileType == ".mp3" {
    81  		return "mpg123", nil
    82  	} else if fileType == ".wav" {
    83  		return "aplay", nil
    84  	} else {
    85  		return "", errors.New("Unknown filetype for audio file.")
    86  	}
    87  }
    88  
    89  var execCommand = exec.Command
    90  
    91  // RunCommand executes the playback command for a sound file and accepts:
    92  //
    93  //  string: The audio command to be use for playback
    94  //  string: The filename of the audio that needs playback
    95  func RunCommand(audioCommand string, filename string) error {
    96  	cmd := execCommand(audioCommand, filename)
    97  	err := cmd.Start()
    98  	return err
    99  }