Skip to content

Instantly share code, notes, and snippets.

@faiface
Created July 6, 2017 19:45
Show Gist options
  • Save faiface/d9bdd46a5bea69c47f23a0a0192e8c26 to your computer and use it in GitHub Desktop.
Save faiface/d9bdd46a5bea69c47f23a0a0192e8c26 to your computer and use it in GitHub Desktop.
package main
import (
"math"
"time"
"github.com/faiface/pixel/audio"
"github.com/faiface/pixel/audio/speaker"
)
func sin(freq, volume float64) audio.Streamer {
t := 0.0
dt := 1.0 / audio.SampleRate
return audio.StreamerFunc(func(samples [][2]float64) (n int, ok bool) {
for i := range samples {
val := math.Sin(math.Pi*freq*t) * volume
samples[i][0] = val
samples[i][1] = val
t += dt
}
return len(samples), true
})
}
func main() {
speaker.Init(time.Second / 15)
speaker.Play(audio.Seq(
audio.Take(time.Second, sin(440, 0.3)),
audio.Take(time.Second, sin(550, 0.3)),
audio.Take(time.Second, sin(880, 0.3)),
))
for {
speaker.Update()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment