4-4 Recording from Microphone

You can also use the MATLAB command "wavrecord" to read the audio signals from the microphone directly. The command format is
y = wavrecord(n, fs);
where "n" is number of samples to be recorded, and "fs" is the sample rate. The following example will record 2 seconds from your microphone.

Example 1: matlab4asp/wavRecord01.mfs=16000; % Sampling rate (取樣頻率) duration=2; % Recording duration (錄音時間) fprintf('Press any key to start %g seconds of recording...', duration); pause fprintf('Recording...'); y=wavrecord(duration*fs, fs); % duration*fs is the total number of sample points fprintf('Finished recording.\n'); fprintf('Press any key to play the recording...'); pause; fprintf('\n'); wavplay(y,fs);

(You need to execute the above program by yourself in order to understand the recording procedure completely.) In the above example, "duration*fs" is the number of sample points to be recorded. The recorded sample points are stored in the variable "y", which is a vector of size 32000x1. The data type of "y" is double and the memory space taken by "y" is 256,000 bytes.

Hint
You can use the command whos to show the memory usage by all variables in the work space

Hint
The commands wavplay and wavrecord are only supported in Microsoft Windows platform.

In the previous example, the number of channels is 1 and the data type for sample points is double. If we want to change these two default settings, we can introduce extra input arguments to the command "wavrecord". A detailed format of "wavrecord" is:

y = wavrecord(n, fs, channel, dataType);
where "channel" (usually 1 or 2) is the number of recording channels, and "dataType" (such as 'double', 'single', 'int16', 'uint8') is the data type of the recorded sample points. Different data types will require different amount of memory space. Example follows.

Example 2: matlab4asp/wavRecord02.mfs=16000; % Sampling rate (取樣頻率) duration=2; % Recording duration (錄音時間) channel=1; % Mono (單聲道) fprintf('Press any key to start %g seconds of recording...', duration); pause fprintf('Recording...'); y=wavrecord(duration*fs, fs, channel, 'uint8'); % duration*fs is the number of total sample points fprintf('Finished recording.\n'); fprintf('Pressy any key to hear the recording...'); pause; fprintf('\n'); wavplay(y,fs);

This example is almost the same as the previous one, except that the data type is 'uint8'. The sample points are still kept in the variable "y" with the same size 32000x1. But the elements within "y" are integers between 0 and 255. The memory space of "y" is now only 32000 bytes, which is only 1/8 of that in the previous example.

Hint
You can use class(y) to display the data type of variable "y".

The following table shows the data types supported by the command wavrecord.

Data typesSpace requirement per sampleRange of the sample data
double8 bytes/sampleReal number within [-1, 1]
single4 bytes/sampleReal number within [-1, 1]
int16 2 bytes/sampleInteger within [-32768, 32767] or [-2^(nbits-1), 2^(nbits-1)-1]
uint8 1 byte/sample Integer within [0, 255] or [0, 2^nbits-1]

Hint
It seems that "wavrecord" is phasing out according to the online help when you type "doc wavrecord". The new command for audio playback is "audiorecorder". Try "doc audiorecorder" for details.

MATLAB also offers another command "audiorecorder" to offer precision control over recording.

Example 3: matlab4asp/audiorecorder01.mfs=16000; % Sampling rate (取樣頻率) nbits=16; nChannels=1; duration=3; % Recording duration (錄音時間) arObj=audiorecorder(fs, nbits, nChannels); fprintf('Press any key to start %g seconds of recording...', duration); pause fprintf('Recording...'); recordblocking(arObj, duration); fprintf('Finished recording.\n'); fprintf('Press any key to play the recording...'); pause; fprintf('\n'); play(arObj); fprintf('Plotting the waveform...\n'); y=getaudiodata(arObj); % Get audio sample data plot(y); % Plot the waveform Press any key to start 3 seconds of recording...Recording...Finished recording. Press any key to play the recording... Plotting the waveform...


Audio Signal Processing and Recognition (音訊處理與辨識)