minimp3

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 6 Imported by: 33 modules, 34 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

minimp3

Go Reference Builder Coverage Status

Decode mp3 base on https://github.com/lieff/minimp3

Installation

  1. The first need Go installed (version 1.15+ is required), then you can use the below Go command to install minimp3.
$ go get -u github.com/tosone/minimp3
  1. Import it in your code:
import "github.com/tosone/minimp3"

Examples are here

Example1: Decode the whole mp3 and play.
package main

import (
	"bytes"
	"log"
	"os"
	"time"

	"github.com/ebitengine/oto/v3"
	"github.com/tosone/minimp3"
)

func main() {
	file, err := os.ReadFile("test.mp3")
	if err != nil {
		log.Fatal(err)
	}

	dec, data, err := minimp3.DecodeFull(file)
	if err != nil {
		log.Fatal(err)
	}

	op := &oto.NewContextOptions{
		SampleRate:   dec.SampleRate,
		ChannelCount: dec.Channels,
		Format:       oto.FormatSignedInt16LE,
	}
	context, readyChan, err := oto.NewContext(op)
	if err != nil {
		log.Fatal(err)
	}
	<-readyChan

	player := context.NewPlayer(bytes.NewReader(data))
	player.Play()
	for player.IsPlaying() {
		time.Sleep(time.Millisecond)
	}

	dec.Close()
	if err = player.Close(); err != nil {
		log.Fatal(err)
	}
}
Example2: Decode and play.
package main

import (
	"log"
	"os"
	"time"

	"github.com/ebitengine/oto/v3"
	"github.com/tosone/minimp3"
)

func main() {
	file, err := os.Open("input.mp3")
	if err != nil {
		log.Fatal(err)
	}
	dec, err := minimp3.NewDecoder(file)
	if err != nil {
		log.Fatal(err)
	}
	<-dec.Started()
	log.Printf("Convert audio sample rate: %d, channels: %d\n", dec.SampleRate, dec.Channels)
	op := &oto.NewContextOptions{
		SampleRate:   dec.SampleRate,
		ChannelCount: dec.Channels,
		Format:       oto.FormatSignedInt16LE,
	}
	context, readyChan, err := oto.NewContext(op)
	if err != nil {
		log.Fatal(err)
	}
	<-readyChan
	player := context.NewPlayer(dec)
	player.Play()
	for player.IsPlaying() {
		time.Sleep(time.Millisecond)
	}
	if err := player.Close(); err != nil {
		log.Fatal(err)
	}
}
Example3: Play the network audio.
package main

import (
	"log"
	"net/http"
	"os"
	"time"

	"github.com/ebitengine/oto/v3"
	"github.com/tosone/minimp3"
)

func main() {
	var args = os.Args
	if len(args) != 2 {
		log.Fatal("Run test like this:\n\n\t./networkAudio.test [mp3url]\n\n")
	}

	response, err := http.Get(args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer response.Body.Close()

	dec, err := minimp3.NewDecoder(response.Body)
	if err != nil {
		log.Fatal(err)
	}
	<-dec.Started()

	log.Printf("Convert audio sample rate: %d, channels: %d\n", dec.SampleRate, dec.Channels)

	op := &oto.NewContextOptions{
		SampleRate:   dec.SampleRate,
		ChannelCount: dec.Channels,
		Format:       oto.FormatSignedInt16LE,
	}
	context, readyChan, err := oto.NewContext(op)
	if err != nil {
		log.Fatal(err)
	}
	<-readyChan

	player := context.NewPlayer(dec)
	player.Play()
	for player.IsPlaying() {
		time.Sleep(time.Millisecond)
	}
	dec.Close()
	if err := player.Close(); err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BufferSize = 1024 * 10

BufferSize Decoded data buffer size.

View Source
var WaitForDataDuration = time.Millisecond * 10

WaitForDataDuration wait for the data time duration.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	SampleRate int
	Channels   int
	Kbps       int
	Layer      int
	// contains filtered or unexported fields
}

Decoder decode the mp3 stream by minimp3

func DecodeFull

func DecodeFull(mp3 []byte) (dec *Decoder, decodedData []byte, err error)

DecodeFull put all of the mp3 data to decode.

func NewDecoder

func NewDecoder(reader io.Reader) (dec *Decoder, err error)

NewDecoder decode mp3 stream and get a Decoder for read the raw data to play.

func (*Decoder) Close

func (dec *Decoder) Close()

Close stop the decode mp3 stream cycle.

func (*Decoder) Read

func (dec *Decoder) Read(data []byte) (n int, err error)

Read read the raw stream

func (*Decoder) Started

func (dec *Decoder) Started() (channel chan bool)

Started check the record mp3 stream started ot not.

Jump to

Keyboard shortcuts

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