function [ad1,ad2] = playrectdt(stim_in,stim_inr,S_Rate)
%[{ad1,ad2}] = playrectdt(stim_1,{stim_2},S_Rate)
%
%Plays and/or records, using S_Rate as sample rate
%on Tucker Davis System II hardware
%
%The number of d-a channels to use is specified by the
%number of input arguments supplied
%
%The number of a-d channels to use is specified by the
%number of output arguments supplied
%
%If you want only to record, pass an array of zeros as input
%to specify the number of samples to record
%
%Values outside the range of 32767 are clipped
%
%If present, pf1's are set to bypass, and pa4's are set to
%20 dB of attenuation
%
%usage examples:
%
%[a,b] = playrectdt(noise,tone,32768);
%...plays the vector noise on d-a channel 1, the vector
%tone on d-a channel 2, and records from both a-d channels
%
%[a,b] = playrectdt(noise,32768);
%...plays the vector noise on d-a channel 1, and records
%from both a-d channels
%
%a = playrectdt(noise,32768);
%...plays the vector noise on d-a 1, and records from a-d 1
%
%playrectdt(noise,32768);
%...plays the vector noise, with no a-d
%
%Version History:
%
%1-14-2002 First release.
%4-01-2002 Originally written for a DD1. Switched to auto-detecting calls
% for compatibility, but this caused some errors. This version fixed some
% of these. Bit numbers weren't being set correctly, and the proper
% A-D and D-A modules weren't being selected.
%
%Christopher Brown, cbrown@phi.luc.edu
%Attenuation for pa4's. Change as necessary:
atten1 = 20;
atten2 = 20;
%Check input arguments...
if (nargin==2) %function also handles 2 columns
if length(stim_inr)==1 %or rows of a single input
S_Rate = stim_inr;
else
error('S_Rate must be a scalar.');
end
if size(stim_in,1)==1 %1 d-a channel
dac = 1;
elseif size(stim_in,2)==1
dac = 1;
stim_in = stim_in.';
elseif size(stim_in,1)==2 %2 d-a channels
dac = 3;
stim_inr = stim_in(:,2);
elseif size(stim_in,2)==2
dac = 3;
stim_in = stim_in.';
stim_inr = stim_in(:,2);
else
error('Input vector must be either 1 or 2 columns or rows');
end
%Check for clipping
a = find(abs(stim_in)>32767);
if length(a) > 0
stim_in(a) = sign(stim_in(a)).*32767;
warning('Clipping has occurred.');
end
clear a;
if dac==3
a = find(abs(stim_inr)>32767);
if length(a) > 0
stim_inr(a) = sign(stim_inr(a)).*32767;
warning('Clipping has occurred.');
end
end
clear a;
elseif (nargin==3) %two channel
dac = 3;
if size(stim_in,2)==1
stim_in = stim_in.';
end
if size(stim_inr,2)==1
stim_inr = stim_inr.';
end
%Make sure both vectors are same size, zero-pad the shorter if necessary
if size(stim_in,2)>size(stim_inr,2)
stim_inr = [stim_inr,zeros(length(stim_in)-length(stim_inr))];
warning('Applying zero-padding');
elseif size(stim_in,2)<size(stim_inr,2)
stim_in = [stim_in,zeros(length(stim_inr)-length(stim_in))];
warning('Applying zero-padding');
end
%check for clipping;
a = find(abs(stim_in)>32767);
if length(a) > 0
stim_in(a) = sign(stim_in(a)).*32767;
warning('Clipping has occurred.');
end
clear a;
a = find(abs(stim_inr)>32767);
if length(a) > 0
stim_inr(a) = sign(stim_inr(a)).*32767;
warning('Clipping has occurred.');
end
clear a;
else
error('Wrong number of input arguments. "help playrectdt" for details');
end
TDsrate=1000000/S_Rate; %get tdt speriod
npts=length(stim_in);
%Check output arguments...
if nargout<3 %Assign bit number for A-D
adc = (4*((2^nargout)-1));
%adc = nargout;
bitnumber = dac + adc;
else
error('Wrong number of output arguments. "help playrectdt" for details');
end
try %Attempt to initialize hardware
S232('S2init', 0, 'INIT_PRIMARY', 1000);
S232('APlock', 100, 0);
S232('XBlock', 100, 0);
catch %Didn't happen, hardware probably locked by another app.
error('TDT initialization failed. Try hitting reset in the S232 control panel.');
end
if S232('XB1device', 'PF1_CODE', 1)
S232('PF1bypass', 1); %**********set pf1 here
end
if S232('XB1device', 'PF1_CODE', 2)
S232('PF1bypass', 2); %**********set pf2 here
end
if S232('XB1device', 'PA4_CODE', 1)
S232('PA4atten', 1, atten1);
end
if S232('XB1device', 'PF1_CODE', 2)
S232('PA4atten', 2, atten2);
end
S232('DAclear',1);
S232('ADclear',1);
S232('DAsrate', 1, TDsrate);
S232('DAnpts', 1, npts);
s232('ADmode', 1, bitnumber); %Corrected 3 March 2002. Was using adc & dac
S232('DAmode', 1, bitnumber);
if dac == 1
PLAYBUFFER1 = S232('_allot16', npts); % allocate DAMA buffer
S232('play', PLAYBUFFER1);
S232('push16', stim_in, npts);
S232('qpop16', PLAYBUFFER1); % pop stimulus into DAMA
elseif dac == 3
PLAYBUFFER1 = S232('_allot16', npts); % allocate DAMA buffer
PLAYBUFFER2 = S232('_allot16', npts); % allocate DAMA buffer
S232('dplay', PLAYBUFFER1, PLAYBUFFER2);
S232('push16', stim_in, npts);
S232('qpop16', PLAYBUFFER1); % pop stimulus into DAMA
S232('push16', stim_inr, npts);
S232('qpop16', PLAYBUFFER2); % pop stimulus into DAMA
end
%S232('DAarm', 1);
%S232('DAtgo', 1);
if adc>0
RECBUFFER1 = S232('_allot16', npts);
if adc==4
S232('record', RECBUFFER1);
elseif adc==12
RECBUFFER2 = S232('_allot16', npts);
s232('drecord', RECBUFFER1, RECBUFFER2);
end
% S232('ADarm', 1);
% S232('ADtgo', 1);
end
%For some reason the auto-detecting commands weren't working here. They worked when
%I originally wrote them, I don't know what was going on, but this should fix it.
%Only tested with a DD1 - please let me know if the other modules work with this code.
if S232('XB1device', 'DD1_CODE', 1)
S232('DD1arm',1);
S232('DD1tgo', 1);
else
if S232('XB1device', 'DA1_CODE', 1)
S232('DA1arm',1);
S232('DA1tgo', 1);
elseif S232('XB1device', 'DA3_CODE', 1)
S232('DA3arm',1);
S232('DA3tgo', 1);
else
error('No D-A module detected.');
end
if adc > 0 then
if S232('XB1device', 'AD1_CODE', 1)
S232('AD1arm',1);
S232('AD1tgo', 1);
elseif S232('XB1device', 'AD2_CODE', 1)
S232('AD2arm',1);
S232('AD2tgo', 1);
else
error('No A-D module detected.');
end
end
end
S232('XB1gtrig');
%Wait for conversion (plus 250 ms, just in case)
pause((npts/S_Rate)+.25);
if adc>0
S232('qpush16', RECBUFFER1);
ad1 = S232('pop16');
if adc>4
S232('qpush16', RECBUFFER2);
ad2 = S232('pop16');
end
end
%pause;
S232('dropall');
S232('trash');
S232('XBunlock',0); %wash
S232('APunlock',0); %rinse
pause(.1);
S232('XBunlock',0); %repeat
S232('APunlock',0); %out of superstition
S232('S2close');