Created
May 30, 2018 14:12
-
-
Save sug0/a37a40eb78e03c6da18402587ea3d3bd to your computer and use it in GitHub Desktop.
Bytebeats in golang
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 ( | |
| "os" | |
| "io" | |
| "github.com/hajimehoshi/oto" | |
| "github.com/sugoiuguu/go-exit" | |
| ) | |
| // blippy, epic: (t*((15&t>>11)%12)&55-(t>>5|t>>12)|t*(t>>10)*32)-1 | |
| // atmospheric, hopeful: t*3&(t>>10)|t*12&(t>>10)|t*10&((t>>8)*55)&128 | |
| // expansive rumbles: t*4&(t>>10)|t*4&(t*6>>8)&t|64 | |
| // electric, repetitive: t*(t+(t>>9|t>>13))%40&120 | |
| func beat(t int) byte { | |
| return byte(t*3&(t>>10)|t*12&(t>>10)|t*10&((t>>8)*55)&128) | |
| } | |
| func main() { | |
| defer exit.Handler() | |
| // get new player | |
| p, err := oto.NewPlayer(8000, 1, 1, 8192) | |
| if err != nil { | |
| exit.WithMsg(os.Stderr, 1, "Error: %s", err) | |
| } | |
| defer p.Close() | |
| // create a pipe to write samples to, | |
| // and read from it in the player | |
| r, w, err := os.Pipe() | |
| if err != nil { | |
| exit.WithMsg(os.Stderr, 1, "Error: %s", err) | |
| } | |
| defer r.Close() | |
| defer w.Close() | |
| // generate samples | |
| go func() { | |
| var b [1]byte | |
| s := b[0:] | |
| for t := 0;; t++ { | |
| b[0] = beat(t) | |
| w.Write(s) | |
| } | |
| }() | |
| // play the generated samples | |
| if _,err := io.Copy(p, r); err != nil { | |
| exit.WithMsg(os.Stderr, 1, "Error: %s", err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment