-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from vladkorotnev/experiment-polyphony
Full 1 bit wave output engine rather than PWM
- Loading branch information
Showing
57 changed files
with
8,479 additions
and
2,407 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,73 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# this is very jank, do not expect it to work as is | ||
# it worked for some melodies though | ||
|
||
import pdb | ||
from sys import argv | ||
from mido import MidiFile | ||
import freq_note_converter | ||
|
||
mid = MidiFile(argv[1]) | ||
name = argv[2] | ||
|
||
last_time = 0 | ||
evts = [] # of (freq in hz or 0, delay in ms) | ||
ended = False | ||
|
||
class Event(): | ||
def __init__(self, kind, chan, arg): | ||
self.kind = kind | ||
self.chan = chan | ||
self.arg = arg | ||
|
||
def __str__(self): | ||
return f" {{{self.kind}, {str(self.chan)}, {str(int(self.arg))}}}," | ||
|
||
class Comment(): | ||
def __init__(self, s): | ||
self.content = s | ||
self.kind = "REM" | ||
|
||
def __str__(self): | ||
return f" /* {self.content} */" | ||
|
||
evts = [] | ||
|
||
def prev_note_off_event(chan): | ||
for i in range(1,len(evts)+1): | ||
e = evts[-i] | ||
if e.kind == "FREQ_SET" and e.arg == 0 and e.chan == chan: | ||
return e | ||
elif e.kind == "DELAY": | ||
return None | ||
return None | ||
|
||
for msg in mid: | ||
print(msg) | ||
if msg.type == "note_on" or msg.type == "note_off": | ||
print(msg) | ||
if msg.time > 0 and len(evts) > 0 and evts[-1][1] == 0: | ||
evts[-1][1] = int(msg.time * 1000) | ||
if msg.time > 0.005: | ||
evts.append(Event("DELAY", 0, msg.time * 1000)) | ||
if msg.type == "note_on" and msg.velocity > 0: | ||
evts.append([int(freq_note_converter.from_note_index(msg.note).freq), 0, ""]) | ||
existing_evt = prev_note_off_event(msg.channel) | ||
if existing_evt is not None: | ||
existing_evt.arg = freq_note_converter.from_note_index(msg.note).freq | ||
else: | ||
evts.append(Event("FREQ_SET", msg.channel, freq_note_converter.from_note_index(msg.note).freq)) | ||
else: | ||
# note off | ||
evts.append([0, 0, ""]) | ||
evts.append(Event("FREQ_SET", msg.channel, 0)) | ||
elif msg.type == "end_of_track": | ||
print(msg) | ||
if ended: | ||
raise Exception("WTF, already ended") | ||
ended = True | ||
if evts[-1][0] == 0: | ||
# pause exists, just extend it | ||
evts[-1][1] = int(msg.time * 1000) | ||
else: | ||
evts.append([0, int(msg.time*1000), ""]) | ||
evts.append(Event("DELAY", 0, msg.time * 1000)) | ||
elif msg.type == "marker": | ||
evts[-1][2] = msg.text | ||
|
||
if msg.time > 0.005: | ||
evts.append(Event("DELAY", 0, msg.time * 1000)) | ||
evts.append(Comment(msg.text)) | ||
if msg.text == "LOOP": | ||
evts.append(Event("LOOP_POINT_SET", 0, 0)) | ||
|
||
print(evts) | ||
|
||
print("static const melody_item_t "+name+"_data[] = {") | ||
i = 0 | ||
while i < len(evts): | ||
if evts[i][0] != 0 or evts[i][1] != 0: | ||
print(" {"+str(evts[i][0])+", "+str(evts[i][1])+"}, ") | ||
if evts[i][2] != "": | ||
print(" ") | ||
print(" // " + evts[i][2]) | ||
i+=1 | ||
for e in evts: | ||
print(str(e)) | ||
print("};") | ||
|
||
print("const melody_sequence_t "+name+" = MELODY_OF("+name+"_data);") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#-*- coding: utf-8 -*- | ||
|
||
# Super jank converter from 8bit WAV into RLE PWM + preview | ||
# Not reading the header or anything hence super jank | ||
|
||
import sys | ||
|
||
MARGIN = 4 | ||
|
||
fname = sys.argv[1] | ||
sname = sys.argv[2] | ||
oname = sys.argv[3] if len(sys.argv) >= 4 else None | ||
sdata = open(fname, 'rb').read() | ||
outf = None | ||
if oname is not None: | ||
outf = open(oname, 'wb') | ||
outf.write(sdata[:0x28]) | ||
|
||
sdata = sdata[0x28::] | ||
i = 0 | ||
min = 999 | ||
max = 0 | ||
sts = 0xFF | ||
last_sts = 0xFF | ||
rle_buf = [0] | ||
|
||
def median(data): | ||
x = list(data) | ||
x.sort() | ||
mid = len(x) // 2 | ||
return (x[mid] + x[~mid]) / 2.0 | ||
|
||
med = median(sdata) | ||
print("Median", med) | ||
HIGH = med + MARGIN | ||
LO = med - MARGIN | ||
|
||
while i < len(sdata): | ||
curSample = sdata[i] | ||
if curSample >= HIGH: | ||
sts = 255 | ||
elif curSample <= LO: | ||
sts = 1 | ||
if curSample < min and curSample > 0: | ||
min = curSample | ||
if curSample > max and curSample > 0: | ||
max = curSample | ||
if outf is not None: | ||
outf.write(bytes([sts, sts])) | ||
if sts != last_sts: | ||
rle_buf.append(0) | ||
last_sts = sts | ||
if rle_buf[-1] == 255: | ||
rle_buf.append(0) | ||
rle_buf.append(0) | ||
rle_buf[-1] += 1 | ||
i += 2 | ||
|
||
print(f"static const uint8_t {sname}_rle_data[] = {{" + str(rle_buf)[1::][:-1:] + "};") | ||
print(f"static const rle_sample_t {sname} = {{ .sample_rate = 4000, .root_frequency = 524 /* C5 */, .rle_data = {sname}_rle_data, .length = {len(rle_buf)} }};") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.