Skip to content
Snippets Groups Projects
__init__.py 2.97 KiB
Newer Older
moon2's avatar
moon2 committed
import bl00mbox

moon2's avatar
moon2 committed
blm = bl00mbox.Channel("Melodic Demo")
moon2's avatar
moon2 committed

import leds
from st3m.goose import List, Optional
q3k's avatar
q3k committed
from st3m.input import InputState, InputController
from st3m.ui.view import ViewManager
from ctx import Context
q3k's avatar
q3k committed

octave = 0
synths: List[bl00mbox.patches.tinysynth] = []
Anon's avatar
Anon committed
scale = [0, 2, 4, 5, 7, 9, 11]

q3k's avatar
q3k committed
def highlight_bottom_petal(num: int, r: int, g: int, b: int) -> None:
Anon's avatar
Anon committed
    start = 4 + 8 * num
    for i in range(7):
        leds.set_rgb(((i + start) % 40), r, g, b)
Anon's avatar
Anon committed

q3k's avatar
q3k committed
def change_playing_field_color(r: int, g: int, b: int) -> None:
    highlight_bottom_petal(0, r, g, b)
    highlight_bottom_petal(1, r, g, b)
    highlight_bottom_petal(3, r, g, b)
    highlight_bottom_petal(4, r, g, b)
moon2's avatar
moon2 committed
    highlight_bottom_petal(2, 55, 0, 55)
    leds.set_rgb(18, 55, 0, 55)
    leds.set_rgb(19, 55, 0, 55)
    leds.set_rgb(27, 55, 0, 55)
    leds.set_rgb(28, 55, 0, 55)
    leds.update()
Anon's avatar
Anon committed

q3k's avatar
q3k committed
def adjust_playing_field_to_octave() -> None:
    global octave
Anon's avatar
Anon committed
    if octave == -1:
        change_playing_field_color(0, 0, 55)
    elif octave == 0:
        change_playing_field_color(0, 27, 27)
    elif octave == 1:
        change_playing_field_color(0, 55, 0)

q3k's avatar
q3k committed
def run(input: InputController) -> None:
    global scale
    global octave
    global synths
q3k's avatar
q3k committed
    any_down = False
    for i in range(10):
q3k's avatar
q3k committed
        petal = input.captouch.petals[i].whole
        if petal.down:
            any_down = True
q3k's avatar
q3k committed
            any_down = True
                octave = -1
                adjust_playing_field_to_octave()
Anon's avatar
Anon committed
            elif i == 5:
                octave = 0
                adjust_playing_field_to_octave()
                octave = 1
                adjust_playing_field_to_octave()
            else:
Anon's avatar
Anon committed
                if k > 3:
                    k -= 10
                k = 3 - k
                note = scale[k] + 12 * octave
                synths[0].signals.pitch.tone = note
                synths[0].signals.trigger.start()
q3k's avatar
q3k committed
    if not any_down:
        synths[0].signals.trigger.stop()

Anon's avatar
Anon committed

q3k's avatar
q3k committed
def init() -> None:
    global synths
    for i in range(1):
        synth = blm.new(bl00mbox.patches.tinysynth)
        synth.signals.output = blm.mixer
        synths += [synth]
    for synth in synths:
        synth.signals.decay = 100
Anon's avatar
Anon committed

q3k's avatar
q3k committed
def foreground() -> None:
    adjust_playing_field_to_octave()
Anon's avatar
Anon committed

from st3m.application import Application, ApplicationContext
Anon's avatar
Anon committed

q3k's avatar
q3k committed
# TODO(q3k): properly port this app
class MelodicApp(Application):
    def __init__(self, app_ctx: ApplicationContext) -> None:
        super().__init__(app_ctx)
Anon's avatar
Anon committed

    def draw(self, ctx: Context) -> None:
q3k's avatar
q3k committed
        ctx.rgb(1, 1, 1).rectangle(-120, -120, 240, 240).fill()
        ctx.rgb(0, 0, 0)
q3k's avatar
q3k committed
        ctx.scope()
q3k's avatar
q3k committed
        ctx.fill()

    def on_enter(self, vm: Optional[ViewManager]) -> None:
q3k's avatar
q3k committed
        super().on_enter(vm)
Anon's avatar
Anon committed

    def think(self, ins: InputState, delta_ms: int) -> None:
        super().think(ins, delta_ms)
q3k's avatar
q3k committed
        run(self.input)