Re: [Tutor] Converting audio samples from 16-bit signed int to float?

2010-06-21 Thread Peter Otten
Joe Veldhuis wrote:

> Hello all. I'm writing a program that needs to capture audio from a
> soundcard and run FFTs to determine peak frequency for further processing.
> The soundcard's native capture format is 16-bit little-endian signed
> integer samples (values 0-65535), and of course the FFT function requires
> floating-point values (-1.0 - +1.0).
> 
> So, what is the most efficient way to do the necessary conversion? I'm
> using the pyalsaaudio module to access the soundcard, if it matters.

Using numpy should be pretty efficient. Assuming you get your data as a 
bytestring:

>>> import numpy
>>> data = "\x00\x80\x00\x00\xff\x7f"
>>> a = numpy.fromstring(data, dtype=">> a
array([-32768,  0,  32767], dtype=int16)
>>> a/32768.
array([-1.,  0.,  0.6948])

Or, if you don't find the half-open interval acceptable:

>>> (a+.5)/32767.5
array([ -1.e+00,   1.52590219e-05,   1.e+00])

If you want to use the full interval and avoid moving the origin:

>>> (a>0)*a/32767.+(a<0)*a/32768.
array([-1.,  0.,  1.])

(There may be a better way as I'm not very familiar with numpy and found the 
above by trial and error)

Peter



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converting audio samples from 16-bit signed int to float?

2010-06-20 Thread Adam Bark
On 20 June 2010 23:52, Joe Veldhuis  wrote:

> Hello all. I'm writing a program that needs to capture audio from a
> soundcard and run FFTs to determine peak frequency for further processing.
> The soundcard's native capture format is 16-bit little-endian signed integer
> samples (values 0-65535), and of course the FFT function requires
> floating-point values (-1.0 - +1.0).
>
> So, what is the most efficient way to do the necessary conversion? I'm
> using the pyalsaaudio module to access the soundcard, if it matters.
>
> -Joe Veldhuis
>

Not sure it's the best way but the most obvious to me would be divide by
32767.5 (ie half 65535) and minus 1 puts you into the right range:

>>> 65535/32767.5-1
1.0
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Converting audio samples from 16-bit signed int to float?

2010-06-20 Thread Joe Veldhuis
Hello all. I'm writing a program that needs to capture audio from a soundcard 
and run FFTs to determine peak frequency for further processing. The 
soundcard's native capture format is 16-bit little-endian signed integer 
samples (values 0-65535), and of course the FFT function requires 
floating-point values (-1.0 - +1.0).

So, what is the most efficient way to do the necessary conversion? I'm using 
the pyalsaaudio module to access the soundcard, if it matters.

-Joe Veldhuis
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor