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

Commit f64aff3

Browse files
committed
Fixed and optimized sampling algorithm, now it's lightning fast
1 parent 0324f47 commit f64aff3

File tree

7 files changed

+83
-69
lines changed

7 files changed

+83
-69
lines changed

app/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-android'
33

44
android {
5-
compileSdkVersion 24
6-
buildToolsVersion "24.0.0"
5+
compileSdkVersion 25
6+
buildToolsVersion "25.0.0"
77
defaultConfig {
88
applicationId "rm.com.audiogram"
99
minSdkVersion 15
10-
targetSdkVersion 24
10+
targetSdkVersion 25
1111
versionCode 1
1212
versionName "1.0"
1313
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -26,10 +26,10 @@ android {
2626
dependencies {
2727
compile fileTree(dir: 'libs', include: ['*.jar'])
2828
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
29-
compile 'com.android.support:appcompat-v7:24.2.1'
30-
compile 'com.github.alxrm:audiowave-progressbar:0.6'
29+
compile 'com.android.support:appcompat-v7:25.0.1'
30+
// compile 'com.github.alxrm:audiowave-progressbar:0.6'
3131

32-
// compile project(':audiowave')
32+
compile project(':audiowave')
3333
}
3434
repositories {
3535
mavenCentral()
Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout
3-
android:id="@+id/activity_another"
4-
xmlns:android="http://schemas.android.com/apk/res/android"
5-
xmlns:tools="http://schemas.android.com/tools"
6-
android:layout_width="match_parent"
7-
android:layout_height="match_parent"
8-
android:paddingBottom="@dimen/activity_vertical_margin"
9-
android:paddingLeft="@dimen/activity_horizontal_margin"
10-
android:paddingRight="@dimen/activity_horizontal_margin"
11-
android:paddingTop="@dimen/activity_vertical_margin"
12-
tools:context="rm.com.audiogram.AnotherActivity"
13-
xmlns:app="http://schemas.android.com/apk/res-auto">
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:id="@+id/activity_another"
7+
android:layout_width="match_parent"
8+
android:layout_height="match_parent"
9+
android:paddingBottom="@dimen/activity_vertical_margin"
10+
android:paddingLeft="@dimen/activity_horizontal_margin"
11+
android:paddingRight="@dimen/activity_horizontal_margin"
12+
android:paddingTop="@dimen/activity_vertical_margin"
13+
tools:context="rm.com.audiogram.AnotherActivity"
14+
>
1415

15-
<rm.com.audiowave.AudioWaveView
16-
android:id="@+id/wave"
17-
android:layout_width="match_parent"
18-
android:layout_height="32dp"
19-
android:layout_centerInParent="true"
20-
android:layout_margin="16dp"
21-
app:chunkWidth="3dp"
22-
app:chunkHeight="24dp"
23-
app:minChunkHeight="2dp"
24-
app:chunkSpacing="1dp"
25-
app:chunkRadius="1dp"
26-
app:waveColor="@android:color/black"
27-
app:progress="0.0"
28-
/>
16+
<rm.com.audiowave.AudioWaveView
17+
android:id="@+id/wave"
18+
android:layout_width="match_parent"
19+
android:layout_height="32dp"
20+
android:layout_centerInParent="true"
21+
android:layout_margin="16dp"
22+
app:chunkHeight="24dp"
23+
app:chunkRadius="1dp"
24+
app:chunkSpacing="1dp"
25+
app:chunkWidth="3dp"
26+
app:minChunkHeight="2dp"
27+
app:progress="0.0"
28+
app:waveColor="@android:color/black"
29+
/>
2930

3031
</RelativeLayout>
Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout
3-
android:id="@+id/activity_main"
4-
xmlns:android="http://schemas.android.com/apk/res/android"
5-
xmlns:tools="http://schemas.android.com/tools"
6-
android:layout_width="match_parent"
7-
android:layout_height="match_parent"
8-
android:background="@color/colorPrimaryDark"
9-
tools:context="rm.com.audiogram.MainActivity"
10-
xmlns:app="http://schemas.android.com/apk/res-auto">
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:id="@+id/activity_main"
7+
android:layout_width="match_parent"
8+
android:layout_height="match_parent"
9+
android:background="@color/colorPrimaryDark"
10+
tools:context="rm.com.audiogram.MainActivity"
11+
>
1112

1213

13-
<rm.com.audiowave.AudioWaveView
14-
android:id="@+id/wave"
15-
android:layout_width="match_parent"
16-
android:layout_height="32dp"
17-
android:layout_centerHorizontal="true"
18-
android:layout_alignParentBottom="true"
19-
android:layout_margin="16dp"
20-
app:chunkWidth="3dp"
21-
app:chunkHeight="24dp"
22-
app:minChunkHeight="2dp"
23-
app:chunkSpacing="1dp"
24-
app:chunkRadius="1dp"
25-
app:waveColor="@android:color/white"
26-
/>
14+
<rm.com.audiowave.AudioWaveView
15+
android:id="@+id/wave"
16+
android:layout_width="match_parent"
17+
android:layout_height="32dp"
18+
android:layout_alignParentBottom="true"
19+
android:layout_centerHorizontal="true"
20+
android:layout_margin="16dp"
21+
app:chunkHeight="24dp"
22+
app:chunkRadius="1dp"
23+
app:chunkSpacing="1dp"
24+
app:chunkWidth="3dp"
25+
app:minChunkHeight="2dp"
26+
app:waveColor="@android:color/white"
27+
/>
2728

28-
<Button
29-
android:id="@+id/play"
30-
android:layout_width="wrap_content"
31-
android:layout_height="wrap_content"
32-
android:layout_centerHorizontal="true"
33-
android:clickable="true"
34-
android:layout_margin="16dp"
35-
android:textSize="16sp"
36-
android:text="Play"
37-
/>
29+
<Button
30+
android:id="@+id/play"
31+
android:layout_width="wrap_content"
32+
android:layout_height="wrap_content"
33+
android:layout_centerHorizontal="true"
34+
android:layout_margin="16dp"
35+
android:clickable="true"
36+
android:text="Play"
37+
android:textSize="16sp"
38+
/>
3839

3940
</RelativeLayout>

audiowave/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ android {
2323
}
2424

2525
dependencies {
26-
compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.5-2'
26+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2727
}
2828

2929
repositories {

audiowave/src/main/kotlin/rm/com/audiowave/AudioWaveView.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class AudioWaveView : View {
1818
inflateAttrs(attrs)
1919
}
2020

21-
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
21+
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs,
22+
defStyleAttr) {
2223
inflateAttrs(attrs)
2324
}
2425

@@ -262,7 +263,8 @@ class AudioWaveView : View {
262263
chunkHeight = getDimensionPixelSize(R.styleable.AudioWaveView_chunkHeight, chunkHeight)
263264
chunkWidth = getDimensionPixelSize(R.styleable.AudioWaveView_chunkWidth, chunkWidth)
264265
chunkSpacing = getDimensionPixelSize(R.styleable.AudioWaveView_chunkSpacing, chunkSpacing)
265-
minChunkHeight = getDimensionPixelSize(R.styleable.AudioWaveView_minChunkHeight, minChunkHeight)
266+
minChunkHeight = getDimensionPixelSize(R.styleable.AudioWaveView_minChunkHeight,
267+
minChunkHeight)
266268
chunkRadius = getDimensionPixelSize(R.styleable.AudioWaveView_chunkRadius, chunkRadius)
267269
waveColor = getColor(R.styleable.AudioWaveView_waveColor, waveColor)
268270
progress = getFloat(R.styleable.AudioWaveView_progress, progress)

audiowave/src/main/kotlin/rm/com/audiowave/Graphics.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package rm.com.audiowave
22

3-
import android.graphics.*
3+
import android.graphics.Bitmap
4+
import android.graphics.Canvas
5+
import android.graphics.Color
6+
import android.graphics.Paint
7+
import android.graphics.PorterDuff
8+
import android.graphics.PorterDuffColorFilter
9+
import android.graphics.RectF
410
import android.view.View
511

612
/**

audiowave/src/main/kotlin/rm/com/audiowave/Sampler.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rm.com.audiowave
22

33
import android.os.Handler
44
import android.os.Looper
5+
import android.util.Log
56
import java.util.concurrent.ExecutorService
67
import java.util.concurrent.Executors
78

@@ -24,7 +25,10 @@ object Sampler {
2425
}
2526

2627
fun downSample(data: ByteArray, targetSize: Int): ByteArray {
27-
val targetSized = ByteArray(targetSize, { 0 })
28+
val targetSized = ByteArray(targetSize)
29+
val chunkSize = data.size / targetSize
30+
val chunkStep = Math.max(Math.floor((chunkSize / 10.0)), 1.0).toInt()
31+
2832
var prevDataIndex = 0
2933
var sampledPerChunk = 0F
3034
var sumPerChunk = 0F
@@ -33,7 +37,7 @@ object Sampler {
3337
return targetSized.paste(data)
3438
}
3539

36-
for (index in 0..data.size) {
40+
for (index in 0..data.size step chunkStep) {
3741
val currentDataIndex = targetSize * index / data.size
3842

3943
if (prevDataIndex == currentDataIndex) {

0 commit comments

Comments
 (0)