Newer
Older
kotlin-webgl-test / src / main / kotlin / games / perses / sound / Sounds.kt
rnentjes on 26 Mar 2017 1 KB IE11 support
package games.perses.sound

import org.w3c.dom.HTMLAudioElement
import kotlin.browser.document

/**
 * User: rnentjes
 * Date: 18-5-16
 * Time: 12:34
 */

class Sound(val name:String, val url: String, val volume: Double = 0.75, val numberOfChannels: Int) {
    var channels: Array<HTMLAudioElement?>
    var nextChannel: Int = 0

    init {
        //println("CREATING: $name")
        channels = Array(numberOfChannels, { document.createElement("audio") as HTMLAudioElement })

        for (audio in channels) {
            if (audio != null) {
                audio.src = url
                audio.pause()
                audio.load()
                audio.volume = volume
            }
        }
    }

    fun play() {
        //println("PLAYING: $name - $nextChannel")
        channels[nextChannel]?.currentTime = 0.0
        channels[nextChannel]?.play()

        nextChannel = (nextChannel + 1) % channels.size
    }

    fun pause() {
        for (audio in channels) {
            audio?.pause()
        }
    }
}

object Sounds {
    val sounds: MutableMap<String, Sound> = HashMap()

    fun load(name: String, url: String, volume: Double = 0.75, channels: Int = 1 ) {
        sounds.put(name, Sound(name, url, volume, channels))
    }

    fun play(name: String, volume: Float = 0.75f) {
        val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!")

        sound.play()
    }

    fun pause(name: String) {
        val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!")

        sound.pause()
    }
}