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

Commit

Permalink
Fixed and optimized sampling algorithm, now it's lightning fast
Browse files Browse the repository at this point in the history
  • Loading branch information
alxrm committed Dec 7, 2016
1 parent 0324f47 commit f64aff3
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 69 deletions.
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "rm.com.audiogram"
minSdkVersion 15
targetSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -26,10 +26,10 @@ android {
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:audiowave-progressbar:0.6'
compile 'com.android.support:appcompat-v7:25.0.1'
// compile 'com.github.alxrm:audiowave-progressbar:0.6'

// compile project(':audiowave')
compile project(':audiowave')
}
repositories {
mavenCentral()
Expand Down
51 changes: 26 additions & 25 deletions app/src/main/res/layout/activity_another.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_another"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="rm.com.audiogram.AnotherActivity"
xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_another"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="rm.com.audiogram.AnotherActivity"
>

<rm.com.audiowave.AudioWaveView
android:id="@+id/wave"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_centerInParent="true"
android:layout_margin="16dp"
app:chunkWidth="3dp"
app:chunkHeight="24dp"
app:minChunkHeight="2dp"
app:chunkSpacing="1dp"
app:chunkRadius="1dp"
app:waveColor="@android:color/black"
app:progress="0.0"
/>
<rm.com.audiowave.AudioWaveView
android:id="@+id/wave"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_centerInParent="true"
android:layout_margin="16dp"
app:chunkHeight="24dp"
app:chunkRadius="1dp"
app:chunkSpacing="1dp"
app:chunkWidth="3dp"
app:minChunkHeight="2dp"
app:progress="0.0"
app:waveColor="@android:color/black"
/>

</RelativeLayout>
65 changes: 33 additions & 32 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
tools:context="rm.com.audiogram.MainActivity"
xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
tools:context="rm.com.audiogram.MainActivity"
>


<rm.com.audiowave.AudioWaveView
android:id="@+id/wave"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
app:chunkWidth="3dp"
app:chunkHeight="24dp"
app:minChunkHeight="2dp"
app:chunkSpacing="1dp"
app:chunkRadius="1dp"
app:waveColor="@android:color/white"
/>
<rm.com.audiowave.AudioWaveView
android:id="@+id/wave"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="16dp"
app:chunkHeight="24dp"
app:chunkRadius="1dp"
app:chunkSpacing="1dp"
app:chunkWidth="3dp"
app:minChunkHeight="2dp"
app:waveColor="@android:color/white"
/>

<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:clickable="true"
android:layout_margin="16dp"
android:textSize="16sp"
android:text="Play"
/>
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="16dp"
android:clickable="true"
android:text="Play"
android:textSize="16sp"
/>

</RelativeLayout>
2 changes: 1 addition & 1 deletion audiowave/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ android {
}

dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.5-2'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

repositories {
Expand Down
6 changes: 4 additions & 2 deletions audiowave/src/main/kotlin/rm/com/audiowave/AudioWaveView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class AudioWaveView : View {
inflateAttrs(attrs)
}

constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs,
defStyleAttr) {
inflateAttrs(attrs)
}

Expand Down Expand Up @@ -262,7 +263,8 @@ class AudioWaveView : View {
chunkHeight = getDimensionPixelSize(R.styleable.AudioWaveView_chunkHeight, chunkHeight)
chunkWidth = getDimensionPixelSize(R.styleable.AudioWaveView_chunkWidth, chunkWidth)
chunkSpacing = getDimensionPixelSize(R.styleable.AudioWaveView_chunkSpacing, chunkSpacing)
minChunkHeight = getDimensionPixelSize(R.styleable.AudioWaveView_minChunkHeight, minChunkHeight)
minChunkHeight = getDimensionPixelSize(R.styleable.AudioWaveView_minChunkHeight,
minChunkHeight)
chunkRadius = getDimensionPixelSize(R.styleable.AudioWaveView_chunkRadius, chunkRadius)
waveColor = getColor(R.styleable.AudioWaveView_waveColor, waveColor)
progress = getFloat(R.styleable.AudioWaveView_progress, progress)
Expand Down
8 changes: 7 additions & 1 deletion audiowave/src/main/kotlin/rm/com/audiowave/Graphics.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package rm.com.audiowave

import android.graphics.*
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.RectF
import android.view.View

/**
Expand Down
8 changes: 6 additions & 2 deletions audiowave/src/main/kotlin/rm/com/audiowave/Sampler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rm.com.audiowave

import android.os.Handler
import android.os.Looper
import android.util.Log
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

Expand All @@ -24,7 +25,10 @@ object Sampler {
}

fun downSample(data: ByteArray, targetSize: Int): ByteArray {
val targetSized = ByteArray(targetSize, { 0 })
val targetSized = ByteArray(targetSize)
val chunkSize = data.size / targetSize
val chunkStep = Math.max(Math.floor((chunkSize / 10.0)), 1.0).toInt()

var prevDataIndex = 0
var sampledPerChunk = 0F
var sumPerChunk = 0F
Expand All @@ -33,7 +37,7 @@ object Sampler {
return targetSized.paste(data)
}

for (index in 0..data.size) {
for (index in 0..data.size step chunkStep) {
val currentDataIndex = targetSize * index / data.size

if (prevDataIndex == currentDataIndex) {
Expand Down

0 comments on commit f64aff3

Please sign in to comment.