Skip to content

Commit

Permalink
Implemented NativeSound lengthInMs
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed May 5, 2017
1 parent 2d525ba commit 7d373d5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 13 additions & 3 deletions korau/src/com/soywiz/korau/awt/AwtNativeSoundProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class AwtNativeSoundProvider : NativeSoundProvider() {

override suspend fun createSound(data: ByteArray): NativeSound {
try {
return AwtNativeSound((AudioFormats.decode(data.openAsync()) ?: AudioData(44100, 2, shortArrayOf())).toWav())
return AwtNativeSound((AudioFormats.decode(data.openAsync()) ?: AudioData(44100, 2, shortArrayOf())).toWav()).init()
}catch (e: Throwable) {
e.printStackTrace()
return AwtNativeSound(AudioData(44100, 2, shortArrayOf()).toWav())
return AwtNativeSound(AudioData(44100, 2, shortArrayOf()).toWav()).init()
}
//return AwtNativeSound(data)
}
Expand Down Expand Up @@ -65,6 +65,16 @@ class AwtNativeSoundProvider : NativeSoundProvider() {
}

class AwtNativeSound(val data: ByteArray) : NativeSound() {
override var lengthInMs: Long = 0L

suspend fun init(): AwtNativeSound {
executeInWorker {
val sound = AudioSystem.getAudioInputStream(ByteArrayInputStream(data))
lengthInMs = (sound.frameLength * 1000.0 / sound.format.frameRate.toDouble()).toLong()
}
return this
}

suspend override fun play(): Unit = suspendCancellableCoroutine { c ->
Thread {
val sound = AudioSystem.getAudioInputStream(ByteArrayInputStream(data))
Expand Down Expand Up @@ -93,4 +103,4 @@ class AwtNativeSound(val data: ByteArray) : NativeSound() {
}
}
}
}
}
10 changes: 9 additions & 1 deletion korau/src/com/soywiz/korau/js/HtmlNativeSoundProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ class HtmlNativeSound(val url: String) : NativeSound(), AsyncDependency {
val audio = jsNew("Audio", url)
//private val once = Once()

suspend override fun init() = suspendCancellableCoroutine<Unit> { c ->
override var lengthInMs: Long = 0L

suspend override fun init() {
initInternal()
lengthInMs = (audio["duration"].toDouble() * 1000L).toLong()
}


suspend fun initInternal() = suspendCancellableCoroutine<Unit> { c ->
var ok: JsDynamic? = null
var error: JsDynamic? = null

Expand Down

0 comments on commit 7d373d5

Please sign in to comment.