golang

MP3 Handling in Go (mp3)

Media content is the native form of entertainment in the modern age. At the forefront of audio that is available in all forms of media entertainment is MP3.

MPEG Audio Layer-3, commonly known as MP3, is one of the most popular audio file formats that is used for audio compression, storage, and playing of digital audio. MP3 was developed as part of the Moving Picture Experts Group (MPEG) standard to provide an efficient form of compression of audio data while preserving the sound quality.

In this tutorial, we will learn how work with MP3 file in the Go programming language using the minimp3 package.

Requirements:

Before we begin, we need to ensure that we have the compiler installed.

Installing the MP3 Package

To get started, we need to install the “github.com/go-audio/audio” package and its dependencies.

Open the terminal and run the following command in your Go project:

$ go get -u github.com/tosone/minimp3

The given command downloads and installs the necessary packages for audio handling in Go.

Decode MP3 in Golang

MP3 decoding refers to the process of converting an MP3 audio file from compressed digital audio format back into its original uncompressed audio format. You will often find it in Pulse Code Modulation format (PCM).

In Go, we can decode an MP3 file and extract its audio data using the “mp3.DecodeFull” function from the minimp3 package that we installed earlier.

Consider an example as follows:

package main
import (
    "log"
    "time"
    "os"
    "github.com/hajimehoshi/oto"
    "github.com/tosone/minimp3"
)
func main() {
    filePath := "./sample4.mp3"
    file, err := os.ReadFile(filePath)
    if err != nil {
        log.Fatalf("Error reading file: %v", err)
    }
    dec, data, err := minimp3.DecodeFull(file)
    if err != nil {
        log.Fatalf("Error decoding MP3: %v", err)
    }
    defer dec.Close()
    context, err := oto.NewContext(dec.SampleRate, dec.Channels, 2, 1024)
    if err != nil {
        log.Fatalf("Error creating audio context: %v", err)
    }
    defer context.Close()
    player := context.NewPlayer()
    player.Write(data)
    <-time.After(time.Second)
    if err := player.Close(); err != nil {
        log.Fatalf("Error closing player: %v", err)
    }
}

In the given example, we start by importing the necessary packages. In our case, we need the log, time, OS, oto, and minimp3 packages to handle the logs, time, I/O, audio output, and mp3 decoding, respectively.

We then read the file contents of the MP3 file using the os.ReadFile() method.

In the next step, we move to the decoding phase where we use the minimp3 library to decode the MP3 audio that is stored in the file variable. We store the resulting data into the data variable.

We also ensure to close the mp3 decoder after the operation is complete using the dec.Close() function.

Using the oto library, we create an audio output context and specify the sample rate and the number of audio channels based on the decoded MP3 data.

Similarly, we also ensure to close the audio context using the context.Close() defer.

In the next two steps, we create an audio play using the audio context that we setup in the previous step.

We then proceed to write the decoded audio data to the player.

We also add a delay for one second using <-time.After(time.Second) to allow the audio playback to complete.

Finally, once the audio playback is complete, we close the audio player using player.Close().

Conclusion

In this fundamental tutorial, we learned how to use the minimp3 library in Go to decode and play the MP3 audio.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list