如何將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.pswp.cn/news/256997.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256997.shtml
英文地址,請注明出處:http://en.pswp.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…