function alpsTrajPlay(spkLoc,pageSize,pageBufCount)
% Loads Cartesian position from posBuff.mat file and converts them to
% spherical coordinates. Uses the vbap.m library written by Archontis
% Politis (2015) based on a core by Ville Pulkki (1997)to calculate gains
% for amplitude panning of a .wav file on speakers defined in spkLoc
% positions. 

% The Matlab/Octave VBAP.m library can be downloaded here:
% http://www.mathworks.com/matlabcentral/fileexchange/53884-vector-base-amplitude-panning-library

% Playrec can be downloaded from here: https://github.com/PlayrecForMatlab


if nargin < 3
    pageBuffCount = 4;
end
if nargin < 2
    pageSize = 2048;
end


playChanList = (1:(size(spkLoc,1)));
fs = playrec('getSampleRate');
c = 343; % m/s
pageNumList = nan(1, pageBufCount);
runMaxSpeed = true;
firstTimeThrough = true;
numSpkLoc = size(spkLoc,1);
fileName = 'FileNameOfaWavYouLike';
wavBuff = wavread(fileName);
[fileSize, fs] = wavread(fileName, 'size');
fileLength = fileSize(1);
endPoint = fileLength;
pageNum = 0;
counter = 0;

posBuff = struct2array(load('posBuff'));
startPoint = 8*pageSize; % don't remember why.
% here convert the mic's position from Cartesian to spherical coordinates,
% the angles are needed for, Xmic,Ymic Zmic 
Xmic = posBuff(:,1); %
Ymic = posBuff(:,2);
Zmic = posBuff(:,3);
[azi, ele, r,]=cart2sph(Xmic,Ymic,Zmic);
% and from radians to degrees
posSphDeg = (cat(2,azi,ele))*180/pi;

% Here we need to convert also the spkLoc from Cartesian to spherical:
Xspk = spkLoc(:,1);
Yspk = spkLoc(:,2);
Zspk = spkLoc(:,3);
[spkAzi,spkEle,~] = cart2sph(Xspk,Yspk,Zspk);


% The speaker setup for vbap.m in spherical coordinates
% ls_dirs = [azi1 elev1; azi2 elev2]; ...; aziK elevK] for 3D
ls_dirs = (cat(2,spkAzi,spkEle))*180/pi;
ls_groups = findLsTriplets(ls_dirs);
% compute inverse matrices for loudspeaker pairs or triplets, needs to be
% done once for any panning direction 
ls_invMtx = invertLsMtx(ls_dirs, ls_groups);


pageNum = length(posSphDeg);
for counter = 1:pageNum
    if(runMaxSpeed)
        while(playrec('isFinished', pageNumList(1)) == 0)
        end
    else
        playrec('block', pageNumList(1));
    end
    
    if(firstTimeThrough)
        playrec('resetSkippedSampleCount');
        firstTimeThrough = false;
    else
        if(playrec('getSkippedSampleCount'))
            fprintf('%d samples skipped!!\n', playrec('getSkippedSampleCount'));
            firstTimeThrough = true;
        end
    end
    
    startSample = startPoint;
    endSample = min(startSample + pageSize - 1, endPoint);
    playBuff = wavBuff(startSample:endSample,:)* 0.03;
    startPoint = startPoint + pageSize;
   
    % The VBAP bit    
    gains = vbap(posSphDeg(counter,:), ls_groups, ls_invMtx);
    gains = repmat(gains,pageSize,1);
    % distribute the mono files onto all channels
    playBuffallChan = repmat(playBuff,1,numSpkLoc);
    playVbapBuff = gains.* playBuffallChan;
    playrec('play',playVbapBuff,playChanList);
    
    counter = counter+1;
end

% alpsTrajPlay plays a trajectory on a multi-loudspeaker set-up using the
% Matlab VBAP library by Archontis Politis (2015) It works together with
% the alpsTRajRec script.

% The autjor of this script is Dominik Schlienger ? 2016
% Copyright 1992-2016 The FreeBSD Project. All rights reserved.
% 
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:  
% 
%     Redistributions of source code must retain the above copyright
%     notice, this list of conditions and the following disclaimer. 
%     Redistributions in binary form must reproduce the above copyright
%     notice, this list of conditions and the following disclaimer in the
%     documentation and/or other materials provided with the distribution. 
% 
% THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
% PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.  
% The views and conclusions contained in the software and documentation are
% those of the authors and should not be interpreted as representing
% official policies, either expressed or implied, of the FreeBSD Project. 
