android音频裁剪(2)——Wav裁剪

=====原创不易,请尊重每一位原创,让我们更有分享的动力,转载请注明=====

转载链接

android音频裁剪(1)——MP3裁剪一文中我分享了对mp3文件裁剪方法。在本文中我将分享对另外一种音频格式——wav格式音频的裁剪。不同于mp3格式的裁剪,对于wav裁剪并不是通过android提供的多媒体库对音频进行处理,而是直接通过java代码对wav音频进行裁剪,所以以下方法对于wav音频裁剪具有平台通用性。
俗话说的好,知己知彼,百战不殆,要对wav文件进行处理,首先要去详细了解wav的文件结构。关于wav文件结构的网上资源很多,但是很杂乱,这里推荐一个地址,http://www.topherlee.com/software/pcm-tut-wavformat.html,个人感觉这里说的算是最简单明了的了。下图是一个该资料的一个关键部分。
What is the header?
The header is the beginning of a WAV (RIFF) file. The header is used to provide specifications on the file type, sample rate, sample size and bit size of the file, as well as its overall length.

The header of a WAV (RIFF) file is 44 bytes long and has the following format:

Positions Sample Value Description
1 - 4 “RIFF” Marks the file as a riff file. Characters are each 1 byte long.
5 - 8 File size (integer) Size of the overall file - 8 bytes, in bytes (32-bit integer). Typically, you’d fill this in after creation.
9 -12 “WAVE” File Type Header. For our purposes, it always equals “WAVE”.
13-16 “fmt “ Format chunk marker. Includes trailing null
17-20 16 Length of format data as listed above
21-22 1 Type of format (1 is PCM) - 2 byte integer
23-24 2 Number of Channels - 2 byte integer
25-28 44100 Sample Rate - 32 byte integer. Common values are 44100 (CD), 48000 (DAT). Sample Rate = Number of Samples per second, or Hertz.
29-32 176400 (Sample Rate * BitsPerSample * Channels) / 8.
33-34 4 (BitsPerSample * Channels) / 8.1 - 8 bit mono2 - 8 bit stereo/16 bit mono4 - 16 bit stereo
35-36 16 Bits per sample
37-40 “data” “data” chunk header. Marks the beginning of the data section.
41-44 File size (data) Size of the data section.

表格说得很清楚,如果还有疑惑,也可以看看这位朋友的分享http://blog.csdn.net/bluesoal/article/details/932395。当了解了wav文件的结构之后,再去处理wav文件就变得轻而易举。类似bmp图片的处理,bmp图片的骨骼精髓就在于它的header,wav文件也是一样,当获取了Wav的文件头,就基本知道了整个wav文件架构。所以希望各位朋友先了解wav结构之后再往下看代码。

既然说到裁剪,肯定是有一个新的裁剪得到的文件,按照正常的产品需求来说,裁剪后的音频文件除了时间长度和大小之外其他信息都应该和源音频保持一致。所以生成裁剪文件的时候肯定需要先读取原始音频的header.所以首先要定义一个header类。

public class WavHeader {

    //RITF标志
    public String mRitfWaveChunkID;
    //wav文件大小(总大小-8)
    public int mRitfWaveChunkSize;
    //wav格式
    public String mWaveFormat;

    //格式数据块ID:值为"fmt "(注意后面有个空格)
    public String mFmtChunk1ID;
    //格式数据块大小,一般为16
    public int mFmtChunkSize;
    //数据格式,一般为1,表示音频是pcm编码
    public short mAudioFormat;
    //声道数
    public short mNumChannel;
    //采样率
    public int mSampleRate;
    //每秒字节数
    public int mByteRate;
    //数据块对齐单位
    public short mBlockAlign;
    //采样位数
    public short mBitsPerSample;

    //data块,音频的真正数据块
    public String mDataChunkID;
    //音频实际数据大小
    public int mDataChunkSize;
}

上面的数据结构是按照之前表格定义的,因为wav中的FACT数据块不是必须的,所以在数据结构中并没有关于她们的定义,但对于一半的wav音频,上面header的数据结构已经是完备。下面是一个完整的裁剪流程:
1,创建文件流,

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值