-
Notifications
You must be signed in to change notification settings - Fork 4
/
rpmidi.py
201 lines (167 loc) · 8.41 KB
/
rpmidi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from machine import Pin, PWM
from math import log2, pow
import utime
"""
RPMidi
A PWM-based MIDI player for the Raspberry Pi Pico. Uses GPIO 6-9 for 4 voices.
Created by MikeDEV for use in https://github.com/MikeDev101/oops-all-picos-computer
========================================================================================================
MIDIS must be converted using https://github.com/LenShustek/miditones.
Recommended defaults:
.\miditones.exe -t=4 .\your-midi-here.mid
Copy-paste your-midi-here.c's array data into a list.
MIDI files must not contain more than 4 voices, as any other channel will be ignored by the converter.
========================================================================================================
THE MEOW LICENSE ("MEOW") 1.3 - Last revised Jan 7, 2022.
COPYRIGHT (C) 2022 MikeDEV.
Under this license:
* You are free to change, remove, or modify the above copyright notice.
* You are free to use the software in private or commercial forms.
* You are free to use, copy, modify, and/or distribute the software for any purpose.
* You are free to distribute this software with or without fee.
* Absolutely no patent use is permitted.
With the above conditions, the author(s) of this software do NOT gurantee warranty. As part of this license,
under no circumstance shall the author(s) and/or copyright holder(s) be held liable for any and all forms of
damages.
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THE SOFTWARE IS
PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
========================================================================================================
"""
class RPMidi:
def __init__(self, picogb):
self.ch_a_0 = PWM(Pin(15))
self.ch_a_1 = PWM(Pin(14))
self.ch_b_0 = PWM(Pin(13))
self.ch_b_1 = PWM(Pin(12))
self.audio_pwm_wrap=5000
self.curve=1.8
self.picogb=picogb
self.vol=self.picogb.vol/18
self.stop_all()
def _pitch(self, freq):
return (2**((freq-69)/12))*440
def _duty_cycle(self, percent):
return round((percent/100)*65535)
def play_note(self, note, channel, duty):
self.vol=self.picogb.vol/18
pwm_divider = 133000000 / self.audio_pwm_wrap / (note+1)
max_count = (note * self.audio_pwm_wrap) / 10000
level = (self.vol / 100.0**self.curve) * max_count
if channel == "a0":
self.ch_a_0.freq(round(self._pitch(note)))
self.ch_a_0.duty_u16(self._duty_cycle(int(level)))
elif channel == "a1":
self.ch_a_1.freq(round(self._pitch(note)))
self.ch_a_1.duty_u16(self._duty_cycle(int(level)))
elif channel == "b0":
self.ch_b_0.freq(round(self._pitch(note)))
self.ch_b_0.duty_u16(self._duty_cycle(int(level)))
elif channel == "b1":
self.ch_b_1.freq(round(self._pitch(note)))
self.ch_b_1.duty_u16(self._duty_cycle(int(level)))
def stop_channel(self, channel):
if channel == "a0":
self.ch_a_0.duty_u16(0)
elif channel == "a1":
self.ch_a_1.duty_u16(0)
elif channel == "b0":
self.ch_b_0.duty_u16(0)
elif channel == "b1":
self.ch_b_1.duty_u16(0)
def stop_all_music(self):
self.done=True
def stop_all(self):
self.ch_a_0.duty_u16(0)
self.ch_a_1.duty_u16(0)
self.ch_b_0.duty_u16(0)
self.ch_b_1.duty_u16(0)
def _opcodes(self):
return [0x90, 0x91, 0x92, 0x93, 0x80, 0x81, 0x82, 0x83, 0xf0, 0xe0]
def _play_note_opcodes(self):
return [0x90, 0x91, 0x92, 0x93]
def _stop_note_opcodes(self):
return [0x80, 0x81, 0x82, 0x83]
def _end_song_opcodes(self):
return [0xf0, 0xe0]
def play_song(self, music, playing=True):
if type(music) == list:
self.done = False
tmp = []
index = 0
isReading = False
opcode = 0x00
self.stop_all() # Silence any existing music
while not self.done:
if index >= len(music):
print("out of range while reading opcode")
self.done = True
break
if not playing:
return
else:
# Read opcode
if music[index] in self._opcodes():
opcode = music[index]
# Scan for timing data
isReading = True
tmp = []
tmp_index = 1
# Read next bytes for timing info
while isReading:
if (index + tmp_index) >= len(music):
self.done = True
break
else:
if not music[index + tmp_index] in self._opcodes():
tmp.append(music[index + tmp_index])
tmp_index += 1
else:
isReading = False
# Execute instruction
if opcode in self._play_note_opcodes():
# Play voice and goto next instruction or play and wait for x milliseconds
if len(tmp) > 0:
if opcode == 0x90:
self.play_note(tmp[0], "a0", 50)
elif opcode == 0x91:
self.play_note(tmp[0], "b0", 50)
elif opcode == 0x92:
self.play_note(tmp[0], "a1", 50)
elif opcode == 0x93:
self.play_note(tmp[0], "b1", 50)
if len(tmp) == 3:
delay = ((tmp[1]*256)+(tmp[2]))
utime.sleep_ms(delay)
else:
print("expecting at least one entry in tmp, got nothing")
self.done = True
break
index += 1
elif opcode in self._stop_note_opcodes():
# Mute voice or mute and wait for x milliseconds
if opcode == 0x80:
self.stop_channel("a0")
elif opcode == 0x81:
self.stop_channel("b0")
elif opcode == 0x82:
self.stop_channel("a1")
elif opcode == 0x83:
self.stop_channel("b1")
if len(tmp) >= 2:
delay = ((tmp[0]*256)+(tmp[1]))
utime.sleep_ms(delay)
index += 1
elif opcode in self._end_song_opcodes():
# End or loop the song
if opcode == 0xf0: # Song is over, stop playing.
self.done = True
break
else:
self.done = False
index = 0 # Song is looping, go back to beginning of song.
else:
index += 1