Lesson 002: Record Sound/Speech in Matlab
As discussed in previous lesson and as shown in figure 1, there are two ways to process sound or speech information using Matlab. One is by reading pre-recorded sound files or data on computer and second is by recording sound in real time and then use that data in Matlab. Prior method is used to do offline processing of speech and sound data in order to create algorithms for doing certain task, while later method is used to verify those algorithms and to perform tasks in real time. Let’s take an example of speaker recognition. In speaker recognition problem our aim is to identify specific speaker and then provide authenticity of a task to specific speaker only. You can think of this as words of a speaker are acting as the password for the valuable information.
In the above mentioned example first we need to train our system for specific user speech and that is done by extracting the features from pre-recorded voice samples and then train our system based on those features. Once our system is trained to the specific user speech then we can apply it in real time. While working is real time speech processing needs to be done with live recorded sound. Hence if we want to demonstrate such system using Matlab then we should also know about how to record sound using Matlab?
Fig. 1: Block Diagram of a typical Audio/Speech Signal Processing
Matlab uses inbuilt sound card of the computer in order to record voice or sound through microphone connected to the system. We can also record from any other sound recording devices if they are properly installed on the computer.
Record Speech/Sound in Matlab
Matlab provides various commands for Recording Device Information and Sound Recording. Speech play back commands provided by Matlab have been discussed in Lesson 001. Here, we have provided a short description of those commands. Full and complete description can be found in Matlab Help.
Info = audiodevinfo: This Command is used to identify available devices installed on computer for sound recording and sound playback. It returns the information in a form of structure containing information about the device including device ids, which are used further while recording the speech.
Recorder = audiorecorder(Fs, n, Channels) : This command is used to create arudio recorder object. It takes in desired sampling frequency for recording, no. of bits in order to digitize the sound samples and no. of channels. It returns the recorder object which is used further to record the sound.
Recordblocking(recorder, length): This command is used to record the sound for the duration specified by the length. This command does not return control until recording is done for the specified duration.
Record(recorder, length): This command also used to record the sound for specified amount of duration specified by length argument. This command returns the control prior to ending the recording time duration.
Getaudiodata(recorder): This command is used to get the recorded sound data from recorder object. We can assign the sound data received from the recorder object to any variable of our choice.
Next, there is an example code in Matlab for recording the sound.
Matlab Code Example for Recording Speech/Sound
% get device information
dev = audiodevinfo;
% create recorder object
rec = audiorecorder(44100, 16, 1);
% start recording
% record(rec, 5); % will record for 5sec. for this command pause needs to be added
% stop recording
% Play recorded sound
% get audio data
y = getaudiodata(rec);
% plot the sound
Play with this code by changing sound file name.
Difference between Recorblocking and Record Command
Recordblocking command does not return the control instantly but it holds the control until complete recording is done while record command releases the control instantly. Hence we can’t use recordblocking command if we want to record data from two different devices simultaneously in that case we have to use record command with pause command. See an example:
rec1 = audiorecorder(16000, 16, 1, 3);
rec2 = audiorecorder(44100, 16, 1, 4);
(Note: Go through the video tutorial on the same. Link is given below)