-
-
Save faiface/b4bfbb35e81b718e415567da7d4a33df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"math" | |
"time" | |
"github.com/faiface/pixel" | |
"github.com/faiface/pixel/audio" | |
"github.com/faiface/pixel/audio/speaker" | |
"github.com/faiface/pixel/pixelgl" | |
) | |
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 run() { | |
speaker.Init(time.Second / 25) | |
cfg := pixelgl.WindowConfig{ | |
Title: "Piano", | |
Bounds: pixel.R(0, 0, 800, 600), | |
VSync: true, | |
} | |
win, err := pixelgl.NewWindow(cfg) | |
if err != nil { | |
panic(err) | |
} | |
keysCtrl := map[pixelgl.Button]*audio.Ctrl{ | |
pixelgl.KeyA: &audio.Ctrl{Streamer: sin(523.25, 0.3)}, | |
pixelgl.KeyS: &audio.Ctrl{Streamer: sin(554.37, 0.3)}, | |
pixelgl.KeyD: &audio.Ctrl{Streamer: sin(587.33, 0.3)}, | |
pixelgl.KeyF: &audio.Ctrl{Streamer: sin(622.25, 0.3)}, | |
pixelgl.KeyG: &audio.Ctrl{Streamer: sin(659.25, 0.3)}, | |
pixelgl.KeyH: &audio.Ctrl{Streamer: sin(698.46, 0.3)}, | |
pixelgl.KeyJ: &audio.Ctrl{Streamer: sin(739.99, 0.3)}, | |
pixelgl.KeyK: &audio.Ctrl{Streamer: sin(783.99, 0.3)}, | |
pixelgl.KeyL: &audio.Ctrl{Streamer: sin(830.61, 0.3)}, | |
} | |
for _, ctrl := range keysCtrl { | |
speaker.Play(ctrl) | |
} | |
for !win.Closed() { | |
for key, ctrl := range keysCtrl { | |
speaker.Lock() | |
ctrl.Paused = !win.Pressed(key) | |
speaker.Unlock() | |
} | |
win.Update() | |
} | |
} | |
func main() { | |
pixelgl.Run(run) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment