-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.py
51 lines (40 loc) · 1.34 KB
/
output.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
import time
import sys
import pygame
import threading
# global constants
FREQ = 44100 # same as audio CD
BITSIZE = -16 # unsigned 16 bit
CHANNELS = 2 # 1 == mono, 2 == stereo
BUFFER = 1024 # audio buffer size in no. of samples
FRAMERATE = 30 # how often to check if playback has finished
loaded_samples = {}
# initialize engine
def initSoundEngine():
try:
pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
except pygame.error, exc:
print >>sys.stderr, "Could not initialize sound system: %s" % exc
system.exit(0)
# play a sample
def playSample(sample_name):
loaded_samples[sample_name].play()
# load a sample into preloaded database
def loadSamples(samples):
for sample in samples:
if(sample):
loaded_samples[sample] = pygame.mixer.Sound(sample)
# play a raw phrase
def playRawPhrase(samples, raw_phrase):
loadSamples(samples.values())
start_time = time.time() + 0.1 # add 0.1 to reduce initial stuttering
t_evt = threading.Event()
while(len(raw_phrase) != 0):
local_time = time.time() - start_time
evt = raw_phrase[0]
raw_phrase = raw_phrase[1:]
if(local_time < evt["time"]):
t_evt.wait(evt["time"] - local_time)
local_time = time.time() - start_time
for sound in evt["sounds"]:
playSample(sound)