Acquire Continuous Audio Data

This example shows how to set up a continuous audio acquisition. This example uses a two-channel microphone.

In this example you generate data using the sound card on your computer using a 5.1 channel speaker setup. Before you begin, verify that your environment is set up so that you can generate data with your sound card. For more information refer to "Troubleshooting in Data Acquisition Toolbox".

View available audio devices

d = daq.getDevices
d = 

Data acquisition devices:

index   Vendor    Device ID                            Description                           
----- ----------- --------- -----------------------------------------------------------------
1     directsound Audio0    DirectSound Primary Sound Capture Driver
2     directsound Audio1    DirectSound Microphone (High Definition Audio Device)
3     directsound Audio2    DirectSound HP 4120 Microphone (2- HP 4120)
4     directsound Audio3    DirectSound Microphone (Plantronics .Audio 400 DSP)
5     directsound Audio4    DirectSound Digital Audio (S/PDIF) (High Definition Audio Device)
6     directsound Audio5    DirectSound Primary Sound Driver
7     directsound Audio6    DirectSound Speakers (Plantronics .Audio 400 DSP)
8     directsound Audio7    DirectSound HP 4120 (2- HP 4120)
9     directsound Audio8    DirectSound Speakers (High Definition Audio Device):1
10    directsound Audio9    DirectSound Speakers (High Definition Audio Device):2

This example uses a microphone with device ID 'Audio1'.

dev = d(2)
dev = 

directsound: DirectSound Microphone (High Definition Audio Device) (Device ID: 'Audio1')
   Audio input subsystem supports:
      -1.0 to +1.0  range
      Rates from 80.0 to 1000000.0 scans/sec
      2 channels ('1','2')
      'Audio' measurement type

Create an audio session

Create a session with directsound as the vendor and add an audio input channel to it.

s = daq.createSession('directsound');
addAudioInputChannel(s, dev.ID, 1:2);

Prepare session for continuous operation.

s.IsContinuous = true
s = 

Data acquisition session using DirectSound hardware:
   Will run continuously at 44100 scans/second until stopped.
   Number of channels: 2
      index Type Device Channel MeasurementType     Range     Name
      ----- ---- ------ ------- --------------- ------------- ----
      1     audi Audio1 1       Audio           -1.0 to +1.0
      2     audi Audio1 2       Audio           -1.0 to +1.0

Set up the plot for an FFT of the live input.

hf = figure;
hp = plot(zeros(1000,1));
T = title('Discrete FFT Plot');
xlabel('Frequency (Hz)')
grid on;

type helper_continuous_fft.m
function continuous_fft(data, Fs, plotHandle)
% Calculate FFT(data) and update plot with it. 

lengthOfData = length(data);
nextPowerOfTwo = 2 ^ nextpow2(lengthOfData); % next closest power of 2 to the length

plotScaleFactor = 4;
plotRange = nextPowerOfTwo / 2; % Plot is symmetric about n/2
plotRange = floor(plotRange / plotScaleFactor);

yDFT = fft(data, nextPowerOfTwo); % Discrete Fourier Transform of data

h = yDFT(1:plotRange);
abs_h = abs(h);

freqRange = (0:nextPowerOfTwo-1) * (Fs / nextPowerOfTwo);  % Frequency range
gfreq = freqRange(1:plotRange);  % Only plotting upto n/2 (as other half is the mirror image)

set(plotHandle, 'ydata', abs_h, 'xdata', gfreq); % Updating the plot
drawnow; % Update the plot


Add DataAvailable listener

Listener updates the figure with the FFT of the live input signal.

plotFFT = @(src, event) helper_continuous_fft(event.Data, src.Rate, hp);
hl = addlistener(s, 'DataAvailable', plotFFT);

Start acquisition

Observe that the figure updates as you use the microphone.


Wait for 10 seconds while continuing to acquire data via the microphone.


Stop the session.

s.IsContinuous = false;