Compressing and decoding pcm audio bare streams

Source: Internet
Author: User

Like videos, audio transmission often requires compression. The following describes the use of the PCM audio bare stream File compression and decoding library provided by hith, comparing the compressed and decoded data of a file, we can clearly find that the audio compression process of PCM> ADPCM is lossy. For file comparison, I am using the BCompare software, which is definitely a must-have for programmers! If you do not have any children's shoes, we suggest you come and have fun. : Click to open the link

Because we use the compression and decoding libraries provided by haisi, we don't need to do anything complicated. We just need to write a few simple statements to call the code. Configuration is rather troublesome, so after the code is pasted below, the configured Project LINK is provided directly.

 

Compression, from PCM to ADPCM

# Include "stdafx. h "# include" hi_voice_api.h "HI_S32 VoiceEngineState [0x100]; # define VOICE_FRAME_SIZE 160 int _ tmain (int argc, _ TCHAR * argv []) // compression, from pcm to adpcm, This compression is lossy {HI_S32 frame; HI_S16 coder, len, frame_size; HI_S16 in_enc_pcmbuf [encoding]; // encoder inputHI_S16 ou_enc_unpacked [encoding]; // encoder outputcoder = 35; FILE * fp_in = NULL, * fp_out = NULL; fopen_s (& fp_in, "paomo. pcm "," rb "); fopen_s (& fp_out," enpaomo. adpcm "," wb "); frame_size = 2 * VOICE_FRAME_SIZE; if (coder = ADPCM_IMA) frame_size + = 1; frame = 0; HI_VOICE_EncReset (HI_VOID *) VoiceEngineState, coder); // compression encoder reset while (fread (random, sizeof (HI_S16), frame_size, fp_in) ==( HI_U32) frame_size) {HI_VOICE_EncodeFrame (HI_VOID *) VoiceEngineState, in_enc_pcmbuf, ou_enc_unpacked, frame_size); // compression code fwrite (ou_enc_unpacked, sizeof (HI_S16), 2 + ou_enc_unpacked [1], fp_out ); // after compression, data is written to the file frame ++; printf ("encoding % d frame \ r", frame) ;}fclose (fp_in); fclose (fp_out); return 0 ;}

Decoding, from ADPCM to PCM

# Include "stdafx. h "# include" hi_voice_api.h "HI_S32 VoiceEngineState [0x100]; int _ tmain (int argc, _ TCHAR * argv []) // decoding, from adpcm to pcm {HI_S16 encode [encoding]; // decoder outputHI_S16 encode [encoding]; // encoder inputHI_S16 encode [encoding]; // encoder outputHI_S16 coder, len; HI_S32 frame; FILE * fp_in = NULL, * fp_out = NULL; fopen_s (& fp_in, "e Npaomo. adpcm "," rb "); fopen_s (& fp_out," depaomo. pcm "," wb "); coder = 35; frame = 0; HI_VOICE_DecReset (HI_VOID *) VoiceEngineState, coder ); // decoder reset while (fread (& ou_enc_unpacked [0], sizeof (HI_S16), 2, fp_in) = 2) {len = (ou_enc_unpacked [1] & 0x00ff ); if (fread (& ou_enc_unpacked [2], sizeof (HI_S16), len, fp_in )! = (HI_U32) (len) {printf ("\ nfile end! \ N "); break;} HI_VOICE_DecodeFrame (HI_VOID *) VoiceEngineState, ou_enc_unpacked, delimiter, & len); // decode fwrite (delimiter, sizeof (HI_S16), len, fp_out); // write the decoded file frame ++; printf ("decoding % d frame \ r", frame) ;}fclose (fp_in); fclose (fp_out ); return 0 ;}

Project: Click the open link


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.