package coffee.liz.rookery.audio import kotlin.math.PI import kotlin.math.exp import kotlin.math.pow import kotlin.math.sin /** * A tiny 8-bit synth: pulse and triangle voices with a plucky envelope, rendered to 16-bit mono PCM. * Generated in code so there's no audio asset to ship or license. */ object Chime { const val SAMPLE_RATE = 44_100 enum class Voice { PULSE25, PULSE50, TRIANGLE } /** A note: MIDI pitch (null = rest), start and length in sixteenth notes. */ data class Note(val midi: Int?, val start: Int, val len: Int, val voice: Voice = Voice.PULSE25, val vol: Double = 0.5) /** The single-PB jingle: a rising arpeggio and a sparkly resolution. */ val PB = listOf( Note(72, 0, 1), Note(76, 1, 1), Note(79, 2, 1), Note(84, 3, 3, Voice.PULSE50), Note(83, 6, 1), Note(84, 7, 4, Voice.PULSE50), Note(48, 0, 3, Voice.TRIANGLE, 0.6), Note(55, 3, 3, Voice.TRIANGLE, 0.6), Note(60, 6, 5, Voice.TRIANGLE, 0.6), Note(91, 8, 1, Voice.PULSE25, 0.25), Note(96, 9, 2, Voice.PULSE25, 0.2), ) /** Longer fanfare for average PBs. */ val AVERAGE_PB = listOf( Note(67, 0, 1), Note(72, 1, 1), Note(76, 2, 1), Note(79, 3, 2), Note(76, 5, 1), Note(79, 6, 4, Voice.PULSE50), Note(77, 10, 1), Note(81, 11, 1), Note(84, 12, 1), Note(89, 13, 5, Voice.PULSE50), Note(43, 0, 5, Voice.TRIANGLE, 0.6), Note(48, 5, 5, Voice.TRIANGLE, 0.6), Note(53, 10, 3, Voice.TRIANGLE, 0.6), Note(48, 13, 5, Voice.TRIANGLE, 0.6), Note(96, 14, 1, Voice.PULSE25, 0.2), Note(100, 15, 1, Voice.PULSE25, 0.2), Note(103, 16, 2, Voice.PULSE25, 0.2), ) /** Inspection calls: one pip at eight seconds, two at twelve. */ val EIGHT = listOf(Note(81, 0, 1, Voice.PULSE50, 0.3)) val TWELVE = listOf(Note(81, 0, 1, Voice.PULSE50, 0.3), Note(81, 2, 1, Voice.PULSE50, 0.3)) /** Short descending blip for a DNF. */ val DNF = listOf(Note(64, 0, 1, Voice.PULSE50, 0.35), Note(57, 1, 2, Voice.PULSE50, 0.35)) fun render(notes: List, bpm: Int = 150, volume: Double = 0.35): ShortArray { val sixteenth = SAMPLE_RATE * 60.0 / bpm / 4 val total = ((notes.maxOf { it.start + it.len } + 2) * sixteenth).toInt() val mix = DoubleArray(total) for (note in notes) { val midi = note.midi ?: continue val freq = 440.0 * 2.0.pow((midi - 69) / 12.0) val start = (note.start * sixteenth).toInt() val len = (note.len * sixteenth).toInt() val release = (0.04 * SAMPLE_RATE).toInt() for (i in 0 until len + release) { val idx = start + i if (idx >= total) break val phase = (i * freq / SAMPLE_RATE) % 1.0 val wave = when (note.voice) { Voice.PULSE25 -> if (phase < 0.25) 1.0 else -1.0 Voice.PULSE50 -> if (phase < 0.5) 1.0 else -1.0 Voice.TRIANGLE -> 4 * kotlin.math.abs(phase - 0.5) - 1 } val attack = minOf(1.0, i / (0.004 * SAMPLE_RATE)) val decay = exp(-i / (len * 0.9 + 1)) val tail = if (i < len) 1.0 else 1.0 - (i - len).toDouble() / release // a dash of vibrato on long notes, NES-style val vib = if (i > SAMPLE_RATE / 10) 1 + 0.003 * sin(2 * PI * 6 * i / SAMPLE_RATE) else 1.0 mix[idx] += wave * vib * note.vol * attack * (0.35 + 0.65 * decay) * tail } } return ShortArray(total) { i -> (mix[i] * volume * Short.MAX_VALUE).coerceIn(-32767.0, 32767.0).toInt().toShort() } } }