func playlistInfo(_ *Cmd, conn *mpd.Client) (r *Result, err error) { pl, err := conn.PlaylistInfo(-1, -1) if err == nil { r = NewResult("Playlist", pl) } return }
func status(cmd *Cmd, conn *mpd.Client) (r *Result, err error) { state, err := conn.Status() if err == nil { r = NewResult("Status", state) } return }
func listPlaylists(_ *Cmd, conn *mpd.Client) (r *Result, err error) { lists, err := conn.ListPlaylists() if err == nil { r = NewResult("StoredPlaylists", lists) } return }
func getFiles(_ *Cmd, conn *mpd.Client) (r *Result, err error) { files, err := conn.GetFiles() if err == nil { r = NewResult("Files", files) } return }
func currentSong(_ *Cmd, conn *mpd.Client) (r *Result, err error) { info, err := conn.CurrentSong() if err == nil { r = NewResult("CurrentSong", info) } return }
func playlistContents(cmd *Cmd, conn *mpd.Client) (r *Result, err error) { pl, err := conn.PlaylistContents(cmd.Playlist) if err == nil { r = NewResult("StoredPlaylist", NewStoredPlaylist(cmd.Playlist, pl)) } return }
// Walk over all music files and create a .mood file for each in mood-dir. func updateMoodDatabase(client *mpd.Client, cfg *Config) error { if cfg.MoodDir == "" { return fmt.Errorf("No mood bar directory given (-mood-dir)") } if err := os.MkdirAll(cfg.MoodDir, 0777); err != nil { return err } paths, err := client.GetFiles() if err != nil { return fmt.Errorf("Cannot get all files from mpd: %v", err) } // Use up to N threads: N := 8 wg := &sync.WaitGroup{} wg.Add(N) moodChan := make(chan *MoodInfo, N) for i := 0; i < N; i++ { go func() { for pair := range moodChan { log.Printf("Processing: %s", pair.MusicFile) cmd := exec.Command("moodbar", pair.MusicFile, "-o", pair.MoodPath) if err := cmd.Run(); err != nil { log.Printf("Failed to execute moodbar on `%s`: %v", pair.MusicFile, err) } } wg.Done() }() } for _, path := range paths { moodName := strings.Replace(path, string(filepath.Separator), "|", -1) moodPath := filepath.Join(cfg.MoodDir, moodName) if _, err := os.Stat(moodPath); err == nil { // Already exists, Skipping. continue } dataPath := filepath.Join(cfg.MusicDir, path) moodChan <- &MoodInfo{ MusicFile: dataPath, MoodPath: moodPath, } } close(moodChan) wg.Wait() return nil }
func setPlaylist(cmd *Cmd, conn *mpd.Client) (*Result, error) { list := conn.BeginCommandList() list.Clear() for _, uri := range cmd.Uris { list.Add(uri) } list.Play(0) return nil, list.End() }
func addMulti(cmd *Cmd, conn *mpd.Client) (*Result, error) { list := conn.BeginCommandList() for i, uri := range cmd.Uris { if cmd.Pos < 0 { list.Add(uri) } else { list.AddId(uri, cmd.Pos+i) } } return nil, list.End() }
//Updates the library func (l *library) update() error { var conn *mpd.Client fmt.Println("Connecting to MPD") conn, err := mpd.Dial("tcp", "127.0.0.1:6600") if err != nil { fmt.Println("Error: could not connect to MPD for lib update") return errors.New("Could not connect to MPD!") } defer conn.Close() _, err = conn.Update("") if err != nil { fmt.Println("Error: could not update library!") return err } //Let the update happen time.Sleep(2 * time.Second) songs, err := conn.ListAllInfo("/") if err != nil { fmt.Println("Error: could not retrieve new library!") return err } l.library = songs return nil }
func (m MPDInstance) Render() (item modules.Item) { mpdItem := MPDItem{Name: m.name, Markup: "pango"} mpdFormatData := MPDFormatData{} var client *go_mpd.Client if c, err := go_mpd.Dial("tcp", m.host_name+":"+strconv.Itoa(m.port)); err != nil { log.Error(err.Error()) return nil } else { client = c } defer func(client *go_mpd.Client) { if err := client.Close(); err != nil { log.Error("Failed to disconnect: " + err.Error()) } }(client) if attrs, err := client.Status(); err == nil { if state, ok := attrs["state"]; ok { mpdFormatData.State = state } else { log.Error("Failed to read state.") return nil } } else { log.Error("Failed to obtain status: " + err.Error()) return nil } if attrs, err := client.CurrentSong(); err != nil { log.Error("Failed to obtain current song: " + err.Error()) return nil } else { obj := reflect.ValueOf(&mpdFormatData).Elem() for key, val := range attrs { if f := obj.FieldByName(key); f.IsValid() { val = strings.TrimSpace(val) f.Set(reflect.ValueOf(val)) } } } buffer := bytes.Buffer{} if err := m.template.Execute(&buffer, mpdFormatData); err != nil { log.Error("Failed to render mpd template: " + err.Error()) return nil } else { mpdItem.Text = buffer.String() } item = mpdItem return }
//Create a new queue func newLibrary() *library { var conn *mpd.Client fmt.Println("Connecting to MPD") conn, err := mpd.Dial("tcp", "127.0.0.1:6600") if err != nil { fmt.Println("Error: could not connect to MPD, exiting") os.Exit(1) } defer conn.Close() songs, err := conn.ListAllInfo("/") if err != nil { fmt.Println("Error: could not connect to MPD, exiting") os.Exit(1) } shuffle(songs) return &library{ library: songs, } }
func add(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.Add(cmd.Uri) }
func setVolume(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.SetVolume(cmd.Volume) }
func shuffle(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.Shuffle(cmd.Start, cmd.End) }
func seekId(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.SeekId(cmd.Id, cmd.Time) }
func moveId(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.MoveId(cmd.Id, cmd.Pos) }
func random(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.Random(cmd.Random) }
func repeat(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.Repeat(cmd.Repeat) }
func playlistSave(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.PlaylistSave(cmd.Playlist) }
func previous(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.Previous() }
func playlistRename(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.PlaylistRename(cmd.Playlist, cmd.Name) }
func next(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.Next() }
func pause(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.Pause(cmd.Pause) }
func stop(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.Stop() }
func playId(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.PlayId(cmd.Id) }
func update(cmd *Cmd, conn *mpd.Client) (*Result, error) { _, err := conn.Update("") return nil, err }
func playlistLoad(cmd *Cmd, conn *mpd.Client) (*Result, error) { return nil, conn.PlaylistLoad(cmd.Playlist, cmd.Start, cmd.End) }
// Pings MPD. func (m *Mpd) ping(c *mpd.Client) { if err := c.Ping(); err != nil { m.err <- err } }
func playlistMove(cmd *Cmd, conn *mpd.Client) (*Result, error) { if err := conn.PlaylistMove(cmd.Playlist, cmd.Id, cmd.Pos); err != nil { return nil, err } return playlistContents(cmd, conn) }