[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] sndarray tutorial?

Joseph Blaylock wrote:
> I was quite surprised when I did this in the python interpreter:
>>>>a = sndarray.array(mixer.Sound('/usr/share/licq/sounds/Monty-Python.wav'))
>>>>s = sndarray.make_sound(a)
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in ?
>       ValueError: Invalid array datatype for sound
> I would have thought that pygame.sndarray.array would return an array useful
> to sndarray.make_sound; this has me pretty bewildered.
> Any pointers or sample code anyone can provide would be very helpful.

sndarray is fairly new, i should put together a small tutorial or 
example for it. i do have some code sitting on my hard drive, i've 
attached a simple one to create an 'echo' effect.

i think the problem you are getting is due to Numeric v22. That new 
version of Numeric added several data types, and for some reason that is 
throwing off pygame's type detection. this is something i've really got 
to look into, but haven't done yet. if you can easily drop back to 
Numeric v21 that might solve the problem.

#!/usr/bin/env python

import os.path
import pygame.mixer, pygame.time, pygame.sndarray
mixer = pygame.mixer
sndarray = pygame.sndarray
time = pygame.time
from math import sin
from Numeric import *

#choose a desired audio format
mixer.init(11025, 16, 0) #raises exception on fail
print mixer.get_init()

sound = mixer.Sound('data/secosmic_lo.wav')

a1 = sndarray.array(sound)
print 'SHAPE1:', a1.shape

length = a1.shape[0]
myarr = zeros(length+12000)
myarr[:length] = a1
myarr[3000:length+3000] += a1>>1
myarr[6000:length+6000] += a1>>2
myarr[9000:length+9000] += a1>>3
myarr[12000:length+12000] += a1>>4

print 'SHAPE2:', myarr.shape
sound2 = sndarray.make_sound(myarr.astype(Int16))

while mixer.get_busy():