如何将Pcm格式的音频文件转换成Wave格式的文件

最近在做一款变声App,其中就用到了将pcm格式转wave格式,下面贴出源代码,希望带有需求的童鞋有帮助!!!这里是c++语言写的,也可以用java实现。当然java调用native函数要用到jni技术。具体jni技术自己到网路上找找资料。

Cpp文件Pcm2Wave.cpp

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "wave.h"
#include "constant.h"class Pcm2Wave
{
private: WAVE_HEAD wave_head;FILE* src_pcm_file;FILE* dest_wave_file;
public:Pcm2Wave(){ memcpy(&wave_head.riff_char, "RIFF", 4);memcpy(&wave_head.wave, "WAVE", 4);memcpy(&wave_head.fmt, "fmt ", 4);memcpy(&wave_head.fccID, "fact", 4);memcpy(&wave_head.data_field, "data", 4);wave_head.package_len = 0;wave_head.format_len = 16;wave_head.fixed =  1;wave_head.channel_number = 1;//  采样率 long int samplespersecwave_head.sample_rate = 8000 ;// 量化数 int bitpersamples=8或16wave_head.bits_per_sample = 16;//  采样一次占字节数 int blockaligwave_head.byte_per_sample = wave_head.channel_number *  wave_head.bits_per_sample / 8; //NumChannels * BitsPerSample/8//  每秒播放字节数 long int bytepersecwave_head.byte_rate =     wave_head.sample_rate * wave_head.channel_number * wave_head.bits_per_sample / 8; //SampleRate * NumChannels * BitsPerSample /8
        wave_head.id = 4;wave_head.dwSize = 0;wave_head.data_len = 0;}Pcm2Wave(WAVE_HEAD* pwave_head){if(pwave_head != NULL){memcpy(&wave_head, pwave_head, sizeof(*pwave_head));}}~Pcm2Wave(){}void set_channel(short pchannel_number){wave_head.channel_number = pchannel_number;wave_head.byte_per_sample = wave_head.channel_number *  wave_head.bits_per_sample / 8;wave_head.byte_rate =     wave_head.sample_rate * wave_head.channel_number * wave_head.bits_per_sample / 8;}void set_sample_rate(int  psample_rate){wave_head.sample_rate = psample_rate;wave_head.byte_rate =     wave_head.sample_rate * wave_head.channel_number * wave_head.bits_per_sample / 8;}void set_bits_per_sample(short pbits_per_sample){wave_head.bits_per_sample =   pbits_per_sample;wave_head.byte_per_sample = wave_head.channel_number *  wave_head.bits_per_sample / 8;wave_head.byte_rate =     wave_head.sample_rate * wave_head.channel_number * wave_head.bits_per_sample / 8;}int change(const char *srcfile, const char *dsetfile){int nFileLen = 0;int nSize = sizeof(wave_head);printf("wave的头的大小: %d\n", nSize);src_pcm_file = NULL;dest_wave_file = NULL;src_pcm_file = fopen(srcfile, "rb");if (src_pcm_file == NULL)return -1;dest_wave_file = fopen(dsetfile, "wb+");if (dest_wave_file == NULL)return -2;int nWrite = fwrite(&wave_head, 1, nSize, dest_wave_file);if (nWrite != nSize){fclose(src_pcm_file);fclose(dest_wave_file);return -3;}while( !feof(src_pcm_file)){char readBuf[4096];int nRead = fread(readBuf, 1, 4096, src_pcm_file);if (nRead > 0){fwrite(readBuf, 1, nRead, dest_wave_file); }nFileLen += nRead;}printf("文件指针位置: %d\n", ftell(dest_wave_file));//将目的文件的游标指针指向文件的开头printf("nFileLen的大小: %d\n nSize的大小:%d\n", nFileLen, nSize);fseek(dest_wave_file, 0, SEEK_SET);wave_head.package_len = nFileLen - 8 + nSize;wave_head.data_len = nFileLen;nWrite = fwrite(&wave_head, 1, nSize, dest_wave_file);if (nWrite != nSize){fclose(src_pcm_file);fclose(dest_wave_file);return -4;}fclose(src_pcm_file);fclose(dest_wave_file);return 0;        }};int main(char args [])
{Pcm2Wave pp;pp.set_sample_rate(11025);pp.change("jj.pcm", "jj.wav");return 0 ;
}

wave.h头文件

typedef struct WAVE_HEAD
{char riff_char[4];   // "RIFF" char riff_id[4]="RIFF"int package_len ;    //文件总长-8 long int size0=文总长-8char wave[4];char fmt[4];int format_len;   short fixed;      //  fmttag=0x01short channel_number;  //  channel=1 或2int   sample_rate;   //  采样率 long int samplespersecint   byte_rate;   //  每秒播放字节数 long int bytepersecshort byte_per_sample;   //  采样一次占字节数 int blockalign=声道数*量化数/8short bits_per_sample;   // 量化数 int bitpersamples=8或16char fccID[4]; // must be "fact"int  id; // must be 0x4int dwSize; // 暂时没发现有啥用char  data_field[4];   //  data_id="data"int  data_len;  //  采样数据字节数 long int size2=文长-44
    
} WAVE_HEAD;

 

转载于:https://www.cnblogs.com/wxishang1991/p/5586830.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/256997.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Dojo仪表板

Dojo它提供了一个良好的仪表板显示器&#xff0c;的影响&#xff0c;如以下&#xff1a; <!DOCTYPE html> <html> <head><title>Dojo仪表盘</title><meta charset"utf-8"><script src"http://192.168.240.186/arcgis_js…

在新的固态硬盘只装ubuntu16.04系统,重启后无启动项解决方案

方法一&#xff1a;不要使用UltraISO制作ubutnu系统启动盘&#xff0c;需要用UniversalUSBInstaller &#xff08;这个是Ubuntu官方推荐的制作u盘的软件&#xff09; 不用UltraISO这个是因为&#xff0c;装完ubuntu系统后&#xff0c;启动的时候只有黑屏幕的光标&#xff0c;是…

学习笔记(22):Python网络编程并发编程-什么是线程

立即学习:https://edu.csdn.net/course/play/24458/296436?utm_sourceblogtoedu 什么是线程&#xff1f;&#xff08;以地铁为例&#xff09; 1.北京地铁、上海地铁、广州地铁称之为三个不同的进程&#xff1b;而上海1号线上海3号线称之为线程&#xff1b; 2.进程是资源单位&a…

流程图制作在云上 https://www.processon.com/

流程图制作在云上 &#xff1a; https://www.processon.com/ 转载于:https://www.cnblogs.com/hopesun/p/4661429.html

机械硬盘变成了动态磁盘怎么压缩不了该磁盘

由于我的电脑原装是win8系统&#xff0c;自从win8系统以及以后的win10系统&#xff0c;磁盘管理方式变成了两种&#xff0c;1是基本磁盘管理&#xff1b;2是动态磁盘管理 由于本人想要在机械硬盘上压缩一个可用的绿色空间出来安装ubuntu系统&#xff1b;由于本人的三星机械100…

Exchange2013恢复已删除用户邮箱

上一篇文章我们讲到了如何恢复已禁用的邮箱。但是在微软的Exchange中&#xff0c;禁用邮箱和删除邮箱毕竟是两个截然不同的概念&#xff0c;这篇文章我们就来看看如何恢复已删除的邮箱。 1.依然是新建用户“张三”&#xff0c;然后给其发一封邮件&#xff0c;作为后续邮箱数据恢…

学习笔记(23):Python网络编程并发编程-开启线程的两种方式

立即学习:https://edu.csdn.net/course/play/24458/296437?utm_sourceblogtoedu 开启线程的两种方法&#xff1a;threading.Thread 1.方法一&#xff1a; from threading import Thread import time#方式一&#xff1a;直接使用thrending下的Thread类 def mythread(name):pri…

调用异步服务时的超时处理

主线程调用异步服务后&#xff0c;通过设定主线程的等待时间来进行超时处理。Demo如下&#xff1a;AsyncTask.cs 1 using System;2 using System.Threading.Tasks;3 using MyCSharp.ExternalService;4 5 namespace MyCSharp.Grammas6 {7 public class AsyncTask8 {9 …

linux下一个apache+tomcat负载均衡和集群

先说一下我的环境 一个ubuntu虚拟机&#xff0c; 一个apache2.2示例 两tomcat1.7示例 1.安装apacheserver sudo apt-get install apache2假设要重新启动的话&#xff0c;执行命令&#xff1a; sudo /etc/init.d/apache2 restart ubuntu下的apache会默认创建路径/var/www&#x…

JS实现css属性动画效果

html代码 <!DOCTYPE html> <html><head><meta charset"UTF-8"><title>css属性运动框架</title><style>body,div{margin: 0;padding: 0;}ul,li{list-style: none;}ul li{width: 200px;height: 100px;background: yellowgre…

学习笔记(24):Python网络编程并发编程-进程与线程的区别

立即学习:https://edu.csdn.net/course/play/24458/296438?utm_sourceblogtoedu 线程与进程的区别&#xff1a; 1.消耗区别&#xff1a;进程>线程 1&#xff09;进程运行结果 #进程运行 from multiprocessing import Process def task(name):print("我是%s"%na…

Codeforces Beta Round #1

A /*************************************************************************> File Name: A.cpp> Author: opas_chenxin> Mail: 1017370773qq.com > Created Time: 2016年05月06日 星期五 02时22分23秒 ************************************************…

windows系统中创建线程常用哪几个函数,有什么区别?

windows系统中创建线程常用哪几个函数&#xff1f; 在windows系统中创建线程的函数有&#xff1a; CreadThread&#xff0c;AfxBeginThread&#xff0c;_beginthread&#xff0c;_beginthreadex。 他们的区别在于使用的场景不同。 CreateThread是Windows的API函数(SDK函数的标…

java.lang.String小测试

还记得java.lang.String么&#xff0c;如果现在给你一个小程序&#xff0c;你能说出它的结果么 1 public static String ab(String a){2 return a "b";3 }4 5 public static void testAb(){6 String x "a";7 ab…

Oracle传输表空间

一、简介 可传输表空间(还有个集)最大的优势是其速度比export/import或unload/load要快的多。因为可传输表空间主要是复制数据文件到目标路径&#xff0c;然后再使用export/import或Data Pump export/import等应用仅导出/导入表空间对象的元数据到新数据库。 &#xff08;1&…

进程控制块PCB结构体 task_struct 描述

进程控制块&#xff0c;英文名&#xff08;Processing Control Block&#xff09;&#xff0c;简称 PCB 。 进程控制块是系统为了管理进程设置的一个专门的数据结构&#xff0c;主要表示进程状态。 每一个进程都对应一个PCB来维护进程相关的信息&#xff1b; 在Linux中&#xf…

基于OpenCV与tensorflow实现实时手势识别

基于OpenCV与tensorflow object detection API使用迁移学习&#xff0c;基于SSD模型训练实现手势识别完整流程&#xff0c;涉及到数据集收集与标注、VOC2012数据集制作&#xff0c;tfrecord数据生成、SSD迁移学习与模型导出&#xff0c;OpenCV摄像头实时视频流读取与检测处理&a…

学习笔记(25):Python网络编程并发编程-Thread其他属性和守护线程

立即学习:https://edu.csdn.net/course/play/24458/296440?utm_sourceblogtoedu 1.Thread其他的属性 CurrentThread().getName():获得当前线程的名字 t.is_Alive:判断线程是否还在运行 t.join():等待线程运行结束再执行join后面的代码 t.setName("name"):为线程…

AIX主机信任关系配置

1.配置主机信任关系的时候&#xff0c;需要先在两台主机/etc/hosts文件中添加要信任主机的&#xff29;&#xff30;&#xff0c;假设有(192.168.8.190 aix190,192.168.8.191 aix191)2个主机,在191主机配置如下所示 192.168.8.190 aix190 2.如果系统配置DNS&#xff0c;则需要去…

Sprint2团队贡献分

团队贡献分&#xff1a; 郭志豪&#xff1a;31% http://www.cnblogs.com/gzh13692021053/ 杨子健&#xff1a;22%http://www.cnblogs.com/yzj666/ 谭宇森&#xff1a;23%http://www.cnblogs.com/yzj666/ 刘森松&#xff1a;24% http://www.cnblogs.com/lssh/转载于:https://w…