-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmusicgen.py
85 lines (74 loc) · 2.33 KB
/
musicgen.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
#!/usr/bin/python
from pyknon.genmidi import Midi
from pyknon.music import NoteSeq, Note
import os
import pdb
import pygame
import signal
gest_id2song = ['roll.mp3', 'fire.mp3', 'burn.mp3', 'allofme.mp3']
def capturePause():
# TODO: Perform signal handling here.
pygame.init() # May suffice
# pygame.mixer.music.pause()
# Gracefully exit.
def getSingleObj(fx, fy, gx, gy):
""" Get 2 note objects and keep writing them. """
""" X direction indicates pitches. """
""" Y direction indicates octaves. """
a = Note(value=int(fx/53), volume=int(fy/48)*12)
b = Note(value=int(gx/53), volume=int(fy/48)*12)
return a, b
# TODO: Add function to generate note str from number moduloed.
def play_music(music_file):
"""
stream music with mixer.music module in blocking manner
this will stream the sound from disk while playing
"""
clock = pygame.time.Clock()
pygame.init()
try:
pygame.mixer.music.load(music_file)
print "Music file %s loaded!" % music_file
except pygame.error:
print "File %s not found! (%s)" % (music_file, pygame.get_error())
return
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
# check if playback has finished
clock.tick(30)
def play_song(gest_id):
"""
Plays song corresponding to the Gesture ID
"""
play_music('songs/'+gest_id2song[gest_id])
def proc(centroid_list, num):
num_notes = len(centroid_list)/4
notes = []
for i in range(num_notes):
if num == 2:
a, b = getSingleObj(centroid_list[i], centroid_list[i+1],\
centroid_list[i+2], centroid_list[i+3])
notes.append(a)
notes.append(b)
elif num == 1:
a, b = getSingleObj(centroid_list[i], centroid_list[i+1], 0, 0)
notes.append(a)
pdb.set_trace()
sequence = NoteSeq(notes)
midi = Midi(i, tempo=90, instrument=0)
midi.seq_notes(sequence, track=0)
midi.write("temp.mid")
play_music("temp.mid")
def main():
note = "D4 E#10 F#8 Gg4 A Bb4 "
notes = ""
for i in xrange(10):
notes += note
notes1 = NoteSeq(notes)
midi = Midi(1, tempo=90, instrument=0)
midi.seq_notes(notes1, track=0)
midi.write("demo.mid")
play_music("demo.mid")
if __name__ == '__main__':
play_music("temp.mid")
#main()