|
|
@ -1,5 +1,5 @@ |
|
|
|
# MIDI Cable |
|
|
|
# Black - Data |
|
|
|
# Black - TX |
|
|
|
# White - VCC 3.3v |
|
|
|
# Brown - GND |
|
|
|
|
|
|
@ -7,6 +7,7 @@ import time |
|
|
|
import board |
|
|
|
import busio |
|
|
|
import adafruit_midi |
|
|
|
import asyncio |
|
|
|
|
|
|
|
# from adafruit_midi.control_change import ControlChange |
|
|
|
from adafruit_midi.note_off import NoteOff |
|
|
@ -39,13 +40,19 @@ def trig(note_nr): |
|
|
|
|
|
|
|
|
|
|
|
def div_to_ns(div): |
|
|
|
# trig(note_nr) |
|
|
|
clk_pulse_per_second = 1000000000 |
|
|
|
beat_per_bar = 4 |
|
|
|
bar_ns = 60 / bpm * beat_per_bar * clk_pulse_per_second |
|
|
|
return bar_ns / div |
|
|
|
|
|
|
|
|
|
|
|
def div_to_ms(div): |
|
|
|
clk_pulse_per_second = 1000 |
|
|
|
beat_per_bar = 4 |
|
|
|
bar_ns = 60 / bpm * beat_per_bar * clk_pulse_per_second |
|
|
|
return bar_ns / div |
|
|
|
|
|
|
|
|
|
|
|
def md_note(nr): |
|
|
|
'''1-16''' |
|
|
|
md_notes = [36, 38, 40, 41, 43, 45, 47, 48, 50, 52, 53, 55, 57, 59, 60, 62] |
|
|
@ -60,58 +67,32 @@ def md_selftest(): |
|
|
|
time.sleep(0.01) |
|
|
|
|
|
|
|
|
|
|
|
async def subdiv(div, note_nr): |
|
|
|
for i in range(0, div): |
|
|
|
trig(note_nr) |
|
|
|
await asyncio.sleep(div_to_ms(div) / 1000 * 0.99) |
|
|
|
|
|
|
|
|
|
|
|
# NEW |
|
|
|
# Summing up time deltas is SHIT because of the drift caused by rounding errors, |
|
|
|
# TOGETHER with the worse part, the INTERFERENCE errors sum up as well |
|
|
|
# no load - 0.0223318ms / 44779.1Hz |
|
|
|
# 1 subdiv(1) - 0.041875ms / 23880.6Hz |
|
|
|
# 4 subdiv(1-8) - 0.112662ms / 8876.11Hz |
|
|
|
# 8 subdiv(1-8) - 0.159569ms / 6266.89Hz |
|
|
|
# 16 subdiv(1-16) - 27.792ms / 35.9816Hz |
|
|
|
|
|
|
|
def main(): |
|
|
|
print("main") |
|
|
|
nr_tracks = 8 |
|
|
|
|
|
|
|
clk_now = time.monotonic_ns() |
|
|
|
|
|
|
|
clk_last = [] |
|
|
|
clk_next = [] |
|
|
|
clk_inc = [] |
|
|
|
|
|
|
|
for i in range(0, nr_tracks): |
|
|
|
clk_inc.append(div_to_ns(i+1)) |
|
|
|
clk_last.append(clk_now) |
|
|
|
clk_next.append(clk_now + clk_inc[i] ) |
|
|
|
|
|
|
|
clk_inc[0] = div_to_ns(1) |
|
|
|
clk_inc[1] = div_to_ns(2) |
|
|
|
clk_inc[2] = div_to_ns(3) |
|
|
|
clk_inc[3] = div_to_ns(4) |
|
|
|
clk_inc[4] = div_to_ns(5) |
|
|
|
clk_inc[5] = div_to_ns(6) |
|
|
|
clk_inc[6] = div_to_ns(7) |
|
|
|
clk_inc[7] = div_to_ns(8) |
|
|
|
tracks = 8 |
|
|
|
|
|
|
|
clk_start = time.monotonic_ns() |
|
|
|
loop_count = 0 |
|
|
|
loop_amt = 10000000 |
|
|
|
# while True: |
|
|
|
while loop_count < loop_amt: |
|
|
|
clk_now = time.monotonic_ns() |
|
|
|
|
|
|
|
for i in range(0, nr_tracks): |
|
|
|
if clk_now >= clk_next[i]: |
|
|
|
trig(md_note(i+1)) |
|
|
|
clk_last[i] = clk_now |
|
|
|
clk_next[i] = clk_now + clk_inc[i] |
|
|
|
async def bar(): |
|
|
|
for i in range(1, tracks + 1): |
|
|
|
asyncio.create_task(subdiv(i, md_note(i))) |
|
|
|
|
|
|
|
loop_count += 1 |
|
|
|
|
|
|
|
clk_end = time.monotonic_ns() |
|
|
|
ms_per_loop = (clk_end - clk_start) / loop_amt / 1000 / 1000 |
|
|
|
print("{}ms / {}Hz".format(ms_per_loop, 1000 / ms_per_loop)) |
|
|
|
async def main(): |
|
|
|
while True: |
|
|
|
asyncio.create_task(bar()) |
|
|
|
await asyncio.sleep(div_to_ms(1) / 1000) |
|
|
|
|
|
|
|
|
|
|
|
init() |
|
|
|
main() |
|
|
|
asyncio.run(main()) |
|
|
|