Skip to content

Commit c94ee11

Browse files
committed
WIP new note parser
1 parent 567b149 commit c94ee11

3 files changed

Lines changed: 316 additions & 68 deletions

File tree

05_advanced/code/PWM_Music2.py

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,24 @@
1-
from machine import Pin, PWM, Timer
1+
#from machine import Pin, PWM, Timer
22
import time
3+
from notes2 import *
34

4-
class Music:
5-
def __init__(self):
6-
self.bpm = 80
7-
self.ms_pb = (1000*60)//self.bpm
8-
self.step = self.ms_pb // 10
9-
self.timer0 = Timer(0)
10-
self.timer1 = Timer(1)
5+
bpm = 89
116

12-
def drum1(self):
13-
sound = PWM(13, freq=50, duty=500)
14-
time.sleep_ms(self.step)
15-
sound.deinit()
7+
ms_pb = (1000*60)//bpm
8+
step = ms_pb // 16
169

17-
def drum2(self):
18-
sound = PWM(13, freq=25, duty=500)
19-
time.sleep_ms(self.step)
20-
sound.deinit()
2110

22-
def pause(self):
23-
time.sleep_ms(self.step)
11+
note_line_groups = read_notes_from_file("mario.lines")
12+
voice_groups = parse_note_line_groups(note_line_groups)
13+
line_length = len(voice_groups[0][0])
14+
line_time = ms_pb * line_length
2415

25-
def drums(self, t):
26-
self.drum1()
27-
self.drum2()
28-
self.pause()
29-
30-
self.drum1()
31-
self.drum2()
32-
self.pause()
33-
34-
self.drum1()
35-
self.drum2()
36-
self.drum2()
37-
self.pause()
16+
print("Number of voice groups:", len(voice_groups))
17+
print("Time per group:", line_time, "ms")
18+
for voice_group in voice_groups:
19+
print("Number of voices in group:", len(voice_group))
3820

39-
def base_note1(self):
40-
sound = PWM(12, freq=100, duty=200)
41-
time.sleep_ms(self.step*4)
42-
sound.deinit()
43-
44-
def base_note2(self):
45-
sound = PWM(12, freq=150, duty=200)
46-
time.sleep_ms(self.step*4)
47-
sound.deinit()
48-
49-
def base_note3(self):
50-
sound = PWM(12, freq=200, duty=200)
51-
time.sleep_ms(self.step*4)
52-
sound.deinit()
53-
54-
def bass(self, t):
55-
self.base_note1()
56-
self.base_note2()
57-
self.base_note3()
58-
self.base_note3()
59-
self.base_note2()
60-
61-
self.base_note2()
62-
self.base_note1()
63-
self.base_note1()
64-
self.base_note2()
65-
self.base_note3()
66-
67-
def start(self):
68-
self.timer0.init(mode=Timer.PERIODIC, period=self.ms_pb, callback=self.drums)
69-
self.timer1.init(mode=Timer.PERIODIC, period=(self.ms_pb * 2), callback=self.bass)
70-
71-
music = Music()
72-
music.start()
73-
74-
while True:
75-
pass
21+
print("Done")
22+
timer0 = Timer(0)
7623

7724

05_advanced/code/mario.lines

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
5|e-e---e---c-e---g---------|
2+
4|a-a---a---a-a---b---------|
3+
4|F-F---F---F-F---g-------g-|
4+
3|------------------------g-|
5+
2|d-d---d---d-d---g-------g-|
6+
1|------------------------g-|
7+
8+
5|------c-------------------|
9+
4|------------g-----------a-|
10+
4|----abe-----c-----e-----c-|
11+
3|------------------g-------|
12+
2|------g-----e-----c-----f-|
13+
14+
15+
5|--------------------a-----|
16+
5|---------------e-g--c---f-|
17+
4|--b---A-a---g-------------|
18+
4|--d---C-c---c--g-b------a-|
19+
3|---------------c-e--f---d-|
20+
2|--g---F-f---e-------------|
21+
22+
23+
6|------------------c-------|
24+
5|------------------------g-|
25+
5|g---e-fec-d-------e-----c-|
26+
4|------------b-------------|
27+
4|b---g---e-f-d-------------|
28+
3|e---c-------------g-----e-|
29+
2|--------a-b-g-----g-----e-|
30+
31+
32+
5|----------a---b---A-a---g-|
33+
5|----e-----c---d---C-c---c-|
34+
4|----g---------------------|
35+
3|----c-----f---g---F-f---e-|
36+
2|----c-----f---g---F-f---e-|
37+
38+
39+
6|-e-g--c---f-g---e---c-d---|
40+
6|------a-------------------|
41+
5|------------------------b-|
42+
5|-g-b------a-b---g---e-f-d-|
43+
3|-c-e--f---d-e---c---------|
44+
2|--------------------a-b-g-|
45+
46+
6|--------e-D-d-D---c-------|
47+
6|--------g-F-f-----e-------|
48+
5|----------------------G-a-|
49+
5|--------------b-------e-f-|
50+
3|----c-----------c---------|
51+
2|----c-----g-----c---f-----|
52+
1|----------g---------f-----|
53+
54+
6|c-----c-d-----e-D-d-D---c-|
55+
6|--------------g-F-f-----e-|
56+
5|----a---------------------|
57+
5|g---c-e-f-----------b-----|
58+
3|c-c-c-----c-------------c-|
59+
2|c-----f---c-----e-----g-c-|
60+
1|----------------e---------|
61+
62+
63+
7|--c---c-c-----------------|
64+
6|--c---c-c-----------e-D-d-|
65+
6|--f---f-f-----------g-F-f-|
66+
6|--g---g-g-----------------|
67+
4|--c---c-c-----------------|
68+
3|--c---c-c---g---c---------|
69+
2|------------g---c-----g---|
70+
1|----------------------g---|
71+
72+
6|D---c-------c-----c-d-----|
73+
6|----e---------------------|
74+
5|--------G-a-----a---------|
75+
5|b-------e-f-g---c-e-f-----|
76+
3|--c---------c-c-c---------|
77+
2|--c---f-----c-----f-------|
78+
1|------f-------------------|
79+
80+
6|D-----d-----c-------------|
81+
5|G-----f-----e-------------|
82+
5|D-----d-----c-------------|
83+
3|------------c-----g-g---c-|
84+
2|G-----A-----c-------------|
85+
1|G-----A-------------------|
86+
87+
5|--c-c---c---c-d---e-c-----|
88+
4|------------------------a-|
89+
4|--G-G---G---G-A---g-e---e-|
90+
3|--------D-----G---g-----c-|
91+
2|--G-----------------------|
92+
93+
5|--------c-c---c---c-d-e---|
94+
4|g-------------------------|
95+
4|c-------G-G---G---G-A-g---|
96+
3|--------------D-----G---g-|
97+
2|----g---G-----------------|
98+
99+
6|--------------c-c---c---c-|
100+
5|--------------G-G---G---G-|
101+
3|----c---------------D-----|
102+
2|----------g---G-----------|
103+
104+
6|d---e-c-------------e-e---|
105+
5|----------a-g-------a-a---|
106+
5|A---g-e---e-c-------e-e---|
107+
3|G---g-----c---------------|
108+
2|----------------g---d-d---|
109+
110+
6|e---c-e---g---------------|
111+
5|a---a-a---b---------------|
112+
5|e---e-e---g-g-F-f-d-------|
113+
4|--------------A-a-f-b-a-G-|
114+
4|--------------C-c---d-c---|
115+
3|------------------b-g-f-e-|
116+
3|------------------------b-|
117+
2|d---d-d---g---------------|
118+
119+
5|----g---------------------|
120+
4|----b---g-----------------|
121+
4|g---g---f---------c-------|
122+
3|----------------c-----a---|
123+
2|--------------------a---A-|
124+
3|d---g---d---------c-------|
125+
2|------------g---c-----a---|
126+
1|--------------------a---A-|
127+
128+
5|------c---c--cD---c-------|
129+
4|-----------A------------c-|
130+
3|A---------------------c---|
131+
4|------c---c--cD---c-------|
132+
3|-----------A------------c-|
133+
2|A---------------------c---|
134+
135+
6|------------c---c-c-c-----|
136+
5|------------A---A-A-A-----|
137+
3|--a---A-------------------|
138+
2|a---A---------------------|
139+
3|--------------------c-----|
140+
2|--a---A-----c-------------|
141+
1|a---A---------------------|
142+
143+
6|--------------c-c---c-c---|
144+
5|--------------A-A---A-A---|
145+
3|----f---d-----------------|
146+
2|--f---d-------------------|
147+
2|----f---d-D-D-------------|
148+
1|--f---d---D-------f-------|
149+
150+
6|-e------------------------|
151+
5|b-------------------------|
152+
3|----------f---d---D-------|
153+
2|--------f---d---D---------|
154+
2|f---------f---d---D-------|
155+
1|--------f---d---D-------f-|
156+
157+
3|----------Dd-Cc---D---d---|
158+
2|------f---Dd-Cc---D---d---|
159+
160+
3|--------C---cF-feA-aG--D--|
161+
2|G---g--------------------b|
162+
2|--------C---cF-feA-aG--D--|
163+
1|G---g--------------------b|
164+
165+
4|----------G---------------|
166+
4|----------aa--c---D---f---|
167+
3|--------------e---g---a---|
168+
2|--A--a-G------------------|
169+
3|----------------------c---|
170+
2|--------------g---A---c---|
171+
1|--A--a-G------g---A-------|
172+
173+
6|----------------c---------|
174+
5|--------------dgg---------|
175+
5|--------------ea----------|
176+
5|--------------cfb---------|
177+
4|-------------g------------|
178+
4|-------------a------------|
179+
4|F-----------df------------|
180+
4|C-----------ceb-----D-----|
181+
3|F-------------------a-----|
182+
2|F---------------c-c-D-e-f-|
183+
184+
6|----------------------c---|
185+
5|------------------------e-|
186+
5|------------------c---D-c-|
187+
4|D---------D-----D---------|
188+
3|a---------a-----a---------|
189+
2|f-G-a-c-c-D-e-f-f-G-a-c-c-|
190+
191+
5|--e-----------------------|
192+
5|D-c-d-c---c---c-Ddc-----c-|
193+
4|--------a-------------a---|
194+
4|b---a-a-f-e---e-F-e---f-a-|
195+
2|D-e-f-f-G-a-c-c-D-e-f-f-G-|
196+
197+
5|----e---e-----------------|
198+
5|--D-c-D-c-d-c---c---c-Ddc-|
199+
4|--------------a-----------|
200+
4|--b---b---a-a-f-e---e-F-e-|
201+
2|a-c-c-D-e-f-f-G-a-c-c-D-e-|
202+
203+
5|----c---------------------|
204+
4|--a-----------------------|
205+
4|--f-a---------------------|
206+
2|f-f-G-a-------------------|

05_advanced/code/notes2.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
# def play_sequence(note_sequence, timer, pin):
3+
# if note_sequence:
4+
# note = note_sequence.pop(0)
5+
# sound = PWM(pin, freq=note[0], duty=512) if note[0] > 0 else None
6+
# timer.init(mode=Timer.ONE_SHOT, period=note[1], callback=(lambda t: play_next_sequence(t, sound, note_sequence, timer, pin)))
7+
8+
# def play_next_sequence(t, sound, note_sequence, timer, pin):
9+
# if sound:
10+
# sound.deinit()
11+
# timer.deinit()
12+
# if note_sequence:
13+
# print(len(note_sequence), "notes left to play")
14+
# note = note_sequence.pop(0)
15+
# sound = PWM(pin, freq=note[0], duty=512) if note[0] > 0 else None
16+
# timer.init(mode=Timer.ONE_SHOT, period=note[1], callback=(lambda t: play_next_sequence(t, sound, note_sequence, timer, pin)))
17+
18+
19+
def read_notes_from_file(file_path):
20+
"""
21+
Reads a text file containing musical notes and returns groups of note lines that should be played together
22+
Groups are seperated by empty lines.
23+
"""
24+
note_lines_groups = []
25+
with open(file_path, 'r') as file:
26+
current_group = []
27+
note_lines_groups.append(current_group)
28+
for line in file:
29+
stripped_line = line.strip()
30+
if not stripped_line:
31+
current_group = []
32+
note_lines_groups.append(current_group)
33+
else:
34+
current_group.append(stripped_line)
35+
return note_lines_groups
36+
37+
def create_merged_frequency_lines(freq_lines):
38+
voice_lines = []
39+
first_voice = freq_lines[0]
40+
voice_lines.append(first_voice)
41+
for line in freq_lines[1:]:
42+
for i in range(len(line)):
43+
if line[i] == 0:
44+
continue
45+
found_spot = False
46+
for voice in voice_lines:
47+
if voice[i] == 0 and line[i] != 0:
48+
voice[i] = line[i]
49+
found_spot = True
50+
break
51+
if not found_spot:
52+
new_voice = [0] * len(line)
53+
new_voice[i] = line[i]
54+
voice_lines.append(new_voice)
55+
56+
return voice_lines
57+
58+
def parse_note_line_groups(note_line_groups):
59+
voice_groups = []
60+
for group in note_line_groups:
61+
if len(group) == 0:
62+
continue
63+
freq_lines = parse_note_lines(group)
64+
voices = create_merged_frequency_lines(freq_lines)
65+
voice_groups.append(voices)
66+
return voice_groups
67+
68+
def parse_note_lines(note_lines):
69+
freq_lines = []
70+
for line in note_lines:
71+
octave = int(line[0])
72+
line_freqs = []
73+
for note in line[2:-1]:
74+
freq = notes[note][octave]
75+
line_freqs.append(freq)
76+
freq_lines.append(line_freqs)
77+
78+
return freq_lines
79+
80+
81+
notes = {
82+
"c" : [16.35, 32.7, 65.41, 130.81, 261.63, 523.25, 1046.5, 2093, 4186],
83+
"C" : [17.32, 34.65, 69.3, 138.59, 277.18, 554.37, 1108.73, 2217.46, 4434.92],
84+
"d" : [18.35, 36.71, 73.42, 146.83, 293.66, 587.33, 1174.66, 2349.32, 4698.64],
85+
"D" : [19.45, 38.89, 77.78, 155.56, 311.13, 622.25, 1244.51, 2489.02, 4978.03],
86+
"e" : [20.6, 41.2, 82.41, 164.81, 329.63, 659.25, 1318.51, 2637.02, 5274.04],
87+
"f" : [21.83, 43.65, 87.31, 174.61, 349.23, 698.46, 1396.91, 2793.83, 5587.65],
88+
"F" : [23.12, 46.25, 92.5, 185, 369.99, 739.99, 1479.98, 2959.96, 5919.91],
89+
"g" : [24.5, 49, 98, 196, 392, 783.99, 1567.98, 3135.96, 6271.93],
90+
"G" : [25.96, 51.91, 103.83, 207.65, 415.3, 830.61, 1661.22, 3322.44, 6644.88],
91+
"a" : [27.5, 55, 110, 220, 440, 880, 1760, 3520, 7040],
92+
"A" : [29.14, 58.27, 116.54, 233.08, 466.16, 932.33, 1864.66, 3729.31, 7458.62],
93+
"b" : [30.87, 61.74, 123.47, 246.94, 493.88, 987.77, 1975.53, 3951.07, 7902.13],
94+
"-" : [0, 0, 0, 0, 0, 0, 0, 0, 0]
95+
}

0 commit comments

Comments
 (0)