1、README
a. demo说明
使用开源项目opencore-amr中test目录下的wavwriter.c/wavwriter.h
和wavreader.c/wavreader.h
文件,将PCM和WAV格式的音频文件进行相互转换。
(注:PCM只是WAV封装规范所支持的其中一种格式,两者不是等价关系。此程序中的wavwriter只支持PCM(format=0x0001)。)
b. 编译
$ make # 或者交叉编译 make CC=your-crosscompile-gcc
$ ls -l pcm2wav wav2pcm # 编译生成的文件
c. 使用
$ ./pcm2wav ./audio/test_8000_16_1.pcm 8000 16 1 ./out_8000_16_1.wav
$ ./pcm2wav ./audio/test_44100_16_2.pcm 44100 16 2 ./out_44100_16_2.wav
$ ./wav2pcm ./audio/test_8000_16_1.wav ./out_8000_16_1.pcm
$ ./wav2pcm ./audio/test_44100_16_2.wav ./out_44100_16_2.pcm
d. 参考文章
e. 目录架构
$ tree
.
├── audio
│ ├── test_44100_16_2.pcm
│ ├── test_44100_16_2.wav
│ ├── test_8000_16_1.pcm
│ └── test_8000_16_1.wav
├── docs
│ ├── PCM音频数据 - 简书.mhtml
│ ├── WAV文件格式分析.pdf
│ └── wav文件格式分析与详解 - nigaopeng - 博客园.mhtml
├── Makefile
├── README.md
├── src_pcm2wav
│ ├── main.c
│ ├── wavwriter.c
│ └── wavwriter.h
└── src_wav2pcm
├── main.c
├── wavreader.c
└── wavreader.h
2、主要代码片段
src_pcm2wav/wavwriter.c
/* ------------------------------------------------------------------
* Copyright (C) 2009 Martin Storsjo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
* -------------------------------------------------------------------
*/
#include "wavwriter.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
struct wav_writer {
FILE *wav;
int data_length;
int sample_rate;
int bits_per_sample;
int channels;
};
static void write_string(struct wav_writer* ww, const char *str) {
fputc(str[0], ww->wav);
fputc(str[1], ww->wav);
fputc(str[2], ww->wav);
fputc(str[3], ww->wav);
}
static void write_int32(struct wav_writer* ww, int value) {
fputc((value >> 0) & 0xff, ww->wav);
fputc((value >> 8) & 0xff, ww->wav);
fputc((value >> 16) & 0xff, ww->wav);
fputc((value >> 24) & 0xff, ww->wav);
}
static void write_int16(struct wav_writer* ww, int value) {
fputc((value >> 0) & 0xff, ww->wav);
fputc((value >> 8) & 0xff, ww->wav);
}
static void write_header(struct wav_writer* ww, int length) {
int bytes_per_frame, bytes_per_sec;
write_string(ww, "RIFF");
write_int32(ww, 4 + 8 + 16 + 8 + length);
write_string(ww, "WAVE");
write_string(ww, "fmt ");
write_int32(ww, 16);
bytes_per_frame = ww->bits_per_sample/8*ww->channels;
bytes_per_sec = bytes_per_frame*ww->sample_rate;
write_int16(ww, 1); // Format
write_int16(ww, ww->channels); // Channels
write_int32(ww, ww->sample_rate); // Samplerate
write_int32(ww, bytes_per_sec); // Bytes per sec
write_int16(ww, bytes_per_frame); // Bytes per frame
write_int16(ww, ww->bits_per_sample); // Bits per sample
write_string(ww, "data");
write_int32(ww, length);
}
void* wav_write_open(const char *filename, int sample_rate, int bits_per_sample, int channels) {
struct wav_writer* ww = (struct wav_writer*) malloc(sizeof(*ww));
memset(ww, 0, sizeof(*ww));
ww->wav = fopen(filename, "wb");
if (ww->wav == NULL) {
free(ww);
return NULL;
}
ww->data_length = 0;
ww->sample_rate = sample_rate;
ww->bits_per_sample = bits_per_sample;
ww->channels = channels;
write_header(ww, ww->data_length);
return ww;
}
void wav_write_close(void* obj) {
struct wav_writer* ww = (struct wav_writer*) obj;
if (ww->wav == NULL) {
free(ww);
return;
}
fseek(ww->wav, 0, SEEK_SET);
write_header(ww, ww->data_length);
fclose(ww->wav);
free(ww);
}
void wav_write_data(void* obj, const unsigned char* data, int length) {
struct wav_writer* ww = (struct wav_writer*) obj;
if (ww->wav == NULL)
return;
fwrite(data, length, 1, ww->wav);
ww->data_length += length;
}
src_pcm2wav/main.c
#include <stdio.h>
#include <stdlib.h>
#include "wavwriter.h"
#define BUF_SIZE 1024
int main(int argc, char *argv[])
{
FILE *fpPcm = NULL;
void *vpWav = NULL;
unsigned char *pucBuf = (unsigned char *)malloc(BUF_SIZE);
if(argc != 6)
{
printf("Usage: %s <input.pcm> <pcm sample rate> <pcm sample bits> <pcm sample channels> <output.wav>\n", argv[0]);
printf("examples: \n"
" %s ./audio/test_8000_16_1.pcm 8000 16 1 ./out_8000_16_1.wav\n"
" %s ./audio/test_44100_16_2.pcm 44100 16 2 ./out_44100_16_2.wav\n",
argv[0], argv[0]);
return -1;
}
printf("\n**************************************\n"
"input: \n"
"\t file name: %s\n"
"\t sample rate: %d Hz\n"
"\t sample bits: %d bits\n"
"\t channels: %d\n"
"output: \n"
"\t file name: %s\n"
"**************************************\n\n",
argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[5]);
/* PCM操作 1/3:打开文件 */
fpPcm = fopen(argv[1], "rb");
if(fpPcm == NULL)
{
perror("argv[1]");
return -1;
}
/* wav操作 1/3:以特定参数打开文件(目前支持PCM的音频数据格式) */
/* 参数: wav文件名 采样率 采样位数 声道数 */
vpWav = wav_write_open(argv[5], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
if(vpWav == NULL)
{
printf("wav_write_open(...) error!\n");
free(pucBuf);
fclose(fpPcm);
return -1;
}
while(1)
{
/* PCM操作 2/3:读取PCM数据 */
int iReadBytes = 0;
iReadBytes = fread(pucBuf, 1, BUF_SIZE, fpPcm);
if(iReadBytes <= 0)
{
break;
}
/* wav操作 2/3:写入数据 */
/* 参数: 句柄 数据 数据长度 */
wav_write_data(vpWav, pucBuf, iReadBytes);
}
/* wav操作 3/3:关闭文件 */
wav_write_close(vpWav);
/* PCM操作 3/3:关闭文件 */
fclose(fpPcm);
free(pucBuf);
printf("\n\033[32m%s -> %s Success!\033[0m\n", argv[1], argv[5]);
return 0;
}
src_wav2pcm/wavreader.c
/* ------------------------------------------------------------------
* Copyright (C) 2009 Martin Storsjo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
* -------------------------------------------------------------------
*/
#include "wavreader.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define TAG(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
struct wav_reader {
FILE *wav;
uint32_t data_length;
int format;
int sample_rate;
int bits_per_sample;
int channels;
int byte_rate;
int block_align;
};
static uint32_t read_tag(struct wav_reader* wr) {
uint32_t tag = 0;
tag = (tag << 8) | fgetc(wr->wav);
tag = (tag << 8) | fgetc(wr->wav);
tag = (tag << 8) | fgetc(wr->wav);
tag = (tag << 8) | fgetc(wr->wav);
return tag;
}
static uint32_t read_int32(struct wav_reader* wr) {
uint32_t value = 0;
value |= fgetc(wr->wav) << 0;
value |= fgetc(wr->wav) << 8;
value |= fgetc(wr->wav) << 16;
value |= fgetc(wr->wav) << 24;
return value;
}
static uint16_t read_int16(struct wav_reader* wr) {
uint16_t value = 0;
value |= fgetc(wr->wav) << 0;
value |= fgetc(wr->wav) << 8;
return value;
}
void* wav_read_open(const char *filename) {
struct wav_reader* wr = (struct wav_reader*) malloc(sizeof(*wr));
long data_pos = 0;
memset(wr, 0, sizeof(*wr));
wr->wav = fopen(filename, "rb");
if (wr->wav == NULL) {
free(wr);
return NULL;
}
while (1) {
uint32_t tag, tag2, length;
tag = read_tag(wr);
if (feof(wr->wav))
break;
length = read_int32(wr);
if (tag != TAG('R', 'I', 'F', 'F') || length < 4) {
fseek(wr->wav, length, SEEK_CUR);
continue;
}
tag2 = read_tag(wr);
length -= 4;
if (tag2 != TAG('W', 'A', 'V', 'E')) {
fseek(wr->wav, length, SEEK_CUR);
continue;
}
// RIFF chunk found, iterate through it
while (length >= 8) {
uint32_t subtag, sublength;
subtag = read_tag(wr);
if (feof(wr->wav))
break;
sublength = read_int32(wr);
length -= 8;
if (length < sublength)
break;
if (subtag == TAG('f', 'm', 't', ' ')) {
if (sublength < 16) {
// Insufficient data for 'fmt '
break;
}
wr->format = read_int16(wr);
wr->channels = read_int16(wr);
wr->sample_rate = read_int32(wr);
wr->byte_rate = read_int32(wr);
wr->block_align = read_int16(wr);
wr->bits_per_sample = read_int16(wr);
} else if (subtag == TAG('d', 'a', 't', 'a')) {
data_pos = ftell(wr->wav);
wr->data_length = sublength;
fseek(wr->wav, sublength, SEEK_CUR);
} else {
fseek(wr->wav, sublength, SEEK_CUR);
}
length -= sublength;
}
if (length > 0) {
// Bad chunk?
fseek(wr->wav, length, SEEK_CUR);
}
}
fseek(wr->wav, data_pos, SEEK_SET);
return wr;
}
void wav_read_close(void* obj) {
struct wav_reader* wr = (struct wav_reader*) obj;
fclose(wr->wav);
free(wr);
}
int wav_get_header(void* obj, int* format, int* channels, int* sample_rate, int* bits_per_sample, unsigned int* data_length) {
struct wav_reader* wr = (struct wav_reader*) obj;
if (format)
*format = wr->format;
if (channels)
*channels = wr->channels;
if (sample_rate)
*sample_rate = wr->sample_rate;
if (bits_per_sample)
*bits_per_sample = wr->bits_per_sample;
if (data_length)
*data_length = wr->data_length;
return wr->format && wr->sample_rate;
}
int wav_read_data(void* obj, unsigned char* data, unsigned int length) {
struct wav_reader* wr = (struct wav_reader*) obj;
int n;
if (wr->wav == NULL)
return -1;
if (length > wr->data_length)
length = wr->data_length;
n = fread(data, 1, length, wr->wav);
wr->data_length -= length;
return n;
}
src_wav2pcm/main.c
#include <stdio.h>
#include <stdlib.h>
#include "wavreader.h"
#define BUF_SIZE 1024
int main(int argc, char *argv[])
{
int format = 0;
int channels = 0;
int sample_rate = 0;
int bits_per_sample = 0;
int data_length = 0;
void *vpWav = NULL;
FILE *fpPcm = NULL;
unsigned char *pucBuf = (unsigned char *)malloc(BUF_SIZE);
int iReadBytes = 0;
if(argc != 3)
{
printf("Usage: %s <input.wav> <output.pcm>\n", argv[0]);
printf("examples: \n"
" %s ./audio/test_8000_16_1.wav ./out_8000_16_1.pcm\n"
" %s ./audio/test_44100_16_2.wav ./out_44100_16_2.pcm\n",
argv[0], argv[0]);
return -1;
}
/* PCM操作 1/3:打开文件 */
fpPcm = fopen(argv[2], "wb");
if(fpPcm == NULL)
{
perror("argv[2]");
return -1;
}
/* wav操作 1/4:打开文件 */
vpWav = wav_read_open(argv[1]);
if(vpWav == NULL)
{
printf("wav_read_open(...) error!\n");
fclose(fpPcm);
free(pucBuf);
return -1;
}
/* wav操作 2/4(可选):获取文件头部--音频数据信息 */
wav_get_header(vpWav, &format, &channels, &sample_rate, &bits_per_sample, &data_length);
printf("\n**************************************\n"
"%s info: \n"
"\t format: %s\n"
"\t sample rate: %d Hz\n"
"\t sample bits: %d bits\n"
"\t chanels: %d\n"
"\t data length: %d bytes\n"
"**************************************\n\n",
argv[1], (format == 1) ? "PCM" : "unknown", /* 注:PCM只是wav支持的其中一种格式 */
sample_rate, bits_per_sample, channels, data_length);
while(1)
{
/* wav操作 3/4:读取数据 */
iReadBytes = wav_read_data(vpWav, pucBuf, BUF_SIZE);
if(iReadBytes <= 0)
{
break;
}
/* PCM操作 2/3:写入数据 */
fwrite(pucBuf, 1, iReadBytes, fpPcm);
}
/* wav操作 4/4:关闭文件 */
wav_read_close(vpWav);
/* PCM操作 3/3:关闭文件 */
fclose(fpPcm);
free(pucBuf);
printf("\n\033[32m%s -> %s Success!\033[0m\n", argv[1], argv[2]);
return 0;
}