-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmusic.py
152 lines (132 loc) · 5.2 KB
/
music.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
############################################################################
# CoderBot, a didactical programmable robot.
# Copyright (C) 2014, 2015 Roberto Previtera <info@coderbot.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
############################################################################
# CoderBot, a didactical programmable robot.
# Copyright (C) 2014, 2015 Roberto Previtera <info@coderbot.org>
#
# MUSICAL EXTENTION for CoderBot
# This extention is develop by:
# Michele Carbonera - miki_992@hotmail.it - m.carbonera@campus.unimib.it - michele.carbonera@unimib.it
# Antonino Tramontana - a.tramontana1@campus.unimib.it
# Copyright (C) 2020
############################################################################
import os
import sox
import time
class Music:
_instance = None
managerPackage = None
noteDict = {
'C2': -7.0, 'D2' : -5.0, 'E2' : -3.0, 'F2' : -2.0, 'F#2' : -1.0, 'G2' : 0.0,
'A2' : 2.0, 'Bb2' : 3.0, 'B2' : 4.0, 'C3' : 5.0, 'D3' : 7.0, 'E3' : 9.0,
'F3' : 10.0, 'G3' : 12.0
}
@classmethod
def get_instance(cls,managerPackage):
if cls._instance is None:
cls._instance = Music(managerPackage)
return cls._instance
def __init__(self,managerPackage):
#os.putenv('AUDIODRIVER', 'alsa')
#os.putenv('AUDIODEV', 'hw:1,0')
self.managerPackage = managerPackage
print("We have create a class: MUSICAL")
def test(self):
tfm = sox.Transformer()
tfm.preview('cat.wav')
tfm.build('cat.wav', 'outMusicDemo.wav')
#play a pause
# @param duration: duration of the pause in seconds
def play_pause(self, duration):
duration = float(duration)
time.sleep(duration)
#play a given note for a given instrument
# @param instrument: name of the instrument to be used
# @param note: name of the note in the following format "A2"
# @para alteration: if it is a diesis or a bemolle
# @param time: duration of the note in seconds
def play_note(self, note, instrument='piano', alteration='none', duration=1.0):
print(note)
tfm = sox.Transformer()
duration = float(duration)
alt = 0.0
if alteration == 'bmolle':
alt = -1.0
elif alteration == 'diesis':
alt = 1.0
if note in self.noteDict :
shift = self.noteDict[note]+ alt
else:
print('note not exist')
return
tfm.pitch(shift, quick=False)
tfm.trim(0.0, end_time=0.5*duration)
if self.managerPackage.isPackageAvailable(instrument):
tfm.preview('./sounds/notes/' + instrument + '/audio.wav')
else:
print("no instrument:"+str(instrument)+" present in this coderbot!")
def play_animal(self, instrument, note='G2', alteration='none', duration=1.0):
tfm = sox.Transformer()
duration = float(duration)
alt = 0.0
if alteration == 'bmolle':
alt = -1.0
elif alteration == 'diesis':
alt = 1.0
if note == 'C2':
shift = -7.0 + alt
elif note == 'D2':
shift = -5.0 + alt
elif note == 'E2':
shift = -3.0 + alt
elif note == 'F2':
shift = -2.0 + alt
elif note == 'F#2':
shift = -1.0 + alt
elif note == 'G2':
shift = 0.0 + alt
elif note == 'A2':
shift = 2.0 + alt
elif note == 'Bb2':
shift = 3.0 + alt
elif note == 'B2':
shift = 4.0 + alt
elif note == 'C3':
shift = 5.0 + alt
elif note == 'D3':
shift = 7.0 + alt
elif note == 'E3':
shift = 9.0 + alt
elif note == 'F3':
shift = 10.0 + alt
elif note == 'G3':
shift = 12.0 + alt
if note in self.noteDict :
shift = self.noteDict[note]+ alt
else:
print('note not exist')
return
if self.managerPackage.isPackageAvailable(instrument):
tfm.preview('./sounds/notes/' + instrument + '/audio.wav')
else:
print("no animal verse:"+str(instrument)+" present in this coderbot!")
return
tfm.pitch(shift, quick=False)
tfm.trim(0.0, end_time=0.5*duration)
#tfm.stretch(time, window=20)
tfm.preview('./sounds/notes/' + instrument + '/audio.wav')