Distortion in IMA ADPCM, suspect block header issue

Hi guys,

I’m implementing IMA ADPCM compression based on algorithm from Microchip (also attached here). and package data into WAV format. However, there is distortion in compressed data
My testing method:

  • generate a 5s of PCM data : 16kHz x 16 bit. File name: “Sine-1KHz-16x16-5s_SWgenerated (modified)”. It’s generated by repeating a complete sine wave cycle multiple times.
  • This data will be fed into Audacity and my compression code; then compare the output
  • “Sine-1KHz-16x16-5s_SWgenerated (IMA ADPCM)”: from Audacity
  • “Voice3011152932_PCM2bytes”: from my code
  • Playing “Voice3011152932_PCM2bytes”, there is a very distinct distortion.
  • When compare the binary, this is what is found at the block header (for example, at offset 572):
    Audacity: DE 39 4F 00
    My Code : 09 CF 4F 00

According to my understanding,

  • Byte 0: LSB of PCM sample
  • Byte 1: MSB of PCM sample
  • Byte 2: current index
  • Byte 3: zero

Apparently, this doesn’t match with Audacity

I would greatly appreciate if anyone could advise me on this issue
Thank you so much in advance

Dennis
++PIC on ADPCM.rar (150 KB)


What do you mean by “fed into Audacity”? Do you mean record or import?

Are you complaining about an IMA ADPCM file that Audacity exports?

If there is distortion in your generated sound, why is that an Audacity bug? Does it distort in other apps?

Have you contacted libsndfile-devel? If there is an Audacity bug, the bug probably lies in the libsndfile library. That list may be able to advise you more about headers of different ADPCM formats.

If you want an IMA ADPCM specification, have you tried http://www.cs.columbia.edu/~hgs/audio/dvi/?


Gale

Hi Gale,
Thanks for your reply
First of all, I did not claim this is Audacity bug and I myself do not think so anyway. I was merely looking for advices from those who have experience in this field.

"What do you mean by “fed into Audacity”? Do you mean record or import? "
==> I used Audacity to open and then export to IMA ADPCM

Yes, I also did check on the specs you link to and also the specs itself and my understanding was what I have described in earlier post. Perhaps I miss out something ?

I will take a look at Libsndfile. Thanks for the advice

Dennis

Hi Gale,
I’ve take a look at libsndfile, in particulare ima_adpcm.c

static int
wav_w64_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
{
	/* Encode the block header. */
	for (chan = 0 ; chan < pima->channels ; chan++)
	{	pima->block [chan*4]	= pima->samples [chan] & 0xFF ;
		pima->block [chan*4+1]	= (pima->samples [chan] >> 8) & 0xFF ;

		pima->block [chan*4+2] = pima->stepindx [chan] ;
		pima->block [chan*4+3] = 0 ;

		pima->previous [chan] = pima->samples [chan] ;
		} ;
..
}

Byte 0 and Byte 1 are LSB and MSB of PCM sample
However, in Audacity block header: (DE 39 4F 00); DE and 39 are not belong to the PCM data that I use to generate ADPCM. That’s what make me quite puzzled …

Dennis

If “Voice3011152932_PCM2bytes” is your file and “Sine-1KHz-16x16-5s_SWgenerated (IMA ADPCM)” is the Audacity exported file, the headers of those two files (before the data) are identical.

Offset 572 in the Audacity exported file and your file appears to be CD. There are no DE hex values anywhere in either file.

If you are you saying that “ima_adpcm.c” is incorrect in some way, please say why.

If you just want advice about how to code internals of your formats, this is the wrong forum, sorry.


Gale

Hi Gale,

Perhaps I did not explain very clear…

  • “Sine-1KHz-16x16-5s_SWgenerated (modified)” is PCM data.
  • I use Audacity to open, and export into “Sine-1KHz-16x16-5s_SWgenerated (IMA ADPCM)”
  • data DE 39 4F 00 is at offset 572 of “Sine-1KHz-16x16-5s_SWgenerated (IMA ADPCM)” file

I’m not expert in this, thus far from qualified to say whether it’s right or wrong. I’m just trying to understand more on how IMA ADPCM is packaged into WAV format in Audacity.
I’m not here to find faults, but rather to learn things.

Thank you

Dennis

If I understand your question correctly, encoding the uncompressed audio data from Audacity to IMA_ADPCM is handled by libsndfile. For more information about how libsndfile handles the conversion, you really need to look at their documentation / ask their developers (libsndfile Mailing Lists).

You did not attach a file of that name.

No developers hang out here. For expert opinion I suggest you ask on libsndfile-devel.

That said, I don’t see why you expect your Microchip algorithm should give the same result as IMA ADPCM. Does this help: http://www.microchip.com/forums/m698891.aspx?


Gale