本文主要是介绍Linux speex音频库-音频数据编解码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
speex音频数据编解码
- speex简述
- speex encoder(编码器)
- speex decoder(解码器)
- denoise vad (降噪,语音活性检测)
speex简述
speex官网
Speex: A Free Codec For Free Speech
Overview
Speex is an Open Source/Free Software patent-free audio compression format designed for speech. The Speex Project aims to lower the barrier of entry for voice applications by providing a free alternative to expensive proprietary speech codecs. Moreover, Speex is well-adapted to Internet applications and provides useful features that are not present in most other codecs. Finally, Speex is part of the GNU Project and is available under the revised BSD license.
The Technology
Speex is based on CELP and is designed to compress voice at bitrates ranging from 2 to 44 kbps. Some of Speex’s features include:
Narrowband (8 kHz), wideband (16 kHz), and ultra-wideband (32 kHz) compression in the same bitstream
Intensity stereo encoding
Packet loss concealment
Variable bitrate operation (VBR)
Voice Activity Detection (VAD)
Discontinuous Transmission (DTX)
Fixed-point port
Acoustic echo canceller
Noise suppression
1
2
3
4
5
6
7
8
9
Note that Speex has a number of features that are not present in other codecs, such as intensity stereo encoding, integration of multiple sampling rates in the same bitstream (embedded coding), and a VBR mode; see our comparison page for more.
Getting Involved
One of the simplest things you can do to get involved in Speex is by using it in your application; Speex is well-suited to handle VoIP, internet audio streaming, data archival (like voice mail), and audio books. Currently, LinPhone, Ekiga, and Asterisk are some of the projects currently using Speex. For a list of projects with Speex support, visit our Plugins & Software page.
If you have questions or are interested in contributing to the project, have a look at our roadmap, join our mailing list, or send us money so we can keep working on Speex. You can also contact the Project Lead, Jean-Marc Valin (though the mailing is usually the best place to ask questions).
Patches can be sent to the mailing list, and should apply on the latest master branch.
speex encoder(编码器)
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>#include <speex/speex.h>
#include <speex/speex_header.h>
#include <speex/speex_stereo.h>/*The frame size in hardcoded for this sample code but it doesn’t have to be*/
#define FRAME_SIZE 160
int main(int argc, char **argv)
{char *inFile;FILE *fin;short in[FRAME_SIZE];float input[FRAME_SIZE];char cbits[200];int nbBytes;if (argc < 2){printf("Usge: speexenc in.wav >> out.wav\n");exit(0);}/*Holds the state of the encoder*/void *state;/*Holds bits so they can be read and written to by the Speex routines*/SpeexBits bits;int i, tmp;/*Create a new encoder state in narrowband mode*/state = speex_encoder_init(&speex_nb_mode);/*Set the quality to 8 (15 kbps)*/tmp = 8;speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);inFile = argv[1];fin = fopen(inFile, "r");if(!fin){printf("open file error.\n");}/*Initialization of the structure that holds the bits*/speex_bits_init(&bits);while (1){/*Read a 16 bits/sample audio frame*/fread(in, sizeof(short), FRAME_SIZE, fin);if(feof(fin))break;/*Copy the 16 bits values to float so Speex can work on them*/for (int i=0; i<FRAME_SIZE; i++){input[i] = in[i];}/*Flush all the bits in the struct so we can encode a new frame*/speex_bits_reset(&bits);/*Encode the frame*/speex_encode(state, input, &bits);/*Copy the bits to an array of char that can be written*/nbBytes = speex_bits_write(&bits, cbits, 200);/*Write the size of the frame first. This is what sampledec expects butit’s likely to be different in your own application*/fwrite(&nbBytes, sizeof(int), 1, stdout);/*Write the compressed data*/fwrite(cbits, 1, nbBytes, stdout);}/*Destroy the encoder state*/speex_encoder_destroy(state);/*Destroy the bit-packing struct*/speex_bits_destroy(&bits);fclose(fin);return 0;}
gcc speexenc.c -o speexenc -lspeex
./speexenc in.wav >> out.wav
speex decoder(解码器)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <speex/speex.h>/*The frame size in hardcoded for this sample code but it doesn’t have to be*/
#define FRAME_SIZE 160int main(int argc, char **argv)
{char *outFile;char *inFile;FILE *fout;FILE *fin;/*Holds the audio that will be written to file (16 bits per sample)*/short out[FRAME_SIZE];/*Speex handle samples as float, so we need an array of floats*/float output[FRAME_SIZE];char cbits[200];int nbBytes;/*Holds the state of the decoder*/void *state;/*Holds bits so they can be read and written to by the Speex routines*/SpeexBits bits;int i, tmp;/*Create a new decoder state in narrowband mode*/state = speex_decoder_init(&speex_nb_mode);/*Set the perceptual enhancement on*/ tmp = 1;speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);outFile = argv[1];fout = fopen(outFile, "w");if (!fout){printf("open out file error\n");}inFile = argv[2];fin = fopen(inFile, "r");if (!fin){printf("open in file error\n");}/*Initialization of the structure that holds the bits*/speex_bits_init(&bits);while (1){/*Read the size encoded by sampleenc, this part will likely be different in your application*/fread(&nbBytes, sizeof(int), 1, fin);fprintf(stderr, "nbBytes: %d\n", nbBytes);if(feof(fin)){break;}/*Read the "packet" encoded by sampleenc*/fread(cbits, 1, nbBytes, fin);/*Copy the data into the bit-stream struct*/speex_bits_read_from(&bits, cbits, nbBytes);/*Decode the data*/speex_decode(state, &bits, output);/*Copy from float to short (16 bits) for output*/for (i=0;i<FRAME_SIZE;i++)out[i]=output[i];/*Write the decoded audio to file*/fwrite(out, sizeof(short), FRAME_SIZE, fout);}/*Destroy the decoder state*/speex_decoder_destroy(state);/*Destroy the bit-stream truct*/speex_bits_destroy(&bits);fclose(fout);return 0;
}
gcc speexdnc.c -o speexdnc -lspeex
./speexdnc new.wav out.wav
denoise vad (降噪,语音活性检测)
VAD:语音活性检测 (Voice activity detection)
#include <speex/speex.h>
#include <speex/speex_preprocess.h>
#include <stdio.h>
#include <ogg/ogg.h>#define FRAME_SIZE 1152
#define FRAME_SAMPLERATE 32000
#define DENOISE_DB (-20)int main(int argn, char* argv[])
{char* szInFilename = NULL;char* szOutFilename = NULL;FILE* pInFileHandle = NULL;FILE* pOutFileHandle = NULL;short in[FRAME_SAMPLERATE];int i;SpeexPreprocessState *st;int count=0;float f;printf("starting....\r\n");if(argn != 3){printf("please input 2 parameters\r\n");return -1;}szInFilename = argv[1];szOutFilename = argv[2];pInFileHandle = fopen(szInFilename, "rb");if(!pInFileHandle){printf("open file %s error\r\n", szInFilename);return -2;}pOutFileHandle = fopen(szOutFilename, "wb");if(!pOutFileHandle){printf("open file %s error\r\n", szOutFilename);fclose(pInFileHandle);return -3;}st = speex_preprocess_state_init(FRAME_SIZE, FRAME_SAMPLERATE);int denoise = 1;int noiseSuppress = DENOISE_DB;speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_DENOISE, &denoise); //降噪speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &noiseSuppress); //设置噪声的dBi=0;speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_AGC, &i);i=8000;speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_AGC_LEVEL, &i);i=0;speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_DEREVERB, &i);f=.0;speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &f);f=.0;speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &f);int vad = 1;int vadProbStart = 80;int vadProbContinue = 65;speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_VAD, &vad); //静音检测speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_PROB_START , &vadProbStart); //Set probability required for the VAD to go from silence to voicespeex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_PROB_CONTINUE, &vadProbContinue); //Set probability required for the VAD to stay in the voice state (integer percent)while (1){int vad;int iLen = fread(in, sizeof(short), FRAME_SIZE, pInFileHandle);if(iLen <= 0){break;}if (feof(pInFileHandle))break;vad = speex_preprocess_run(st, in);if(vad != 0){printf("speech.\r\n");fwrite(in, sizeof(short), FRAME_SIZE, pOutFileHandle);}else{printf("slience---------\r\n");fwrite(in, sizeof(short), FRAME_SIZE, pOutFileHandle);}count++;}speex_preprocess_state_destroy(st);fclose(pInFileHandle);fclose(pOutFileHandle);return 0;
}
这篇关于Linux speex音频库-音频数据编解码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!