Update window caption and play Sound with pygame at same time
<p>I've been trying to get a word to pop up with a color and audio in a pygame window for a while but I've been getting this error <a href="https://stackoverflow.com/questions/22030935/failure-on-import-pygame-font">Failure on import pygame.font</a> and I can't figure it out.  (I have OSX) . </p>

<p>I thought the next best thing would be to have the word in the title of the window (caption) while it played the audio and showed the color. My only problem at this point is that it updates the caption AFTER the tone, not during.  How can I change this? </p>

<p>I'd prefer to move to Tkinter but I don't think you can have the computer generate sounds with frequencies. </p>

<pre><code>#!/usr/bin/python

#Dependencies
import pygame,colorsys,time
import numpy as np
from pygame.locals import *

#Primordial
duration = 2.5
window_size = (320,320)#(320, 568)#(1366, 720)
bits = 16
sample_rate = 44100
n_samples = int(round(duration*sample_rate))
qnty = 3
#basicfont = pygame.font.SysFont(None, 48)
#Setup
pygame.mixer.pre_init(44100, -bits, 2)
pygame.init()

#setup our np array to handle 16 bit ints, which is what we set our mixer to expect with "bits" up above
buf = np.zeros((n_samples, 2), dtype = np.int16)
max_sample = 2**(bits - 1) - 1

D_value_freq = {
                "Gamma1" : 300,
                "Gamma2" : 350,    
                "Alpha" :  400, 
                "Beta1" : 450,
                "Beta2" : 500,
                "Cappa" : 550,
                "Delta1" : 600,
                "Delta2" : 650,
                "Epsilon1" : 700,
                "Epsilon2" : 750,
                "Feta" : 800
                }

D_value_color = {
                "Gamma1" : (45,0,45),
                "Gamma2" : (70,0,43),
                "Alpha": (100,39,43),
                 "Beta1" : (100,93,22),
                "Beta2" : (60,100,56),
                "Cappa" : (16,100,84),
                "Delta1" : (12,100,91),
                "Delta2" : (34,49,100),
                "Epsilon1" : (2,32,100),
                "Epsilon2" : (27,44,92),
                "Feta" : (34,65,62)
                }       

for i in range(qnty):
    #Choose note
    value = np.random.choice(D_value_color.keys(),size=1)[0]
    color, frequency = D_value_color[value], D_value_freq[value]
    frequency_l = frequency_r = frequency/2.0
    #Color screen
    screen = pygame.display.set_mode(window_size, pygame.HWSURFACE | pygame.DOUBLEBUF)
    pygame.display.set_caption(value)
    screen.fill(color)
    pygame.display.update()
    #Sounds
    for s in range(n_samples):
        t = float(s)/sample_rate    # time in seconds
        #grab the x-coordinate of the sine wave at a given time, while constraining the sample to what our mixer is set to with "bits"
        buf[s][0] = int(round(max_sample*np.sin(2*np.pi*frequency_l*t)))        # left
        buf[s][1] = int(round(max_sample*0.5*np.sin(2*np.pi*frequency_r*t)))    # right


    sound = pygame.sndarray.make_sound(buf)
    #play once, then loop forever

    sound.play(loops = 0)

    time.sleep(duration)
</code></pre>