juhara.com

  • Narrow screen resolution
  • Wide screen resolution
  • Decrease font size
  • Default font size
  • Increase font size
Home Articles Multimedia Programming Sound Recording with Wave API
Sound Recording with Wave API PDF Print E-mail
Written by Zamrony P. Juhara   
Friday, 13 November 2009 15:26
Article Index
Sound Recording with Wave API
Sound Recording Callback
Add Buffer to Driver
Recording Encapsulation Design
Recording Encapsulation Design (continued)
Implementation
Callback Implementation
Start Recording Implementation
Main Application
Short Description of WAV Format
All Pages

This short article teach you how to create simple sound recorder software with Windows Wave Application Programming Interface (Wave API).

Introduction

Actually with TMediaPlayer, you can do sound recording and it's simple. For example:

FMediaPlayer.Filename:='test.wav';
FMediaPlayer.DeviceType:=dtWaveAudio;
FMediaPlayer.StartRecording;

but we don't have access to audio data currently being recorded, so if you want to develop professional multiple track sound recorder such as Cakewalk, TMediaPlayer is obviously not an option. Why? Because TMediaPlayer was built on the top of MCI (Media Control Interface). MCI is a generic interface for playback and recording multimedia.

To record audio in Windows, we can use DirectX or Windows's built-in Multimedia API. This article will explain how to do sound recording with wavein*** functions in Windows's Multimedia API.

Source code is available to download here

In Delphi, multimedia API functions are declared in mmsystem.pas unit.

 

Opening Device

To be able to use sound card to record sound we need to open device for recording. Function we need to use is WaveInOpen().

function waveInOpen(lphWaveIn: PHWAVEIN; uDeviceID:UINT;
lpFormatEx: PWaveFormatEx;
dwCallback, dwInstance, dwFlags: DWORD): MMRESULT; stdcall;

If waveInOpen succeeds, this function returns handle (of type of HWAVEIN) in lphwavein. uDeviceID is device ID we are going to open. If we do not know what device to open, just use WAVE_MAPPER constant. lpFormatEx is our requested recording format. See "Recording Format".

dwCallback is callback mechanism that we use. Callback will be called evertytime when an event occured, for example when sound card is finished filling buffer with data, device is open and etc. This parameter is related with dwFlags flag.

If dwFlags=CALLBACK_NULL then no callback mechanism is used, if dwFlags=CALLBACK_FUNCTION then dwCallback must hold address of callback function. For info about callback, please see "Callback". If dwFlags=CALLBACK_WINDOW then dwCallback must hold handle of window to receive messages.

Messages are:

  • MM_WIM_OPEN = device is closed with waveinClose()
  • MM_WIM_CLOSE = device is open with waveinOpen()
  • MM_WIM_DATA = device is finished filling buffer with data.

There are other callback type, but usually CALLBACK_FUNCTION or CALLBACK_WINDOW are used more often.

dwInstance adalah user-defined data yang akan dikirim ke callback.

Recording Format

To set recording format, we use TWaveFormatEx record.

  tWAVEFORMATEX = packed record
wFormatTag: Word;
nChannels: Word;
nSamplesPerSec: DWORD;
nAvgBytesPerSec: DWORD;
nBlockAlign: Word;
wBitsPerSample: Word;
cbSize: Word;
end;
  • wFormatTag holds format type. For PCM (Pulse Code Modulation) format, we set with WAVE_FORMAT_PCM.
  • nChannels, number of channel used, 1=mono, 2=stereo.
  • nSamplePerSec, data sampling frequency. For PCM, we can use frequency 8000 Hz, 11025 Hz, 22050 Hz, 44100 Hz. Higher sample rate means higher sound quality and higher data size.
  • nAvgBytesPerSec, average data transfer speed needed. To calculate data transfer, we multiply nSamplesPerSec with nBlockAlign.
  • nBlockAlign is smallest data unit for a wave format. For PCM, nBlockAlign is nChannels*wBitsPerSample divided by 8 (1 byte=8 bit).
  • wBitsPerSample, number of bits for each data sample. For PCM, it must be 8 or 16. cbSize additional data size in bytes. For PCM, this field is ignored.


Last Updated on Wednesday, 18 November 2009 09:31
 

Language

IndonesianEnglish (United Kingdom)

Game Institute
DAZ3D

Is this article helpful? Help this site improve by donating. Any amount is appreciated.