play

package
v0.0.0-...-5668e1b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package play implements a simple mp3 player. In general, to use this package first create a Player using NewPlayer, start a goroutine that listens for events on Player.Event, Load an mp3 using Player.Load, and play it using Player.Play(). GetMetadata can be used to retrieve ID3 information from mp3 files.

Index

Constants

View Source
const (
	Move queueCmdType = iota
	MoveToTop
	Remove
	Clear
)

Variables

This section is empty.

Functions

func DebugMetadata

func DebugMetadata(filename string)

Print debugging information about the metadata in `filename` to stdout.

func GetVolume

func GetVolume() (volume byte, err error)

Get the volume as a percentage between 0 and 100 inclusive.

func Play

func Play(filename string)

Play the specified file. Return when playback is complete.

func SetVolume

func SetVolume(pct byte) (err error)

Set the volume as a percentage between 0 and 100 inclusive. This method sets the volume on the default ALSA card.

func SetVolumeAll

func SetVolumeAll(pct byte) (err error)

Set the volume as a percentage between 0 and 100 inclusive. This method sets the volume on all ALSA cards.

Types

type Asc

type Asc []int

Sort ints

func (Asc) Len

func (a Asc) Len() int

func (Asc) Less

func (a Asc) Less(i, j int) bool

func (Asc) Swap

func (a Asc) Swap(i, j int)

type Desc

type Desc []int

func (Desc) Len

func (a Desc) Len() int

func (Desc) Less

func (a Desc) Less(i, j int) bool

func (Desc) Swap

func (a Desc) Swap(i, j int)

type Event

type Event struct {
	// Type is the type of event
	Type EventType
	// Data is the event data.
	// For OffsetChange, it is an int representing the offset.
	// For StateChange, it is a PlayerState.
	// For VolumeChange, it is a byte in the range 0-100 representing the volume.
	// For QueueChange, it is not set.
	// For Error, its an error.
	Data interface{}
}

Event represents events sent by the Player.

type EventType

type EventType int
const (
	OffsetChange EventType = iota
	StateChange
	VolumeChange
	QueueChange
	Error
)

func (EventType) String

func (e EventType) String() string

type Info

type Info struct {
	// The bitrate of the mp3, in kbps
	BitRate int

	// Sampling rate in Hz
	Rate int

	// Duration of mp3 in seconds (if available) or zero otherwise
	Duration float64

	// Seconds per sample (if available) or zero otherwise
	Sps float64
}

Information about an mp3 determined once the mp3 is loaded.

type Metadata

type Metadata struct {
	Title    string
	Artist   string
	Album    string
	Tracknum int
}

Metadata is information about an mp3 stored in id3 tags.

func GetMetadata

func GetMetadata(filename string) Metadata

GetMetadata extracts the id3 information from the mp3 file `filename`. For integer fields (like Tracknum) for which there is no data the field is set to -1.

type Player

type Player struct {

	// Events is a channel to which Player events are written, such as state changes, volume changes, or offset changes.
	// When an event is recieved the user should call GetStatus() to get the current player information.
	Events chan Event
	// contains filtered or unexported fields
}

Player is an mp3 player.

func NewPlayer

func NewPlayer() (p Player)

Create a new Player.

func (Player) GetInfo

func (p Player) GetInfo() *Info

GetInfo returns an Info object about the loaded mp3. If an mp3 is not loaded, nil is returned

func (Player) GetStatus

func (p Player) GetStatus() PlayerStatus

GetStatus gets the player status

func (Player) Load

func (p Player) Load(filename string) (size int, err error)

Load the specified file into the Player. Call Play to play the file. Returns the size of the file in samples. On failure, err is non nil. On success, the player will write the current playing offset (in samples) to offchan periodically.

func (Player) Pause

func (p Player) Pause()

Pause the currently playing file.

func (Player) Play

func (p Player) Play() (err error)

Play the currently loaded file. The player must have an mp3 loaded and not currently playing.

func (Player) Seek

func (p Player) Seek(offset int)

Seek to the specified sample in the loaded mp3 file.

func (Player) SetRepeat

func (p Player) SetRepeat(r bool)

SetRepeat sets whether the track should be repeated when we reach the end or not.

func (Player) SetVolume

func (p Player) SetVolume(pct byte)

SetVolume calls the regular SetVolume function but also writes a VolumeChange event to the Player's Event channel.

func (Player) SetVolumeAll

func (p Player) SetVolumeAll(pct byte)

SetVolumeAll calls the regular SetVolumeAll function but also writes a VolumeChange event to the Player's Event channel.

func (Player) Stop

func (p Player) Stop()

Stop the currently playing file if it's playing, and unload the file from the mp3 player.

type PlayerState

type PlayerState int

PlayerState represents the current state of a Player. Must be one of Empty, Playing, or Paused.

const (
	// The player has no mp3 loaded
	Empty PlayerState = iota
	// The player has an mp3 loaded and is playing it
	Playing
	// The player has an mp3 loaded and it is paused
	Paused
)

func (PlayerState) String

func (s PlayerState) String() string

type PlayerStatus

type PlayerStatus struct {
	// Offset is the offset within the current track in samples
	Offset int
	// Size of the current track in samples
	Size int
	// State of the Player
	State PlayerState
	// Volume
	Volume byte
	// Path to current mp3
	Path string
}

PlayerStatus contains information about the current status of the player

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue implements a queue of metadata for a player. Items from the queue are added to the player when the player is Empty.

func NewQueue

func NewQueue(player Player) Queue

func NewQueueWithEvents

func NewQueueWithEvents(player Player, events chan Event) Queue

func (Queue) Clear

func (q Queue) Clear()

Clear the queue

func (Queue) Enqueue

func (q Queue) Enqueue(filename string)

func (Queue) List

func (q Queue) List() []QueueElem

List returns the contents of the queue.

func (Queue) Move

func (q Queue) Move(indexes []int, delta int)

Move moves the specified queue element `index` by `delta`. Currently only -1 and +1 are supported for delta.

func (Queue) MoveToTop

func (q Queue) MoveToTop(indexes []int)

Move the specified queue element to the top of the queue.

func (Queue) Remove

func (q Queue) Remove(indexes []int)

Remove removes the specified queue element from the queue.

type QueueElem

type QueueElem struct {
	Filename string
	// A unique id for the element in the queue. This can be used to identify elements
	// after the queue has been modified (i.e. if items have been moved).
	Id uint32
}

QueueElem is an element in the play queue.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL