BrianPugh/tamp

compress stream of sound data

zaiafat opened this issue · 3 comments

Hello guys

First, Thank to Brain Pugh for share the good tamp compression library. I have large uint8 data (sound data). for example assume I want to compress this 240 byte to use it in ardinu or stm32. I want to compress it with this lib.
Please give me an example of the C language, if it possible.
Thanks in advance

const char data[] =
{136, 133, 128, 117, 114, 144, 136, 130, 122, 107, 138, 139, 130, 128, 104, 128, 139, 132, 133, 104,
120, 135, 132, 141, 110, 114, 136, 129, 145, 119, 104, 132, 130, 150, 130, 98 , 120, 122, 151, 145,
99 , 114, 110, 141, 162, 108, 111, 107, 126, 168, 120, 111, 110, 116, 171, 136, 111, 114, 110, 162,
150, 108, 111, 107, 151, 165, 119, 110, 107, 138, 169, 130, 108, 105, 122, 168, 142, 108, 113, 110,
153, 154, 108, 111, 111, 144, 169, 123, 110, 105, 126, 174, 139, 111, 105, 108, 160, 153, 111, 99 ,
99 , 150, 169, 125, 98 , 89 , 125, 174, 142, 101, 92 , 105, 160, 162, 108, 98 , 105, 147, 174, 120,
93 , 122, 174, 148, 102, 98 , 107, 150, 168, 122, 98 , 107, 126, 165, 150, 104, 107, 116, 151, 174,
101, 102, 119, 168, 150, 105, 102, 110, 150, 172, 125, 95 , 104, 129, 171, 148, 93 , 92 , 113, 156,
119, 90 , 105, 129, 169, 148, 98 , 99 , 113, 136, 159, 122, 99 , 116, 114, 139, 145, 104, 111, 117,
156, 138, 107, 113, 104, 132, 166, 129, 104, 99 , 107, 163, 171, 119, 101, 90 , 126, 181, 144, 102,
104, 160, 181, 126, 98 , 101, 116, 165, 165, 114, 107, 107, 125, 169, 147, 117, 117, 104, 139, 163,
113, 105, 113, 156, 168, 128, 108, 110, 120, 166, 156, 104, 104, 102, 132, 174, 139, 108, 101, 102};

Hi @zaiafat!

It seems like you want to store the compressed data as part of your firmware and decode it on-device, correct? So first, I would check to see if tamp provides meaningful compression for your data. You can first install tamp on your computer.

pip install tamp

and then we can write a short python script:

data = [
  136, 133, 128, 117, 114, 144, 136, 130, 122, 107, 138, 139, 130, 128, 104, 128, 139, 132, 133, 104,
  120, 135, 132, 141, 110, 114, 136, 129, 145, 119, 104, 132, 130, 150, 130, 98 , 120, 122, 151, 145,
  99 , 114, 110, 141, 162, 108, 111, 107, 126, 168, 120, 111, 110, 116, 171, 136, 111, 114, 110, 162,
  150, 108, 111, 107, 151, 165, 119, 110, 107, 138, 169, 130, 108, 105, 122, 168, 142, 108, 113, 110,
  153, 154, 108, 111, 111, 144, 169, 123, 110, 105, 126, 174, 139, 111, 105, 108, 160, 153, 111, 99 ,
  99 , 150, 169, 125, 98 , 89 , 125, 174, 142, 101, 92 , 105, 160, 162, 108, 98 , 105, 147, 174, 120,
  93 , 122, 174, 148, 102, 98 , 107, 150, 168, 122, 98 , 107, 126, 165, 150, 104, 107, 116, 151, 174,
  101, 102, 119, 168, 150, 105, 102, 110, 150, 172, 125, 95 , 104, 129, 171, 148, 93 , 92 , 113, 156,
  119, 90 , 105, 129, 169, 148, 98 , 99 , 113, 136, 159, 122, 99 , 116, 114, 139, 145, 104, 111, 117,
  156, 138, 107, 113, 104, 132, 166, 129, 104, 99 , 107, 163, 171, 119, 101, 90 , 126, 181, 144, 102,
  104, 160, 181, 126, 98 , 101, 116, 165, 165, 114, 107, 107, 125, 169, 147, 117, 117, 104, 139, 163,
  113, 105, 113, 156, 168, 128, 108, 110, 120, 166, 156, 104, 104, 102, 132, 174, 139, 108, 101, 102
]
data = bytes(data)
print(len(data))

import tamp
compressed_data = tamp.compress(data)
print(len(compressed_data))

you can, alternatively, use the cli-interface if you have your data as a standalone file (see tamp --help for more details).

Unfortunately, tamp doesn't compress this data well (infact, it makes it larger 240->257). Generic lossless-compression algorithms (like tamp, heatshrink, zlib, lzma, and so on) work by finding and deduplicating patterns in the data. Raw audio data typically contains a lot of random noise/data, and it's pretty much impossible to compress noise.

If tamp actually helped in this situation, the resulting data could be decompressed by the C library by modifying the examples in the documentation.

For completeness, with this exact data, zlib was able to compress it to 218 bytes (90.8%); as-is, the savings are most likely not worth it.

closing due to lack of activity.