Code: Select all
def queue_blips(who, what, cps):
"""
Queue the blips. This creates a blip every other character and resets the blip when a comma or space is detected.
If the CPS is higher frequency than the blip length, it switches to Wendy Oldbag mode where it just beeps as fast
as it can.
"""
computed_blip_file = "cache/%d.wav" % ( hash( (who, what, cps) ) )
computed_blip_path = os.path.normpath(renpy.loader.get_path(computed_blip_file)).replace("\\", "/")
if os.path.isfile(computed_blip_path):
renpy.sound.play(computed_blip_file)
return
tokens = textsupport.tokenize(unicode(what))
odd = False
blipout = wave.open(computed_blip_path, "wb")
blipout.setframerate(blip_framerate)
blipout.setsampwidth(blip_sample_width)
blipout.setnchannels(blip_channels)
cps_stack = []
# initial character gap
def silence(seconds):
silence_byte_length = seconds * blip_framerate * 2
return audioop.tostereo(b'\0' * int(silence_byte_length), 1, 1, 1)
def blip(seconds):
silence_byte_length = ((seconds - blip_length) * blip_framerate ) * 2
return blip_frames + audioop.tostereo(b'\0' * int(silence_byte_length), 1, 1, 1)
blipout.writeframes(silence(1.0/cps))
for token_type, token_text in tokens:
if token_type == TEXT:
if blip_length > 1.0/cps:
# Wendy Oldbag Speed at this point assume 0.05 seconds and just keep on playing until it reaches the end of the segment.
beeps_needed = int(len(token_text) / cps / blip_length) * 2
for i in xrange(beeps_needed):
# queue.append("<from 0 to %0.3f>%s" % (blip_length, blip_sound))
blipout.writeframes(blip_frames)
elif cps == 0:
pass
else:
speed = 1.0/cps
for letter in token_text:
odd = not odd
if letter in ', ':
# queue.append("<silence %0.3f>" % speed)
blipout.writeframes(silence(speed))
odd = False
else:
if odd:
# queue.append("<from 0 to %0.3f>%s" % (speed, blip_sound))
blipout.writeframes(blip(speed))
else:
# queue.append("<silence %0.3f>" % speed)
blipout.writeframes(silence(speed))
if token_type == TAG:
match_cps_multiplier = re.match( r'cps=\*([0-9\.]+)', token_text)
match_cps = re.match( r'cps=([0-9\.]+)', token_text)
match_close_cps = re.match( r'/cps', token_text)
if match_cps_multiplier:
cps_stack.append(cps)
cps *= float(match_cps_multiplier.group(1))
elif match_cps:
cps_stack.append(cps)
cps = float(match_cps.group(1))
elif match_close_cps:
cps = cps_stack.pop()
odd = False
blipout.close()
renpy.sound.play(computed_blip_file)
# renpy.sound.queue(queue, clear_queue=True, tight=True)