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

相关文章

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

linux-基础知识3

打包和压缩 zip 安装zip软件包 yum -y install zip unzip 压缩打包命令: zip -q -r -d -u 压缩包文件名 目录和文件名列表 -q:不显示命令执行过程-r:递归处理,打包各级子目录和文件-u:把文件增加/替换到压缩包中-d:从压缩包中删除指定的文件 解压:unzip 压缩包名 打包文件 把压缩包从服务器下载到本地 把压缩包上传到服务器(zip

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor