a simple linux audio player pkg?

151 views
Skip to first unread message

Mark

unread,
Nov 22, 2023, 5:02:22 PM11/22/23
to golang-nuts
Is there a simple vorbis/oga audio player package for Go that works on Linux.
The only API I need is something like this:

player.SetFilename(string) error // string is say tune.ogg; stops playing previous if any
player.Play(float32) error // plays current filename from given second e.g. 0.0 for the beginning
player.Pause() (float32, error) // pauses current playing and returns position
player.Resume() error // resumes playing
player.Secs() float32 // returns current position

The ones I've seen on awsome go either don't seem to play ogg format or are far more sophisticated than I need.

Raffaele Sena

unread,
Nov 22, 2023, 5:31:25 PM11/22/23
to Mark, golang-nuts
I have used github.com/jfreymuth/oggvorbis to read the ogg file (and convert to PCM) and github.com/ebitengine/oto/v3 to play the PCM.
I don't know of a full ogg player in Go


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/a38e3a9b-a357-4c85-aa4d-a7a4322ec216n%40googlegroups.com.

Mark

unread,
Nov 23, 2023, 3:33:44 AM11/23/23
to golang-nuts
Thank you, I'll try them. (Well, for oto I'll try https://pkg.go.dev/github.com/hajimehoshi/oto/v2 since that seems to have more importers)

Mark

unread,
Nov 23, 2023, 5:03:24 AM11/23/23
to golang-nuts
I've now tried using those libraries, but there seems to be an incompatibility []float32 vs []byte.
Here's the error:

./play.go:24:29: cannot use reader (variable of type *oggvorbis.Reader) as io.Reader value in argument to otoCtx.NewPlayer: *oggvorbis.Reader does not implement io.Reader (wrong type for method Read)
have Read([]float32) (int, error)
want Read([]byte) (int, error)

And here's the code (main.go):

package main

import (
"log"
"os"
"time"

//"github.com/ebitengine/oto/v3"
"github.com/hajimehoshi/oto/v2"
"github.com/jfreymuth/oggvorbis"
)

func main() {
log.SetFlags(0)
file, err := os.Open(os.Args[0])
checkErr(err)
defer file.Close()
reader, err := oggvorbis.NewReader(file)
checkErr(err)
otoCtx, readyCh, err := oto.NewContext(reader.SampleRate(),
reader.Channels(), 2)
checkErr(err)
<-readyCh // wait for h/w
player := otoCtx.NewPlayer(reader)
defer player.Close()
player.Play()
for player.IsPlaying() {
time.Sleep(time.Millisecond * 10)
}
}

func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}

On Wednesday, November 22, 2023 at 10:31:25 PM UTC Raffaele Sena wrote:

Mark

unread,
Nov 23, 2023, 11:52:09 AM11/23/23
to golang-nuts
I just converted the []float32 to []byte (see function below) and it works.
But the sound produced while recognizable is very staticy and I don't know why.

func floats32ToBytes(fs []float32) []byte {
var buf bytes.Buffer
for _, f := range fs {
if err := binary.Write(&buf, binary.LittleEndian, f); err != nil {
panic(err)
}
}
return buf.Bytes()
}

Mark

unread,
Nov 23, 2023, 12:57:15 PM11/23/23
to golang-nuts
Solved.
I switched to the github.com/ebitengine/oto/v3 pkg and it works perfectly.
The relevant changes:

options := &oto.NewContextOptions{}
options.SampleRate = format.SampleRate
options.ChannelCount = 2
options.Format = oto.FormatFloat32LE
otoCtx, readyCh, err := oto.NewContext(options)
Reply all
Reply to author
Forward
0 new messages