音频demo:将PCM与wav格式相互转换

1、README

a. demo说明

使用开源项目opencore-amr中test目录下的wavwriter.c/wavwriter.hwavreader.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, 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

R-QWERT

你的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值