[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

RE: [pygame] sndarray with numpy array in Windows



Hi, Gumm,

That works a treat - many thanks indeed!!

best wishes
Caroline

From: owner-pygame-users@xxxxxxxx [owner-pygame-users@xxxxxxxx] on behalf of stabbingfinger [stabbingfinger@xxxxxxxxx]
Sent: 04 May 2012 15:42
To: pygame-users@xxxxxxxx
Subject: Re: [pygame] sndarray with numpy array in Windows

Hi, Caroline.

If you initialize the display before playing the sound it works. I know it is weird, but I've encountered this gotcha before--maybe someone familiar with the C source could comment.

If you don't want to display a window, there is a recipe for headless pygame<http://pygame.org/wiki/DummyVideoDriver> you can use.

Gumm

On Fri, May 4, 2012 at 2:13 AM, Witton, Caroline <c.witton@xxxxxxxxxxx<mailto:c.witton@xxxxxxxxxxx>> wrote:
Hello,

I'm a researcher using pygame to measure sensory processing in children.  I want to use sndarray to play out a numpy array I have created, containing a sound stimulus. But I can't seem to get any sound at all.  I'm not an expert programmer so I would welcome any advice.

I'm using Windows 7, with python 2.7, and the array will play out fine (as float) if I use pyaudiere.  A simplified version of my code is below - interestingly I also get no sound when I run the example which someone has kindly posted as a comment on the sndarray doc page. I am baffled!

Many thanks
Caroline

from numpy import *
import pygame

SRATE=44100 # sample rate in Hz
DURATION=1 # duration in sec
# an array of floating-point random numbers
noise= array(random.randn(SRATE*DURATION))
pygame.mixer.pre_init(SRATE, 16, 1,4096)
pygame.init()
# make it int16, scale it for 16 bit
mysound=int16(noise.copy()*2**15)
snd=pygame.sndarray.make_sound(mysound)
snd.play()
pygame.time.wait(DURATION*1000)