Skip to content

Instantly share code, notes, and snippets.

@bobbicodes
Created December 3, 2021 10:02
Show Gist options
  • Save bobbicodes/d9ec35b9f3c015739580c7fc77225772 to your computer and use it in GitHub Desktop.
Save bobbicodes/d9ec35b9f3c015739580c7fc77225772 to your computer and use it in GitHub Desktop.
Use Web Audio API with nbb
(ns example
(:require ["fs" :as fs]
["web-audio-api$default" :as wa]))
(def ^:dynamic context (wa/AudioContext.))
(set! (.-outStream context) (.-stdout js/process))
(fs/readFile "outfoxing.mp3"
(fn [err buffer]
(when err (throw err))
(.decodeAudioData context buffer
(fn [audioBuffer]
(let [bufferNode (.createBufferSource context)]
(.connect bufferNode (.-destination context))
(set! (.-buffer bufferNode) audioBuffer)
(set! (.-loop bufferNode) true)
(.start bufferNode 0))))))
@bobbicodes
Copy link
Author

bobbicodes commented Dec 3, 2021

Play in Linux with aplay:

nbb example.cljs | aplay -f cd

@bobbicodes
Copy link
Author

See the web-audio-api repo for more information: https://github.com/audiojs/web-audio-api/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment