70 lines
1.9 KiB
Kotlin
70 lines
1.9 KiB
Kotlin
package nl.voidcorp.alarmclock
|
|
|
|
import com.github.salomonbrys.kotson.fromJson
|
|
import java.io.File
|
|
import java.io.FileReader
|
|
import javax.sound.sampled.*
|
|
import kotlin.concurrent.fixedRateTimer
|
|
|
|
|
|
/**
|
|
* @author J0QUE
|
|
* @version 1.0
|
|
*
|
|
*/
|
|
fun main(args: Array<String>) {
|
|
|
|
|
|
val config: Settings = gson.fromJson(FileReader("settings.json"))
|
|
|
|
val loaded = listf(config.filedir)
|
|
loaded.removeIf { it.isDirectory or (!it.name.endsWith("wav")) }
|
|
|
|
|
|
var last = getTime()
|
|
|
|
val st = AudioSystem.getAudioInputStream(loaded.getRandom())!!
|
|
val fm = st.format
|
|
val inf = DataLine.Info(Clip::class.java, fm)
|
|
(AudioSystem.getLine(inf as Line.Info) as Clip).apply {
|
|
open(st)
|
|
start()
|
|
addLineListener {
|
|
if (it.type == LineEvent.Type.STOP) {
|
|
this.close()
|
|
}
|
|
}
|
|
}
|
|
|
|
fixedRateTimer(period = 100) {
|
|
if (last != getTime()) {
|
|
last = getTime()
|
|
config.music.forEach {
|
|
if (last.hour == it.t.hour && last.mins == it.t.mins && last.secs == it.t.secs) {
|
|
var f = loaded.getRandom()
|
|
if ((it.song != null) && File(it.song).exists()) {
|
|
f = File(it.song)
|
|
}
|
|
println("playing ${f.path}")
|
|
val stream = AudioSystem.getAudioInputStream(f)!!
|
|
val format = stream.format
|
|
val info = DataLine.Info(Clip::class.java, format)
|
|
(AudioSystem.getLine(info as Line.Info) as Clip).apply {
|
|
open(stream)
|
|
start()
|
|
addLineListener {
|
|
if (it.type == LineEvent.Type.STOP) {
|
|
this.close()
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|