Re: [linux-audio-dev] Converting float to int

New Message Reply About this list Date view Thread view Subject view Author view Other groups

Subject: Re: [linux-audio-dev] Converting float to int
From: Steve Harris (S.W.Harris_AT_ecs.soton.ac.uk)
Date: Mon Oct 29 2001 - 16:47:54 EET


On Mon, Oct 29, 2001 at 09:03:53PM +0800, Daniel V. wrote:
> Hi all. I know this has been answered before but I can't find the post :(
> Anyway, the problem is I have a buffer of audio represented as floats [-1,+1]
> that I want to convert to signed shorts for writing to my card. I recall
> there is an efficient way of doing this, and an inefficient way. Can someone
> remind me with a snippet of code?

inline int f_round(float f) {
        f += (3<<22);
        return *((int*)&f) - 0x4b400000;
}

output = f_round(32768.0f * input)

You still need to clamp output to [SHRT_MIN,SHRT_MAX], otherwise you might
get overflow.

- Steve


New Message Reply About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b28 : Mon Oct 29 2001 - 16:42:56 EET