{
"id": "170",
"prompt": "A sequencer that OUTSOURCES its music theory: every 20 frames play step (t / 20) % 8 of a scale, asking the mounted \"lib-notes\" library for the pitch via call(h, \"freq\", step, 0, 0, 0). Only trust the value when status(h) == 1 and call_ok() == 0; otherwise sound a 440 Hz fallback and say so on screen. Latch spawn_lib once in a state slot, draw the 8 steps with the sounding one lit, and print the last frequency played (kept in a slot, since the tone only fires on step-boundary frames).",
"solution_rl": "fn frame(t: i32) {\n host::display::clear(0x0c0c14);\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n host::display::state_set(1, host::compose::spawn_lib(\"lib-notes\"));\n }\n let h: i32 = host::display::state_get(1);\n let step: i32 = (t / 20) % 8;\n if t % 20 == 0 {\n let mut f: i32 = 440;\n let mut src: i32 = 0;\n if host::compose::status(h) == 1 {\n let lf: i32 = host::compose::call(h, \"freq\", step, 0, 0, 0);\n if host::compose::call_ok() == 0 {\n f = lf;\n src = 1;\n }\n }\n host::audio::tone(f, 160, 0);\n host::display::state_set(2, f);\n host::display::state_set(3, src);\n }\n if host::display::state_get(3) == 1 {\n host::display::draw_string(8, 8, \"PITCHES: LIB-NOTES\", 0x00ff66, 1);\n } else {\n host::display::draw_string(8, 8, \"PITCHES: FALLBACK A440\", 0xffff00, 1);\n }\n host::display::draw_number(8, 26, host::display::state_get(2), 0xffffff, 2);\n let mut i: i32 = 0;\n while i < 8 {\n let mut c: i32 = 0x303040;\n if i == step { c = 0x00ffcc; }\n host::display::fill_rect(20 + i * 60, 200, 48, 48, c);\n i = i + 1;\n }\n host::display::present();\n}\n",
"tags": [
"audio",
"compose",
"library",
"state"
]
}