Dan Muresan | 3 Nov 00:03 2010
Picon

Re: lossless conversion broken?

Hi,

> libecasound/eca-sample-conversion.h:
> http://ecasound.git.sourceforge.net/git/gitweb.cgi?p=ecasound/ecasound;a=blob;f=libecasound/eca-sample-conversion.h;h=f11aced93bd9ff2616c5500205d661cc8c554201;hb=HEAD#l96

I looked at the code. I'm surprised at a different asymmetry -- the
one between eca_sample_convert_float_to_s32 vs.
eca_sample_convert_s32_to_float:

int32_t eca_sample_convert_float_to_s32(float inval) {
  if (inval < 0.0f)
    return((int32_t)((float)(inval * (INT32_MAX))));

  return((int32_t)((float)(inval * INT32_MAX) - 0.5f));
}

float eca_sample_convert_s32_to_float(int32_t inval) {
   return(((float)inval) / INT32_MAX);
}

The conversion functions should be (at least mathematically) inverse
functions, i.e. f (g (x)) = x. If you don't distinguish between
positive and negative int's, why do you do so for +/- floats?

> This is unit-tested in libecasound/eca-sample-conversion_test.h:
> http://ecasound.git.sourceforge.net/git/gitweb.cgi?p=ecasound/ecasound;a=blob;f=libecasound/eca-sample-conversion_test.h;h=fa0c52247290c4886dc98889afde9eaac0f8e70d;hb=HEAD

Yes, but that unit test only looks at 0.0f, ((float) INT_MAX) and
((float) INT_MIN). Zero is not distorted, but 1,2,3 etc are:

int main () {
 int32_t i;
 while ((cin >> i)) {
   float f = eca_sample_convert_s32_to_float (i);
   cout << "float " << f << " -> int " <<
eca_sample_convert_float_to_s32 (f) << "\n";
 }
 return 0;
}

1
float 4.65661e-10 -> int 0
2
float 9.31323e-10 -> int 1
3
float 1.39698e-09 -> int 2

If you get rid of the 0.5f bias for positive int's in
eca_sample_convert_float_to_s32, all problems seem to disappear.

> So this is basicly the "asymmetric" method in:
> http://blog.bjornroche.com/2009/12/linearity-and-dynamic-range-in-int.html

I agree with the jackit threads -- introducing a nonlinear
transformation in a chain is not a good thing (from a signals &
systems POV). Sounds like 0x8000, symmetrical would be best for the
future. I assume this breaks existing files, which is why you'd have
to wait for a major release.

-- Dan

------------------------------------------------------------------------------
Achieve Improved Network Security with IP and DNS Reputation.
Defend against bad network traffic, including botnets, malware, 
phishing sites, and compromised hosts - saving your company time, 
money, and embarrassment.   Learn More! 
http://p.sf.net/sfu/hpdev2dev-nov
_______________________________________________
Ecasound-list mailing list
Ecasound-list <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ecasound-list


Gmane