ADPCM壓縮演算法

來源:互聯網
上載者:User
ADPCM壓縮演算法 ADPCM(Adaptive Differential Pulse Code Modulation),是一種針對 16bits( 或8bits或者更高) 聲音波形資料的一種有損壓縮演算法,它將聲音流中每次採樣的 16bit 資料以 4bit 儲存,所以壓縮比 1:4. 而且壓縮/解壓縮演算法非常簡單,所以是一種低空間消耗,高品質高效率聲音獲得的好途徑。儲存聲音的資料檔案尾碼名為 .AUD 的大多用ADPCM 壓縮。
  ADPCM 主要是針對連續的波形資料的,儲存的是波形的變化情況,以達到描述整個波形的目的,由於它的編碼和解碼的過程卻很簡潔,列在後面,相信大家能夠看懂。
  8bits採樣的聲音人耳是可以勉強接受的,而 16bit 採樣的聲音可以算是高音質了。ADPCM 演算法卻可以將每次採樣得到的 16bit 資料壓縮到 4bit 。需要注意的是,如果要壓縮/解壓縮得是立體聲訊號,採樣時,聲音訊號是放在一起的,需要將兩個聲道分別處理。

ADPCM 壓縮過程

  首先我們認為聲音訊號都是從零開始的,那麼需要初始化兩個變數

    int index=0,prev_sample=0;

  下面的迴圈將依次處理聲音資料流,注意其中的 getnextsample() 應該得到一個 16bit 的採樣資料,而 outputdata() 可以將計算出來的資料儲存起來,程式中用到的 step_table[],index_adjust[] 附在後面:

    int index=0,prev_sample:=0;

    while (還有資料要處理)
    {
      cur_sample=getnextsample();        // 得到當前的採樣資料
      delta=cur_sample-prev_sample;       // 計算出和上一個的增量
      if (delta<0) delta=-delta,sb=8;      // 取絕對值
      else sb = 0 ;               // sb 儲存的是符號位
      code = 4*delta / step_table[index];    // 根據 steptable[]得到一個 0-7 的值
      if (code>7) code=7;            // 它描述了聲音強度的變化量
      index += index_adjust[code] ;       // 根據聲音強度調整下次取steptable 的序號
      if (index<0) index=0;           // 便於下次得到更精確的變化量的描述
      else if (index>88) index=88;
      prev_sample=cur_sample;
      outputode(code|sb);            // 加上符號位儲存起來
    }

 

ADPCM 解壓縮過程

  接壓縮實際是壓縮的一個逆過程,同樣其中的 getnextcode() 應該得到一個編碼,,而 outputsample() 可以將解碼出來的聲音訊號儲存起來。這段代碼同樣使用了同一個的 setp_table[] 和 index_adjust() 附在後面:

    int index=0,cur_sample=0;

    while (還有資料要處理)
    {
        code=getnextcode();                       // 得到下一個資料
        if ((code & 8) != 0) sb=1 else sb=0;
        code&=7;                            // 將 code 分離為資料和符號
        delta = (step_table[index]*code)/4+step_table[index]/8;     // 後面加的一項是為了減少誤差
        if (sb==1) delta=-delta;
        cur_sample+=delta;                        // 計算出當前的波形資料
        if (cur_sample>32767) output_sample(32767);
        else if (cur_sample<-32768) output_sample(-32768);
        else output_sample(cur_sample);
        index+=index_adjust[code];
        if (index<0) index=0;
        if (index>88) index=88;
     }

附表

     int index_adjust[8] = {-1,-1,-1,-1,2,4,6,8};      int step_table[89] =
     {
       7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,
       50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,
       408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,
       2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,
       10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767
     }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.