wavfile is a simple sound library for use in CSE 20211. This library allows you to generate arbitrary sound waveforms in an array, then write them out to a standard WAV format file, which can then be played back by almost any kind of computer.

Note: As a courtesy to others in the course or the lab, please use headphones when working with sounds.

Digital Sound Primer

A computer generates digital sounds by creating a digital waveform. A waveform is simply a sequence of digital values that describe how a speaker is to be physically pulsed by an digital-to-analog (DAC) converter. Positive values in the waveform indicate the speaker is to be pushed slightly outward, creating positive air pressure, while negative values indicate the speaker is to be pulled slightly inward, creating negative air pressure.

The sampling rate of a digital sound indicates how many digital values are used per second of playback. The standard for CD-quality sound is a sampling rate of 44.1KHz. The volume of a digital sound is simply the amplitude of the waveform: bigger changes mean louder sounds.

The character and quality of a sound is entirely described by the shape of its waveform. Try clicking on the waveforms below to see what they sound like. A sine wave is smooth and open, like a flute. A square wave is piercing, like a smoke alarm. A triangle wave sounds rather brassy. The random wave sounds like white noise.

Sine Wave
Square Wave
Triangle Wave
Random Wave

A sound with a regular pattern has a fundamental frequency which is the number of peaks in the waveform per second. The sine, square, and triangle waves above all have a frequency of 440Hz, which is a concert-A pitch. (Note also that the frequency is not the same as the sampling rate.)

Most sounds in the real world are not as clean and simple as the waveforms above. Instead, they are a sum of multiple waveforms at different frequencies. Musical instruments tend to produce a strong waveform at the pitch actually played. This is known as the fundamental frequency. At the same time, they also produce quieter sounds at integer multiples of the fundamental frequency, known has harmonics.

For example, click on the three sounds below. The first is a sine wave at 440Hz. The second adds a harmonic at 880Hz. The third adds another harmonic at 1760 Hz. If you listen carefully, you will see that the sounds with more harmonics are more pleasant and more realistic. (I think it sounds like a pipe organ.)

Fundamental
Frequency
Fundamental
+ 1 Harmonic
Fundamental
+ 2 Harmonics

Getting Started

It is very easy to generate waveforms like those above to create digital sounds and (eventually) music. To get started, download the following files:

  • wavfile.h
  • wavfile.c
  • example.c
  • And compile them together as follows:

    gcc example.c wavfile.c -o example -lm

    Then, run the example program:

    ./example

    The program creates a file called sound.wav. To play back the file on Linux, use the play command:

    play sound.wav

    How it Works

    The sound library only has three functions:

    FILE * wavfile_open( const char *filename );

    wavfile_open creates a new file whose name is given by the first argument. It automatically puts the standard WAV header into the file for you. If successful, it returns a pointer to a FILE object. If unsucessful, it returns null.

    void wavfile_write( FILE *file, short data[], int length );

    wavfile_write writes data to an open file. The first argument must be a pointer to a FILE object returned by wavfile_open. The second argument is an array of waveform data, and the third argument is the number of samples to write. This function may be called multiple times to add more sounds to an open wavfile.

    void wavfile_close( FILE * file );

    wavfile_close completes writing to a wavfile. It is required to call wavfile_close when your sound is complete, otherwise the file will not be usable.

    #define WAVFILE_SAMPLES_PER_SECOND 44100

    Finally, the header contains a constant WAVFILE_SAMPLES_PER_SECOND which indicates how many samples are in a waveform per second of playback.

    Example Program

    The program example.c creates a simple wavfile that plays a sine-wave for one second. It works as follows:

    First, an array of shorts is created to hold a waveform that will last for two seconds:

    const int NUM_SAMPLES = WAVFILE_SAMPLES_PER_SECOND*2;

    short waveform[NUM_SAMPLES];

    For clarity, we define a few variables to indicate the frequency and volume of the sound. 440Hz is a concert A pitch, and the volume of the waveform is simply the amplitude, which can be up to 32768.

    double frequency = 440.0;

    int volume = 32000;

    Then, we fill the array with a sine wave of the desired frequency:

    int i;

    for(i=0;i<NUM_SAMPLES;i++) {

    double t = (double) i / WAVFILE_SAMPLES_PER_SECOND;

    waveform[i] = volume*sin(frequency*t*2*M_PI);

    }

    Finally, we use the wavfile library to write out the waveform to a file:

    FILE * f = wavfile_open("sound.wav");

    wavfile_write(f,waveform,length);

    wavfile_close(f);

    Using the simple sound library, writing out the sound file is easy. All of the challenge lies in constructing a waveform that plays the desired sound.

    转自:

    https://www3.nd.edu/~dthain/courses/cse20211/fall2013/wavfile/

    wav 格式音频文件生成例子的更多相关文章

    1. (原创)speex与wav格式音频文件的互相转换

      我们的司信项目又有了新的需求,就是要做会议室.然而需求却很纠结,要继续按照原来发语音消息那样的形式来实现这个会议的功能,还要实现语音播放的计时,暂停,语音的拼接,还要绘制频谱图等等. 如果是wav,m ...

    2. (原创)speex与wav格式音频文件的互相转换(二)

      之前写过了如何将speex与wav格式的音频互相转换,如果没有看过的请看一下连接 http://www.cnblogs.com/dongweiq/p/4515186.html 虽然自己实现了相关的压缩 ...

    3. c#使用SoundPlayer播放wav格式音频

      1.引用System.Media名称空间下的类SoundPlayer   SoundPlayer player = new SoundPlayer(); 2.方法调用Play(); public vo ...

    4. 调用CImg库显示WAV格式音频波形

      最近在做傅里叶变换和小波变换时经常要通过显示波形来检验算法,但通过visual studio之类显示波形又显得麻烦,而且不能跨平台. CImg是一个跨平台的C++的图像处理库,提供的图像处理等功能十分 ...

    5. ffmpeg 合并aac格式音频文件

      1:连接到一起 'ffmpeg - i "concat:D:\learn\audio\1.aac|D:\learn\audio\2.aac" - acodec copy D:\le ...

    6. S3C2416裸机开发系列19_Fatfs播放录像wav音频文件

      S3C2416裸机开发系列19 Fatfs播放录像wav音频文件 国际象棋男孩    1048272975 多媒体资源,一般都是以文件的形式存储在固化存储器中.Fatfs所支持的fat32为windo ...

    7. 解析WAV音频文件----》生成WAV音频文件头

      前言:请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i WAV音频文件介绍: WAV文件是在PC机平台上很常见的.最经典的多媒体音频文件,最早于1991年8月出现在Windows3.1操作系统 ...

    8. WAV格式文件无损合并&帧头数据体解析(python)(原创)

      一,百度百科 WAV为微软公司(Microsoft)开发的一种声音文件格式,它符合RIFF(Resource Interchange File Format)文件规范,用于保存Windows平台的音频 ...

    9. Android 音视频开发(一):PCM 格式音频的播放与采集

      什么是 PCM 格式 声音从模拟信号转化为数字信号的技术,经过采样.量化.编码三个过程将模拟信号数字化. 采样 顾名思义,对模拟信号采集样本,该过程是从时间上对信号进行数字化,例如每秒采集 44100 ...

    10. .NET winform播放音频文件

      前提:最近要求做一个在winform端做一个音频文件播放的功能,至此,总结最近搜寻的相关资料. 一.微软提供了三种方式来播放音频文件 1.通过System.Media.SoundPlayer来播放 2 ...

    随机推荐

    1. centos7安装protobuf|序列化和反序列化工具

      前言 那么这里博主先安利一些干货满满的专栏了! 首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助. 高质量博客汇总https://blog.cs ...

    2. P7907 [Ynoi2005] rmscne题解

      题目链接:rmscne 神仙经典数据结构难题.看到求区间种类数有关的东西,需要下意识的反应到经典老题 HH的项链,这里可以学习我这篇 题解.具体学习下扫描线怎么做这类东西的. 看看本题,首先处理区间查 ...

    3. Java - CodeForces - 469C

      题目: 现在有一个容器,里面有n个物品,编号为1-n,现在小q可以进行一些操作,每次取出任意两个数,可以把这两个数的编号相加,相减,相乘,再把结果放回容器.问最后小q能否在n-1次操作后使得容器里的唯 ...

    4. shell 两个数组比较,得到元素的并集、交集等

      linux shell 实现数组比较,取元素的并集.交集时,可以使用sort排序.uniq统计和awk数据过滤. shell 实现如下 file_list_1=("test1" & ...

    5. HBase相关面试题汇总

      1.HBase是什么? (1) HBase一个分布式的基于列式存储的数据库,基于Hadoop的hdfs存储,zookeeper进行管理. (2) HBase适合存储半结构化或非结构化数据,对于数据结构 ...

    6. JS 判断对象属性是否存在,判断是否包含某个属性,是否为自身属性

      壹 ❀ 引 看过博主JS 疫情宅在家,学习不能停,七千字长文助你彻底弄懂原型与原型链这篇文章的同学应该知道,文中有专门介绍这个问题.那么为什么我要另起一篇再说一次呢?原因有两个,一是介绍原型与原型链的 ...

    7. NC16611 [NOIP2009]最优贸易

      题目链接 题目 题目描述 C国有n个大城市和m条道路,每条道路连接这n个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这m条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向 ...

    8. Git实战系列教程

      介绍 本文详细记录了Git一系列核心概念和工作中常用的操作命令,通篇以实际出发拒绝过度理论,值得典藏:). 概念 版本管理系统 版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的 ...

    9. Navicat 12连接mysql8.x报错2059 - authentication plugin 'caching_sha2_password' 解决办法

      // %表示远程连接允许所有ip,如果只是连接本地,将%改为localhost即可 ALTER USER 'root'@'%' IDENTIFIED BY '你自己的mysql的密码' PASSWOR ...

    10. django中的一些装饰器用法

      1.require_http_methods() 1.要求视图只接受特定的请求方法 2.该装饰器允许传递多个请求方法参数,以列表的形式,请求方法名要大写 from django.views.decor ...