omnipython-apps 0.1.4.dev8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- apps/__init__.py +5 -0
- apps/about/__init__.py +257 -0
- apps/about/app.json +1 -0
- apps/about/credits.py +12 -0
- apps/about/facts.py +398 -0
- apps/about/fonts/OFL.txt +93 -0
- apps/about/fonts/latin1_14.bin +0 -0
- apps/about/fonts/latin1_16.bin +0 -0
- apps/about/fonts/latin1_24.bin +0 -0
- apps/about/fonts/latin1_32.bin +0 -0
- apps/about/fonts/latin1_40.bin +0 -0
- apps/backline/__init__.py +58 -0
- apps/backline/app.json +1 -0
- apps/backline/app.py +475 -0
- apps/backline/art.py +717 -0
- apps/backline/ask.py +781 -0
- apps/backline/box.py +502 -0
- apps/backline/cast.py +137 -0
- apps/backline/desk/__init__.py +329 -0
- apps/backline/desk/editor.py +331 -0
- apps/backline/desk/grid.py +154 -0
- apps/backline/desk/look.py +169 -0
- apps/backline/desk/pedals.py +102 -0
- apps/backline/desk/perform.py +157 -0
- apps/backline/desk/prompt.py +210 -0
- apps/backline/desk/strip.py +159 -0
- apps/backline/desk_main.py +226 -0
- apps/backline/engine.py +622 -0
- apps/backline/floor.py +509 -0
- apps/backline/footswitch.py +119 -0
- apps/backline/glyphs.py +82 -0
- apps/backline/kits.py +428 -0
- apps/backline/midipedal.py +362 -0
- apps/backline/page_data.py +413 -0
- apps/backline/parts.py +145 -0
- apps/backline/pedal.py +107 -0
- apps/backline/remote.py +134 -0
- apps/backline/songs/ballad_68.json +1 -0
- apps/backline/songs/bossa.json +1 -0
- apps/backline/songs/country_train.json +1 -0
- apps/backline/songs/funk.json +1 -0
- apps/backline/songs/metal.json +1 -0
- apps/backline/songs/pop.json +1 -0
- apps/backline/songs/rock.json +1 -0
- apps/backline/songs/shuffle.json +1 -0
- apps/battery/__init__.py +101 -0
- apps/battery/app.json +1 -0
- apps/bit_world/act_deck.py +683 -0
- apps/bit_world/act_game.py +762 -0
- apps/bit_world/act_pet.py +1032 -0
- apps/bit_world/app.json +1 -0
- apps/bit_world/bit_output.py +45 -0
- apps/bit_world/bit_palette.py +503 -0
- apps/bit_world/bit_sound.py +431 -0
- apps/bit_world/bit_sprites.py +632 -0
- apps/bit_world/bit_world.py +534 -0
- apps/btreceiver/__init__.py +690 -0
- apps/btreceiver/app.json +1 -0
- apps/btreceiver/chime.py +155 -0
- apps/btreceiver/demo.py +529 -0
- apps/btreceiver/screen.py +633 -0
- apps/camera/__init__.py +316 -0
- apps/camera/app.json +1 -0
- apps/card/__init__.py +113 -0
- apps/card/app.json +1 -0
- apps/cast/__init__.py +631 -0
- apps/cast/app.json +1 -0
- apps/cast/receivers.py +193 -0
- apps/chess/__init__.py +18 -0
- apps/chess/app.json +1 -0
- apps/chess/app.py +922 -0
- apps/chess/art.py +323 -0
- apps/chess/ble.py +411 -0
- apps/chess/engine.py +925 -0
- apps/chess/game.py +327 -0
- apps/chess/net.py +261 -0
- apps/clock/__init__.py +38 -0
- apps/clock/app.json +1 -0
- apps/detect/__init__.py +214 -0
- apps/detect/app.json +1 -0
- apps/fmsynth/__init__.py +41 -0
- apps/fmsynth/algorithms.py +61 -0
- apps/fmsynth/app.json +1 -0
- apps/fmsynth/app.py +355 -0
- apps/fmsynth/editor.py +206 -0
- apps/fmsynth/engine.py +257 -0
- apps/fmsynth/meters.py +262 -0
- apps/fmsynth/midi.py +375 -0
- apps/fmsynth/panel_view.py +1071 -0
- apps/fmsynth/patches/fmsynth-01.syx +0 -0
- apps/fmsynth/settings.py +138 -0
- apps/fmsynth/voice.py +398 -0
- apps/gamepad/__init__.py +685 -0
- apps/gamepad/app.json +1 -0
- apps/gamepad/pad.py +94 -0
- apps/gamepad/watch.py +352 -0
- apps/hidden/__init__.py +72 -0
- apps/hidden/app.json +1 -0
- apps/home/__init__.py +483 -0
- apps/home/app.json +1 -0
- apps/home/main.py +36 -0
- apps/homeassistant/__init__.py +213 -0
- apps/homeassistant/api.py +104 -0
- apps/homeassistant/app.json +1 -0
- apps/huey/__init__.py +287 -0
- apps/huey/acting.py +184 -0
- apps/huey/actor.py +194 -0
- apps/huey/app.json +1 -0
- apps/huey/client.py +125 -0
- apps/huey/servers.py +121 -0
- apps/huey/settings.example.json +8 -0
- apps/huey/settings.py +97 -0
- apps/huey/turn.py +504 -0
- apps/huey/voice.py +217 -0
- apps/info/__init__.py +810 -0
- apps/info/app.json +1 -0
- apps/lora/__init__.py +62 -0
- apps/lora/app.json +1 -0
- apps/manifest.py +71 -0
- apps/medialink_baby_monitor/__init__.py +222 -0
- apps/medialink_baby_monitor/app.json +1 -0
- apps/medialink_doorbell/__init__.py +251 -0
- apps/medialink_doorbell/app.json +1 -0
- apps/medialink_hands_free/__init__.py +217 -0
- apps/medialink_hands_free/app.json +1 -0
- apps/medialink_surveillance_camera/__init__.py +258 -0
- apps/medialink_surveillance_camera/app.json +1 -0
- apps/medialink_video_doorbell/__init__.py +289 -0
- apps/medialink_video_doorbell/app.json +1 -0
- apps/medialink_videophone/__init__.py +273 -0
- apps/medialink_videophone/app.json +1 -0
- apps/medialink_walkie_talkie/__init__.py +228 -0
- apps/medialink_walkie_talkie/app.json +1 -0
- apps/midicontroller/__init__.py +774 -0
- apps/midicontroller/app.json +1 -0
- apps/midicontroller/links.py +421 -0
- apps/midicontroller/surface.py +343 -0
- apps/midiengine/__init__.py +977 -0
- apps/midiengine/app.json +1 -0
- apps/midiengine/banks.py +85 -0
- apps/midiengine/midiin.py +165 -0
- apps/midiengine/player.py +392 -0
- apps/midiengine/umidiparser.py +1413 -0
- apps/mp3/__init__.py +240 -0
- apps/mp3/app.json +1 -0
- apps/perch/__init__.py +119 -0
- apps/perch/app.json +1 -0
- apps/perch/huey.py +481 -0
- apps/perch/huey_props.py +386 -0
- apps/perch/huey_shift.py +42 -0
- apps/perch/huey_views.py +296 -0
- apps/perch/scene_bench.py +680 -0
- apps/perch/scene_coast.py +479 -0
- apps/perch/scene_desk.py +492 -0
- apps/perch/stage.py +1274 -0
- apps/perch/vec.py +366 -0
- apps/repl/__init__.py +325 -0
- apps/repl/app.json +1 -0
- apps/roku/__init__.py +156 -0
- apps/roku/app.json +1 -0
- apps/rokucam/__init__.py +362 -0
- apps/rokucam/app.json +1 -0
- apps/security/__init__.py +261 -0
- apps/security/app.json +1 -0
- apps/settings/__init__.py +221 -0
- apps/settings/app.json +1 -0
- apps/shell/__init__.py +33 -0
- apps/shell/app.json +1 -0
- apps/spotify/__init__.py +669 -0
- apps/spotify/app.json +1 -0
- apps/spotify/screen.py +82 -0
- apps/steps/__init__.py +58 -0
- apps/steps/app.json +1 -0
- apps/stompbox/__init__.py +31 -0
- apps/stompbox/app.json +1 -0
- apps/stompbox/app.py +668 -0
- apps/stompbox/boards.json +20 -0
- apps/stompbox/effects.py +241 -0
- apps/stompbox/engine.py +340 -0
- apps/stompbox/footswitch.py +331 -0
- apps/stompbox/inputs.py +449 -0
- apps/stompbox/meter.py +72 -0
- apps/stompbox/outputs.py +135 -0
- apps/stompbox/pedals.txt +45 -0
- apps/stompbox/presets.py +106 -0
- apps/stompbox/styles.py +135 -0
- apps/stompbox/ui.py +817 -0
- apps/stompbox/wavfile.py +73 -0
- apps/synthstation/__init__.py +20 -0
- apps/synthstation/app.json +1 -0
- apps/synthstation/audio.py +397 -0
- apps/synthstation/catalog.py +50 -0
- apps/synthstation/meters.py +136 -0
- apps/synthstation/midi.py +380 -0
- apps/synthstation/pads.py +177 -0
- apps/synthstation/parts.py +128 -0
- apps/synthstation/station.py +1069 -0
- apps/system/__init__.py +11 -0
- apps/system/app.json +1 -0
- apps/touchcal/__init__.py +324 -0
- apps/touchcal/app.json +1 -0
- apps/touchcal/fit.py +262 -0
- apps/wake/__init__.py +268 -0
- apps/wake/app.json +1 -0
- apps/wake/feed.py +155 -0
- apps/watch/__init__.py +355 -0
- apps/watch/app.json +1 -0
- apps/watch/faces.py +35 -0
- apps/watch/wheel.py +157 -0
- apps/weather/__init__.py +278 -0
- apps/weather/app.json +1 -0
- apps/weather/icons.py +172 -0
- apps/weather/sizing.py +71 -0
- apps/weather/zippad.py +172 -0
- omnipython_apps-0.1.4.dev8.dist-info/METADATA +33 -0
- omnipython_apps-0.1.4.dev8.dist-info/RECORD +218 -0
- omnipython_apps-0.1.4.dev8.dist-info/WHEEL +5 -0
- omnipython_apps-0.1.4.dev8.dist-info/top_level.txt +1 -0
apps/backline/engine.py
ADDED
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Backline's player: a song's parts, switched on the bar line by a footswitch.
|
|
5
|
+
|
|
6
|
+
A song is a songmodel project with one drum track. The track's patterns are
|
|
7
|
+
the parts, and the project's ``"parts"`` key says which pattern plays which
|
|
8
|
+
role::
|
|
9
|
+
|
|
10
|
+
"parts": {"intro": 0, "mains": [1, 2], "fills": [3, 4],
|
|
11
|
+
"transitions": [5], "outro": 6, "accent": 7}
|
|
12
|
+
|
|
13
|
+
``mains`` is the song's main grooves in order and ``fills`` holds one fill
|
|
14
|
+
for each. ``transitions`` holds one pattern, played going into any main, or
|
|
15
|
+
one for each main, played going into it. ``intro``, ``outro`` and ``accent``
|
|
16
|
+
are optional.
|
|
17
|
+
|
|
18
|
+
`Form` is the song's plan, with no sound: what plays from which tick, and
|
|
19
|
+
how each gesture changes it. Stopped, a tap starts the intro and then the
|
|
20
|
+
first main. Playing a main, a tap plays its fill at the next bar line and
|
|
21
|
+
comes back to it; a hold plays the transition and moves to the next main
|
|
22
|
+
(after the last, the first); a double tap plays the outro and stops. During
|
|
23
|
+
the outro, a double tap stops at once. The second pedal's tap plays the
|
|
24
|
+
accent over whatever is playing, and its hold stops at once.
|
|
25
|
+
|
|
26
|
+
A song with a ``"repeat"`` count plays itself: the intro, the mains in
|
|
27
|
+
order that many times, then the outro. A tap still plays a fill, in the
|
|
28
|
+
last bars of the main playing, so the form keeps its length; a hold or a
|
|
29
|
+
double tap takes over from the plan. A ``"tempo_map"`` entry for the outro
|
|
30
|
+
(``{"part": "outro", "tempo": 56}``) slows it smoothly from the song's
|
|
31
|
+
tempo to that one by its last beat, whichever way the outro was reached.
|
|
32
|
+
|
|
33
|
+
`Engine` plays the plan through a kit (see kits.py). The frame clock is
|
|
34
|
+
given: the frame the output renders next, from
|
|
35
|
+
``audiopump.now()`` or a count of what a test pulled. Each `Engine.sequence`
|
|
36
|
+
hands the kit every note due by now plus ``lookahead`` frames, the delay
|
|
37
|
+
until its frame passed along to a kit that can hold a note back. The plan
|
|
38
|
+
reaches the song's track as its ``current`` and ``next``, so
|
|
39
|
+
``songmodel.model.segments`` splits the time at each switch.
|
|
40
|
+
|
|
41
|
+
Imports on CPython and MicroPython with nothing but songmodel: the kit and
|
|
42
|
+
the clock are given, so a check runs it with numbers alone.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
import math
|
|
46
|
+
|
|
47
|
+
try:
|
|
48
|
+
from time import ticks_ms
|
|
49
|
+
except ImportError: # CPython
|
|
50
|
+
import time
|
|
51
|
+
|
|
52
|
+
def ticks_ms():
|
|
53
|
+
return int(time.monotonic() * 1000)
|
|
54
|
+
|
|
55
|
+
from songmodel import model
|
|
56
|
+
|
|
57
|
+
from . import pedal as pedals
|
|
58
|
+
|
|
59
|
+
RATE = 48000
|
|
60
|
+
BAR = model.BAR
|
|
61
|
+
BEAT = model.PPQ
|
|
62
|
+
ACCENT = ((0, 49, 120), (0, 36, 115)) # crash and kick, when a song names no accent
|
|
63
|
+
LABELS = {"intro": "Intro", "main": "Main", "fill": "Fill", "transition": "Transition",
|
|
64
|
+
"outro": "Outro", "end": "Stop"}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
#: The mixer's voice groups: which GM drum notes each one turns up or down.
|
|
68
|
+
GROUPS = (
|
|
69
|
+
("kick", (35, 36)),
|
|
70
|
+
("snare", (37, 38, 39, 40)),
|
|
71
|
+
("hats", (42, 44, 46)),
|
|
72
|
+
("toms", (41, 43, 45, 47, 48, 50)),
|
|
73
|
+
("cymbals", (49, 51, 52, 53, 55, 57, 59)),
|
|
74
|
+
)
|
|
75
|
+
OTHER = "perc" # every note no group names
|
|
76
|
+
GROUP_OF = {}
|
|
77
|
+
for _name, _notes in GROUPS:
|
|
78
|
+
for _n in _notes:
|
|
79
|
+
GROUP_OF[_n] = _name
|
|
80
|
+
MIX = dict((g, 100) for g in [n for n, _ in GROUPS] + [OTHER])
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def meter(project):
|
|
84
|
+
"""``(steps, group)``: the segments a bar's ring shows, and how many of
|
|
85
|
+
them make one beat. 12/8 is twelve eighths in four beats of three; 4/4 is
|
|
86
|
+
four beats of one."""
|
|
87
|
+
try:
|
|
88
|
+
n, d = [int(x) for x in str(project.get("meter") or "4/4").split("/")]
|
|
89
|
+
except ValueError:
|
|
90
|
+
return 4, 1
|
|
91
|
+
if n < 1:
|
|
92
|
+
return 4, 1
|
|
93
|
+
if d == 8 and n % 3 == 0 and n > 3:
|
|
94
|
+
return n, 3
|
|
95
|
+
return n, 1
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def bar_after(tick):
|
|
99
|
+
"""The first bar line at or after ``tick``."""
|
|
100
|
+
return int((tick + BAR - 1) // BAR) * BAR
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def label(entry):
|
|
104
|
+
"""What a screen calls a plan entry: ``"Main 2"``, ``"Fill 1"``, ``"Outro"``."""
|
|
105
|
+
if entry is None:
|
|
106
|
+
return ""
|
|
107
|
+
role, number = entry[0], entry[1]
|
|
108
|
+
text = LABELS[role]
|
|
109
|
+
return text if number is None else "%s %d" % (text, number + 1)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class Form:
|
|
113
|
+
"""A song's plan: entries ``(role, number, pattern, at)``, in tick order.
|
|
114
|
+
|
|
115
|
+
``number`` is which main a main or a fill belongs to (from 0), else None.
|
|
116
|
+
The entry with the latest ``at`` not after a tick is what plays then, and
|
|
117
|
+
a main plays on until the plan changes. An ``"end"`` entry stops the song.
|
|
118
|
+
"""
|
|
119
|
+
|
|
120
|
+
def __init__(self, project):
|
|
121
|
+
parts = project.get("parts") or {}
|
|
122
|
+
patterns = project["tracks"][0]["patterns"]
|
|
123
|
+
self.bars = [max(1, p.get("bars", 1)) for p in patterns]
|
|
124
|
+
self.intro = parts.get("intro")
|
|
125
|
+
self.mains = list(parts.get("mains") or [0])
|
|
126
|
+
self.fills = list(parts.get("fills") or [])
|
|
127
|
+
trans = parts.get("transitions")
|
|
128
|
+
if trans is None:
|
|
129
|
+
trans = parts.get("transition")
|
|
130
|
+
self.transitions = list(trans) if isinstance(trans, list) else ([] if trans is None else [trans])
|
|
131
|
+
self.outro = parts.get("outro")
|
|
132
|
+
self.accent = parts.get("accent")
|
|
133
|
+
self.repeat = max(0, int(project.get("repeat") or 0))
|
|
134
|
+
self.plan = []
|
|
135
|
+
self.before = None # the entry advance() dropped last: still sounding while the kit runs ahead
|
|
136
|
+
|
|
137
|
+
@property
|
|
138
|
+
def playing(self):
|
|
139
|
+
return bool(self.plan) and self.plan[0][0] != "end"
|
|
140
|
+
|
|
141
|
+
def length(self, pattern):
|
|
142
|
+
return self.bars[pattern] * BAR
|
|
143
|
+
|
|
144
|
+
def fill_for(self, n):
|
|
145
|
+
return self.fills[n] if n < len(self.fills) else None
|
|
146
|
+
|
|
147
|
+
def transition_into(self, n):
|
|
148
|
+
if not self.transitions:
|
|
149
|
+
return None
|
|
150
|
+
return self.transitions[n] if len(self.transitions) == len(self.mains) else self.transitions[0]
|
|
151
|
+
|
|
152
|
+
def start(self, tick=0):
|
|
153
|
+
"""The plan from a standing start at ``tick``: the intro, then main 1.
|
|
154
|
+
With a repeat count, the whole song: the mains in order that many
|
|
155
|
+
times, then the outro and the end."""
|
|
156
|
+
self.plan = []
|
|
157
|
+
self.before = None
|
|
158
|
+
at = tick
|
|
159
|
+
if self.intro is not None:
|
|
160
|
+
self.plan.append(("intro", None, self.intro, at))
|
|
161
|
+
at += self.length(self.intro)
|
|
162
|
+
if not self.repeat:
|
|
163
|
+
self.plan.append(("main", 0, self.mains[0], at))
|
|
164
|
+
return
|
|
165
|
+
for k in range(self.repeat):
|
|
166
|
+
for n, main in enumerate(self.mains):
|
|
167
|
+
trans = self.transition_into(n) if len(self.mains) > 1 and (k or n) else None
|
|
168
|
+
if trans is not None:
|
|
169
|
+
self.plan.append(("transition", n, trans, at))
|
|
170
|
+
at += self.length(trans)
|
|
171
|
+
self.plan.append(("main", n, main, at))
|
|
172
|
+
at += self.length(main)
|
|
173
|
+
if self.outro is not None:
|
|
174
|
+
self.plan.append(("outro", None, self.outro, at))
|
|
175
|
+
at += self.length(self.outro)
|
|
176
|
+
self.plan.append(("end", None, -1, at))
|
|
177
|
+
|
|
178
|
+
def _fill_in_plan(self, now, number, fill, tick):
|
|
179
|
+
"""A tap while a planned song plays: the fill over the last bars of
|
|
180
|
+
the main playing (or the next main, when those have begun), so
|
|
181
|
+
what follows keeps its place."""
|
|
182
|
+
earliest = bar_after(tick)
|
|
183
|
+
for i, e in enumerate(self.plan):
|
|
184
|
+
if e[0] != "main" or i + 1 >= len(self.plan) or self.plan[i + 1][0] == "fill":
|
|
185
|
+
continue
|
|
186
|
+
fill = self.fill_for(e[1])
|
|
187
|
+
if fill is None:
|
|
188
|
+
continue
|
|
189
|
+
at = self.plan[i + 1][3] - self.length(fill)
|
|
190
|
+
if at >= earliest and at >= e[3]:
|
|
191
|
+
self.plan.insert(i + 1, ("fill", e[1], fill, at))
|
|
192
|
+
return "fill"
|
|
193
|
+
return None
|
|
194
|
+
|
|
195
|
+
def stop(self):
|
|
196
|
+
self.plan = []
|
|
197
|
+
self.before = None
|
|
198
|
+
|
|
199
|
+
def advance(self, tick):
|
|
200
|
+
"""Drop the entries that ended before ``tick``; returns what plays then."""
|
|
201
|
+
while len(self.plan) > 1 and self.plan[1][3] <= tick:
|
|
202
|
+
self.before = self.plan.pop(0)
|
|
203
|
+
return self.plan[0] if self.plan else None
|
|
204
|
+
|
|
205
|
+
def sounding(self, tick):
|
|
206
|
+
"""The entry playing at ``tick``, without dropping anything."""
|
|
207
|
+
found = None
|
|
208
|
+
if self.before is not None and self.before[3] <= tick:
|
|
209
|
+
found = self.before
|
|
210
|
+
for e in self.plan:
|
|
211
|
+
if e[3] <= tick:
|
|
212
|
+
found = e
|
|
213
|
+
return found
|
|
214
|
+
|
|
215
|
+
def following(self, tick):
|
|
216
|
+
"""The first entry starting after ``tick``, or None."""
|
|
217
|
+
for e in self.plan:
|
|
218
|
+
if e[3] > tick:
|
|
219
|
+
return e
|
|
220
|
+
return None
|
|
221
|
+
|
|
222
|
+
def gesture(self, pedal, gesture, tick):
|
|
223
|
+
"""Change the plan for ``gesture`` on pedal ``pedal`` (0 or 1), made
|
|
224
|
+
when ``tick`` is the earliest tick still to be played. Returns what
|
|
225
|
+
it did: ``"start"``, ``"fill"``, ``"transition"``, ``"outro"``,
|
|
226
|
+
``"stop"``, ``"accent"`` or None for a gesture that means nothing now."""
|
|
227
|
+
now = self.sounding(tick)
|
|
228
|
+
if pedal == 1:
|
|
229
|
+
if gesture == "tap":
|
|
230
|
+
return "accent"
|
|
231
|
+
if gesture == "hold" and self.playing:
|
|
232
|
+
self.plan = [("end", None, -1, tick)]
|
|
233
|
+
return "stop"
|
|
234
|
+
return None
|
|
235
|
+
if now is None or now[0] == "end":
|
|
236
|
+
if gesture == "tap":
|
|
237
|
+
self.start(tick)
|
|
238
|
+
return "start"
|
|
239
|
+
return None
|
|
240
|
+
role, number = now[0], now[1]
|
|
241
|
+
at = bar_after(tick)
|
|
242
|
+
if role == "outro":
|
|
243
|
+
if gesture == "double":
|
|
244
|
+
self.plan = [("end", None, -1, tick)]
|
|
245
|
+
return "stop"
|
|
246
|
+
return None
|
|
247
|
+
if gesture == "double":
|
|
248
|
+
self._cut(tick, now, at, [("outro", None, self.outro, at)] if self.outro is not None else [])
|
|
249
|
+
end = at + (self.length(self.outro) if self.outro is not None else 0)
|
|
250
|
+
self.plan.append(("end", None, -1, end))
|
|
251
|
+
return "outro"
|
|
252
|
+
if role != "main":
|
|
253
|
+
return None # the intro, a fill or a transition: it finishes first
|
|
254
|
+
if gesture == "tap":
|
|
255
|
+
fill = self.fill_for(number)
|
|
256
|
+
if fill is None:
|
|
257
|
+
return None
|
|
258
|
+
if self.repeat and self.following(tick) is not None:
|
|
259
|
+
return self._fill_in_plan(now, number, fill, tick)
|
|
260
|
+
self._cut(tick, now, at, [("fill", number, fill, at), ("main", number, self.mains[number],
|
|
261
|
+
at + self.length(fill))])
|
|
262
|
+
return "fill"
|
|
263
|
+
if gesture == "hold":
|
|
264
|
+
n = (number + 1) % len(self.mains)
|
|
265
|
+
trans = self.transition_into(n)
|
|
266
|
+
steps = []
|
|
267
|
+
start = at
|
|
268
|
+
if trans is not None:
|
|
269
|
+
steps.append(("transition", n, trans, at))
|
|
270
|
+
start += self.length(trans)
|
|
271
|
+
steps.append(("main", n, self.mains[n], start))
|
|
272
|
+
self._cut(tick, now, at, steps)
|
|
273
|
+
return "transition"
|
|
274
|
+
return None
|
|
275
|
+
|
|
276
|
+
def _cut(self, tick, now, at, steps):
|
|
277
|
+
"""Keep what is playing, drop whatever was still to come, and add ``steps``."""
|
|
278
|
+
self.plan = [now] + steps
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
class Engine:
|
|
282
|
+
"""A song played through a kit, two pedals steering it.
|
|
283
|
+
|
|
284
|
+
``kit`` has ``note_on(pitch, velocity, delay)`` (delay in frames) and
|
|
285
|
+
``name``; ``clock()`` is the frame the output renders next. ``lookahead``
|
|
286
|
+
is how far ahead of the clock notes go to the kit: 0 for a kit that plays
|
|
287
|
+
a note the moment it gets it.
|
|
288
|
+
"""
|
|
289
|
+
|
|
290
|
+
def __init__(self, project, kit=None, clock=None, rate=RATE, lookahead=0, now_ms=ticks_ms):
|
|
291
|
+
self.rate = rate
|
|
292
|
+
self.kit = kit
|
|
293
|
+
self.clock = clock or (lambda: 0)
|
|
294
|
+
self.now_ms = now_ms
|
|
295
|
+
self.lookahead = lookahead
|
|
296
|
+
self.pedals = [pedals.Pedal(), pedals.Pedal()]
|
|
297
|
+
self.hits = [] # (frame, pitch, velocity): accents not yet played
|
|
298
|
+
self.played = [] # (frame, pitch, velocity), when ``record`` is set: what a check reads
|
|
299
|
+
self.record = False
|
|
300
|
+
self.changes = 0 # bumps when the plan changes, for a screen
|
|
301
|
+
self.last = None # the last thing a gesture did
|
|
302
|
+
self.load(project)
|
|
303
|
+
|
|
304
|
+
# -- the song --------------------------------------------------------------
|
|
305
|
+
|
|
306
|
+
def load(self, project):
|
|
307
|
+
model.migrate(project)
|
|
308
|
+
self.project = project
|
|
309
|
+
self.track = project["tracks"][0]
|
|
310
|
+
self.form = Form(project)
|
|
311
|
+
mix = self.track.get("mix")
|
|
312
|
+
if not isinstance(mix, dict):
|
|
313
|
+
mix = self.track["mix"] = {}
|
|
314
|
+
for k, v in MIX.items():
|
|
315
|
+
mix.setdefault(k, v)
|
|
316
|
+
self.transport = {"playing": False, "anchor_frame": 0, "anchor_tick": 0.0}
|
|
317
|
+
self.sequenced_to = None
|
|
318
|
+
self._tempo = project.get("tempo", 120.0)
|
|
319
|
+
self.ramp = None # (from tick, to tick, to tempo): the outro slowing, once it's planned
|
|
320
|
+
self.changes += 1
|
|
321
|
+
|
|
322
|
+
def set_kit(self, kit):
|
|
323
|
+
self.kit = kit
|
|
324
|
+
|
|
325
|
+
def reform(self):
|
|
326
|
+
"""The song's parts or a pattern's length changed: plan with the new
|
|
327
|
+
ones from here on, keeping what plays and what is queued."""
|
|
328
|
+
old = self.form
|
|
329
|
+
new = Form(self.project)
|
|
330
|
+
new.plan = old.plan
|
|
331
|
+
new.before = old.before
|
|
332
|
+
self.form = new
|
|
333
|
+
self._place()
|
|
334
|
+
self.changes += 1
|
|
335
|
+
|
|
336
|
+
# -- time -------------------------------------------------------------------
|
|
337
|
+
|
|
338
|
+
def ticks_per_frame(self):
|
|
339
|
+
return self.project["tempo"] * model.PPQ / (60.0 * self.rate)
|
|
340
|
+
|
|
341
|
+
def tempo_at(self, tick):
|
|
342
|
+
"""The tempo at ``tick``: the song's, or on the way to the outro's
|
|
343
|
+
end tempo across the outro (`ramp`)."""
|
|
344
|
+
base = self.project["tempo"]
|
|
345
|
+
r = self.ramp
|
|
346
|
+
if r is None or tick <= r[0]:
|
|
347
|
+
return base
|
|
348
|
+
if tick >= r[1]:
|
|
349
|
+
return r[2]
|
|
350
|
+
return base + (r[2] - base) * (tick - r[0]) / (r[1] - r[0])
|
|
351
|
+
|
|
352
|
+
def _seconds(self, tick):
|
|
353
|
+
"""Seconds from tick 0 to ``tick`` at the song's tempo, slowing
|
|
354
|
+
across the ramp: the tempo changes evenly with the ticks there, so
|
|
355
|
+
the time is a logarithm."""
|
|
356
|
+
per = 60.0 / model.PPQ
|
|
357
|
+
base = self.project["tempo"]
|
|
358
|
+
r = self.ramp
|
|
359
|
+
if r is None or tick <= r[0]:
|
|
360
|
+
return tick * per / base
|
|
361
|
+
t0, t1, end = r
|
|
362
|
+
span = min(tick, t1) - t0
|
|
363
|
+
if end == base:
|
|
364
|
+
inside = span * per / base
|
|
365
|
+
else:
|
|
366
|
+
inside = per * (t1 - t0) / (end - base) * math.log((base + (end - base) * span / (t1 - t0)) / base)
|
|
367
|
+
after = (tick - t1) * per / end if tick > t1 else 0.0
|
|
368
|
+
return t0 * per / base + inside + after
|
|
369
|
+
|
|
370
|
+
def _tick(self, seconds):
|
|
371
|
+
"""The tick `_seconds` puts at ``seconds``."""
|
|
372
|
+
per = 60.0 / model.PPQ
|
|
373
|
+
base = self.project["tempo"]
|
|
374
|
+
r = self.ramp
|
|
375
|
+
if r is None or seconds <= r[0] * per / base:
|
|
376
|
+
return seconds * base / per
|
|
377
|
+
t0, t1, end = r
|
|
378
|
+
s0 = t0 * per / base
|
|
379
|
+
s1 = self._seconds(t1)
|
|
380
|
+
if seconds >= s1:
|
|
381
|
+
return t1 + (seconds - s1) * end / per
|
|
382
|
+
if end == base:
|
|
383
|
+
return t0 + (seconds - s0) * base / per
|
|
384
|
+
bpm = base * math.exp((seconds - s0) * (end - base) / (per * (t1 - t0)))
|
|
385
|
+
return t0 + (bpm - base) * (t1 - t0) / (end - base)
|
|
386
|
+
|
|
387
|
+
def tick_at(self, frame):
|
|
388
|
+
tr = self.transport
|
|
389
|
+
if self.ramp is None:
|
|
390
|
+
return tr["anchor_tick"] + (frame - tr["anchor_frame"]) * self.ticks_per_frame()
|
|
391
|
+
return self._tick(self._seconds(tr["anchor_tick"]) + (frame - tr["anchor_frame"]) / float(self.rate))
|
|
392
|
+
|
|
393
|
+
def frame_at(self, tick):
|
|
394
|
+
tr = self.transport
|
|
395
|
+
if self.ramp is None:
|
|
396
|
+
return tr["anchor_frame"] + (tick - tr["anchor_tick"]) / self.ticks_per_frame()
|
|
397
|
+
return tr["anchor_frame"] + (self._seconds(tick) - self._seconds(tr["anchor_tick"])) * self.rate
|
|
398
|
+
|
|
399
|
+
def retempo(self):
|
|
400
|
+
"""Keep the playhead where it is when the tempo changes; the project
|
|
401
|
+
already holds the new tempo."""
|
|
402
|
+
if self.transport["playing"]:
|
|
403
|
+
now = self.clock()
|
|
404
|
+
tr = self.transport
|
|
405
|
+
tick = tr["anchor_tick"] + (now - tr["anchor_frame"]) * self._tempo * model.PPQ / (60.0 * self.rate)
|
|
406
|
+
tr["anchor_frame"] = now
|
|
407
|
+
tr["anchor_tick"] = tick
|
|
408
|
+
self._tempo = self.project["tempo"]
|
|
409
|
+
|
|
410
|
+
def edge(self):
|
|
411
|
+
"""The first tick not yet handed to the kit: where a change can still land."""
|
|
412
|
+
if not self.transport["playing"]:
|
|
413
|
+
return 0.0
|
|
414
|
+
frame = self.sequenced_to if self.sequenced_to is not None else self.clock()
|
|
415
|
+
return max(0.0, self.tick_at(frame))
|
|
416
|
+
|
|
417
|
+
# -- the pedals -------------------------------------------------------------
|
|
418
|
+
|
|
419
|
+
def press(self, which, now=None):
|
|
420
|
+
self._gestures(which, self.pedals[which].press(self.now_ms() if now is None else now))
|
|
421
|
+
|
|
422
|
+
def release(self, which, now=None):
|
|
423
|
+
self._gestures(which, self.pedals[which].release(self.now_ms() if now is None else now))
|
|
424
|
+
|
|
425
|
+
def poll(self, now=None):
|
|
426
|
+
now = self.now_ms() if now is None else now
|
|
427
|
+
for i, p in enumerate(self.pedals):
|
|
428
|
+
self._gestures(i, p.poll(now))
|
|
429
|
+
|
|
430
|
+
def _gestures(self, which, gestures):
|
|
431
|
+
for g in gestures:
|
|
432
|
+
self.gesture(which, g)
|
|
433
|
+
|
|
434
|
+
def gesture(self, which, gesture):
|
|
435
|
+
"""Act on one gesture of pedal ``which``; returns what it did."""
|
|
436
|
+
playing = self.transport["playing"]
|
|
437
|
+
did = self.form.gesture(which, gesture, self.edge())
|
|
438
|
+
if did == "start" and not playing:
|
|
439
|
+
self.play()
|
|
440
|
+
elif did == "accent":
|
|
441
|
+
self.accent()
|
|
442
|
+
if did is not None:
|
|
443
|
+
self.last = did
|
|
444
|
+
self.changes += 1
|
|
445
|
+
self._place()
|
|
446
|
+
return did
|
|
447
|
+
|
|
448
|
+
def accent(self):
|
|
449
|
+
"""The accent pattern (or a crash and a kick) over what plays, from now."""
|
|
450
|
+
now = self.clock()
|
|
451
|
+
idx = self.form.accent
|
|
452
|
+
notes = ACCENT
|
|
453
|
+
if idx is not None and 0 <= idx < len(self.track["patterns"]):
|
|
454
|
+
notes = [(n[0], n[2], n[3]) for n in self.track["patterns"][idx]["notes"]]
|
|
455
|
+
tpf = self.ticks_per_frame()
|
|
456
|
+
for start, pitch, vel in notes:
|
|
457
|
+
self.hits.append((now + int(start / tpf), pitch, vel))
|
|
458
|
+
self.hits.sort()
|
|
459
|
+
|
|
460
|
+
# -- the transport ----------------------------------------------------------
|
|
461
|
+
|
|
462
|
+
def play(self, at_frame=None):
|
|
463
|
+
"""Start the clock from tick 0 at ``at_frame`` (now when None)."""
|
|
464
|
+
if not self.form.playing:
|
|
465
|
+
self.form.start(0)
|
|
466
|
+
self.transport = {"playing": True, "anchor_frame": self.clock() if at_frame is None else at_frame,
|
|
467
|
+
"anchor_tick": 0.0}
|
|
468
|
+
self.sequenced_to = None
|
|
469
|
+
self._place()
|
|
470
|
+
|
|
471
|
+
def stop(self):
|
|
472
|
+
self.transport["playing"] = False
|
|
473
|
+
self.form.stop()
|
|
474
|
+
self.sequenced_to = None
|
|
475
|
+
self._place()
|
|
476
|
+
|
|
477
|
+
def _place(self):
|
|
478
|
+
"""The plan as the track's ``current`` and ``next``, for songmodel,
|
|
479
|
+
and the outro's ramp once the plan holds the outro."""
|
|
480
|
+
t = self.track
|
|
481
|
+
plan = self.form.plan
|
|
482
|
+
self._ramp()
|
|
483
|
+
if not plan:
|
|
484
|
+
t["current"], t["current_at"], t["next"] = self.form.mains[0], 0, None
|
|
485
|
+
return
|
|
486
|
+
first = plan[0]
|
|
487
|
+
t["current"] = first[2]
|
|
488
|
+
t["current_at"] = int(first[3])
|
|
489
|
+
t["next"] = {"pattern": plan[1][2], "at": int(plan[1][3])} if len(plan) > 1 else None
|
|
490
|
+
|
|
491
|
+
def _ramp(self):
|
|
492
|
+
if not self.transport["playing"]:
|
|
493
|
+
self.ramp = None
|
|
494
|
+
return
|
|
495
|
+
end = model.ramp_to(self.project, "outro")
|
|
496
|
+
if end is None or end <= 0:
|
|
497
|
+
self.ramp = None
|
|
498
|
+
return
|
|
499
|
+
for e in ([self.form.before] if self.form.before else []) + self.form.plan:
|
|
500
|
+
if e[0] == "outro":
|
|
501
|
+
self.ramp = (e[3], e[3] + self.form.length(e[2]), end)
|
|
502
|
+
return
|
|
503
|
+
self.ramp = None
|
|
504
|
+
|
|
505
|
+
# -- what plays -------------------------------------------------------------
|
|
506
|
+
|
|
507
|
+
def sequence(self):
|
|
508
|
+
"""Hand the kit every note due by now plus the lookahead. One pass."""
|
|
509
|
+
now = self.clock()
|
|
510
|
+
until = now + self.lookahead + 1
|
|
511
|
+
ons = []
|
|
512
|
+
if self.hits:
|
|
513
|
+
while self.hits and self.hits[0][0] < until:
|
|
514
|
+
ons.append(self.hits.pop(0))
|
|
515
|
+
if self.transport["playing"]:
|
|
516
|
+
start = now if self.sequenced_to is None else max(self.sequenced_to, now - self.rate // 4)
|
|
517
|
+
if until > start:
|
|
518
|
+
ons.extend(self._schedule(start, until))
|
|
519
|
+
self.sequenced_to = until
|
|
520
|
+
if ons and self.kit is not None:
|
|
521
|
+
kit = self.kit
|
|
522
|
+
mix = self.track["mix"]
|
|
523
|
+
for frame, pitch, vel in ons:
|
|
524
|
+
vel = vel * mix.get(GROUP_OF.get(pitch, OTHER), 100) // 100
|
|
525
|
+
if vel > 0:
|
|
526
|
+
kit.note_on(pitch, min(127, vel), max(0, frame - now))
|
|
527
|
+
if self.record:
|
|
528
|
+
self.played.extend(ons)
|
|
529
|
+
|
|
530
|
+
def _schedule(self, f0, f1):
|
|
531
|
+
"""The notes whose frame falls in [f0, f1), moving the plan on as it goes."""
|
|
532
|
+
t0, t1 = self.tick_at(f0), self.tick_at(f1)
|
|
533
|
+
if t1 <= 0:
|
|
534
|
+
return []
|
|
535
|
+
t0 = max(t0, 0.0)
|
|
536
|
+
found = []
|
|
537
|
+
while t0 < t1:
|
|
538
|
+
entry = self.form.advance(t0)
|
|
539
|
+
self._place()
|
|
540
|
+
if entry is None or entry[0] == "end":
|
|
541
|
+
self.transport["playing"] = False
|
|
542
|
+
self.form.stop()
|
|
543
|
+
self._place()
|
|
544
|
+
self.changes += 1
|
|
545
|
+
break
|
|
546
|
+
nxt = self.form.following(t0)
|
|
547
|
+
stop = t1 if nxt is None else min(t1, nxt[3])
|
|
548
|
+
found.extend(self._notes(t0, stop))
|
|
549
|
+
if nxt is not None and stop >= nxt[3]:
|
|
550
|
+
self.changes += 1
|
|
551
|
+
t0 = stop
|
|
552
|
+
return found
|
|
553
|
+
|
|
554
|
+
def _notes(self, a, b):
|
|
555
|
+
swing = self.project.get("swing", 0)
|
|
556
|
+
found = []
|
|
557
|
+
for s0, s1, base0, pats in model.segments(self.project, a, b):
|
|
558
|
+
idx = pats.get(self.track["id"], -1)
|
|
559
|
+
start_at = base0
|
|
560
|
+
if isinstance(idx, tuple):
|
|
561
|
+
idx, start_at = idx
|
|
562
|
+
if not 0 <= idx < len(self.track["patterns"]):
|
|
563
|
+
continue
|
|
564
|
+
pat = self.track["patterns"][idx]
|
|
565
|
+
length = pat["bars"] * BAR
|
|
566
|
+
k0 = int((s0 - start_at) // length)
|
|
567
|
+
k1 = int((s1 - start_at) // length)
|
|
568
|
+
for k in range(k0, k1 + 1):
|
|
569
|
+
base = start_at + k * length
|
|
570
|
+
for start, _dur, pitch, vel in pat["notes"]:
|
|
571
|
+
tick = base + model.swung(start, swing)
|
|
572
|
+
if s0 <= tick < s1:
|
|
573
|
+
found.append((int(self.frame_at(tick)), pitch, vel))
|
|
574
|
+
found.sort()
|
|
575
|
+
return found
|
|
576
|
+
|
|
577
|
+
# -- what a screen shows ------------------------------------------------------
|
|
578
|
+
|
|
579
|
+
def telemetry(self):
|
|
580
|
+
"""Where the song is, for every panel: bar and beat within the part,
|
|
581
|
+
the part and the next one, the pedals, the tempo and the kit."""
|
|
582
|
+
playing = self.transport["playing"]
|
|
583
|
+
tick = self.tick_at(self.clock()) if playing else 0.0
|
|
584
|
+
now = self.form.sounding(tick) if playing else None
|
|
585
|
+
nxt = self.form.following(tick) if playing else None
|
|
586
|
+
if now is not None and now[0] == "end":
|
|
587
|
+
playing = False # stopped; the next pass says so to the transport too
|
|
588
|
+
now = nxt = None
|
|
589
|
+
bar = beat = step = 0
|
|
590
|
+
bars = 1
|
|
591
|
+
into = 0.0
|
|
592
|
+
steps, group = meter(self.project)
|
|
593
|
+
if now is not None and now[2] >= 0:
|
|
594
|
+
bars = self.form.bars[now[2]]
|
|
595
|
+
into = max(0.0, tick - now[3]) % (bars * BAR)
|
|
596
|
+
bar = int(into // BAR) + 1
|
|
597
|
+
step = min(steps, int((into % BAR) * steps // BAR) + 1)
|
|
598
|
+
beat = (step - 1) // group + 1
|
|
599
|
+
return {
|
|
600
|
+
"playing": playing,
|
|
601
|
+
"song": self.project.get("name", ""),
|
|
602
|
+
"tempo": self.tempo_at(tick) if playing else self.project.get("tempo", 120.0),
|
|
603
|
+
"meter": self.project.get("meter", "4/4"),
|
|
604
|
+
"kit": getattr(self.kit, "name", "") if self.kit is not None else "",
|
|
605
|
+
"role": now[0] if now else "stopped",
|
|
606
|
+
"part": label(now) if now else "Ready",
|
|
607
|
+
"next": label(nxt) if nxt else "",
|
|
608
|
+
"next_role": nxt[0] if nxt else "",
|
|
609
|
+
"bar": bar,
|
|
610
|
+
"bars": bars,
|
|
611
|
+
"beat": beat,
|
|
612
|
+
"beats": steps // group,
|
|
613
|
+
"step": step,
|
|
614
|
+
"steps": steps,
|
|
615
|
+
"number": now[1] if now else None,
|
|
616
|
+
"next_number": nxt[1] if nxt else None,
|
|
617
|
+
"pattern": now[2] if now else -1,
|
|
618
|
+
"at": int(into),
|
|
619
|
+
"pedals": [p.state for p in self.pedals],
|
|
620
|
+
"gesture": self.last or "",
|
|
621
|
+
"changes": self.changes,
|
|
622
|
+
}
|