package main import ( "fmt" "log" "os/exec" // osc "github.com/hypebeast/go-osc/osc" ) // Base structure for audiovisual media. // Media is simply a file containing audio and video material. type Media struct { active bool cmd *exec.Cmd args []string } // Interface for Media type structs type MediaPlayer interface { play() stop() } // Run executable func run(name string, args ...string) *exec.Cmd { // Find executable //path, err := exec.LookPath(name) //if err != nil { // log.Printf("can't find '%s'\n", name) // return nil //} //println("got path") //log.Println("got path:", path, args) cmd := exec.Command(name, args...) if err := cmd.Run(); err != nil { log.Print(err) return nil } return cmd } // Run executable func (player *Media) run(name string, args ...string) { player.cmd = exec.Command(name, args...) if err := player.cmd.Run(); err != nil { log.Print(err) } } // Play media file via mpv, osc message sets the file path func (player *Media) play(msg *osc.Message) { if player.active { log.Print("please stop current player before playing another") log.Print(player.cmd) return } uri := getOSC[string](msg) osc.PrintMessage(msg) if uri == "" { log.Print("player won't play without uri") return } player.active = true // you can route audio directly through alsa/hdmi but beware of clipping //run(player, "mpv", "-fs", "--audio-device=alsa/hdmi", "--volume=25", uri) args := append(player.args, uri) player.run(args[0], args[1:]...) player.active = false } // Stop the player by shutting down MPV func (player *Media) stop(msg *osc.Message) { player.quit() } // Change volume using amixer command func (player *Media) volume(msg *osc.Message) { level := getOSC[float32](msg) run("amixer", "-q", "sset", "Master", fmt.Sprintf("%.2f%%", level*100.0)) } func (player *Media) quit() { if !player.active { log.Print("can't kill player if it ain't running") return } if err := player.cmd.Process.Kill(); err != nil { log.Print("failed to kill process: ", err) } player.active = false } func NewMPV() *Media { media := new(Media) media.args = []string{"mpv", "-fs"} return media } func NewXLEmulator() *Media { media := new(Media) media.args = []string{"atari800", "-stretch", "3"} return media }