Skip to content

Commit

Permalink
sound level
Browse files Browse the repository at this point in the history
  • Loading branch information
dsokolov committed Dec 8, 2016
1 parent 24eeff9 commit 51407db
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ class MainActivity : AppCompatActivity() {

startLevelButton.setOnClickListener {
val source = RecordObservable.create16bit(recordAudioOptions)
val lowpassFilter = LowpassFilter.crate16bit(32.0F, 1.0F, recordAudioOptions)
subs = Observable.
create(source).
sample(500L, TimeUnit.MILLISECONDS).
map { samples -> lowpassFilter.filter(samples) }.
map { samples ->
AudioLevel.maxDecibel(samples)
}.
map { peek -> peek + 110.0 }.
map { peek -> peek + 160.0 }.
subscribeOn(Schedulers.newThread()).
observeOn(AndroidSchedulers.mainThread()).
subscribe {
Expand Down
19 changes: 16 additions & 3 deletions rxaa/src/main/kotlin/me/ilich/rxandroidaudio/AudioLevel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class AudioLevel {

companion object {

private const val EPSILON = 0.001;

fun rms(samples: ShortArray) = if (samples.isEmpty()) {
0.0
} else {
Expand All @@ -12,11 +14,22 @@ class AudioLevel {

fun maxDecibel(samples: ShortArray): Double =
samples.
map { sh -> sh.toDouble() / Short.MAX_VALUE }.
map { db -> 20.0 * Math.log10(db) }.
map { sh ->
sh.toDouble() / Short.MAX_VALUE
}.
map { db ->
if (db == 0.0) {
EPSILON
} else {
Math.abs(db)
}
}.
map { db ->
20.0 * Math.log10(db)
}.
filter(Double::isFinite).
max() ?:
0.0
Double.NEGATIVE_INFINITY

}

Expand Down

0 comments on commit 51407db

Please sign in to comment.