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

Commit ab5bdc3

Browse files
committed
Fixed sampling algorithm
1 parent 8f8f6df commit ab5bdc3

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

app/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ dependencies {
2727
compile fileTree(dir: 'libs', include: ['*.jar'])
2828
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2929
compile 'com.android.support:appcompat-v7:24.2.1'
30-
compile 'com.github.alxrm:Audiogram:0.4'
30+
// compile 'com.github.alxrm:Audiogram:0.5'
31+
32+
compile project(':audiowave')
3133
}
3234
repositories {
3335
mavenCentral()

audiowave/src/main/kotlin/rm/com/audiowave/Samplings.kt renamed to audiowave/src/main/kotlin/rm/com/audiowave/Sampler.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,29 @@ object Sampler {
2525

2626
fun downSample(data: ByteArray, targetSize: Int): ByteArray {
2727
val targetSized = ByteArray(targetSize, { 0 })
28-
val reducedSample = mutableListOf<Byte>()
29-
3028
var prevDataIndex = 0
29+
var sampledPerChunk = 0F
30+
var sumPerChunk = 0F
3131

3232
if (targetSize >= data.size) {
3333
return targetSized.paste(data)
3434
}
3535

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

3939
if (prevDataIndex == currentDataIndex) {
40-
reducedSample += byte.abs
40+
sampledPerChunk += 1
41+
sumPerChunk += data[index].abs
4142
} else {
42-
targetSized[currentDataIndex - 1] = reducedSample.average().toByte()
43-
reducedSample.clear()
44-
}
43+
targetSized[prevDataIndex] = (sumPerChunk / sampledPerChunk).toByte()
4544

46-
prevDataIndex = currentDataIndex
45+
sumPerChunk = 0F
46+
sampledPerChunk = 0F
47+
prevDataIndex = currentDataIndex
48+
}
4749
}
4850

49-
targetSized[prevDataIndex] = reducedSample.average().toByte()
50-
5151
return targetSized
5252
}
5353
}

0 commit comments

Comments
 (0)