Skip to content

Commit

Permalink
0.6.0 mp3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
tapochqa committed Mar 14, 2023
1 parent 256b97c commit cadc719
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 29 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/checkouts
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
Expand Down
6 changes: 4 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject org.clojars.tapochqa/lufs "0.5.0"
(defproject org.clojars.tapochqa/lufs "0.6.0"
:description "LUFS meter"

:url "https://github.com/tapochqa/lufs"
Expand All @@ -10,9 +10,11 @@
:target-path "target/%s"
:uberjar-name "lufs-clj.jar"
:jvm-opts ^:replace []
:resource-paths ["resources/jlayer-1.0.1.jar"
"resources/mp3spi-1.9.5.jar"
"resources/tritonus_share-0.3.6.jar"]
:profiles { :uberjar {
:aot :all
:global-vars {*unchecked-math* :true}}
:dev {

:global-vars {*unchecked-math* :true}}})
11 changes: 6 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Integrated LUFS Meter in Clojure. Implements EBU R 128 standard.
LUFS Meter in Clojure. Measures Integrated, Short-Term, Momentary LUFS and LRA.

[![Clojars Project](https://img.shields.io/clojars/v/org.clojars.tapochqa/lufs.svg)](https://clojars.org/org.clojars.tapochqa/lufs)

```clojure
(ns lufsomer.core
(:require [lufs.core :refer [lufs lufs*]]))
(:require [lufs.core :as lufs]))



; Measure LUFS from file or filename

(lufs "audio.wav") ; -18.860580104601013
(lufs/integrated "audio.wav") ; -18.860580104601013



Expand All @@ -25,14 +25,15 @@ Integrated LUFS Meter in Clojure. Implements EBU R 128 standard.
(/ 1000.0))))

(let [sr 44100 len 10]
(lufs* [(gen-data len sr)
(lufs/lufs*
[(gen-data len sr)
(gen-data len sr)]
sr)) ; 1.4325250705544224
```



For now it's relatively slow and works only with 2-channel stereo WAV.
For now it's relatively slow. Works with 2-channel WAV and MP3.

Algorithm is copied from [csteinmetz1/pyloudnorm](https://github.com/csteinmetz1/pyloudnorm).

Expand Down
Binary file added resources/jlayer-1.0.1.jar
Binary file not shown.
Binary file added resources/mp3spi-1.9.5.jar
Binary file not shown.
Binary file added resources/tritonus_share-0.3.6.jar
Binary file not shown.
22 changes: 10 additions & 12 deletions src/lufs_clj/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@
(next gtd)

(unchecked-inc i)))

(energy
(unchecked-add
(dbl/amean res-1)
(dbl/amean res-2)))))))
(do
(energy
(unchecked-add
(dbl/amean (double-array (remove #(= 0.0 %) res-1)))
(dbl/amean (double-array (remove #(= 0.0 %) res-2))))))))))


(defn pmap-filters
Expand Down Expand Up @@ -131,17 +131,15 @@
blocks-0
blocks-1)



; calculate 2nd relative threshold

Gamma_r (+
-10
Gamma_r (-
(tg-mean-e
blocks-0
blocks-1
J_g
len-2))
len-2)
10)

zag (map #(if (> Gamma_r (or % 0)) nil %) J_g)

Expand All @@ -150,6 +148,7 @@
; then drop 'em from original coll and calculate mean on every channel
; then calculate energy of sum


lufs
(tg-mean-e
blocks-0
Expand Down Expand Up @@ -237,7 +236,6 @@


(comment

(lra "test/media/test-sad.wav"))
(integrated "test/media/test.wav"))


77 changes: 68 additions & 9 deletions src/lufs_clj/file.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
(ns lufs-clj.file
(:import [java.io File ByteArrayOutputStream
FileOutputStream BufferedOutputStream RandomAccessFile]
[java.nio ByteBuffer ByteOrder]
[javax.sound.sampled AudioFormat
AudioFormat$Encoding AudioInputStream AudioSystem]))

(:import
[java.io
File
ByteArrayOutputStream
FileOutputStream
BufferedOutputStream
RandomAccessFile]
[java.nio
ByteBuffer
ByteOrder]
[javax.sound.sampled
AudioFormat
AudioFormat$Encoding
AudioInputStream
AudioSystem
Clip
SourceDataLine
TargetDataLine
BooleanControl
CompoundControl
EnumControl
FloatControl
Line
Line$Info
DataLine$Info
Mixer
Port]))


(defn- locate-file
Expand Down Expand Up @@ -49,7 +72,7 @@
(AudioSystem/getAudioInputStream
(AudioFormat. AudioFormat$Encoding/PCM_SIGNED
(long sr)
16
16
(.getChannels src-format)
4
(long sr)
Expand All @@ -58,12 +81,48 @@
channels (.getChannels src-format)
baos (ByteArrayOutputStream.)
buffer (byte-array 4096)]
(loop [cnt (.read ain buffer)]
(loop [cnt (.read ain0 buffer)]
(if (> cnt 0)
(do
(.write baos buffer 0 cnt)
(recur (.read ain buffer)))
{ :channels channels
:sample-rate (int sr)
:data (convert-to-double-arrays baos channels)
}))))
:data (convert-to-double-arrays baos channels)
}))))







(comment

(def s "test/media/test.wav")

(-> (load-table s)
:src-format
.getEncoding
.toString)


(do (load-table s))

(-> (java.io.File. s)
(AudioSystem/getAudioInputStream))

)













0 comments on commit cadc719

Please sign in to comment.