Skip to content

Commit 808d69f

Browse files
committed
1 parent 0843480 commit 808d69f

1 file changed

Lines changed: 119 additions & 12 deletions

File tree

14.music/music.py

Lines changed: 119 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,109 @@
11
# Author: qiren123
2-
# This file is part of MicroPython RTTTL Music
2+
# This file is part of MicroPython MIDI Music
33
# Copyright (c) 2018 qiren123
44
#
55
# Licensed under the MIT license:
66
# http://www.opensource.org/licenses/mit-license.php
77
#
8-
from music_bulid_in import *
8+
9+
DADADADUM = ['r4:2', 'g', 'g', 'g', 'eb:8', 'r:2', 'f', 'f', 'f', 'd:8']
10+
11+
ENTERTAINER = [
12+
'd4:1', 'd#', 'e', 'c5:2', 'e4:1', 'c5:2', 'e4:1', 'c5:3', 'c:1', 'd',
13+
'd#', 'e', 'c', 'd', 'e:2', 'b4:1', 'd5:2', 'c:4'
14+
]
15+
16+
PRELUDE = [
17+
'c4:1', 'e', 'g', 'c5', 'e', 'g4', 'c5', 'e', 'c4', 'e', 'g', 'c5', 'e',
18+
'g4', 'c5', 'e', 'c4', 'd', 'g', 'd5', 'f', 'g4', 'd5', 'f', 'c4', 'd',
19+
'g', 'd5', 'f', 'g4', 'd5', 'f', 'b3', 'd4', 'g', 'd5', 'f', 'g4', 'd5',
20+
'f', 'b3', 'd4', 'g', 'd5', 'f', 'g4', 'd5', 'f', 'c4', 'e', 'g', 'c5',
21+
'e', 'g4', 'c5', 'e', 'c4', 'e', 'g', 'c5', 'e', 'g4', 'c5', 'e'
22+
]
23+
24+
ODE = [
25+
'e4', 'e', 'f', 'g', 'g', 'f', 'e', 'd', 'c', 'c', 'd', 'e', 'e:6', 'd:2',
26+
'd:8', 'e:4', 'e', 'f', 'g', 'g', 'f', 'e', 'd', 'c', 'c', 'd', 'e', 'd:6',
27+
'c:2', 'c:8'
28+
]
29+
30+
NYAN = [
31+
'f#5:1', 'g#', 'c#:1', 'd#:2', 'b4:1', 'd5:1', 'c#', 'b4:2', 'b', 'c#5',
32+
'd', 'd:1', 'c#', 'b4:1', 'c#5:1', 'd#', 'f#', 'g#', 'd#', 'f#', 'c#', 'd',
33+
'b4', 'c#5', 'b4', 'd#5:2', 'f#', 'g#:1', 'd#', 'f#', 'c#', 'd#', 'b4',
34+
'd5', 'd#', 'd', 'c#', 'b4', 'c#5', 'd:2', 'b4:1', 'c#5', 'd#', 'f#', 'c#',
35+
'd', 'c#', 'b4', 'c#5:2', 'b4', 'c#5', 'b4', 'f#:1', 'g#', 'b:2', 'f#:1',
36+
'g#', 'b', 'c#5', 'd#', 'b4', 'e5', 'd#', 'e', 'f#', 'b4:2', 'b', 'f#:1',
37+
'g#', 'b', 'f#', 'e5', 'd#', 'c#', 'b4', 'f#', 'd#', 'e', 'f#', 'b:2',
38+
'f#:1', 'g#', 'b:2', 'f#:1', 'g#', 'b', 'b', 'c#5', 'd#', 'b4', 'f#', 'g#',
39+
'f#', 'b:2', 'b:1', 'a#', 'b', 'f#', 'g#', 'b', 'e5', 'd#', 'e', 'f#',
40+
'b4:2', 'c#5'
41+
]
42+
43+
RINGTONE = [
44+
'c4:1', 'd', 'e:2', 'g', 'd:1', 'e', 'f:2', 'a', 'e:1', 'f', 'g:2', 'b',
45+
'c5:4'
46+
]
47+
48+
FUNK = [
49+
'c2:2', 'c', 'd#', 'c:1', 'f:2', 'c:1', 'f:2', 'f#', 'g', 'c', 'c', 'g',
50+
'c:1', 'f#:2', 'c:1', 'f#:2', 'f', 'd#'
51+
]
52+
53+
BLUES = [
54+
'c2:2', 'e', 'g', 'a', 'a#', 'a', 'g', 'e', 'c2:2', 'e', 'g', 'a', 'a#',
55+
'a', 'g', 'e', 'f', 'a', 'c3', 'd', 'd#', 'd', 'c', 'a2', 'c2:2', 'e', 'g',
56+
'a', 'a#', 'a', 'g', 'e', 'g', 'b', 'd3', 'f', 'f2', 'a', 'c3', 'd#',
57+
'c2:2', 'e', 'g', 'e', 'g', 'f', 'e', 'd'
58+
]
59+
60+
BIRTHDAY = [
61+
'c4:4', 'c:1', 'd:4', 'c:4', 'f', 'e:8', 'c:3', 'c:1', 'd:4', 'c:4', 'g',
62+
'f:8', 'c:3', 'c:1', 'c5:4', 'a4', 'f', 'e', 'd', 'a#:3', 'a#:1', 'a:4',
63+
'f', 'g', 'f:8'
64+
]
65+
66+
WEDDING = [
67+
'c4:4', 'f:3', 'f:1', 'f:8', 'c:4', 'g:3', 'e:1', 'f:8', 'c:4', 'f:3',
68+
'a:1', 'c5:4', 'a4:3', 'f:1', 'f:4', 'e:3', 'f:1', 'g:8'
69+
]
70+
71+
FUNERAL = [
72+
'c3:4', 'c:3', 'c:1', 'c:4', 'd#:3', 'd:1', 'd:3', 'c:1', 'c:3', 'b2:1',
73+
'c3:4'
74+
]
75+
76+
PUNCHLINE = ['c4:3', 'g3:1', 'f#', 'g', 'g#:3', 'g', 'r', 'b', 'c4']
77+
78+
PYTHON = [
79+
'd5:1', 'b4', 'r', 'b', 'b', 'a#', 'b', 'g5', 'r', 'd', 'd', 'r', 'b4',
80+
'c5', 'r', 'c', 'c', 'r', 'd', 'e:5', 'c:1', 'a4', 'r', 'a', 'a', 'g#',
81+
'a', 'f#5', 'r', 'e', 'e', 'r', 'c', 'b4', 'r', 'b', 'b', 'r', 'c5', 'd:5',
82+
'd:1', 'b4', 'r', 'b', 'b', 'a#', 'b', 'b5', 'r', 'g', 'g', 'r', 'd', 'c#',
83+
'r', 'a', 'a', 'r', 'a', 'a:5', 'g:1', 'f#:2', 'a:1', 'a', 'g#', 'a',
84+
'e:2', 'a:1', 'a', 'g#', 'a', 'd', 'r', 'c#', 'd', 'r', 'c#', 'd:2', 'r:3'
85+
]
86+
87+
BADDY = ['c3:3', 'r', 'd:2', 'd#', 'r', 'c', 'r', 'f#:8']
88+
89+
CHASE = [
90+
'a4:1', 'b', 'c5', 'b4', 'a:2', 'r', 'a:1', 'b', 'c5', 'b4', 'a:2', 'r',
91+
'a:2', 'e5', 'd#', 'e', 'f', 'e', 'd#', 'e', 'b4:1', 'c5', 'd', 'c',
92+
'b4:2', 'r', 'b:1', 'c5', 'd', 'c', 'b4:2', 'r', 'b:2', 'e5', 'd#', 'e',
93+
'f', 'e', 'd#', 'e'
94+
]
95+
96+
BA_DING = ['b5:1', 'e6:3']
97+
98+
WAWAWAWAA = ['e3:3', 'r:1', 'd#:3', 'r:1', 'd:4', 'r:1', 'c#:8']
99+
100+
JUMP_UP = ['c5:1', 'd', 'e', 'f', 'g']
101+
102+
JUMP_DOWN = ['g5:1', 'f', 'e', 'd', 'c']
103+
104+
POWER_UP = ['g4:1', 'c5', 'e4', 'g5:2', 'e5:1', 'g5:3']
105+
106+
POWER_DOWN = ['g5:1', 'd#', 'c', 'g4:2', 'b:1', 'c5:3']
9107

10108
normal_tone = {
11109
'A1': 55, 'B1': 62, 'C1': 33, 'D1': 37, 'E1': 41, 'F1': 44, 'G1': 49,
@@ -69,7 +167,7 @@
69167

70168
Letter = 'ABCDEFG#R'
71169

72-
class RTTTL():
170+
class MIDI():
73171

74172
def set_tempo(self, ticks=4, bpm=120):
75173
self.ticks = ticks
@@ -87,6 +185,9 @@ def reset(self):
87185
self.set_octave()
88186
self.set_tempo()
89187

188+
def stop(self):
189+
self.play(['r'])
190+
90191
def __init__(self):
91192
self.reset()
92193

@@ -109,7 +210,7 @@ def parse(self, tone, dict):
109210
# print(int(freq), int(time))
110211
return int(freq), int(time)
111212

112-
def RTTTL(self, tone):
213+
def midi(self, tone):
113214
# print(tone)
114215
pos = tone.find('#')
115216
if pos != -1:
@@ -125,7 +226,7 @@ def set_default(self, tone):
125226
self.set_duration(int(tone[(pos + 1):]))
126227
tone = tone[:pos]
127228

128-
def play(self, tune, pin=25, duration=None):
229+
def play(self, tune, wait=False, loop=False, pin=25, duration=None):
129230
from machine import Pin, PWM
130231
from utime import sleep_ms
131232

@@ -139,13 +240,17 @@ def play(self, tune, pin=25, duration=None):
139240
tone = tone.upper() # all to upper
140241
if tone[0] not in Letter:
141242
continue
142-
RTTTL = self.RTTTL(tone)
143-
pwm.freq(RTTTL[0]) # set frequency
144-
pwm.duty(RTTTL[1]) # set duty cycle
145-
sleep_ms(RTTTL[1])
243+
midi = self.midi(tone)
244+
pwm.freq(midi[0]) # set frequency
245+
pwm.duty(midi[1]) # set duty cycle
246+
sleep_ms(midi[1])
146247
finally:
147248
pwm.deinit()
148249

250+
if loop:
251+
while True:
252+
self.play(tune)
253+
149254
def pitch(self, freq, tim, pin=25):
150255
from machine import Pin, PWM
151256
from utime import sleep_ms
@@ -162,7 +267,7 @@ def pitch(self, freq, tim, pin=25):
162267
def unit_test():
163268
print('The unit test code is as follows')
164269
print('\n\
165-
music = RTTTL()\n\
270+
music = MIDI()\n\
166271
music.play(BIRTHDAY)\n\
167272
music.play(NYAN)\n\
168273
music.play(PRELUDE)\n\
@@ -172,7 +277,7 @@ def unit_test():
172277
for freq in range(1760, 880, -16):\n\
173278
music.pitch(freq, 30)\n\
174279
')
175-
music = RTTTL()
280+
music = MIDI()
176281
music.play(BIRTHDAY)
177282
music.play(NYAN)
178283
music.play(PRELUDE)
@@ -183,11 +288,13 @@ def unit_test():
183288
music.pitch(freq, 30)
184289

185290

186-
__music__ = RTTTL()
291+
__music__ = MIDI()
187292

188293
play = __music__.play
189294
pitch = __music__.pitch
190295
set_tempo = __music__.set_tempo
296+
reset = __music__.reset
297+
stop = __music__.stop
191298

192299
if __name__ == '__main__':
193300
unit_test()

0 commit comments

Comments
 (0)