Categorygithub.com/mewpull/beep
modulepackage
0.0.0-20170818183929-724c0fa035a3
Repository: https://github.com/mewpull/beep.git
Documentation: pkg.go.dev

# README

Beep

A small package that brings sound to any Go program (and real-time audio processing and other casual stuff)

$ go get github.com/faiface/beep

A (very short) Tour of Beep

Let's get started! Open an audio file (let's ignore errors for now, never do that in production).

f, _ := os.Open("song.wav")

Decode the file into a Streamer.

// import "github.com/faiface/beep/wav"
s, format, _ := wav.Decode(f)

Streamers are super important in Beep. Streamer is anything that can Stream audio (lazily) and perhaps do other interesting things along the way. The streamer returned from wav.Decode streams audio from the file.

Now, let's play the streamer. First, we need to initialize the speaker.

// import "github.com/faiface/beep/speaker"
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))

We set the sample rate of the speaker to the sample rate of the file and we set the buffer size to 1/10s. Buffer size determines the stability and responsiveness of the playback. With smaller buffer, you get more responsiveness and less latency. With bigger buffer, you get more stability and reliability.

Finally, let's play!

speaker.Play(s)

The streamer now starts playing, but in order to hear anything, we need to prevent our program from exiting.

select {} // for now

Now, this is kind of a hack. Let's fix it. To do that, we'll use beep.Seq function, which takes some streamers and streams them one by one and we'll use beep.Callback function, which creates a streamer, that does not stream any audio, but instead calls our own function. So, here's what we do. First, we create a channel, which will signal the end of the playback.

done := make(chan struct{})

Now, we'll change speaker.Play(s) into this.

speaker.Play(beep.Seq(s, beep.Callback(func() {
        close(done)
})))

And finally, we'll replace the hacky select {} with a receive from the channel.

<-done

And that's it!

Take a look at the documentation for other interesting things you can do with Beep, such as mixing, looping, audio effects, and other useful stuff.

# Packages

No description provided by the author
Package flac implements audio data decoding in FLAC format.
Package mp3 implements audio data decoding in MP3 format.
Package speaker implements playback of beep.Streamer values through physical speakers.
Package wav implements audio data decoding and encoding in WAVE format.

# Functions

Callback returns a Streamer, which does not stream any samples, but instead calls f the first time its Stream method is called.
Dup returns two Streamers which both stream the same data as the original s.
Iterate returns a Streamer which successively streams Streamers obtains by calling the provided g function.
Loop takes a StreamSeeker and plays it count times.
Mix takes zero or more Streamers and returns a Streamer which streames them mixed together.
NewBuffer creates a new empty Buffer which stores samples in the provided format.
Resample takes a Streamer which is assumed to stream at the old sample rate and returns a Streamer, which streams the data from the original Streamer resampled to the new sample rate.
ResampleRatio is same as Resample, except it takes the ratio of the old and the new sample rate, specifically, the old sample rate divided by the new sample rate.
Seq takes zero or more Streamers and returns a Streamer which streams them one by one without pauses.
Silence returns a Streamer which streams num samples of silence.
Take returns a Streamer which streams at most num samples from s.

# Structs

Buffer is a storage for audio data.
Ctrl allows for pausing a Streamer.
Format is the format of a Buffer or another audio source.
Mixer allows for dynamic mixing of arbitrary number of Streamers.
Resampler is a Streamer created by Resample and ResampleRatio functions.

# Interfaces

StreamCloser is a Streamer streaming from a resource which needs to be released, such as a file or a network connection.
Streamer is able to stream a finite or infinite sequence of audio samples.
StreamSeekCloser is a union of StreamSeeker and StreamCloser.
StreamSeeker is a finite duration Streamer which supports seeking to an arbitrary position.

# Type aliases

SampleRate is the number of samples per second.
StreamerFunc is a Streamer created by simply wrapping a streaming function (usually a closure, which encloses a time tracking variable).