[Tutor] array and ndarray
Mr Gerard Kelly
gerard.kelly at uqconnect.edu.au
Fri Mar 6 02:38:07 CET 2009
I am trying to convert something using the old Numeric module to the numpy module.
This is the code so far::
from __future__ import division
import pygame, time, random, pygame.sndarray
sample_rate = 44100
from numpy import *
def sine_array(hz,peak,n_samples=sample_rate):
length=sample_rate/hz
omega=pi*2/length
xvalues=arange(length)*omega
sinarray=(peak*sin(xvalues).astype(int16))
sa=resize(sinarray,sample_rate)
return sa
def play_for(sample_array, ms):
pygame.mixer.pre_init(sample_rate, -16, 1)
pygame.init()
sound = pygame.sndarray.make_sound(sample_array)
sound.play(-1)
pygame.time.delay(ms)
sound.stop()
def main():
pygame.mixer.pre_init(sample_rate, -16, 1)
pygame.init()
play_for(sine_array(440,4096), 4000)
if __name__ == '__main__': main()
My error is the following:
Traceback (most recent call last):
File "na.py", line 30, in <module>
if __name__ == '__main__': main()
File "na.py", line 28, in main
play_for(sine_array(440,4096), 4000)
File "na.py", line 20, in play_for
sound = pygame.sndarray.make_sound(sample_array)
File "/usr/lib/python2.5/site-packages/pygame/sndarray.py", line 123, in make_sound
return numericsnd.make_sound (array)
TypeError: argument 1 must be array, not numpy.ndarray
How could I modify this to get an array instead of an ndarray?
More information about the Tutor
mailing list