Skip to content

Commit f1aa221

Browse files
committed
dev. pause and init. notes
1 parent 249726b commit f1aa221

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

music.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import os
3030
import sox
31+
import time
3132

3233
class Music:
3334

@@ -47,11 +48,51 @@ def __init__(self):
4748
def test(self):
4849
tfm = sox.Transformer()
4950
tfm.preview('cat.wav')
51+
tfm.build('cat.wav', 'outMusicDemo.wav')
52+
53+
#play a pause
54+
# @param duration: duration of the pause in seconds
55+
def play_pause(self, duration):
56+
time.sleep(duration)
57+
58+
#play a given note for a given instrument
59+
# @param instrument: name of the instrument to be used
60+
# @param note: name of the note in the following format "A2"
61+
# @para alteration: if it is a diesis or a bemolle
62+
# @param time: duration of the note in seconds
63+
def play_note(self, note, alteration='none', time=1.0, instrument='piano'):
64+
tfm = sox.Transformer()
65+
66+
time = float(time)
67+
68+
alt = 0.0
69+
70+
#noteArray={}
71+
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)
85+
86+
tfm.trim(0.0, end_time=0.5*time)
87+
88+
tfm.preview(instrument + '.wav')
89+
print("play_note: note " + note )
5090

5191
if __name__ == "__main__":
5292
a = Music()
5393

54-
a.test()
55-
a.test()
56-
a.test()
94+
a.play_note('C2')
95+
a.play_pause(1)
96+
a.play_note('E2')
97+
a.play_note('D2')
5798
a.test()

0 commit comments

Comments
 (0)