Skip to content

Commit

Permalink
Add sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
neworld committed May 8, 2017
1 parent da1d3be commit af0c0c3
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 5 deletions.
8 changes: 7 additions & 1 deletion app/src/assets/climber_timer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ run_time=300
wait_time=30
warning_time=10
title=ClimbTimer

color_of_run_time=FFFFFF
color_of_wait_time=00FF00
color_of_warning_time=FF0000
color_of_warning_time=FF0000

sound_start=sounds/start.wav
sound_last_minute=sounds/minute.wav
sound_last_seconds=sounds/ding.wav
sound_finish=sounds/finish.wav
Binary file added app/src/assets/sounds/ding.wav
Binary file not shown.
Binary file added app/src/assets/sounds/finish.wav
Binary file not shown.
Binary file added app/src/assets/sounds/minute.wav
Binary file not shown.
Binary file added app/src/assets/sounds/start.wav
Binary file not shown.
21 changes: 21 additions & 0 deletions app/src/main/java/lt/neworld/climbtimer/AppProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ object AppProperties {
private const val PROP_WAIT_TIME = "wait_time"
private const val PROP_WARNING_TIME = "warning_time"
private const val PROP_TITLE = "title"

private const val PROP_COLOR_OF_RUN_TIME = "color_of_run_time"
private const val PROP_COLOR_OF_WAIT_TIME = "color_of_wait_time"
private const val PROP_COLOR_OF_WARNING_TIME = "color_of_warning_time"

private const val PROP_SOUND_START = "sound_start"
private const val PROP_SOUND_LAST_MINUTE = "sound_last_minute"
private const val PROP_SOUND_LAST_SECONDS = "sound_last_seconds"
private const val PROP_SOUND_FINISH = "sound_finish"

private val file = File("climber_timer.properties")

private val properties by lazy {
Expand Down Expand Up @@ -49,6 +55,11 @@ object AppProperties {
var colorOfWaitTime: Int by ColorField(PROP_COLOR_OF_WAIT_TIME, 0x00FF00)
var colorOfWarning: Int by ColorField(PROP_COLOR_OF_WARNING_TIME, 0xFF0000)

var soundStart: File? by FileField(PROP_SOUND_START, null)
var soundLastMinute: File? by FileField(PROP_SOUND_LAST_MINUTE, null)
var soundLastSeconds: File? by FileField(PROP_SOUND_LAST_SECONDS, null)
var soundFinish: File? by FileField(PROP_SOUND_FINISH, null)

class TimeField(key: String, default: Long) : Field<Long>(key, default) {
override fun deserialize(raw: String): Long = raw.toLong() * 1000

Expand All @@ -67,6 +78,16 @@ object AppProperties {
override fun serialize(value: Int) = "%06X".format(value)
}

class FileField(key: String, default: File?) : Field<File?>(key, default) {
override fun deserialize(raw: String): File? {
return if (raw.isNotBlank()) File(raw) else null
}

override fun serialize(value: File?): String {
return value?.relativeTo(File("."))?.path ?: ""
}
}

abstract class Field<T>(
val key: String,
val default: T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import javafx.scene.paint.Color
import javafx.stage.Stage
import lt.neworld.climbtimer.AppProperties
import lt.neworld.climbtimer.extensions.startAnimationTimer
import lt.neworld.climbtimer.utils.Sounds
import lt.neworld.climbtimer.utils.Timer
import java.net.URL
import java.util.*
Expand Down Expand Up @@ -67,6 +68,13 @@ class TimerController : Initializable {
}

clock.textFill = Color.web("#%06X".format(color))

when (state.event) {
Timer.Event.START -> Sounds.playStart()
Timer.Event.LAST_MINUTE -> Sounds.playLastMinute()
Timer.Event.LAST_SECONDS -> Sounds.playLastSeconds()
Timer.Event.FINISH -> Sounds.playFinish()
}
}

companion object {
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/lt/neworld/climbtimer/utils/Sounds.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lt.neworld.climbtimer.utils

import javafx.scene.media.Media
import javafx.scene.media.MediaPlayer
import lt.neworld.climbtimer.AppProperties

/**
* @author Andrius Semionovas
* @since 2017-05-08
*/
object Sounds {
private val startMedia by lazy {
AppProperties.soundStart?.let { Media(it.toURI().toString()) }
}
private val finishMedia by lazy {
AppProperties.soundFinish?.let { Media(it.toURI().toString()) }
}
private val lastMinuteMedia by lazy {
AppProperties.soundLastMinute?.let { Media(it.toURI().toString()) }
}
private val lastSecondsMedia by lazy {
AppProperties.soundLastSeconds?.let { Media(it.toURI().toString()) }
}

private fun play(media: Media?) {
media ?: return
MediaPlayer(media).play()
}

fun playStart() {
play(startMedia)
}

fun playFinish() {
play(finishMedia)
}

fun playLastMinute() {
play(lastMinuteMedia)
}

fun playLastSeconds() {
play(lastSecondsMedia)
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/lt/neworld/climbtimer/utils/Timer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Timer(
var curTime = waitTime
while (curTime < waitTime + warningTime) {
val upper = Math.min(curTime + SECOND_MS, waitTime + warningTime)
add(curTime..upper - 1 to Event.LAST_SECOND)
add(curTime..upper - 1 to Event.LAST_SECONDS)
curTime = upper
}

Expand Down Expand Up @@ -101,7 +101,7 @@ class Timer(
}

enum class Event {
START, LAST_MINUTE, LAST_SECOND, FINISH
START, LAST_MINUTE, LAST_SECONDS, FINISH
}

private val LAST_MINUTE_MS = 60_000L
Expand Down
4 changes: 2 additions & 2 deletions app/src/test/java/lt/neworld/climbtimer/utils/TimerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class TimerTest {
fixture.state
whenever(clock.millis()).thenReturn(runTime - warningTime + 1)

assertEquals(LAST_SECOND, fixture.state.event)
assertEquals(LAST_SECONDS, fixture.state.event)
}

@Test
Expand All @@ -156,7 +156,7 @@ class TimerTest {
fixture.state
whenever(clock.millis()).thenReturn(64_000L)

assertEquals(LAST_SECOND, fixture.state.event)
assertEquals(LAST_SECONDS, fixture.state.event)
}

@Test
Expand Down

0 comments on commit af0c0c3

Please sign in to comment.