package com.cottondb.music import com.cottondb.music.faces.Card import com.cottondb.music.faces.HifiDeck import com.cottondb.music.faces.HifiKnobs import com.cottondb.music.faces.Measure import com.cottondb.music.faces.TrackPlay import com.cottondb.music.faces.TraySpot import com.cottondb.music.faces.hifiBlank import com.cottondb.music.faces.hifiByline import com.cottondb.music.faces.hifiTitle import com.cottondb.music.faces.hifiTracked import com.cottondb.shell.core.Lines import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test import kotlin.math.cos import kotlin.math.sin /** The hi-fi's setting rules (SongHifiFace.swift's HifiDeck and HifiTitle), on invented cards. */ class HifiSettingTest { private val words = Lines(mapOf("faces.hifi.below" to "−{n}", "faces.hifi.above" to "+{n}", "cards.join" to " · "), "Cotton Music") /** Every letter half its size wide. */ private val m = Measure { said, _, size -> said.length * size * 0.5f } private fun card(slots: String) = Card(Json.parseToJsonElement("""{"id":"c","face":"song.hifi","slots":$slots}""").jsonObject) @Test fun theDeckIsDrawnItsOwnWidthNeverWiderThan340() { assertEquals(340f, HifiDeck.width(400f, 800f), 1e-4f) assertEquals(300f, HifiDeck.width(300f, 800f), 1e-4f) assertEquals(400f * 1135f / 1475f, HifiDeck.width(400f, 400f), 1e-3f) } @Test fun theKnobSweepsTheSong() { assertEquals(-135.0, HifiDeck.knob(0.0, 242.0), 0.0) assertEquals(0.0, HifiDeck.knob(121.0, 242.0), 1e-9) assertEquals(135.0, HifiDeck.knob(900.0, 242.0), 0.0) assertEquals(-135.0, HifiDeck.knob(60.0, 0.0), 0.0) // A turn of the whole sweep is the whole song, held to it. assertEquals(242.0, HifiDeck.turned(0.0, 270.0, 242.0), 1e-9) assertEquals(60.5, HifiDeck.turned(0.0, 67.5, 242.0), 1e-9) assertEquals(0.0, HifiDeck.turned(30.0, -200.0, 242.0), 0.0) assertEquals(0.0, HifiDeck.elapsed(TrackPlay.still, 50.0), 0.0) assertEquals(50.0, HifiDeck.elapsed(TrackPlay(held = true, elapsed = 10.0), 50.0), 0.0) assertEquals(10.0, HifiDeck.elapsed(TrackPlay(held = true, elapsed = 10.0), null), 0.0) } @Test fun theNeedlesReadThePaintedScale() { assertEquals(-55.5, HifiDeck.needle(-40.0), 1e-9) assertEquals(0.0, HifiDeck.needle(-10.0), 1e-9) assertEquals((-34.6 + -21.9) / 2, HifiDeck.needle(-25.0), 1e-9) assertEquals(48.6, HifiDeck.needle(10.0), 1e-9) assertEquals(-40.0, HifiDeck.decibels(0.0), 0.0) assertEquals(3.0, HifiDeck.decibels(1.0), 0.0) // No level known (the player meters none): at rest while it plays, too. val playing = TrackPlay(held = true, playing = true, elapsed = 134.0, duration = 242.0) assertEquals(listOf(-55.5, -55.5), HifiDeck.needles(playing)) assertEquals(listOf(-55.5, -55.5), HifiDeck.needles(playing.copy(playing = false), 0.5 to 0.5)) assertEquals(listOf(-55.5, 48.6), HifiDeck.needles(playing, 0.0 to 1.0)) } @Test fun theFiguresAreTheLines() { assertEquals(listOf("−40", "−30", "−20", "−10", "0", "+3"), HifiKnobs.marks.map { HifiDeck.figure(it.decibels, words) }) // −10's tick stands upright; its figure past the tick's end, under its window's ceiling. val upright = HifiDeck.figureAt(HifiKnobs.marks[3], 0) assertEquals(307.0, upright.x, 1e-9) assertEquals(maxOf(80.0 + 1 + 9.5, 326.0 - (224 + 4 + 9.5)), upright.y, 1e-9) val low = HifiDeck.figureAt(HifiKnobs.marks[0], 1) val r = Math.toRadians(-55.5) assertEquals(826.0 + 262.5 * sin(r), low.x, 1e-6) assertEquals(maxOf(90.5, 326.0 - 262.5 * cos(r)), low.y, 1e-6) } @Test fun theTimesAboutTheKnob() { val t = HifiDeck.times(134.7, 242.0) assertEquals("2:14", t.elapsed) assertEquals("0:00", t.start) assertEquals("4:02", t.length) assertEquals("−1:48", t.left) val unknown = HifiDeck.times(61.0, 0.0) assertEquals("1:01", unknown.elapsed) assertNull(unknown.start); assertNull(unknown.length); assertNull(unknown.left) val (start, end) = HifiDeck.ends() val reach = 326f * 518f / 700f * (Math.sqrt(0.5).toFloat()) assertEquals(567f - reach - 14f - 180f, start.x, 1e-3f) assertEquals(567f + reach + 14f, end.x, 1e-3f) assertEquals(730f + reach + 6f - 22f, end.y, 1e-3f) } @Test fun theWordsAreTheCards() { val song = card("""{"title":"Harbour Lights","artist":"The Low Tide","album":"Night Ferry","duration":242}""") assertEquals("The Low Tide · Night Ferry", HifiDeck.byline(song, words)) assertEquals("Night Ferry · The Low Tide", HifiDeck.sleeve(song, words)) assertEquals(242.0, HifiDeck.length(TrackPlay.still, song), 0.0) assertNull(HifiDeck.byline(card("""{"title":"Harbour Lights"}"""), words)) } @Test fun theJewelTheNeedlesAndTheTouch() { val on = HifiDeck.jewel(true) assertEquals(567f - 96.5f * 42f / 143f, on.x, 1e-3f) val needle = HifiDeck.needleBox(0) val scale = 231f / 337f assertEquals(307f - 35f * scale, needle.x, 1e-3f) assertEquals(375f * scale, needle.h, 1e-3f) assertTrue(HifiDeck.onKnob(HifiKnobs.knobCentre)) assertFalse(HifiDeck.onKnob(TraySpot(567.0, 500.0))) assertTrue(HifiDeck.onJewel(TraySpot(600.0, 450.0))) assertEquals(-1, HifiDeck.onPlate(TraySpot(100.0, 100.0))) assertEquals(1, HifiDeck.onPlate(TraySpot(1000.0, 100.0))) assertEquals(0, HifiDeck.onPlate(TraySpot(1000.0, 500.0))) } @Test fun theTitleStepsDownThenTakesTwoLines() { val spaced = hifiTracked(m, 0.16f) // 14 letters at 50, spaced: 14 * 50 * 0.66 = 462, one line at its size. assertEquals(listOf("HARBOUR LIGHTS"), hifiTitle("Harbour Lights", m).lines) assertEquals(50f, hifiTitle("Harbour Lights", m).size, 0f) // 26 letters: 858 at 50, 737.9 at 43. assertEquals(43f, hifiTitle("A Slow Train Home Tonights", m).size, 1e-4f) val long = hifiTitle("The Slow Train Home Through Sixteen Lanterns", m) assertEquals(2, long.lines.size) assertEquals(30f, long.size, 1e-4f) assertTrue(long.lines.all { spaced.wide(it, Measure.Face.Caslon, long.size) <= 815f }) val by = hifiByline("Mara Vance and The Low Tide · Sixteen Lanterns and the Blue Hour Sessions", m) // 73 letters at 0.58 of the size each: taken down to fit, not past 0.6 of 30. assertEquals(815f / (73 * 0.58f), by.size, 1e-3f) assertFalse(by.words.endsWith("…")) val longer = hifiByline("Mara Vance and The Low Tide and the Quiet Part of the Album Orchestra · Sixteen Lanterns and the Blue Hour Sessions", m) assertEquals(18f, longer.size, 1e-4f) assertTrue(longer.words.endsWith("…")) } @Test fun theBlankTypesTheAlbumAndItsArtist() { val (album, artist) = hifiBlank("Sixteen Lanterns and the Blue Hour Sessions", "Mara Vance", m) assertTrue(album!!.lines.size <= 3) assertTrue(album.lines.all { m.wide(it, Measure.Face.Caslon, album.size) <= 790f }) assertEquals("Mara Vance", artist!!.words) assertEquals(64f, artist.size, 0f) val (none, nobody) = hifiBlank(null, null, m) assertNull(none); assertNull(nobody) } }