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.

public Sub WriteZeroByteFile()
Dim sampleRate as Integer
Dim bitSize as Integer
Dim numChannels as Integer
Dim numSeconds as Integer
Dim fileName as String
Dim fileSize as Integer
Dim dataPos as Integer
Dim headerLength as Integer
Dim totalSamples as Integer ' Set up our parameters
sampleRate = 44100 ' CD-Quality Sound.
bitSize = 16 ' Bit Size is 16 (CD-Quality).
numChannels = 2 ' Stereo mode (2-channel).
numSeconds = 1 ' We're going to make a 1 second sample.
fileSize = 0 ' Just set it to zero for now.
fileName = "c:\temp.wav" ' Pick a temporary file name. ' Open the file. This will fail if the file exists.
Open fileName For Binary Access Write As #1 ' Write the header
Put #1, 1, "RIFF" ' RIFF marker
Put #1, 5, CInt(0) ' file-size (equals file-size - 8)
Put #1, 9, "WAVE" ' Mark it as type "WAVE"
Put #1, 13, "fmt " ' Mark the format section.
Put #1, 17, CLng(16) ' Length of format data. Always 16
Put #1, 21, CInt(1) ' Wave type PCM
Put #1, 23, CInt(2) ' 2 channels
Put #1, 25, CLng(44100) ' 44.1 kHz Sample Rate (CD-Quality)
Put #1, 29, CLng(88200) ' (Sample Rate * Bit Size * Channels) / 8
Put #1, 33, CInt(2) ' (Bit Size * Channels) / 8
Put #1, 35, CInt(16) ' Bits per sample (=Bit Size * Samples)
Put #1, 37, "data" ' "data" marker
Put #1, 41, CInt(0) ' data-size (equals file-size - 44). ' headerLength is the length of the header. It is used for offsetting
' the data position.
headerLength = 44 ' Determine the total number of samples
totalSamples = sampleRate * numSeconds ' Populate with 0 bit data.
' This isn't a good reference for creating PCM data. Since we are
' just dumping 0 bit data, we're dumping it in 32 bit chunks.
For dataPos = 1 to (totalSamples * 4) step 4
' We're doing 16-bit, so we need to write 2 bytes per channel.
' Write both channels using a 32 bit integer.
' Again, this isn't a good reference. Ignore this data writing
' process. It's useless for anything but 0 bit data.
Put #1, dataPos + headerLength, CInt(0)
Next ' Finalize the file. Write the file size to the header.
fileSize = LOF(1) ' Get the actual file size.
Put #1, 5, CLng(fileSize - 8) ' Set first file size marker.
Put #1, 41, CLng(fileSize - 44) ' Set data size marker.
Close #1 ' Close the file.
End Sub

Conclusion
This tutorial should have provided enough information to understand the WAV (RIFF) file format and to create one. Now that you've examined the creation of a WAV file, the next step is to populate it with meaningful data.

Digital Audio - Creating a WAV (RIFF) file的更多相关文章

  1. Building a Radio Listening Station to Decode Digital Audio & Police Dispatches

    On April 7, 2017, residents in Dallas, Texas, woke to the sound of emergency sirens blaring all over ...

  2. Creating a simple static file server with Rewrite--reference

    Today, I’d like to take a quick moment to demonstrate how to make a simple file server using Rewrite ...

  3. Creating your own header file in C

    终于跑起来了,含自定义 include .h 的c语言程序,超开心呀! header files contain prototypes for functions you define in a .c ...

  4. Creating a PXE Configuration File

      The PXE configuration file defines the menu displayed to the pxe client host as it boots up and co ...

  5. Digital Tutors - Creating an Action Adventure Puzzle in Unity学习笔记

    遇到的问题: 1 第11节Scripting the pressure plates中需要获取子物体的Animator组件,教程使用的语句如下: ”SwitchAnim = GetComponentI ...

  6. [NLP-ASR] 语音识别项目整理(一) 语音预处理

      简介 之前参与过114对话系统的项目,中间搁置很久,现在把之前做过的内容整理一下,一是为自己回顾,二是也希望分享自己看的内容,中间也遇到一些问题,如果您可以提一些建议将不胜感激. 114查询主要分 ...

  7. 我使用过的Linux命令之file - 检测并显示文件类型

    摘自:http://codingstandards.iteye.com/blog/804463 我使用过的Linux命令之file - 检测并显示文件类型 用途说明 file命令是用来检测并显示文件类 ...

  8. Android音频: 怎样使用AudioTrack播放一个WAV格式文件?

    翻译 By Long Luo 原文链接:Android Audio: Play a WAV file on an AudioTrack 译者注: 1. 因为这是技术文章,所以有些词句使用原文,表达更准 ...

  9. 痞子衡嵌入式:PCM编码与Waveform音频文件(.wav)格式详解

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是PCM编码及Waveform音频文件格式. 嵌入式里有时候也会和音频打交道,比如最近特别火的智能音箱产品,离不开前端的音频信号采集.降噪 ...

随机推荐

  1. iOS swift跑马灯滚动可以点击

    跑马灯,从右至左循环滚动显示信息,并且支持点击事件,使用swift4.0语法完成,更加简介,通用性强,布局部分全部使用snpkit 代码: // // HXQMarqueeView.swift // ...

  2. linux下出现ping:unknown host www.baidu.com问题时的解决办法——ubuntu下局域网络的配置

    如果ping域名的时候出现ping:unknown host  xxx.xxx 但是ping IP地址的时候可以通的话 可知是dns服务器没有配置好, 查看一下配置文件/etc/resolv.conf ...

  3. HDU 2254 奥运(矩阵高速幂+二分等比序列求和)

    HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意:  中问题不解释. 分析:  依据floyd的算法,矩阵的k次方表示这个矩阵走了k步.  所以k ...

  4. 【实验一 】Spring Boot 集成 hibernate & JPA

    转眼间,2018年的十二分之一都快过完了,忙于各类事情,博客也都快一个月没更新了.今天我们继续来学习Springboot对象持久化. 首先JPA是Java持久化API,定义了一系列对象持久化的标准,而 ...

  5. COCOS2D-HTML5 开发之二】cocos2d-html5项目定义成员,局部变量,函数笔记随笔

    本站文章均为李华明Himi原创,转载务必在明显处注明:(作者新浪微博:@李华明Himi) 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/cocos2d- ...

  6. Oracle启动中,spfile.ora、init<SID>.ora、spfile<SID>.ora 这三个文件正确的先后顺序是什么?

    Oracle启动中,spfile.ora.init<SID>.ora.spfile<SID>.ora 这三个文件正确的先后顺序是什么? 解答:启动数据库,使用startup命令 ...

  7. 【BZOJ】3396: [Usaco2009 Jan]Total flow 水流 (最大流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3396 裸的最大流跑过.. #include <cstdio> #include < ...

  8. Linux USB 鼠标输入驱动具体解释

    平台:mini2440 内核:linux 2.6.32.2 USB设备插入时.内核会读取设备信息,接着就把id_table里的信息与读取到的信息做比較.看是否匹配,假设匹配.就调用probe函数. U ...

  9. python update from 2.6 to 2.7

    1 .wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 2.tar -jxvf Python-2.7.3.tar.bz2 &am ...

  10. iOS-更新CocoaPods出现错误 提示重复文件

    当多人开发的时候,或者引入了一些别人的第三方库文件的时候,当我们再更新CocoaPods时会出现错误,错误提示有一些文件 出现重复,这个时候我们需要查看一些是什么文件出现了重复,错误提示是xxxx三方 ...