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

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



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...

Russell

import pygame
import pygame.mixer as pm
import pygame.sndarray as sa
import numpy as np
import time

pygame.init()
pm.init(frequency=44100, size=16, channels=1, buffer=4096)

sample=sa.array(pm.Sound("sound.wav"))
fft=np.fft.rfft(np.array(sample,dtype=np.int32))

ch=None
while not ch:
  ch=pm.find_channel()
  time.sleep(1)Â

s=sa.make_sound(np.array(np.fft.irfft(fft), dtype=np.int16))
ch.queue(s)
time.sleep(len(sample)*2/44100.0)
s=sa.make_sound(np.array(fft, dtype=np.int16))
ch.queue(s)
time.sleep(len(sample)*2/44100.0)