diff --git a/app/build.gradle b/app/build.gradle index 1d9e95f..d7b436f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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() diff --git a/audiowave/src/main/kotlin/rm/com/audiowave/Samplings.kt b/audiowave/src/main/kotlin/rm/com/audiowave/Sampler.kt similarity index 76% rename from audiowave/src/main/kotlin/rm/com/audiowave/Samplings.kt rename to audiowave/src/main/kotlin/rm/com/audiowave/Sampler.kt index 17551a1..3f082a0 100644 --- a/audiowave/src/main/kotlin/rm/com/audiowave/Samplings.kt +++ b/audiowave/src/main/kotlin/rm/com/audiowave/Sampler.kt @@ -25,29 +25,29 @@ object Sampler { fun downSample(data: ByteArray, targetSize: Int): ByteArray { val targetSized = ByteArray(targetSize, { 0 }) - val reducedSample = mutableListOf() - 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 } }