Multichannel Audio Input and Output

You can acquire and generate audio signals using one or more available channels of a supported audio device. You can also simultaneously operate channels on multiple supported audio devices. Currently Data Acquisition Toolbox™ supports audio channels for devices that work with DirectSound interface. Using the session-based interface, you can:

  • Acquire and generate audio signals either in sequence or as separate operations.

  • Acquire and generate signals in parallel where the signals may share the start time.

  • Acquire the data in the background and filter or process the input data simultaneously. You can generate data immediately in response to the processed input data. In this case, both the acquisition and generation operations start and stop together.

You cannot read directly from or write directly to files using the multichannel audio feature. Use audioread and audiowrite.

Multichannel Audio Session Rate

The session rate in an audio session is the rate at which the session samples audio data. All channels in a session have the same session rate. The default session rate for an audio session is 44100 Hz. If you have multiple devices in the session, make sure that they can all operate at a common session rate. For standard sample rates, see StandardSampleRates property.

You can choose a value that is in between the standard values. The toolbox will quantize the set rate to the closest standard rate. If you choose a rate outside the ranges of the standard rates, the session may use it if the device you are using supports it. To use non-standard rates you must set UseStandardSampleRates to false. You cannot set the rate below the standard minimum rate or above the standard maximum rate.

Multichannel Audio Range

Data you acquire or generate using audio channels contains double-precision values. These values are normalized to the range of -1 to +1. The session represents data acquired or generated in amplitude without units. The audio session Range property is read-only and set at [-1 1].

Acquire Multichannel Audio Data

This example shows how to acquire audio data for seven seconds and plot the data.

Discover audio devices installed on your system and create a session for DirectSound devices.

d = daq.getDevices
s = daq.createSession('directsound')

Add two audio input channels for the microphone with id Audio1. Make sure that a microphone is plugged into the appropriate jack.

addAudioInputChannel(s,'Audio1', 1:2);

Set the session to run for 7 seconds and play an audio segment for the microphone to pick up.

s.DurationInSeconds = 7

Acquire data in the foreground and plot the data versus time.

[data,t] = startForeground(s);
plot(t, data);

Generate Audio Signals

This example shows how to generate audio signals using a Session. This example uses, but does not require, a 5.1 channel sound system.

In this example you generate an audio signal 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".

Load Audio Signal

Load an audio file containing a sample of Handel's "Hallelujah Chorus."

load handel;

Plot Audio Signal

Plot data in order to identify five distinct segments. Each segment represents a "Hallelujah" in the chorus. The segments are annotated as 1 to 5.

ly = length(y);
lspan = 1:ly;
t = lspan/Fs;

hf = figure;
axis tight;
title('Signal (Handel''s Hallelujah Chorus) vs Time');
xlabel('Time (s)');

markers = struct('xpos',[0.2,0.4,0.55,0.65,0.8],'string',num2str([1:5]'));
for i = 1:5,
    annotation(hf,'textbox',[markers.xpos(i) 0.48 0.048 0.080],'String', markers.string(i),'BackgroundColor','w','FontSize',16);

View All 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 5.1 channel sound system with device ID 'Audio8'.

dev = d(9)
dev = 

directsound: DirectSound Speakers (High Definition Audio Device):1 (Device ID: 'Audio8')
   Audio output subsystem supports:
      -1.0 to +1.0  range
      Rates from 80.0 to 1000000.0 scans/sec
      8 channels ('1' - '8')
      'Audio' measurement type

Create an Audio Session

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

s = daq.createSession('directsound');
noutchan = 6;
addAudioOutputChannel(s, dev.ID, 1:noutchan);

2. Update the session rate to match the audio sampling rate.

s.Rate = Fs

3. Queue the same waveform to all available channels/speakers. If additional, different voices are available, these should be queued to the appropriate channels.


4. Start foreground generation. You should hear a sample of Handel's "Hallelujah Chorus." "Hallelujah" should be voiced five times, one for each segment depicted in the figure on all channels of the speaker system.


5. Close the figure.

s = 

Data acquisition session using DirectSound hardware:
   No data queued.  Will run at 8192 scans/second.
   Number of channels: 6
      index Type Device Channel MeasurementType     Range     Name
      ----- ---- ------ ------- --------------- ------------- ----
      1     audo Audio8 1       Audio           -1.0 to +1.0
      2     audo Audio8 2       Audio           -1.0 to +1.0
      3     audo Audio8 3       Audio           -1.0 to +1.0
      4     audo Audio8 4       Audio           -1.0 to +1.0
      5     audo Audio8 5       Audio           -1.0 to +1.0
      6     audo Audio8 6       Audio           -1.0 to +1.0