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

Re: [pygame] Surprising result of applying pygame.sndarray.make_sound() to output of numpy.fft.rfft()



On Sat, Oct 8, 2011 at 5:04 PM, Russell Jones <russell.jones@xxxxxxxxx> wrote:
Hello all,

I had a look in the mailing list archives for FFT and Fourier, and couldn't find anything that looked relevant.

The following code has a surprising result: it outputs sound.wav twice. I'd expect some random sounding noise the second time. There's nothing in the documentation for pygame.sndarray about make_sound understanding FFTs. How/why does it work this way? I've tried this with a few different sounds in case it was a property of the one sound.

I'm interested in playing with real number Fourier coefficients to manipulate and produce sounds. However, I'm not sure what pygame is doing exactly, which makes it harder to work on. Perhaps it's something about FFTs I don't understand? Can anyone explain?  I've just noticed that the second playback is only on one side, whilst the first is on both.  Curiouser and curiouser...

It's definitely not make_sound understanding FFTs....

You should make sure that the FFT you're taking actually looks like you expect it to, because I don't think it does. Under the default mixer settings, sndarray.sound returns a 2-dimensional array, indexed first on the sample number and second on the channel. numpy.fft.rfft takes the 1-dimensional FFT, so when you call it on this array, you're getting the FFT of thousands of length-2 arrays. I suspect what you really want is the FFT of two arrays that have thousands of samples. Do you get what you want when you take the FFT of the transpose of the sound array? Check the shapes of all the arrays you're working with to see if they're what you expect.

You're setting channels=1 in mixer.init, but this is probably not working if you're hearing the second playback on only one side. You should make a call to mixer.pre_init with channels=1 if you want a single audio channel.

-Christopher