Linux speex音频库-音频数据编解码

2024-09-07 04:32

本文主要是介绍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音频库-音频数据编解码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144059

相关文章

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

Linux命令之firewalld的用法

《Linux命令之firewalld的用法》:本文主要介绍Linux命令之firewalld的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux命令之firewalld1、程序包2、启动firewalld3、配置文件4、firewalld规则定义的九大

Linux之计划任务和调度命令at/cron详解

《Linux之计划任务和调度命令at/cron详解》:本文主要介绍Linux之计划任务和调度命令at/cron的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux计划任务和调度命令at/cron一、计划任务二、命令{at}介绍三、命令语法及功能 :at

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Linux内核参数配置与验证详细指南

《Linux内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1