`
隐形的翅膀
  • 浏览: 493917 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

MuLaw and ALaw

 
阅读更多
The mu-law byte bit arrangement is SEEEMMMM (Sign, Exponent, and Mantissa.)

MuLaw
/// <summary>
        /// Encode one mu-law byte from a 16-bit signed integer. Internal use only.
        /// </summary>
        /// <param name="pcm">A 16-bit signed pcm value</param>
        /// <returns>A mu-law encoded byte</returns>
        private static byte encode(int pcm)
        {
            //Get the sign bit.  Shift it for later use without further modification
			// xxxx xxxx xxxx xxxx  
			// 1000 0000 0000 0000 
			// ---------------------
			// ?000 0000 0000 0000
            int sign = (pcm & 0x8000) >> 8;
            //If the number is negative, make it positive (now it's a magnitude)
            if (sign != 0)
                pcm = -pcm;
            //The magnitude must be less than 32635 to avoid overflow
			//MAX = 32635
            if (pcm > MAX) pcm = MAX;
            //Add 132 to guarantee a 1 in the eight bits after the sign bit
			//132 = 1000 0100
            pcm += BIAS;

            /* Finding the "exponent"
             * Bits:
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * S 7 6 5 4 3 2 1 0 . . . . . . .
             * We want to find where the first 1 after the sign bit is.
             * We take the corresponding value from the second row as the exponent value.
             * (i.e. if first 1 at position 7 -> exponent = 2) */
            int exponent = 7;
            //Move to the right and decrement exponent until we hit the 1
			// 0x4000 = 100 0000 0000 0000
			// Sxxx xxxx xxxx xxxx
			//  100 0000 0000 0000
			// ---------------------
			//  ?00 0000 0000 0000
            for (int expMask = 0x4000; (pcm & expMask) == 0; exponent--, expMask >>= 1) { }

            /* The last part - the "mantissa"
             * We need to take the four bits after the 1 we just found.
             * To get it, we shift 0x0f :
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * S 0 0 0 0 0 1 . . . . . . . . . (meaning exponent is 2)
             * . . . . . . . . . . . . 1 1 1 1
             * We shift it 5 times for an exponent of two, meaning
             * we will shift our four bits (exponent + 3) bits.
             * For convenience, we will actually just shift the number, then and with 0x0f. */
			 // 0x0f = 1111
            int mantissa = (pcm >> (exponent + 3)) & 0x0f;

            //The mu-law byte bit arrangement is SEEEMMMM (Sign, Exponent, and Mantissa.)
            byte mulaw = (byte)(sign | exponent << 4 | mantissa);

            //Last is to flip the bits
            //~01111111 127
            //----------
            // 10000000 128
            return (byte)~mulaw;
        }


//decode 

/// <summary>
        /// Decode one mu-law byte. For internal use only.
        /// </summary>
        /// <param name="mulaw">The encoded mu-law byte</param>
        /// <returns>A short containing the 16-bit result</returns>
        private static short decode(byte mulaw)
        {
            //Flip all the bits
            mulaw = (byte)~mulaw;

            //Pull out the value of the sign bit
            // xxxx xxxx
            // 1000 0000
            // -----------
            // ?000 0000
            int sign = mulaw & 0x80;
            //Pull out and shift over the value of the exponent

            // xxxx xxxx
            //  111 0000
            // ----------
            //  ??? 0000
            int exponent = (mulaw & 0x70) >> 4;
            //Pull out the four bits of data

            // xxxx xxxx
            //      1111
            // ----------
            //      ????

            int data = mulaw & 0x0f;

            //Add on the implicit fifth bit (we know the four data bits followed a one bit)
            // xxxx xxxx
            //    1 0000
            //----------
            //    x xxxx
            data |= 0x10;

            /* Add a 1 to the end of the data by shifting over and adding one.  Why?
             * Mu-law is not a one-to-one function.  There is a range of values that all
             * map to the same mu-law byte.  Adding a one to the end essentially adds a
             * "half byte", which means that the decoding will return the value in the
             * middle of that range.  Otherwise, the mu-law decoding would always be
             * less than the original data. */
            // x xxxx0
            data <<= 1;
            // x xxxx1
            data += 1;
            /* Shift the five bits to where they need to be: left (exponent + 2) places
             * Why (exponent + 2) ?
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * . 7 6 5 4 3 2 1 0 . . . . . . . <-- starting bit (based on exponent)
             * . . . . . . . . . . 1 x x x x 1 <-- our data
             * We need to move the one under the value of the exponent,
             * which means it must move (exponent + 2) times
             */
            data <<= exponent + 2;
            //Remember, we added to the original, so we need to subtract from the final
            data -= MuLawEncoder.BIAS;
            //If the sign bit is 0, the number is positive. Otherwise, negative.
            return (short)(sign == 0 ? data : -data);
        }




ALaw
//The a-law byte bit arrangement is SEEEMMMM (Sign, Exponent, and Mantissa.)
 /// <summary>
        /// Encode one a-law byte from a 16-bit signed integer. Internal use only.
        /// </summary>
        /// <param name="pcm">A 16-bit signed pcm value</param>
        /// <returns>A a-law encoded byte</returns>
        private static byte encode(int pcm)
        {
            //Get the sign bit.  Shift it for later use without further modification
            int sign = (pcm & 0x8000) >> 8;
            //If the number is negative, make it positive (now it's a magnitude)
            if (sign != 0)
                pcm = -pcm;
            //The magnitude must fit in 15 bits to avoid overflow
            if (pcm > MAX) pcm = MAX;

            /* Finding the "exponent"
             * Bits:
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * S 7 6 5 4 3 2 1 0 0 0 0 0 0 0 0
             * We want to find where the first 1 after the sign bit is.
             * We take the corresponding value from the second row as the exponent value.
             * (i.e. if first 1 at position 7 -> exponent = 2)
             * The exponent is 0 if the 1 is not found in bits 2 through 8.
             * This means the exponent is 0 even if the "first 1" doesn't exist.
             */
            int exponent = 7;
            //Move to the right and decrement exponent until we hit the 1 or the exponent hits 0
            for (int expMask = 0x4000; (pcm & expMask) == 0 && exponent>0; exponent--, expMask >>= 1) { }

            /* The last part - the "mantissa"
             * We need to take the four bits after the 1 we just found.
             * To get it, we shift 0x0f :
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * S 0 0 0 0 0 1 . . . . . . . . . (say that exponent is 2)
             * . . . . . . . . . . . . 1 1 1 1
             * We shift it 5 times for an exponent of two, meaning
             * we will shift our four bits (exponent + 3) bits.
             * For convenience, we will actually just shift the number, then AND with 0x0f. 
             * 
             * NOTE: If the exponent is 0:
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * S 0 0 0 0 0 0 0 Z Y X W V U T S (we know nothing about bit 9)
             * . . . . . . . . . . . . 1 1 1 1
             * We want to get ZYXW, which means a shift of 4 instead of 3
             */
            int mantissa = (pcm >> ((exponent == 0) ? 4 : (exponent + 3))) & 0x0f;

            //The a-law byte bit arrangement is SEEEMMMM (Sign, Exponent, and Mantissa.)
            byte alaw = (byte)(sign | exponent << 4 | mantissa);

            //Last is to flip every other bit, and the sign bit (0xD5 = 1101 0101)
            return (byte)(alaw^0xD5);
        }
		
		/// <summary>
        /// Decode one a-law byte. For internal use only.
        /// </summary>
        /// <param name="alaw">The encoded a-law byte</param>
        /// <returns>A short containing the 16-bit result</returns>
        private static short decode(byte alaw)
        {
            //Invert every other bit, and the sign bit (0xD5 = 1101 0101)
            alaw ^= 0xD5;

            //Pull out the value of the sign bit
            int sign = alaw & 0x80;
            //Pull out and shift over the value of the exponent
            int exponent = (alaw & 0x70) >> 4;
            //Pull out the four bits of data
            int data = alaw & 0x0f;

            //Shift the data four bits to the left
            data <<= 4;
            //Add 8 to put the result in the middle of the range (like adding a half)
            data += 8;
            
            //If the exponent is not 0, then we know the four bits followed a 1,
            //and can thus add this implicit 1 with 0x100.
            if (exponent != 0)
                data += 0x100;
            /* Shift the bits to where they need to be: left (exponent - 1) places
             * Why (exponent - 1) ?
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * . 7 6 5 4 3 2 1 . . . . . . . . <-- starting bit (based on exponent)
             * . . . . . . . Z x x x x 1 0 0 0 <-- our data (Z is 0 only when exponent is 0)
             * We need to move the one under the value of the exponent,
             * which means it must move (exponent - 1) times
             * It also means shifting is unnecessary if exponent is 0 or 1.
             */
            if (exponent > 1)
                data <<= (exponent - 1);

            return (short)(sign == 0 ? data : -data);
        }
分享到:
评论

相关推荐

    PCM-Alaw_table.zip_Table_alaw_alaw pcm_pcm_speech aL

    mulaw(mu-law)编码是北美和日本的电话系统中常用的一种编码方式,原理类似,但压缩曲线略有不同,更适合当地人的听觉特性。 "speech_al"标签可能表示这个资源特别关注语音信号的处理,因为Alaw编码在语音通信中...

    ffmpeg库5.1版本,修改了源码,支持openssl,支持http-flv,mp4封装支持g711-alaw,mulaw

    3. **MP4封装支持G.711-alaw和mulaw**:G.711是一种脉冲编码调制(PCM)音频编码标准,分为alaw(欧洲)和mulaw(北美)两种变体。它们在电话系统中广泛应用。FFmpeg 5.1版本现在可以在MP4容器中封装这两种音频格式...

    audio-pcm-alaw-ulaw-g711-convert-master.zip

    16位小字节序的PCM数据与alaw/mulaw/g711数据的相互转换程序。

    pcm_mulaw.rar_conversion_pcm

    标题中的"pcm_mulaw.rar_conversion_pcm"涉及到两个主要的音频编码技术:PCM(脉冲编码调制)和Mu-Law编码。PCM是数字音频的基本表示形式,而Mu-Law编码是一种非线性的压缩方法,常用于电话系统和某些音频处理应用中...

    libffmpegJNI.so

    全格式: ffmpeg2.4 exoplayer2.14.2 vorbis opus flac alac pcm_mulaw pcm_alaw mp3 amrnb amrwb aac ac3 eac3 dca mlp truehd

    酷派F800万能播放器

    AMR, ADPCM, ALaw, MuLaw, Midi A2:视频: H.264 (CoreAVC), MPEG-1, MPEG-4 part 2 (ASP), DivX, XviD, Theora, Dirac, VP3, MJPEG, NRE, MSVIDEO1 A3:图像: JPEG, BMP, GIF, PNG (no progressive JPEG support...

    extension-ffmpeg-release.2.16.1

    Exoplayer扩展ffmpeg模块编译好的库,直接引用即可。编译参数:ENABLED_DECODERS=(vorbis opus flac alac pcm_mulaw pcm_alaw mp3 amrnb amrwb aac ac3 eac3 dca mlp truehd) 编译版本:2.16.1

    exoplayer-extension-ffmpeg-2.14.2

    Exoplayer扩展ffmpeg模块编译好的库,直接引用即可。编译参数:ENABLED_DECODERS=(vorbis opus flac alac pcm_mulaw pcm_alaw mp3 amrnb amrwb aac ac3 eac3 dca mlp truehd) 编译版本:2.14.2

    extension-ffmpeg-release.2.15.1

    Exoplayer扩展ffmpeg模块编译好的库,直接引用即可。编译参数:ENABLED_DECODERS=(vorbis opus flac alac pcm_mulaw pcm_alaw mp3 amrnb amrwb aac ac3 eac3 dca mlp truehd) 编译版本:2.15.1

    lib-decoder-ffmpeg-release-1.2.1

    Exoplayer扩展ffmpeg模块编译好的库,直接引用即可。编译参数:ENABLED_DECODERS=(vorbis opus flac alac pcm_mulaw pcm_alaw mp3 amrnb amrwb aac ac3 eac3 dca mlp truehd) 编译版本:androidx.media3:1.2.1

    matlab开发-MuLawQuantize

    首先,我们要了解Mu律编码(Mu-Law Quantization),它是一种非线性的声音编码方法,主要应用于语音通信系统,特别是电话网络。Mu律编码通过对原始模拟语音信号进行压缩,然后进行量化,以减少数据量的同时保持声音...

    extension-ffmpeg-release-2.18.2

    Exoplayer扩展ffmpeg模块编译...解决exoplayer部分视频、直播没有声音的问题,编译参数:ENABLED_DECODERS=(vorbis opus flac alac pcm_mulaw pcm_alaw mp3 amrnb amrwb aac ac3 eac3 dca mlp truehd) 编译版本:2.18.2

    声音小兔子(SoundBunny)v1.80免费安装版

    SoundBunny(声音小兔子)是一个高性能多用途的应用程序(声音管理器 音乐播放器 音频转换器),用来管理、播放和转换成吨的音频文件,可以处理宽范围的...MS/ IMA adpcm, aLaw, muLaw) RAW (pcm, lpcm, alaw, mulaw, gsm

    matlab开发-MuLawCompander

    6. `5.4 Mu Law compression.pdf`:这可能是一个包含MuLaw压缩理论和技术详细解释的PDF文档,为用户提供理论背景和理解。 7. `mu_law_compander_GUI25.prj`:MATLAB项目文件,保存了项目的元数据和相关设置。 8. `...

    anaopg-hyperlink.rar_Linux/Unix编程_processing

    - "Minvlaw13.m"、"law13.m"、"pinvalaw1.m"、"walaw1.m":这些可能是用MATLAB编写的函数,分别对应不同的音频编码算法,如A-law(ALaw)和μ-law(muLaw),这两种量化方法常用于电话系统中。 - "mulaw.m"、...

    Mu Law Quantizer:说明 mu-law 压扩(压缩和扩展)和语音量化的影响-matlab开发

    本练习使用 mu-law 量化器量化指定的语音文件,每个样本的比特率为 nbits,其中 nbits 通常在 2-10 的范围内。 对于选定的 nbits 值,程序绘制量化信号和误差信号以及信号和误差功率谱的估计值,以及误差信号值的...

    QT下开发的音视频播放器,支持单路与多路播放,软硬解码,本地与实时视频播放,录像截图,YUV与RGB显示,音量调节,进度条跳转等

    本播放器支持PCM_MULAW、PCM_ALAW、AAC编码格式音频的播放。本播放器支持单路播放器与多路播放器,单路播放器中ctYuvOpenglWidget是让让YUV转RGB在GPU中处理而重写的类,ctOpenglWidget则是直接对RGB图像进行渲染。...

    DirectShow过滤器-写WAV音频文件过滤器

    本过滤器将PCM音频流,或ADPCM,IEEE_FLOAT,ALAW,MULAW,GSM610音频流写入WAV音频文件。 参见介绍文章:...

    塞班手机全能播放器coreplayer

     MP3, MP2, AAC, MKA, WMA, Midi, WAV, OGG, Speex, WAVPACK, TTA, FLAC, MPC, AMR, ADPCM, ALaw, MuLaw, G.729支持的视频 H.264 (AVC), MKV, MPEG-1, MPEG-4 part 2 (ASP), DivX, XviD,F4V ,FLV , MJPEG, MSVIDEO...

    Mu Law Compander:这个练习展示了 mu 值范围从 1 到 500 的 mu-law 输入-输出特性-matlab开发

    这个 MATLAB 练习展示了 mu-law 输入-输出特性在 mu 值从 1 到 500 的范围内。该程序说明了 mu-law 压扩对语音信号的影响,显示了原始语音信号的信号直方图以及mu-law编码信号的直方图。

Global site tag (gtag.js) - Google Analytics