Created
October 7, 2016 13:48
-
-
Save wkf/57f3c76e93fa5343a3eeb8e7fc6db85c to your computer and use it in GitHub Desktop.
Detect beats in Clojure
This file contains 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
(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