////// Something the game wants heard.////// `MarchStep` carries which of the four notes of the bassline is due. Its tempo is not a/// timer -- it is driven by how far the formation has actually moved, so it speeds up/// exactly in step with the invaders.///pubenumSoundCmdwithEq, Order, ToString {case Shootcase InvaderDiescase PlayerDiescase MysteryAppearscase MysteryDiescase ShieldBlockcase ExtraLifecase MarchStep(Int32)}mod SoundCmd {////// How many notes the marching bassline cycles through.///pubdefmarchNotes(): Int32 = 4////// Every sound the game can make, which is what the runtime pre-builds voices for.///pubdefall(): List[SoundCmd] = SoundCmd.Shoot :: SoundCmd.InvaderDies :: SoundCmd.PlayerDies :: SoundCmd.MysteryAppears :: SoundCmd.MysteryDies :: SoundCmd.ShieldBlock :: SoundCmd.ExtraLife :: List.map(n -> SoundCmd.MarchStep(n), List.range(0, marchNotes()))}////// Making a noise, as an effect.////// The rules say *what* should be heard and never learn *how*. The runtime interprets this/// onto a pool of clips, the tests interpret it into a list, and a machine with no sound/// card interprets it into nothing -- all three running the identical simulation.///pubeffSound {defplay(cmd: SoundCmd): Unit}mod Sound {/// The type of a `Sound` interpretation that returns a result and the recorded commands.typealiasSoundCollector[a: Type, b: Type, ef: Eff] = a -> (b, List[SoundCmd]) \ ef - Sound////// Interprets `Sound` by recording what was played instead of playing it.////// Pure: the accumulator lives in a region that is fully discharged here, so this needs/// no `IO` and touches no device. This is what the sound tests use.///pubdefhandleWithCollector(f: a -> b \ ef): SoundCollector[a, b, ef] =input -> regionrc {letacc: Ref[List[SoundCmd], rc] = Ref.fresh(rc, Nil);letemit = cmd -> Ref.put(cmd :: Ref.get(acc), acc);letresult = run {f(input) } withhandlerSound {defplay(cmd, k) = {emit(cmd);k() } }; (result, List.reverse(Ref.get(acc))) }////// Runs `f`, returning its result together with everything it played.///pubdefrunWithCollector(f: Unit -> a \ ef): (a, List[SoundCmd]) \ ef - Sound =handleWithCollector(f)()////// Runs `f` and returns only what it played.///pubdefplayed(f: Unit -> a \ ef): List[SoundCmd] \ ef - Sound = snd(runWithCollector(f))////// Lifts a step that makes no sound into one that is allowed to.////// The teaching sketches have nothing to play, but the runtime asks for a step that may./// `checked_ecast` widens the effect without changing behaviour -- the escape hatch the/// Flix style guide sanctions for exactly this ("if necessary, effect casts are OK").///pubdefsilent(f: (a, b) -> c): (a, b) -> c \ Sound = (x, y) -> checked_ecast(f(x, y))////// Interprets `Sound` by discarding it.////// Deliberately not a `@DefaultHandler`, for the same reason as `Canvas` and `Input`:/// a default would not save the callers anything (the effect still has to appear in/// every `@Test` signature) and it would let a forgotten interpretation pass unnoticed.///pubdefrunWithNoOp(f: Unit -> a \ ef): a \ ef - Sound =run {f() } withhandlerSound {defplay(_cmd, k) = k() }}