First install the “Data Acquisition Toolbox Support Package For Windows Sound
Cards”. Go to Add-Ons->Get Add-Ons on the right hand side of the HOME ribbon
tab to do this. You may be asked to sign in with a Mathworks account. After
installation is complete, exit MATLAB and open it again.
Now, for the neccesary code (All lines beginning with % are comments)
%Create a data acquisition session interface object for the windows sound card
i = daq.createSession("directsound");
%To Add an audio input channel to the DAQ session object.
addAudioInputChannel(DAQ Session Object, "Device ID", "Channel ID");
%Use “daq.getDevices” at the command window to see the list of available
%channels offered by your sound card. Select either the one labeled as “Primary
%sound capture driver” or “Microphone Array”(if you use a headset/earpiece to
%record). Use the corresponding Device ID as the argument to the
%addAudioInputChanel function. The Channel ID value can be either 1 or 2.
%Below is the output of “daq.getDevices” on my PC.
%index Vendor Device ID Description
%----- ----------- --------- -------------------------------------------------------------
%1 directsound Audio0 DirectSound Primary Sound Capture Driver
%2 directsound Audio1 DirectSound Microphone Array (Realtek High
%Definition Audio)
%3 directsound Audio2 DirectSound Primary Sound Driver
%4 directsound Audio3 DirectSound Speaker/Headphone (Realtek High
%Definition Audio)
% So to add an input channel using my Primary sound capture driver, I would use
addAudioInputChannel(i, "Audio0", "1");
%Set the duration of the data acquisition
i.DurationInSeconds = 10;
%Windows sound card has standard dampling rates(e.g 8192, 11025, 16000)
%that it limits users to by default. Set this session property to false in order to
%use any sample rate between 8000 and 192000
i.UseStandardSampleRates = false;
%Set sampling frequency of audio input channel, in this 9.6kHz
i.Rate = 9600;
%Prompt to show start of recording
disp("Start Recording...");
%Start Data Acquistion
voiceData = startForeground(i);
%Prompt to show that data acquisition has ended.
disp("Recording Finished..")
% Create another DAQ session object, this one will contain the output channel
o = daq.createSession("directsound");
%From the “daq.GetDevices” output above, I’m using the “Primary Sound Driver”
%as my output channel. The relevant function is addOutputChannel(DAQ
%Session Object, “Device ID”, “Channel ID”)
%Add output chanel
addAudioOutputChannel(o, "Audio2", "1");
% As in the input session object, make sure set this property to false to enable a
%wider frequency range
o.UseStandardSampleRates = false;
%Set output channel frequency(i.e Playback frequency) in this case, 9.6kHz
o.Rate = 9600;
%Queue the earlier acquired voice data in the output channel
queueOutputData(o, voiceData);
%Prompt to show playback start
disp("Begin Playback..");
%Play the output. Note that there must be queued data in the output channel to
%be processed, and that after each execution of startForeground(), the queue is
%emptied, so to repeat playback, the data must be queued again.
startForeground(o);