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