Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Fixed sampling algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
alxrm committed Nov 5, 2016
1 parent 8f8f6df commit ab5bdc3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.github.alxrm:Audiogram:0.4'
// compile 'com.github.alxrm:Audiogram:0.5'

compile project(':audiowave')
}
repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@ object Sampler {

fun downSample(data: ByteArray, targetSize: Int): ByteArray {
val targetSized = ByteArray(targetSize, { 0 })
val reducedSample = mutableListOf<Byte>()

var prevDataIndex = 0
var sampledPerChunk = 0F
var sumPerChunk = 0F

if (targetSize >= data.size) {
return targetSized.paste(data)
}

data.forEachIndexed { i, byte ->
val currentDataIndex = targetSize * i / data.size
for (index in 0..data.size) {
val currentDataIndex = targetSize * index / data.size

if (prevDataIndex == currentDataIndex) {
reducedSample += byte.abs
sampledPerChunk += 1
sumPerChunk += data[index].abs
} else {
targetSized[currentDataIndex - 1] = reducedSample.average().toByte()
reducedSample.clear()
}
targetSized[prevDataIndex] = (sumPerChunk / sampledPerChunk).toByte()

prevDataIndex = currentDataIndex
sumPerChunk = 0F
sampledPerChunk = 0F
prevDataIndex = currentDataIndex
}
}

targetSized[prevDataIndex] = reducedSample.average().toByte()

return targetSized
}
}
Expand Down

0 comments on commit ab5bdc3

Please sign in to comment.