Giter Club home page Giter Club logo

beep's Introduction

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!

For more in-depth journey into the design of Beep, take a look at my blog post.

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.

beep's People

Contributors

faiface avatar mewmew avatar cswank avatar hundemeier avatar lordwelch avatar christopher-dg avatar jwangsadinata avatar

Watchers

Stas Arsoba avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.