Skip to content

Commit 794b695

Browse files
committed
add packages structure and manager
1 parent f1aa221 commit 794b695

File tree

2 files changed

+174
-24
lines changed

2 files changed

+174
-24
lines changed

music.py

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232

3333
class Music:
3434

35+
noteDict = {
36+
'C2': -7.0, 'D2' : -5.0, 'E2' : -3.0, 'F2' : -2.0, 'F#2' : -1.0, 'G2' : 0.0,
37+
'A2' : 2.0, 'Bb2' : 3.0, 'B2' : 4.0, 'C3' : 5.0, 'D3' : 7.0, 'E3' : 9.0,
38+
'F3' : 10.0, 'G3' : 12.0
39+
}
3540
_instance = None
3641

3742
@classmethod
@@ -61,38 +66,78 @@ def play_pause(self, duration):
6166
# @para alteration: if it is a diesis or a bemolle
6267
# @param time: duration of the note in seconds
6368
def play_note(self, note, alteration='none', time=1.0, instrument='piano'):
64-
tfm = sox.Transformer()
65-
66-
time = float(time)
69+
tfm = sox.Transformer()
70+
71+
time = float(time)
6772

68-
alt = 0.0
69-
70-
#noteArray={}
73+
alt = 0.0
74+
if alteration == 'bmolle':
75+
alt = -1.0
76+
elif alteration == 'diesis':
77+
alt = 1.0
7178

72-
if note == 'C2':
73-
# pitch shift combined audio up n semitones, quick may reduce audio quality
74-
shift = -7.0 + alt
75-
elif note == 'D2':
76-
shift = -5.0 + alt
77-
elif note == 'E2':
78-
shift = -3.0 + alt
79-
elif note == 'F2':
80-
shift = -2.0 + alt
81-
elif note == 'F#2':
82-
shift = -1.0 + alt
83-
84-
tfm.pitch(shift, quick=False)
79+
if note in self.noteDict :
80+
shift = self.noteDict[note]+ alt
81+
else:
82+
print('note not exist')
8583

86-
tfm.trim(0.0, end_time=0.5*time)
84+
tfm.pitch(shift, quick=False)
85+
tfm.trim(0.0, end_time=0.5*time)
86+
tfm.preview('./sounds/notes/' + instrument + '/audio.wav')
87+
88+
def play_animal(self, instrument, note='G2', alteration='none', time=1.0):
89+
tfm = sox.Transformer()
8790

88-
tfm.preview(instrument + '.wav')
89-
print("play_note: note " + note )
91+
time = float(time)
92+
93+
alt = 0.0
94+
if alteration == 'bmolle':
95+
alt = -1.0
96+
elif alteration == 'diesis':
97+
alt = 1.0
98+
99+
if note == 'C2':
100+
shift = -7.0 + alt
101+
elif note == 'D2':
102+
shift = -5.0 + alt
103+
elif note == 'E2':
104+
shift = -3.0 + alt
105+
elif note == 'F2':
106+
shift = -2.0 + alt
107+
elif note == 'F#2':
108+
shift = -1.0 + alt
109+
elif note == 'G2':
110+
shift = 0.0 + alt
111+
elif note == 'A2':
112+
shift = 2.0 + alt
113+
elif note == 'Bb2':
114+
shift = 3.0 + alt
115+
elif note == 'B2':
116+
shift = 4.0 + alt
117+
elif note == 'C3':
118+
shift = 5.0 + alt
119+
elif note == 'D3':
120+
shift = 7.0 + alt
121+
elif note == 'E3':
122+
shift = 9.0 + alt
123+
elif note == 'F3':
124+
shift = 10.0 + alt
125+
elif note == 'G3':
126+
shift = 12.0 + alt
127+
tfm.pitch(shift, quick=False)
128+
tfm.trim(0.0, end_time=0.5*time)
129+
#tfm.stretch(time, window=20)
130+
tfm.preview('./sounds/notes/' + instrument + '/audio.wav')
131+
132+
90133

91134
if __name__ == "__main__":
92135
a = Music()
93136

94137
a.play_note('C2')
95138
a.play_pause(1)
96139
a.play_note('E2')
97-
a.play_note('D2')
98-
a.test()
140+
a.play_note('C2')
141+
a.play_note('E2')
142+
a.play_note('C2')
143+
a.play_animal(instrument='cat')

musicPackages.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
############################################################################
2+
# CoderBot, a didactical programmable robot.
3+
# Copyright (C) 2014, 2015 Roberto Previtera <info@coderbot.org>
4+
#
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 2 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License along
16+
# with this program; if not, write to the Free Software Foundation, Inc.,
17+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
############################################################################
19+
# CoderBot, a didactical programmable robot.
20+
# Copyright (C) 2014, 2015 Roberto Previtera <info@coderbot.org>
21+
#
22+
# MUSICAL EXTENTION for CoderBot
23+
# This extention is develop by:
24+
# Michele Carbonera - miki_992@hotmail.it - m.carbonera@campus.unimib.it - michele.carbonera@unimib.it
25+
# Antonino Tramontana - a.tramontana1@campus.unimib.it
26+
# Copyright (C) 2020
27+
############################################################################
28+
29+
import json
30+
class MusicPackage:
31+
name_IT = None
32+
name_EN = None
33+
category = None
34+
version = None
35+
date = None
36+
interfaces = None
37+
nameID = None
38+
39+
def __init__(self,nameID,category,name_IT,name_EN,version,date):
40+
self.nameID = nameID
41+
self.category = category
42+
self.name_IT = name_IT
43+
self.name_EN = name_EN
44+
self.version = version
45+
self.date = date
46+
self.interfaces = list()
47+
48+
def getNameID(self):
49+
return self.nameID
50+
def getCategory(self):
51+
return self.category
52+
def getNameIT(self):
53+
return self.name_IT
54+
def getNameEN(self):
55+
return self.name_EN
56+
def getVersion(self):
57+
return self.version
58+
def getDate(self):
59+
return self.date
60+
def getInterfaces(self):
61+
return self.interfaces
62+
63+
def addInterface(self,musicPackageInterface):
64+
self.interfaces.append(musicPackageInterface)
65+
66+
class MusicPackageInterface:
67+
interfaceName = None
68+
available = None
69+
icon = None
70+
71+
def __init__(self,interfaceName,available,icon):
72+
self.interfaceName = interfaceName
73+
self.available = available
74+
self.icon = icon
75+
76+
def getInterfaceName(self):
77+
return self.interfaceName
78+
79+
def getAvaiable(self):
80+
return self.available
81+
82+
def getIcon(self):
83+
return self.icon
84+
85+
class MusicPackageManager:
86+
packages = None
87+
88+
def __init__(self):
89+
packages = dict()
90+
with open('./sounds/notes/music_package.json') as json_file:
91+
data = json.load(json_file)
92+
for p in data['packages']:
93+
94+
package = data['packages'][p]
95+
mp = MusicPackage(p,package['category'],package['name_IT'],package['name_EN'],package['version'],package['date'])
96+
for i in package['interface']:
97+
interfaceItem = package['interface'][i]
98+
mpi = MusicPackageInterface(i,interfaceItem['available'],interfaceItem['icon'])
99+
mp.addInterface(mpi)
100+
101+
if p not in packages:
102+
packages[p] = mp
103+
104+
if __name__ == "__main__":
105+
a = MusicPackageManager()

0 commit comments

Comments
 (0)