Speex 音频编解码 示例

2024-02-10 19:08
文章标签 音频 示例 编解码 speex

本文主要是介绍Speex 音频编解码 示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

地址:http://wmnmtm.blog.163.com/blog/static/38245714201110801617165/

Ubuntu下编译gcc -o speexdec speexdec.c -lspeex -logg

// demo.cpp : Defines the entry point for the console application.  
//  
#include "stdafx.h"  
#include <speex/speex.h>  
#include <stdio.h>   
#include <ostream>  #include <speex/speex_preprocess.h>  
#include <speex/speex_echo.h>   
#pragma comment(lib,"libspeexdsp.lib")   
#define FRAME_SIZE 160  
int main(int argc, char **argv)  
{  char *inFile;  FILE *fin,*fout1,*fout2,*fout3;  short in[FRAME_SIZE];  short out[FRAME_SIZE];    float input[FRAME_SIZE];  float output[FRAME_SIZE];     char cbits[200];  int nbBytes;  void *stateEncode;  void *stateDecode;  SpeexBits bitsEncode;  SpeexBits bitsDecode;     int i, tmp;  //新建一个新的编码状态在窄宽(narrowband)模式下  stateEncode = speex_encoder_init(&speex_nb_mode);  stateDecode = speex_decoder_init(&speex_nb_mode);  //设置质量为8(15kbps)  tmp=0;  speex_encoder_ctl(stateEncode, SPEEX_SET_VBR, &tmp);  float q=4;  speex_encoder_ctl(stateEncode, SPEEX_SET_VBR_QUALITY, &q);  speex_encoder_ctl(stateEncode, SPEEX_SET_QUALITY, &tmp);  inFile = argv[1];  fin = fopen("c:/demo.pcm", "rb");  fout1 = fopen("c:/demo_speex.raw", "wb");  fout2 = fopen("c:/demo1.pcm", "wb");  fout3 = fopen("c:/demo_slience.pcm", "wb");  //初始化结构使他们保存数据  speex_bits_init(&bitsEncode);  speex_bits_init(&bitsDecode);  int ret;  int j=0;  SpeexPreprocessState * m_st;  SpeexEchoState *echo_state;   m_st=speex_preprocess_state_init(160, 8000);  
//  echo_state = speex_echo_state_init(160, 8000);   int denoise = 1;  int noiseSuppress = -25;  speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_DENOISE, &denoise); //降噪  speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &noiseSuppress); //设置噪声的dB  int agc = 1;  q=24000;  //actually default is 8000(0,32768),here make it louder for voice is not loudy enough by default. 8000  speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_AGC, &agc);//增益  speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_AGC_LEVEL,&q);  int vad = 1;  int vadProbStart = 80;  int vadProbContinue = 65;  speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_VAD, &vad); //静音检测  speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_PROB_START , &vadProbStart); //Set probability required for the VAD to go from silence to voice   speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_PROB_CONTINUE, &vadProbContinue); //Set probability required for the VAD to stay in the voice state (integer percent)   while (1)  {  memset(out,0,FRAME_SIZE*sizeof(short));  //读入一帧16bits的声音  j++;  int r=fread(in, sizeof(short), FRAME_SIZE, fin);  if (r<FRAME_SIZE)  break;  //把16bits的值转化为float,以便speex库可以在上面工作  spx_int16_t * ptr=(spx_int16_t *)in;  if (speex_preprocess_run(m_st, ptr))//预处理 打开了静音检测和降噪  {  printf("speech,");  fwrite(in, sizeof(short), FRAME_SIZE, fout3);  }  else  {  printf("slience,");  fwrite(out, sizeof(short), FRAME_SIZE, fout3);  }  for (i=0;i<FRAME_SIZE;i++)  input[i]=in[i];  //清空这个结构体里所有的字节,以便我们可以编码一个新的帧  speex_bits_reset(&bitsEncode);  //对帧进行编码  ret=speex_encode(stateEncode, input, &bitsEncode);  //把bits拷贝到一个利用写出的char型数组  nbBytes = speex_bits_write(&bitsEncode, cbits, 200);  fwrite(cbits, sizeof(char), nbBytes, fout1);  printf("d,",nbBytes);  //清空这个结构体里所有的字节,以便我们可以编码一个新的帧  speex_bits_reset(&bitsDecode);  //将编码数据如读入bits  speex_bits_read_from(&bitsDecode, cbits, nbBytes);    //对帧进行解码  ret = speex_decode(stateDecode, &bitsDecode,output);  for (i=0;i<FRAME_SIZE;i++)  out[i]=output[i];  fwrite(out, sizeof(short), FRAME_SIZE, fout2);  }  //释放编码器状态量  speex_encoder_destroy(stateEncode);  //释放bit_packing结构  speex_bits_destroy(&bitsEncode);  speex_decoder_destroy(stateDecode);  //释放bit_packing结构  speex_bits_destroy(&bitsDecode);  fclose(fin);  fclose(fout1);  fclose(fout2);  fclose(fout3);  return 0;  }  


这篇关于Speex 音频编解码 示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

MySQL CTE (Common Table Expressions)示例全解析

《MySQLCTE(CommonTableExpressions)示例全解析》MySQL8.0引入CTE,支持递归查询,可创建临时命名结果集,提升复杂查询的可读性与维护性,适用于层次结构数据处... 目录基本语法CTE 主要特点非递归 CTE简单 CTE 示例多 CTE 示例递归 CTE基本递归 CTE 结

Spring AI使用tool Calling和MCP的示例详解

《SpringAI使用toolCalling和MCP的示例详解》SpringAI1.0.0.M6引入ToolCalling与MCP协议,提升AI与工具交互的扩展性与标准化,支持信息检索、行动执行等... 目录深入探索 Spring AI聊天接口示例Function CallingMCPSTDIOSSE结束语

go动态限制并发数量的实现示例

《go动态限制并发数量的实现示例》本文主要介绍了Go并发控制方法,通过带缓冲通道和第三方库实现并发数量限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录带有缓冲大小的通道使用第三方库其他控制并发的方法因为go从语言层面支持并发,所以面试百分百会问到

PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例

《PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例》词嵌入解决NLP维度灾难,捕捉语义关系,PyTorch的nn.Embedding模块提供灵活实现,支持参数配置、预训练及变长... 目录一、词嵌入(Word Embedding)简介为什么需要词嵌入?二、PyTorch中的nn.Em

Python Web框架Flask、Streamlit、FastAPI示例详解

《PythonWeb框架Flask、Streamlit、FastAPI示例详解》本文对比分析了Flask、Streamlit和FastAPI三大PythonWeb框架:Flask轻量灵活适合传统应用... 目录概述Flask详解Flask简介安装和基础配置核心概念路由和视图模板系统数据库集成实际示例Stre

Spring Bean初始化及@PostConstruc执行顺序示例详解

《SpringBean初始化及@PostConstruc执行顺序示例详解》本文给大家介绍SpringBean初始化及@PostConstruc执行顺序,本文通过实例代码给大家介绍的非常详细,对大家的... 目录1. Bean初始化执行顺序2. 成员变量初始化顺序2.1 普通Java类(非Spring环境)(

Java Spring的依赖注入理解及@Autowired用法示例详解

《JavaSpring的依赖注入理解及@Autowired用法示例详解》文章介绍了Spring依赖注入(DI)的概念、三种实现方式(构造器、Setter、字段注入),区分了@Autowired(注入... 目录一、什么是依赖注入(DI)?1. 定义2. 举个例子二、依赖注入的几种方式1. 构造器注入(Con

Spring Boot 3.x 中 WebClient 示例详解析

《SpringBoot3.x中WebClient示例详解析》SpringBoot3.x中WebClient是响应式HTTP客户端,替代RestTemplate,支持异步非阻塞请求,涵盖GET... 目录Spring Boot 3.x 中 WebClient 全面详解及示例1. WebClient 简介2.