Skip to content

Commit

Permalink
Merge pull request #10 from lincollincol/develop
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
lincollincol authored Feb 12, 2023
2 parents 1527f2e + e8a10e2 commit 304251c
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 34 deletions.
3 changes: 2 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/build
/lint-baseline.xml
33 changes: 26 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "com.linc.audiowaveform.sample"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand All @@ -22,12 +22,31 @@ android {
setProperty("archivesBaseName", "audiowaveform_sample_$versionCode")
}

buildTypes {
lintOptions {
checkReleaseBuilds false
}

signingConfigs {
release {
storeFile file("/Users/andriiserbeniuk/Documents/Projects/AudioWaveform/audiowaveform.jks")
storePassword 'android'
keyAlias = 'audiowaveform-debug'
keyPassword 'android'
}
}

buildTypes {
debug {
minifyEnabled false
applicationIdSuffix = ".beta"
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand All @@ -39,7 +58,7 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerExtensionVersion kotlin_compiler_extension_version
}
packagingOptions {
resources {
Expand All @@ -61,7 +80,7 @@ dependencies {
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

/* Navigation */
implementation "androidx.navigation:navigation-compose:2.5.2"
implementation "androidx.navigation:navigation-compose:2.5.3"
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'

/* Hilt */
Expand All @@ -74,8 +93,8 @@ dependencies {
implementation "androidx.media3:media3-session:$media3_version"
implementation "androidx.media3:media3-ui:$media3_version"

implementation 'androidx.activity:activity-compose:1.5.1'
implementation 'com.github.lincollincol:amplituda:2.2.1'
implementation 'androidx.activity:activity-compose:1.6.1'
implementation 'com.github.lincollincol:amplituda:2.2.2'
implementation 'org.jodd:jodd-util:6.1.0'

}
Binary file added app/release/audiowaveform_sample_2-release.apk
Binary file not shown.
20 changes: 20 additions & 0 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": 3,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "com.linc.audiowaveform.sample",
"variantName": "release",
"elements": [
{
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 2,
"versionName": "1.1.0",
"outputFile": "audiowaveform_sample_2-release.apk"
}
],
"elementType": "File"
}
4 changes: 2 additions & 2 deletions audiowaveform/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerExtensionVersion kotlin_compiler_extension_version
}
packagingOptions {
resources {
Expand All @@ -42,7 +42,7 @@ android {

dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'androidx.appcompat:appcompat:1.6.0'
compileOnly "androidx.compose.ui:ui:$compose_version"
compileOnly "androidx.compose.material:material:$compose_version"
compileOnly "androidx.compose.ui:ui-tooling-preview:$compose_version"
Expand Down
28 changes: 13 additions & 15 deletions audiowaveform/src/main/java/com/linc/audiowaveform/AudioWaveform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ private const val MaxProgress: Float = 1F

private const val MinSpikeHeight: Float = 1F
private const val DefaultGraphicsLayerAlpha: Float = 0.99F
private const val AmplitudeMultiplier: Float = 2F

@OptIn(ExperimentalComposeUiApi::class)
@Composable
Expand Down Expand Up @@ -63,9 +62,8 @@ fun AudioWaveform(
var canvasSize by remember { mutableStateOf(Size(0f, 0f)) }
var spikes by remember { mutableStateOf(0F) }
val spikesAmplitudes = remember(amplitudes, spikes, amplitudeType) {
getSpikesAmplitudes(
amplitudes.toDrawableAmplitudes(
amplitudeType = amplitudeType,
amplitudes = amplitudes,
spikes = spikes.toInt(),
minHeight = MinSpikeHeight,
maxHeight = canvasSize.height.coerceAtLeast(MinSpikeHeight)
Expand Down Expand Up @@ -126,25 +124,25 @@ fun AudioWaveform(
}
}

private fun getSpikesAmplitudes(
private fun List<Int>.toDrawableAmplitudes(
amplitudeType: AmplitudeType,
amplitudes: List<Int>,
spikes: Int,
minHeight: Float,
maxHeight: Float
): List<Float> {
val amplitudes = map(Int::toFloat)
if(amplitudes.isEmpty() || spikes == 0) {
return List(spikes) { minHeight }
}
if(amplitudes.count() < spikes) {
return amplitudes.map(Int::toFloat)
val transform = { data: List<Float> ->
when(amplitudeType) {
AmplitudeType.Avg -> data.average()
AmplitudeType.Max -> data.max()
AmplitudeType.Min -> data.min()
}.toFloat().coerceIn(minHeight, maxHeight)
}
return amplitudes.map(Int::toFloat)
.chunkedToSize(spikes) {
when(amplitudeType) {
AmplitudeType.Avg -> it.average()
AmplitudeType.Max -> it.max()
AmplitudeType.Min -> it.min()
}.toFloat().times(AmplitudeMultiplier).coerceIn(minHeight, maxHeight)
}
return when {
spikes > amplitudes.count() -> amplitudes.fillToSize(spikes, transform)
else -> amplitudes.chunkToSize(spikes, transform)
}.normalize(minHeight, maxHeight)
}
15 changes: 12 additions & 3 deletions audiowaveform/src/main/java/com/linc/audiowaveform/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ 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> {
internal fun <T> Iterable<T>.fillToSize(size: Int, transform: (List<T>) -> T): List<T> {
val capacity = ceil(size.safeDiv(count())).roundToInt()
return map { data -> List(capacity) { data } }.flatten().chunkToSize(size, transform)
}

internal fun <T> Iterable<T>.chunkToSize(size: Int, transform: (List<T>) -> T): List<T> {
val chunkSize = count() / size
val remainder = count() % size
val remainderIndex = ceil(count().safeDiv(remainder)).roundToInt()
Expand All @@ -12,10 +17,14 @@ internal fun <T> Iterable<T>.chunkedToSize(size: Int, transform: (List<T>) -> T)
}.chunked(chunkSize, transform)
return when (size) {
chunkIteration.count() -> chunkIteration
else -> chunkIteration.chunkedToSize(size, transform)
else -> chunkIteration.chunkToSize(size, transform)
}
}

internal fun Int.safeDiv(value: Int): Float {
internal fun Iterable<Float>.normalize(min: Float, max: Float): List<Float> {
return map { (max-min) * ((it - min()) / (max() - min())) + min }
}

private fun Int.safeDiv(value: Int): Float {
return if(value == 0) return 0F else this / value.toFloat()
}
11 changes: 6 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
buildscript {
ext {
compose_version = '1.3.0-beta01'
hilt_version = '2.42'
kotlin_compiler_extension_version = '1.4.0'
compose_version = '1.4.0-beta01'
hilt_version = '2.44'
}
dependencies {
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
id 'com.android.application' version '7.4.1' apply false
id 'com.android.library' version '7.4.1' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}

task clean(type: Delete) {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Sep 06 21:22:14 EEST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit 304251c

Please sign in to comment.