Updated code with added functionality. It reads settings from the ASIO401.toml file, updates them and runs a test, where recorded data are plotted in the end. Looping outputs to inputs should produce a nice sine wave plot.
Some attenuator values takes some time to perform, hence the 1s pause.
To test, copy below into an m file and run it:
OutputAttnVals = [-12, -2, +8, +18];
InputAttnVals = [0.0, 6.0, 12.0, 18.0, 24.0, 30.0, 36.0, 42.0];
OutputAttnVals = [-2];
InputAttnVals = [0.0];
FSampleVals = [48000, 96000, 192000, 384000];
close all;
FSInx = 3;
for(k=1:length(OutputAttnVals))
for(m=1:length(InputAttnVals))
[InputFSlevel, OutputFSlevel, BlockSize]=GetSettings();
PutSettings(InputAttnVals(m), OutputAttnVals(k), BlockSize);
[NewInputFSlevel, NewOutputFSlevel, NewBlockSize]=GetSettings();
if((InputAttnVals(m) ~= NewInputFSlevel)|(OutputAttnVals(k) ~= NewOutputFSlevel)|(BlockSize ~= NewBlockSize))
disp(‘Error: ASIO401.toml was not updated correctly, exiting test’);
break;
end
title = sprintf(‘Fs: %d, InputAtt: %2.1f, OutputAtt: %2.1f’, FSampleVals(FSInx), InputAttnVals(m), OutputAttnVals(k));
TestQADevice(FSampleVals(FSInx), title);
end
end
function PutSettings(InputFSlevel, OutputFSlevel, BlockSize)
%Possible fullScaleOutputLevelDBV values: -12.0, -2.0, +8.0, +18.0
%Possible fullScaleInputLevelDBV values: 0.0, +6.0, +12.0, +18.0, +24.0, +30.0, +36.0, +42.0
Path = getenv(‘userprofile’);
fName = ‘ASIO401.toml’;
fullFilePath = strcat(Path, '', fName);
fid = fopen(fullFilePath, ‘w’);
fprintf(fid, ‘fullScaleInputLevelDBV = %2.1f\n’, InputFSlevel);
fprintf(fid, ‘fullScaleOutputLevelDBV = %2.1f\n’, OutputFSlevel);
fprintf(fid, ‘bufferSizeSamples = %d\n’, BlockSize);
fclose(fid);
end
function [InputFSLevel, OutputFSLevel, BlockSize] = GetSettings()
%Possible fullScaleOutputLevelDBV values: -12.0, -2.0, +8.0, +18.0
%Possible fullScaleInputLevelDBV values: 0.0, +6.0, +12.0, +18.0, +24.0, +30.0, +36.0, +42.0
%Assign default values to output variables, in case ASIO401.toml is missing line(s)
InputFSLevel = 0;
OutputFSLevel = -2;
BlockSize = 1024;
Path = getenv(‘userprofile’);
fName = ‘ASIO401.toml’;
fullFilePath = strcat(Path, '', fName);
fid = fopen(fullFilePath, ‘r’);
%Read the variables from the file
tline = ‘’;
while (ischar(tline))
tline = fgetl(fid);
if(~isempty(strfind(tline, ‘fullScaleInputLevelDBV’)))
inx = strfind(tline, ‘=’);
InputFSLevel = sscanf(tline(inx+1:end), “%d”);
continue;
end
if(~isempty(strfind(tline, ‘fullScaleOutputLevelDBV’)))
inx = strfind(tline, ‘=’);
OutputFSLevel = sscanf(tline(inx+1:end), “%d”);
continue;
end
if(~isempty(strfind(tline, ‘bufferSizeSamples’)))
inx = strfind(tline, ‘=’);
BlockSize = sscanf(tline(inx+1:end), “%d”);
continue;
end
end
fclose(fid);
end
function TestQADevice(FSample, Title)
deviceReader = audioDeviceReader(FSample, ‘Driver’, “ASIO”, ‘BitDepth’, “32-bit float”, ‘NumChannels’, 2);
deviceWriter = audioDeviceWriter(FSample, ‘Driver’, “ASIO”, ‘BitDepth’, “32-bit float”);
[Input_FS_Level, Output_FS_Level, BlockSize] = GetSettings();
setup(deviceWriter,zeros(BlockSize,2));
[audioFromDevice,numOverrun] = deviceReader();
pause(1);
OutputAmpCorrection = 1./(10.^(Output_FS_Level/20));
InputAmpCorrection = (10.^(Input_FS_Level/20));
Amp = 0.5;
tAccumulated = 0;
TotalRec = zeros(2*FSample, 2);
blockCnt = 0;
tic
while toc < 2
acquiredAudio = deviceReader();
TotalRec(blockCnt*BlockSize+1:(blockCnt+1)*BlockSize, :) = acquiredAudio;
blockCnt = blockCnt+1;
t=[0:1:(BlockSize-1)]./FSample+tAccumulated;
OutputAudio(:,1) = Amp*OutputAmpCorrection*sin(2*pi*1000*t);
OutputAudio(:,2) = Amp*OutputAmpCorrection*sin(2*pi*2000*t);
tAccumulated = tAccumulated + BlockSize/FSample;
deviceWriter(OutputAudio);
end
disp('Recording complete.')
release(deviceReader);
release(deviceWriter);
figure
plot(TotalRec);
title(Title);
end