-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from lincollincol/develop
Fix amplitudes chunked function
- Loading branch information
Showing
3 changed files
with
39 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
audiowaveform/src/main/java/com/linc/audiowaveform/Utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.linc.audiowaveform | ||
|
||
import kotlin.math.ceil | ||
import kotlin.math.roundToInt | ||
|
||
internal fun <T> Iterable<T>.chunkedToSize(size: Int, transform: (List<T>) -> T): List<T> { | ||
val chunkSize = count() / size | ||
val remainder = count() % size | ||
val remainderIndex = ceil(count().safeDiv(remainder)).roundToInt() | ||
val chunkIteration = filterIndexed { index, _ -> | ||
remainderIndex == 0 || index % remainderIndex != 0 | ||
}.chunked(chunkSize, transform) | ||
return when (size) { | ||
chunkIteration.count() -> chunkIteration | ||
else -> chunkIteration.chunkedToSize(size, transform) | ||
} | ||
} | ||
|
||
internal fun Int.safeDiv(value: Int): Float { | ||
return if(value == 0) return 0F else this / value.toFloat() | ||
} |