简体   繁体   中英

how use ADPCM microchips

I have some problems with adpcm in.wav (sound)files. at first of this question, I should say that I didn't read all things about ADPCM, just wanted to implement that fast and work on it... (just training code) I implemented it from MicroChip's pdf guid adpcm .(it's better to say copy/pasted and edited,created class)

Test code:

    const std::vector<int8_t> data = {
    64,  67,  71,  75,  79,  83,  87,  91,  94,  98,  101, 104, 107, 110, 112,
    115, 117, 119, 121, 123, 124, 125, 126, 126, 127, 127, 127, 126, 126, 125,
    124, 123, 121, 119, 117, 115, 112, 110, 107, 104, 101, 98,  94,  91,  87,
    83,  79,  75,  71,  67,  64,  60,  56,  52,  48,  44,  40,  36,  33,  29,
    26,  23,  20,  17,  15,  12,  10,  8,   6,   4,   3,   2,   1,   1,   0,
    0,   0,   1,   1,   2,   3,   4,   6,   8,   10,  12,  15,  17,  20,  23,
    26,  29,  33,  36,  40,  44,  48,  52,  56,  60,  64};
void function() {
  std::vector<uint8_t> en;
  std::vector<uint8_t> de;
  {  // encode
    wave::ADPCM adpcm;
    // 32768
    for (size_t i{0}; i < data.size() - 3; i += 4) {
      int16_t first{static_cast<int16_t>(
          ~((static_cast<uint16_t>(data[i]) & 0xff) |
            ((static_cast<uint16_t>(data[i + 1]) << 8) & 0xff00)) +
          1)};
      int16_t second{static_cast<int16_t>(
          ~((static_cast<uint16_t>(data[i + 2]) & 0xff) |
            ((static_cast<uint16_t>(data[i + 3]) << 8) & 0xff00)) +
          1)};
      en.push_back(static_cast<uint8_t>((adpcm.ADPCMEncoder(first) & 0x0f) |
                                        (adpcm.ADPCMEncoder(second) << 4)));
    }
  }
  {  // decode
    wave::ADPCM adpcm;
    for (auto val : en) {
      int16_t result = ~adpcm.ADPCMDecoder(val & 0xf) + 1;
      int8_t temp0 = ((result)&0xff);
      int8_t temp1 = ((result)&0xff00) >> 8;
      de.push_back(temp0);
      de.push_back(temp1);
      result = ~adpcm.ADPCMDecoder(val >> 4) + 1;
      temp0 = ((result)&0xff);
      temp1 = (result & 0xff00) >> 8;
      de.push_back(temp0);
      de.push_back(temp1);
    }
  }
  int i{0};
  for (auto val : de) {
    qDebug() << "real:" << data[i] << "decoded: " << val;
    i++;
  }
}

I'm sure that my class and encode/decode is right,just after decode I should do somethings to show correct numbers(but I donw know which casting is failed) .

why I'm sure? because when I see my output in QDebug, every other sample(after decode) are correct (with few errors, in big datas errors will be smaller than now), but anothers are failed

my output:

real: 26 decoded:  6
real: 29 decoded:  32
real: 33 decoded:  5
real: 36 decoded:  48
real: 40 decoded:  6
real: 44 decoded:  32
real: 48 decoded:  5
real: 52 decoded:  48
real: 56 decoded:  4
real: 60 decoded:  64

data are 8bits in device

Ok, I found my answer

when you have error on any number, your error is in lower bits, so my predict was on two numbers that was anded to gether , then that number is in lower bits position have much errors!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM