Skip to content

Instantly share code, notes, and snippets.

@wkf
Created October 7, 2016 13:48
Show Gist options
  • Save wkf/57f3c76e93fa5343a3eeb8e7fc6db85c to your computer and use it in GitHub Desktop.
Save wkf/57f3c76e93fa5343a3eeb8e7fc6db85c to your computer and use it in GitHub Desktop.
Detect beats in Clojure
(ns little-lambda.server.audio
(:require [clojure.java.io :as io])
(:import (be.tarsos.transcoder Transcoder DefaultAttributes)
(be.tarsos.dsp.onsets ComplexOnsetDetector OnsetHandler)
(be.tarsos.dsp.beatroot BeatRootOnsetEventHandler)
(be.tarsos.dsp.io.jvm AudioDispatcherFactory)))
(defn mp3->wav [source target]
(Transcoder/transcode
(io/file source)
(io/file target)
DefaultAttributes/WAV_PCM_S16LE_STEREO_44KHZ))
(defn detect-beats [source]
(let [dispatcher (AudioDispatcherFactory/fromFile (io/file source) 512 256)
detector (ComplexOnsetDetector. 512)
handler (BeatRootOnsetEventHandler.)
beats (atom [])]
(.setHandler detector handler)
(.addAudioProcessor dispatcher detector)
(.run dispatcher)
(.trackBeats
handler
(reify OnsetHandler
(handleOnset [this time salience]
(swap! beats conj time))))
@beats))
(comment
(mp3->wav "resources/contact.mp3" "resources/contact.wav")
(detect-beats "resources/contact.wav")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment