MATLAB Examples

Generating Multichannel-Audio

This example shows how to set up continuous audio generation using multiple audio channels in a session. The signal, a sample of Handel's "Hallelujah Chorus" is broken up into contiguous segments and played back in two parts. The first part of the example plays each segment on a single speaker and a sub-woofer. The second part plays each segment on a different set of speakers (a choir of voices).

Contents

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".

Load audio data

Load Handel's "Hallelujah".

Load variables:

  • y representing the Hallelujah waveform
  • Fs representing the sampling frequency
load handel;

Create an audio session

View audio devices available to your system

d = daq.getDevices
%
% Create a data acquisition session using |directSound| as the vendor ID.
% DirectSound sound cards are available on all windows platforms.
%
s = daq.createSession('directsound')
d = 

Data acquisition devices:

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

s = 

Data acquisition session using DirectSound hardware:
   Will run for 1 second (44100 scans) at 44100 scans/second.
   No channels have been added.
   

Add channels and adjust rate to match the audio sampling frequency.

Add six audio output channels and set the session rate to the audio
sampling rate.
addAudioOutputChannel(s, 'Audio7', 1:6);
s.Rate = Fs
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 Audio7 1       Audio           -1.0 to +1.0
      2     audo Audio7 2       Audio           -1.0 to +1.0
      3     audo Audio7 3       Audio           -1.0 to +1.0
      4     audo Audio7 4       Audio           -1.0 to +1.0
      5     audo Audio7 5       Audio           -1.0 to +1.0
      6     audo Audio7 6       Audio           -1.0 to +1.0
   

Plot audio data

Visually Identify audio segments that correspond to each "Hallelujah" in the chorus and select samples numbers at which these segments start and stop. Each color in the plot corresponds to a different segment of the chorus.

Create (or mark) the end of each segment

Visually identify the segment boundaries and mark them.

segmentEnd = [20000, 36000, 45000, 55000, length(y)];

Define speaker parameters

Set up a selection of speakers named speakerselection to play five segments of "Hallelujah" on six different speakers.

nspeakers = 6;
nspeakergroups = 5;
speakerselection = cell(1, nspeakergroups);

Assign speakers to groups for the first part of the example

Each speaker selection specifies which speakers from the 5.1 channel Speaker system shall play each audio segment (these assignments may vary for your speaker system). For the first part of the example, we use single speakers paired with the sub-woofer (4).

  • Speaker 1: Left-Front
  • Speaker 2: Right-Front
  • Speaker 3: Center
  • Speaker 4: Sub-Woofer
  • Speaker 5: Left-Rear
  • Speaker 6: Right-Rear
speakerselection{1} = [4, 6]; % Segment 1; speakers 4 and 6
speakerselection{2} = [4, 5]; % Segment 2; speakers 4 and 5
speakerselection{3} = [1, 4]; % Segment 3; speakers 1 and 4
speakerselection{4} = [2, 4]; % Segment 4; speakers 2 and 4
speakerselection{5} = [3, 4]; % Segment 5; speakers 3 and 4

[singleChannelOutputs] = ...
    helper_surround_sound_single_voices(y, segmentEnd, nspeakers, nspeakergroups, speakerselection);

Queue data

Enqueue speaker output data

queueOutputData(s, singleChannelOutputs);

Start generation

Start the session to generate data on single speakers in a sequence and pause between the two examples.

startForeground(s);
pause(3);

Assign speakers to groups for the second part of the example

Each speaker selection specifies which speakers from the 5.1 channel Speaker system shall play each audio segment (these assignments may vary for your speaker system). For the second part of the example, we use groups of speakers. Note that the sub-woofer (4) is included in all speaker selections

  • Speaker 1: Left-Front
  • Speaker 2: Right-Front
  • Speaker 3: Center
  • Speaker 4: Sub-Woofer
  • Speaker 5: Left-Rear
  • Speaker 6: Right-Rear
speakerselection{1} = [4, 5, 6];          % Segment 1; speakers 4, 5, 6
speakerselection{2} = [1, 2, 4];          % Segment 2; speakers 1, 2, 4
speakerselection{3} = [3, 4];             % Segment 3; speakers 3, 4
speakerselection{4} = [1, 2, 3, 4];       % Segment 4; speakers 1, 2, 3, 4
speakerselection{5} = [1, 2, 3, 4, 5, 6]; % Segment 5; all speakers

[multiChannelOutput] = ...
    helper_surround_sound_single_voices(y, segmentEnd, nspeakers, nspeakergroups, speakerselection);

Queue Data

queueOutputData(s, multiChannelOutput);

Start generation

Start the session to generate data on groups of speakers in a sequence.

startForeground(s);