main content

generating multichannel audio -凯发k8网页登录

this example shows how to set up continuous audio generation using multiple audio channels. 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).

load audio data

load handel's "hallelujah".

load variables:

  • y representing the hallelujah waveform

  • fs representing the sampling frequency

load handel;

create a data acquisition

create a dataacquisition object using directsound as the vendor id.

dq = daq("directsound")

add channels and adjust generation scan rate to match the audio sampling frequency

add six audio output channels and set the generation scan rate to the audio sampling rate.

addoutput(dq,"audio7",1:6,"audio");
dq.rate = fs;

plot audio data

visually identify audio segments that correspond to each "hallelujah" in the chorus and select sample numbers at which these segments start and stop. each color in the plot corresponds to a different segment of the chorus.

identify 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 in a cell array named speakerselection to play five segments of "hallelujah" on six different speakers.

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

assign speakers to groups

each speaker selection specifies which speakers from the 5.1 channel speaker system play each audio segment (these assignments may vary for your speaker system). for the first part of the example, 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] = ...
    surroundsoundvoices(y, segmentend, nspeakers, nspeakergroups, speakerselection);

write single channel outputs

write a sequence of single channel outputs and then pause before proceeding to the next section.

write(dq, singlechanneloutputs);
pause(3);

assign speakers to groups

each speaker selection specifies which speakers from the 5.1 channel speaker system play each audio segment (these assignments may vary for your speaker system). for the second part of the example, 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] = ...
    surroundsoundvoices(y, segmentend, nspeakers, nspeakergroups, speakerselection);

write multichannel outputs

write(dq, multichanneloutput);
function [multichanneloutput] = surroundsoundvoices(audioout, segmentends, numspeakers, numspeakergroups, speakergroups)
% distribute contiguous segments of an output waveform to multiple groups 
% of speakers in a one-to-one relationship. the input waveform is broken up 
% into contiguous segments. each segment is output by one and only one 
% group of speakers, with each group being visited in turn.
% break up the input waveform into segments to be played by various groups
% of speakers. in this demonstration, we would like to slowly add "voices"
% by incrementally having more speakers generate the output waveform.
% in particular, we will regard the output waveform as a contiguous
% sequence of segments (one segment per group of speakers). for example, if
% we have 3 groups of speakers, we can think of breaking up the output
% waveform into 3 segments: output = [s1 s2 s3]
% speaker group 1 outputs: s1 0  0
% speaker group 2 outputs: 0  s2 0
% speaker group 3 outputs: 0  0  s3
multichanneloutput = repmat(0.01, length(audioout), numspeakers);
startofsegment = [1 (segmentends(1:end-1) 1)];
for i = 1:numspeakergroups
    speakergroup = speakergroups{i};
    n = numel(speakergroup);
    for j = 1:n
        range = startofsegment(i):segmentends(i);
        multichanneloutput(range, speakergroup(j)) = audioout(range);
    end
end
end
网站地图