29
29
import os
30
30
import sox
31
31
import time
32
+ from musicPackages import MusicPackageManager ,MusicPackageInterface ,MusicPackage
32
33
33
34
class Music :
34
35
36
+ managerPackage = None
37
+
35
38
noteDict = {
36
39
'C2' : - 7.0 , 'D2' : - 5.0 , 'E2' : - 3.0 , 'F2' : - 2.0 , 'F#2' : - 1.0 , 'G2' : 0.0 ,
37
40
'A2' : 2.0 , 'Bb2' : 3.0 , 'B2' : 4.0 , 'C3' : 5.0 , 'D3' : 7.0 , 'E3' : 9.0 ,
@@ -45,9 +48,10 @@ def get_instance(cls):
45
48
cls ._instance = Music ()
46
49
return cls ._instance
47
50
48
- def __init__ (self ):
51
+ def __init__ (self , managerPackage ):
49
52
os .putenv ('AUDIODRIVER' , 'alsa' )
50
53
os .putenv ('AUDIODEV' , 'hw:1,0' )
54
+ self .managerPackage = managerPackage
51
55
print ("We have create a class: MUSICAL" )
52
56
53
57
def test (self ):
@@ -83,7 +87,10 @@ def play_note(self, note, alteration='none', time=1.0, instrument='piano'):
83
87
84
88
tfm .pitch (shift , quick = False )
85
89
tfm .trim (0.0 , end_time = 0.5 * time )
86
- tfm .preview ('./sounds/notes/' + instrument + '/audio.wav' )
90
+ if self .managerPackage .isPackageAvailable (instrument ):
91
+ tfm .preview ('./sounds/notes/' + instrument + '/audio.wav' )
92
+ else :
93
+ print ("no instrument:" + str (instrument )+ " present in this coderbot!" )
87
94
88
95
def play_animal (self , instrument , note = 'G2' , alteration = 'none' , time = 1.0 ):
89
96
tfm = sox .Transformer ()
@@ -132,12 +139,13 @@ def play_animal(self, instrument, note='G2', alteration='none', time=1.0):
132
139
133
140
134
141
if __name__ == "__main__" :
135
- a = Music ()
142
+ b = MusicPackageManager ()
143
+ a = Music (b )
136
144
137
145
a .play_note ('C2' )
138
146
a .play_pause (1 )
139
147
a .play_note ('E2' )
140
- a .play_note ('C2' )
141
- a .play_note ('E2' )
148
+ a .play_note ('C2' , instrument = "piano" )
149
+ a .play_note ('E2' , instrument = "guitar" )
142
150
a .play_note ('C2' )
143
151
a .play_animal (instrument = 'cat' )
0 commit comments