Re: [ecasound] lossless conversion broken?

From: Dan Muresan <danmbox@email-addr-hidden>
Date: Wed Nov 03 2010 - 01:03:17 EET


> libecasound/eca-sample-conversion.h:

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

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:

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;

float 4.65661e-10 -> int 0
float 9.31323e-10 -> int 1
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:

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!
Ecasound-list mailing list
Received on Wed Nov 3 04:15:01 2010

This archive was generated by hypermail 2.1.8 : Wed Nov 03 2010 - 04:15:01 EET