最新消息:iOS编程开发交流群(6906921) ,Mac.Cocoa开发交流群(7758675) 欢迎iOS/macOS开发编程爱好及学习者加入!

iOS音频开发为Wav文件写入Header

iOS 天狐 1842浏览 0评论

WAVE 文件作为多媒体中使用的声波文件格式之一,它是以RIFF格式为标准的。RIFF是英文Resource Interchange File Format的缩写,每个WAVE文件的头四个字节便是"RIFF"。WAVE文件由文件头和数据体两大部分组成。其中文件头又分为RIFF/WAV文件 标识段和声音数据格式说明段两部分。

使用

 

Abstract:
This tutorial covers the creation of a WAV (RIFF) audio file. It covers bit size, sample rate, channels, data, headers and finalizing the file. This document is designed to cover uncompressed PCM audio files, the most common type of RIFF files. This document does not cover inserting useful data into the WAV (RIFF) audio file.

What's a WAV (RIFF) File?
A WAV (RIFF) file is a multi-format file that contains a header and data. For the purposes of this document, only a simple PCM file will be explored. A WAV file contains a header and the raw data, in time format.

What's bit size?
Bit size determines how much information can be stored in a file. For most of today's purposes, bit size should be 16 bit. 8 bit files are smaller (1/2 the size), but have less resolution.

Bit size deals with amplitude. In 8 bit recordings, a total of 256 (0 to 255) amplitude levels are available. In 16 bit, a total of 65,536 (-32768 to 32767) amplitude levels are available. The greater the resolution of the file is, the greater the realistic dynamic range of the file. CD-Audio uses 16 bit samples.

What is Sample Rate?
Sample rate is the number of samples per second. CD-Audio has a sample rate of 44,100. This means that 1 second of audio has 44,100 samples. DAT tapes have a sample rate of 48,000.

When looking at frequency response, the highest frequency can be considered to be 1/2 of the sample rate.

What are Channels?
Channels are the number of separate recording elements in the data. For a real quick example, one channel is mono and two channels are stereo. In this document, both single and dual channel recordings will be discussed.

What is the data?
The data is the individual samples. An individual sample is the bit size times the number of channels. For example, a monaural (single channel), eight bit recording has an individual sample size of 8 bits. A monaural sixteen-bit recording has an individual sample size of 16 bits. A stereo sixteen-bit recording has an individual sample size of 32 bits.

Samples are placed end-to-end to form the data. So, for example, if you have four samples (s1, s2, s3, s4) then the data would look like: s1s2s3s4.

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.
Sample values are given above for a 16-bit stereo source.

So, that's the header. It shouldn't be difficult to write an application that creates the header, but in case you don't want to bother, I've included some Visual Basic code to do just that at the end of this document.

Finalizing the file
Finalizing the file is actually incredibly easy. You don't need to do anything except making sure that the file size fields are filled in correctly.

Putting it together.
For the first WAV file example, we're going to create the simplest possible file. This file will be full of zero-bit data. Zero-bit data is basically a sample with 0 amplitude. While very boring, zero-bit files are important in testing stereos. Because there is an amplitude (volume) of zero, noise induced by various components can be found.

Here's visual basic code to create the file. This code is as simple as possible, and is designed to provide a look at the process.

 

http://www.topherlee.com/software/pcm-tut-wavformat.html

 

转载请注明:天狐博客 » iOS音频开发为Wav文件写入Header

微信 OR 支付宝 扫描二维码
为天狐 打赏
非常感谢你的支持,哥会继续努力!
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址