44from dumbdisplay .layer_lcd import LayerLcd
55
66
7+ song = "G C E C E D C A G G C E C E D G E G E G E C G A C C A G G C E C E D C Z"
8+ octave = "0 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 Z"
9+ beat = "2 4 1 1 4 2 4 2 4 2 4 1 1 4 2 8 2 1 1 1 1 4 2 4 1 1 1 4 2 4 1 1 4 2 8 Z"
10+
11+ beatSpeed = 300
12+
713TOP_HEIGHT = 30
814WIDTH = 14
915HEIGHT = 80
2430 dd = DumbDisplay (io4Inet ())
2531
2632
33+ # noteName: C, D, E, F, G, A, B
34+ # halfNote: #, b
35+ def ToNoteIdx (noteName , halfNote ):
36+ if noteName == 'C' :
37+ noteIdx = 0
38+ elif noteName == 'D' :
39+ noteIdx = 2
40+ elif noteName == 'E' :
41+ noteIdx = 4
42+ elif noteName == 'F' :
43+ noteIdx = 5
44+ elif noteName == 'G' :
45+ noteIdx = 7
46+ elif noteName == 'A' :
47+ noteIdx = 9
48+ elif noteName == 'B' :
49+ noteIdx = 11
50+ if halfNote == '#' :
51+ noteIdx = noteIdx + 1
52+ elif halfNote == 'b' :
53+ noteIdx = noteIdx - 1
54+ return noteIdx
55+
56+
57+ # octave: can be negative
58+ # noteIdx: 0 to 11; i.e. 12 note indexes in an octave
59+ def GetNoteFreq (octave , noteIdx ):
60+ n = noteIdx + 12 * octave - 8
61+ freq = 440.0 * pow (2 , n / 12.0 ) # 440 is A
62+ return int (freq + 0.5 )
63+
64+
65+
66+ def PlayTone (freq , duration , playToSpeaker : bool ):
67+ # #ifdef SPEAKER_PIN
68+ # if (playToSpeaker) {
69+ # PlayTone(freq, duration);
70+ # return;
71+ # }
72+ # #endif
73+ dd .tone (freq , duration )
74+ dd .delay_ms (duration )
75+
76+
2777def FeedbackHandler (layer , type , x , y ):
2878 #print("FeedbackHandler", melodyApp)
2979 melodyApp .feedbackHandler (layer , type , x , y )
@@ -56,11 +106,39 @@ def __init__(self):
56106
57107 def run (self ):
58108 while True :
59- dd .timeslice ()
60- if self .adhocFreq != - 1 :
61- # key on DumbDisplay pressed ... play the note/tone of the key press
62- self .playTone (self .adhocFreq , 200 , self .playToSpeaker )
63- self .adhocFreq = - 1
109+ i = 0
110+ while True :
111+ dd .timeslice ()
112+ if self .adhocFreq != - 1 :
113+ # key on DumbDisplay pressed ... play the note/tone of the key press
114+ PlayTone (self .adhocFreq , 200 , self .playToSpeaker )
115+ self .adhocFreq = - 1
116+ if self .restart :
117+ # restarting ... reset restart flag and break out of loop
118+ self .restart = False
119+ break
120+ if not self .play :
121+ continue
122+ noteName = song [i ]
123+ if noteName == "Z" :
124+ # reached end of song => break out of loop
125+ break
126+
127+ halfNote = song [i + 1 ]
128+
129+ # convert the song note into tone frequency
130+ noteIdx = ToNoteIdx (noteName , halfNote )
131+ freq = GetNoteFreq (ord (octave [i ]) - ord ('0' ), noteIdx )
132+
133+ # get the how to to play the note/tone for
134+ duration = beatSpeed * (ord (beat [i ]) - ord ('0' ))
135+
136+ # play the note/tone
137+ PlayTone (freq , duration , self .playToSpeaker )
138+
139+ # increment i by 2
140+ i = i + 2
141+
64142
65143 def setupKey (self , octaveOffset : int , noteIdx : int ) -> LayerGraphical :
66144 width = WIDTH - 2 * BORDER
@@ -112,7 +190,7 @@ def setupButton(self, label: str) -> LayerLcd:
112190 return buttonLayer
113191
114192 def feedbackHandler (self , layer , type , x , y ):
115- print ("clicked" )
193+ # print("clicked")
116194 if layer == self .playLayer :
117195 self .play = not self .play
118196 if self .play :
@@ -130,23 +208,14 @@ def feedbackHandler(self, layer, type, x, y):
130208 else :
131209 octaveOffset = layer .octaveOffset
132210 noteIdx = layer .noteIdx
133- freq = self . getNoteFreq (octaveOffset , noteIdx )
211+ freq = GetNoteFreq (octaveOffset , noteIdx )
134212 self .adhocFreq = freq
135213
136- def getNoteFreq (self , octave , noteIdx ):
137- n = noteIdx + 12 * octave - 8
138- freq = 440.0 * pow (2 , n / 12.0 ); # 440 is A
139- return int (freq + 0.5 )
140-
141- def playTone (self , freq , duration , playToSpeaker ):
142- # #ifdef SPEAKER_PIN
143- # if (playToSpeaker) {
144- # PlayTone(freq, duration);
145- # return;
146- # }
147- # #endif
148- dd .tone (freq , duration )
149- dd .delay_ms (duration )
214+ # def getNoteFreq(self, octave, noteIdx):
215+ # n = noteIdx + 12 * octave - 8
216+ # freq = 440.0 * pow(2, n / 12.0) # 440 is A
217+ # return int(freq + 0.5)
218+
150219
151220melodyApp = MelodyApp ()
152221melodyApp .run ()
0 commit comments