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

相关文章

使用MongoDB进行数据存储的操作流程

《使用MongoDB进行数据存储的操作流程》在现代应用开发中,数据存储是一个至关重要的部分,随着数据量的增大和复杂性的增加,传统的关系型数据库有时难以应对高并发和大数据量的处理需求,MongoDB作为... 目录什么是MongoDB?MongoDB的优势使用MongoDB进行数据存储1. 安装MongoDB

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

2.1/5.1和7.1声道系统有什么区别? 音频声道的专业知识科普

《2.1/5.1和7.1声道系统有什么区别?音频声道的专业知识科普》当设置环绕声系统时,会遇到2.1、5.1、7.1、7.1.2、9.1等数字,当一遍又一遍地看到它们时,可能想知道它们是什... 想要把智能电视自带的音响升级成专业级的家庭影院系统吗?那么你将面临一个重要的选择——使用 2.1、5.1 还是

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

Oracle数据库使用 listagg去重删除重复数据的方法汇总

《Oracle数据库使用listagg去重删除重复数据的方法汇总》文章介绍了在Oracle数据库中使用LISTAGG和XMLAGG函数进行字符串聚合并去重的方法,包括去重聚合、使用XML解析和CLO... 目录案例表第一种:使用wm_concat() + distinct去重聚合第二种:使用listagg,

高效管理你的Linux系统: Debian操作系统常用命令指南

《高效管理你的Linux系统:Debian操作系统常用命令指南》在Debian操作系统中,了解和掌握常用命令对于提高工作效率和系统管理至关重要,本文将详细介绍Debian的常用命令,帮助读者更好地使... Debian是一个流行的linux发行版,它以其稳定性、强大的软件包管理和丰富的社区资源而闻名。在使用

Python实现将实体类列表数据导出到Excel文件

《Python实现将实体类列表数据导出到Excel文件》在数据处理和报告生成中,将实体类的列表数据导出到Excel文件是一项常见任务,Python提供了多种库来实现这一目标,下面就来跟随小编一起学习一... 目录一、环境准备二、定义实体类三、创建实体类列表四、将实体类列表转换为DataFrame五、导出Da

Python实现数据清洗的18种方法

《Python实现数据清洗的18种方法》本文主要介绍了Python实现数据清洗的18种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1. 去除字符串两边空格2. 转换数据类型3. 大小写转换4. 移除列表中的重复元素5. 快速统

Python数据处理之导入导出Excel数据方式

《Python数据处理之导入导出Excel数据方式》Python是Excel数据处理的绝佳工具,通过Pandas和Openpyxl等库可以实现数据的导入、导出和自动化处理,从基础的数据读取和清洗到复杂... 目录python导入导出Excel数据开启数据之旅:为什么Python是Excel数据处理的最佳拍档