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

Re: [pygame] SndArray Question



Hey Kris (and everyone),
   pygame.sndarray is going to return a two-dimensional array of sample data.  The two dimensions are for left and right stereo channels (they will just hold the same data in each channel if it is a mono file).  Each value in the array is an amplitude value, and the range of amplitude values is defined by the bit depth of the sound file.  (Signed 16-bit audio data should vary between around +-17000, I think. (2**16)/4.. that's pretty close)
   I'm pretty new to this stuff, but as far as I know, I would recommend running the IPython interpreter for testing, and matplotlib and numarray for graphing and manipulating array data (numarray is just a version of Numeric).
  You can then start IPython with the -pylab flag, and do fun graphing stuff.  For graphing you have to choose one of the axes to use, like for the left channel:
>>>plot(myarray[::, 0])
  Fun stuff like reversing:
>>>reversed_sound = pygame.sndarray.make_sound([myarray::-1])
  Or doubling the pitch:
>>>higher_sound = pygame.sndarray.make_sound([myarray::2])
  Other useful things I've found in Numeric/numarray are concatenate((arr1, arr2)) and zeros((length_in_samples, 2)). 
 
-David.
  

 
On 12/4/05, Peter Shinners <pete@xxxxxxxxxxxx> wrote:
On Sun, 2005-12-04 at 23:00 -0500, Kris Schnee wrote:
> array([[2, 3],
>         [9, 6],
>         [7, 2],
>         [5, 0],
>         [3, 3]],'s')
>
> What does this data mean, exactly? I assume that since the length
> depends on the sound's length and each entry is a two-part list, and the
> pairs of numbers usually are the same, each entry represents stereo
> frequency data at a point in time. Is this so? If so, where's the volume
> data?

You are correct about the format. The sound is using signed integers to
represent the audio data. There is no real "volume" for sound sample
data, but if you had an array of oscillating 0/FULL/0/FULL type values
it would give you the loudest possible static.

When the computer is dealing with sound, it really only lowers the
volume of a given sound. It does this by multiplying each sample to a
lower number. You can multiply the values up, but will often run into
"clipping" when you must clamp numbers to the largest possible amount.

You may start with by creating simple sin waves of sample data. You can
turn those into pygame sound objects and listen to them. The frequence
of the wave controls the pitch, and the amplitude will control the
volume.





--
David Tweet
__________________
http://davidtweet.com