Sound Record, Save & Playback in MATLAB

Yes Matlab is also capable to handle the devices that are connected to the computer system ports. It can gather data from these devices.
In this example we will gather sound data from the microphone connected to your system. The code is optimized for the system which is having more than one audio input device connected to them. In that case, although from a single instance of the code run you will be able to collect data only from one of the device (will be selected though the device id). But if run multiple instance of MATLAB with the same code you will be able to record simultaneously from different input devices.

The following program will require you to select the device id, set the duration of the recoding, & the bit depth of recording during runtime.
The recording is happening in the sampling rate of 48kHz & dual channel, i.e., stereo. The audio data will be stored by the name 'xyz.wav'.

NOTE: you can select different sampling rate, that is supported by your recording device (or sound card) just see the microphone properties in "Control Panel".

MATLAB code for  Sound Record, Save & Playback:

clc;
clear all;
close all;
t=audiodevinfo;
%getting audio device status connected with the computer system
n=size(t.input,2); %getting especially the input device status
disp('Details Of Audio Recording device Connected:');
for i=0:n-1
    disp('Device Name:') 

    disp(t.input(1,i+1).Name) %display the audio input device name
    disp('Device id:')
    disp(i)
%diaplay the device id
end
d=input('Enter your Device ID from the above Device List to Start Recording:');
%getting the device id for recording
t=input('Enter the Duration of Recording(s):');
%getting the duration of recording
b=input('Enter Bit Depth of Recording(e.g. 8bit):'); %getting the bit depth of recording
disp('Recording at 48kHz Sampling frequency');
aud=audiorecorder(48000,b,2,d);
disp('Recording....')
recordblocking(aud,t);
myRecording = getaudiodata(aud);
%start audio recoding
plot(myRecording); %plot the recorded data
audiowrite('xyz.wav',myRecording, 48000); %write the recorded audio to a file
disp('Saving the Audio File as xyz.wav in the working directory...!');
disp('Playing....');
play(aud);
%play the recorded file


Sound Record Plot: Sound Record, Save & Playback in MATLAB
Sound Record Plot
Recorded File: As seen on Explorer for the current working directory:Sound Record, Save & Playback in MATLAB
Recorded File: As seen on Explorer for the current working directory
Sound Record, Save & Playback in MATLAB
Audio Recorder in Action


0 comments: