[Tutor] array and ndarray
Kent Johnson
kent37 at tds.net
Fri Mar 6 12:31:50 CET 2009
On Thu, Mar 5, 2009 at 8:38 PM, Mr Gerard Kelly
<gerard.kelly at uqconnect.edu.au> wrote:
> 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 "/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?
Do you still have Numeric installed and available? The docs for
pygame.sndarray say,
"Supported array systems are
numeric
numpy
The default will be Numeric, if installed. Otherwise, numpy will be
set as default if installed. If neither Numeric nor numpy are
installed, the module will raise an ImportError.
The array type to use can be changed at runtime using the
use_arraytype() method, which requires one of the above types as
string. "
Judging from this, and the error coming from
*numeric*snd.make_sound(), I think pygame is finding Numeric and
expecting its arguments to be Numeric arrays.
Try adding a call to pygame.sndarray.use_arraytype('numpy') before any
other pygame calls.
Kent
More information about the Tutor
mailing list