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

Commit

Permalink
Added gif + switched from tabs to spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
alxrm committed Nov 5, 2016
1 parent 8dc98c8 commit 8f8f6df
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 78 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/rm/com/audiogram/AnotherActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_another);

final AudioWaveView waveView = (AudioWaveView) findViewById(R.id.wave);
final byte[] data = { 1, 3, 37, 117, 69, 0, 0, 58 };
final byte[] data = {1, 3, 37, 117, 69, 0, 0, 58};

waveView.setScaledData(data);

Expand Down
36 changes: 18 additions & 18 deletions app/src/main/kotlin/rm/com/audiogram/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ import rm.com.audiowave.AudioWaveView

class MainActivity : AppCompatActivity() {

val wave by lazy { findViewById(R.id.wave) as AudioWaveView }
val play by lazy { findViewById(R.id.play) as Button }
val wave by lazy { findViewById(R.id.wave) as AudioWaveView }
val play by lazy { findViewById(R.id.play) as Button }

val progressAnim: ObjectAnimator by lazy {
ObjectAnimator.ofFloat(wave, "progress", 0F, 100F).apply {
interpolator = LinearInterpolator()
duration = 1000
}
}
val progressAnim: ObjectAnimator by lazy {
ObjectAnimator.ofFloat(wave, "progress", 0F, 100F).apply {
interpolator = LinearInterpolator()
duration = 1000
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

play.setOnClickListener {
inflateWave()
}
}
play.setOnClickListener {
inflateWave()
}
}

fun inflateWave() {
wave.setRawData(assets.open("End.mp3").readBytes()) { progressAnim.start() }
}
fun inflateWave() {
wave.setRawData(assets.open("End.mp3").readBytes()) { progressAnim.start() }
}
}
40 changes: 20 additions & 20 deletions audiowave/src/main/kotlin/rm/com/audiowave/Graphics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,44 @@ import android.view.View
internal fun View.dip(value: Int): Int = (value * resources.displayMetrics.density).toInt()

internal fun smoothPaint(color: Int = Color.WHITE): Paint =
Paint().apply {
isAntiAlias = true
this.color = color
}
Paint().apply {
isAntiAlias = true
this.color = color
}

internal fun filterPaint(color: Int = Color.BLACK): Paint =
Paint().apply {
isAntiAlias = true
colorFilter = filterOf(color)
}
Paint().apply {
isAntiAlias = true
colorFilter = filterOf(color)
}

internal fun filterOf(color: Int = Color.BLACK) =
PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)
PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)

internal inline fun Canvas.transform(crossinline init: Canvas.() -> Unit) {
save()
init()
restore()
save()
init()
restore()
}

internal fun rectFOf(left: Int, top: Int, right: Int, bottom: Int) = RectF(
left.toFloat()
, top.toFloat()
, right.toFloat()
, bottom.toFloat()
left.toFloat()
, top.toFloat()
, right.toFloat()
, bottom.toFloat()
)

internal fun Int.withAlpha(alpha: Int): Int {
require(alpha in 0x00..0xFF)
return this and 0x00FFFFFF or (alpha shl 24)
require(alpha in 0x00..0xFF)
return this and 0x00FFFFFF or (alpha shl 24)
}

internal fun Bitmap.inCanvas(): Canvas = Canvas(this)

internal fun Bitmap?.safeRecycle() =
if (this != null && !isRecycled) recycle() else Unit
if (this != null && !isRecycled) recycle() else Unit

internal fun Bitmap?.flush() = this?.eraseColor(0)

internal fun Bitmap?.fits(neededW: Int, neededH: Int): Boolean =
this?.let { it.height == neededH && it.width == neededW } ?: false
this?.let { it.height == neededH && it.width == neededW } ?: false
78 changes: 39 additions & 39 deletions audiowave/src/main/kotlin/rm/com/audiowave/Samplings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,58 @@ internal val SAMPLER_THREAD: ExecutorService = Executors.newSingleThreadExecutor

object Sampler {

fun downSampleAsync(data: ByteArray, targetSize: Int, answer: (ByteArray) -> Unit) {
SAMPLER_THREAD.submit {
val scaled = downSample(data, targetSize)
fun downSampleAsync(data: ByteArray, targetSize: Int, answer: (ByteArray) -> Unit) {
SAMPLER_THREAD.submit {
val scaled = downSample(data, targetSize)

MAIN_THREAD.post {
answer(scaled)
}
}
}
MAIN_THREAD.post {
answer(scaled)
}
}
}

fun downSample(data: ByteArray, targetSize: Int): ByteArray {
val targetSized = ByteArray(targetSize, { 0 })
val reducedSample = mutableListOf<Byte>()
fun downSample(data: ByteArray, targetSize: Int): ByteArray {
val targetSized = ByteArray(targetSize, { 0 })
val reducedSample = mutableListOf<Byte>()

var prevDataIndex = 0
var prevDataIndex = 0

if (targetSize >= data.size) {
return targetSized.paste(data)
}
if (targetSize >= data.size) {
return targetSized.paste(data)
}

data.forEachIndexed { i, byte ->
val currentDataIndex = targetSize * i / data.size
data.forEachIndexed { i, byte ->
val currentDataIndex = targetSize * i / data.size

if (prevDataIndex == currentDataIndex) {
reducedSample += byte.abs
} else {
targetSized[currentDataIndex - 1] = reducedSample.average().toByte()
reducedSample.clear()
}
if (prevDataIndex == currentDataIndex) {
reducedSample += byte.abs
} else {
targetSized[currentDataIndex - 1] = reducedSample.average().toByte()
reducedSample.clear()
}

prevDataIndex = currentDataIndex
}
prevDataIndex = currentDataIndex
}

targetSized[prevDataIndex] = reducedSample.average().toByte()
targetSized[prevDataIndex] = reducedSample.average().toByte()

return targetSized
}
return targetSized
}
}

internal val Byte.abs: Byte
get() = when (this) {
Byte.MIN_VALUE -> Byte.MAX_VALUE
in (Byte.MIN_VALUE + 1..0) -> (-this).toByte()
else -> this
}
get() = when (this) {
Byte.MIN_VALUE -> Byte.MAX_VALUE
in (Byte.MIN_VALUE + 1..0) -> (-this).toByte()
else -> this
}

internal fun ByteArray.paste(other: ByteArray): ByteArray {
if (size == 0) return byteArrayOf()
if (size == 0) return byteArrayOf()

return this.apply {
forEachIndexed { i, byte ->
this[i] = other.getOrElse(i, { this[i].abs })
}
}
return this.apply {
forEachIndexed { i, byte ->
this[i] = other.getOrElse(i, { this[i].abs })
}
}
}
Binary file added imgs/wave.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8f8f6df

Please sign in to comment.