51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

java G711 编码

/**
 * G711  A-alw 
 * @author Administrator
 *
 */
public class CMG711 {

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">short</span> aLawDecompressTable[] = <span class="hljs-keyword">new</span> <span class="hljs-title class_">short</span>[]
            { -<span class="hljs-number">5504</span>, -<span class="hljs-number">5248</span>, -<span class="hljs-number">6016</span>, -<span class="hljs-number">5760</span>, -<span class="hljs-number">4480</span>, -<span class="hljs-number">4224</span>, -<span class="hljs-number">4992</span>, -<span class="hljs-number">4736</span>, -<span class="hljs-number">7552</span>, -<span class="hljs-number">7296</span>, -<span class="hljs-number">8064</span>, -<span class="hljs-number">7808</span>, -<span class="hljs-number">6528</span>, -<span class="hljs-number">6272</span>, -<span class="hljs-number">7040</span>, -<span class="hljs-number">6784</span>, -<span class="hljs-number">2752</span>, -<span class="hljs-number">2624</span>, -<span class="hljs-number">3008</span>, -<span class="hljs-number">2880</span>, -<span class="hljs-number">2240</span>, -<span class="hljs-number">2112</span>, -<span class="hljs-number">2496</span>, -<span class="hljs-number">2368</span>, -<span class="hljs-number">3776</span>, -<span class="hljs-number">3648</span>, -<span class="hljs-number">4032</span>, -<span class="hljs-number">3904</span>, -<span class="hljs-number">3264</span>, -<span class="hljs-number">3136</span>, -<span class="hljs-number">3520</span>, -<span class="hljs-number">3392</span>, -<span class="hljs-number">22016</span>, -<span class="hljs-number">20992</span>, -<span class="hljs-number">24064</span>, -<span class="hljs-number">23040</span>, -<span class="hljs-number">17920</span>, -<span class="hljs-number">16896</span>, -<span class="hljs-number">19968</span>, -<span class="hljs-number">18944</span>, -<span class="hljs-number">30208</span>, -<span class="hljs-number">29184</span>, -<span class="hljs-number">32256</span>, -<span class="hljs-number">31232</span>, -<span class="hljs-number">26112</span>, -<span class="hljs-number">25088</span>, -<span class="hljs-number">28160</span>, -<span class="hljs-number">27136</span>, -<span class="hljs-number">11008</span>, -<span class="hljs-number">10496</span>, -<span class="hljs-number">12032</span>, -<span class="hljs-number">11520</span>, -<span class="hljs-number">8960</span>, -<span class="hljs-number">8448</span>, -<span class="hljs-number">9984</span>, -<span class="hljs-number">9472</span>, -<span class="hljs-number">15104</span>, -<span class="hljs-number">14592</span>, -<span class="hljs-number">16128</span>, -<span class="hljs-number">15616</span>, -<span class="hljs-number">13056</span>, -<span class="hljs-number">12544</span>, -<span class="hljs-number">14080</span>, -<span class="hljs-number">13568</span>, -<span class="hljs-number">344</span>, -<span class="hljs-number">328</span>, -<span class="hljs-number">376</span>,
                    -<span class="hljs-number">360</span>, -<span class="hljs-number">280</span>, -<span class="hljs-number">264</span>, -<span class="hljs-number">312</span>, -<span class="hljs-number">296</span>, -<span class="hljs-number">472</span>, -<span class="hljs-number">456</span>, -<span class="hljs-number">504</span>, -<span class="hljs-number">488</span>, -<span class="hljs-number">408</span>, -<span class="hljs-number">392</span>, -<span class="hljs-number">440</span>, -<span class="hljs-number">424</span>, -<span class="hljs-number">88</span>, -<span class="hljs-number">72</span>, -<span class="hljs-number">120</span>, -<span class="hljs-number">104</span>, -<span class="hljs-number">24</span>, -<span class="hljs-number">8</span>, -<span class="hljs-number">56</span>, -<span class="hljs-number">40</span>, -<span class="hljs-number">216</span>, -<span class="hljs-number">200</span>, -<span class="hljs-number">248</span>, -<span class="hljs-number">232</span>, -<span class="hljs-number">152</span>, -<span class="hljs-number">136</span>, -<span class="hljs-number">184</span>, -<span class="hljs-number">168</span>, -<span class="hljs-number">1376</span>, -<span class="hljs-number">1312</span>, -<span class="hljs-number">1504</span>, -<span class="hljs-number">1440</span>, -<span class="hljs-number">1120</span>, -<span class="hljs-number">1056</span>, -<span class="hljs-number">1248</span>, -<span class="hljs-number">1184</span>, -<span class="hljs-number">1888</span>, -<span class="hljs-number">1824</span>, -<span class="hljs-number">2016</span>, -<span class="hljs-number">1952</span>, -<span class="hljs-number">1632</span>, -<span class="hljs-number">1568</span>, -<span class="hljs-number">1760</span>, -<span class="hljs-number">1696</span>, -<span class="hljs-number">688</span>, -<span class="hljs-number">656</span>, -<span class="hljs-number">752</span>, -<span class="hljs-number">720</span>, -<span class="hljs-number">560</span>, -<span class="hljs-number">528</span>, -<span class="hljs-number">624</span>, -<span class="hljs-number">592</span>, -<span class="hljs-number">944</span>, -<span class="hljs-number">912</span>, -<span class="hljs-number">1008</span>, -<span class="hljs-number">976</span>, -<span class="hljs-number">816</span>, -<span class="hljs-number">784</span>, -<span class="hljs-number">880</span>, -<span class="hljs-number">848</span>, <span class="hljs-number">5504</span>, <span class="hljs-number">5248</span>, <span class="hljs-number">6016</span>, <span class="hljs-number">5760</span>, <span class="hljs-number">4480</span>, <span class="hljs-number">4224</span>, <span class="hljs-number">4992</span>, <span class="hljs-number">4736</span>, <span class="hljs-number">7552</span>, <span class="hljs-number">7296</span>, <span class="hljs-number">8064</span>, <span class="hljs-number">7808</span>, <span class="hljs-number">6528</span>, <span class="hljs-number">6272</span>, <span class="hljs-number">7040</span>, <span class="hljs-number">6784</span>, <span class="hljs-number">2752</span>, <span class="hljs-number">2624</span>,
                    <span class="hljs-number">3008</span>, <span class="hljs-number">2880</span>, <span class="hljs-number">2240</span>, <span class="hljs-number">2112</span>, <span class="hljs-number">2496</span>, <span class="hljs-number">2368</span>, <span class="hljs-number">3776</span>, <span class="hljs-number">3648</span>, <span class="hljs-number">4032</span>, <span class="hljs-number">3904</span>, <span class="hljs-number">3264</span>, <span class="hljs-number">3136</span>, <span class="hljs-number">3520</span>, <span class="hljs-number">3392</span>, <span class="hljs-number">22016</span>, <span class="hljs-number">20992</span>, <span class="hljs-number">24064</span>, <span class="hljs-number">23040</span>, <span class="hljs-number">17920</span>, <span class="hljs-number">16896</span>, <span class="hljs-number">19968</span>, <span class="hljs-number">18944</span>, <span class="hljs-number">30208</span>, <span class="hljs-number">29184</span>, <span class="hljs-number">32256</span>, <span class="hljs-number">31232</span>, <span class="hljs-number">26112</span>, <span class="hljs-number">25088</span>, <span class="hljs-number">28160</span>, <span class="hljs-number">27136</span>, <span class="hljs-number">11008</span>, <span class="hljs-number">10496</span>, <span class="hljs-number">12032</span>, <span class="hljs-number">11520</span>, <span class="hljs-number">8960</span>, <span class="hljs-number">8448</span>, <span class="hljs-number">9984</span>, <span class="hljs-number">9472</span>, <span class="hljs-number">15104</span>, <span class="hljs-number">14592</span>, <span class="hljs-number">16128</span>, <span class="hljs-number">15616</span>, <span class="hljs-number">13056</span>, <span class="hljs-number">12544</span>, <span class="hljs-number">14080</span>, <span class="hljs-number">13568</span>, <span class="hljs-number">344</span>, <span class="hljs-number">328</span>, <span class="hljs-number">376</span>, <span class="hljs-number">360</span>, <span class="hljs-number">280</span>, <span class="hljs-number">264</span>, <span class="hljs-number">312</span>, <span class="hljs-number">296</span>, <span class="hljs-number">472</span>, <span class="hljs-number">456</span>, <span class="hljs-number">504</span>, <span class="hljs-number">488</span>, <span class="hljs-number">408</span>, <span class="hljs-number">392</span>, <span class="hljs-number">440</span>, <span class="hljs-number">424</span>, <span class="hljs-number">88</span>, <span class="hljs-number">72</span>, <span class="hljs-number">120</span>, <span class="hljs-number">104</span>, <span class="hljs-number">24</span>, <span class="hljs-number">8</span>, <span class="hljs-number">56</span>, <span class="hljs-number">40</span>, <span class="hljs-number">216</span>, <span class="hljs-number">200</span>, <span class="hljs-number">248</span>, <span class="hljs-number">232</span>, <span class="hljs-number">152</span>, <span class="hljs-number">136</span>, <span class="hljs-number">184</span>, <span class="hljs-number">168</span>, <span class="hljs-number">1376</span>, <span class="hljs-number">1312</span>, <span class="hljs-number">1504</span>, <span class="hljs-number">1440</span>, <span class="hljs-number">1120</span>,
                    <span class="hljs-number">1056</span>, <span class="hljs-number">1248</span>, <span class="hljs-number">1184</span>, <span class="hljs-number">1888</span>, <span class="hljs-number">1824</span>, <span class="hljs-number">2016</span>, <span class="hljs-number">1952</span>, <span class="hljs-number">1632</span>, <span class="hljs-number">1568</span>, <span class="hljs-number">1760</span>, <span class="hljs-number">1696</span>, <span class="hljs-number">688</span>, <span class="hljs-number">656</span>, <span class="hljs-number">752</span>, <span class="hljs-number">720</span>, <span class="hljs-number">560</span>, <span class="hljs-number">528</span>, <span class="hljs-number">624</span>, <span class="hljs-number">592</span>, <span class="hljs-number">944</span>, <span class="hljs-number">912</span>, <span class="hljs-number">1008</span>, <span class="hljs-number">976</span>, <span class="hljs-number">816</span>, <span class="hljs-number">784</span>, <span class="hljs-number">880</span>, <span class="hljs-number">848</span> };

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-variable">cClip</span> <span class="hljs-operator">=</span> <span class="hljs-number">32635</span>;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">byte</span> aLawCompressTable[] = <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[]
            { <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span> };

    <span class="hljs-comment">/**
     * 编码  pcm  to  G711 a-law
     * <span class="hljs-doctag">@param</span> b
     * <span class="hljs-doctag">@return</span>
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">byte</span>[] encode( <span class="hljs-type">byte</span>[] b){
        <span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;
        <span class="hljs-type">int</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> b.length / <span class="hljs-number">2</span>;
        <span class="hljs-type">short</span> <span class="hljs-variable">sample</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;
        <span class="hljs-type">byte</span>[] res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[count];
        <span class="hljs-keyword">for</span> ( <span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; count; i++ )
        {
            sample = (<span class="hljs-type">short</span>) ( ( ( b[j++] &amp; <span class="hljs-number">0xff</span> ) | ( b[j++] ) &lt;&lt; <span class="hljs-number">8</span> ) );
            res[i] = linearToALawSample( sample );
        }
        <span class="hljs-keyword">return</span> res;
    }


    <span class="hljs-comment">/**
     * 解码
     * <span class="hljs-doctag">@param</span> b
     * <span class="hljs-doctag">@return</span>
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">byte</span>[] decode( <span class="hljs-type">byte</span>[] b){
        <span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;
        <span class="hljs-type">byte</span>[] res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[b.length*<span class="hljs-number">2</span>];
        <span class="hljs-keyword">for</span> ( <span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; b.length; i++ )
        {
            <span class="hljs-type">short</span> <span class="hljs-variable">s</span> <span class="hljs-operator">=</span> aLawDecompressTable[b[i] &amp; <span class="hljs-number">0xff</span>];
            res[j++] = (<span class="hljs-type">byte</span>) s;
            res[j++] = (<span class="hljs-type">byte</span>) ( s &gt;&gt; <span class="hljs-number">8</span> );
        }
        <span class="hljs-keyword">return</span> res;
    }




    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">byte</span> <span class="hljs-title function_">linearToALawSample</span><span class="hljs-params">( <span class="hljs-type">short</span> sample )</span>{
        <span class="hljs-type">int</span> sign;
        <span class="hljs-type">int</span> exponent;
        <span class="hljs-type">int</span> mantissa;
        <span class="hljs-type">int</span> s;

        sign = ( ( ~sample ) &gt;&gt; <span class="hljs-number">8</span> ) &amp; <span class="hljs-number">0x80</span>;
        <span class="hljs-keyword">if</span> ( !( sign == <span class="hljs-number">0x80</span> ) )
        {
            sample = (<span class="hljs-type">short</span>) -sample;
        }
        <span class="hljs-keyword">if</span> ( sample &gt; cClip )
        {
            sample = cClip;
        }
        <span class="hljs-keyword">if</span> ( sample &gt;= <span class="hljs-number">256</span> )
        {
            exponent = (<span class="hljs-type">int</span>) aLawCompressTable[( sample &gt;&gt; <span class="hljs-number">8</span> ) &amp; <span class="hljs-number">0x7F</span>];
            mantissa = ( sample &gt;&gt; ( exponent + <span class="hljs-number">3</span> ) ) &amp; <span class="hljs-number">0x0F</span>;
            s = ( exponent &lt;&lt; <span class="hljs-number">4</span> ) | mantissa;
        }
        <span class="hljs-keyword">else</span>
        {
            s = sample &gt;&gt; <span class="hljs-number">4</span>;
        }
        s ^= ( sign ^ <span class="hljs-number">0x55</span> );
        <span class="hljs-keyword">return</span> (<span class="hljs-type">byte</span>) s;
    }




}


赞(0)
未经允许不得转载:工具盒子 » java G711 编码