Re: Canonical format max amp limits?
Re: Canonical format max amp limits?
- Subject: Re: Canonical format max amp limits?
- From: Steve Checkoway <email@hidden>
- Date: Tue, 13 Jul 2004 23:07:14 -0700
On Jul 13, 2004, at 8:13 PM, James Chandler Jr wrote:
double Mul16Bit = 1.0 / 32768.0;
float FloatSample = IntSample * Mul16Bit;
//multiply is usually faster than divide
short IntSample = FloatSample * 32768.0;
Since you are going for efficiency here, shouldn't you use a float for
Mul16Bit? Also, don't you need to deal with the fact that the range of
a 16 bit integer is -32768 to 32767? It seems to me that perhaps
something like:
const float Mul16BitPos = 1.0f / 32767.0f;
const float Mul16BitNeg = 1.0f / 32768.0f;
float FloatSample = IntSample * (IntSample < 0 ? Mul16BitNeg :
Mul16BitPos);
This doesn't seem like so much of an issue for int -> float but what
about the other way around? Say that FloatSample = 1.0f. IntSample =
1.0f * 32768.0f; Which would be 32768. Since this is a signed 16 bit
integer, would that not be a negative number? Not only negative, but as
negative as possible?
[DualG5:~/temp] steve$ cat size.c
#include <stdio.h>
int main()
{
short int i = (int)(1.0f * 32768.0f);
printf("%hd\n", i);
return 0;
}
[DualG5:~/temp] steve$ gcc size.c -Wall
[DualG5:~/temp] steve$ ./a.out
-32768
That is about as far away from what you want as possible. So then I
suppose we have:
int IntSample = FloatSample * (FloatSample < 0 ? 32768.0f : 32767.0f);
Then again, is a difference of one such a big deal that one couldn't
simply divide by 32768 and multiply by 32767?
It can be a bit of work to write efficient tight loops to do such
conversions, so it is nice that CoreAudio provides optimized format
conversions.
int intBuf[100];
float floatBuf[100];
int *ip = intBuf;
float *fp = floatBuf;
int i;
const float Mul16BitPos = 1.0f / 32767.0f;
const float Mul16BitNeg = 1.0f / 32768.0f;
for (i = 0; i < 100; ++i)
{
int sample = *(ip++);
*(fp++) = sample * (sample < 0 : Mul16BitNeg : Mul16BitPos);
}
I'd like to be able to rely on the compiler to at least partially
unroll this loop and schedule instructions intelligently so as to avoid
stalls.
Since CoreAudio provides a generic conversion from one format to
another, would it not be simpler if you could tell ahead of time the
formats between which you were converting and if they required a simple
16 bit int to 32 bit float conversion like this to simply do it
yourself? If the formats don't match the ones you expected then you
could go ahead and use the AudioConverter.
- Steve
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.