How to use audioDeviceWriter ?

3 views (last 30 days)
Dora Kokai
Dora Kokai on 23 Nov 2020
Edited: Dora Kokai on 28 Nov 2020
I would like to use a compressor/expander and I get the following message:
Error using audioDeviceWriter/setup
The number of input channels must be less than or equal to 255.
Error in audioDeviceWriter/setupImpl
Error in gyak (line 29)
audiowriter(y); - Show complete stack trace
The compressor's code is from the dafx book and i would like to see the result in timescope.
function[y,g] = compexp(x)
CT=0.00016;
CS=1;
ET=2.6;
ES=5;
tav = 0.01;
at = 0.03;
rt = 0.003;
delay = 150;
xrms = 0;
g = 1;
buffer = zeros(1,delay);
for n = 1:length(x)
xrms = (1-tav) * xrms + tav * x(n)^2;
X = 10*log10(xrms);
G = min([0, CS*(CT-X), ES*(ET-X)]);
f = 10^(G/20);
if f < g
coeff = at;
else
coeff = rt;
end
g = (1-coeff) * g + coeff * f;
y(n) = g * buffer(end);
buffer = [x(n) buffer(1:end-1)];
end
end
%using
frameLength = 1024;
audioreader= dsp.AudioFileReader( ...
'Filename','mintaa.wav', ...
'SamplesPerFrame',frameLength);
audiowriter = audioDeviceWriter( ...
'SampleRate',audioreader.SampleRate);
scope = timescope( ...
'SampleRate',audioreader.SampleRate, ...
'TimeSpanOverrunAction','Scroll', ...
'TimeSpanSource','property',...
'TimeSpan',1, ...
'BufferLength',44100*4, ...
'YLimits',[-1 1], ...
'ShowGrid',true, ...
'LayoutDimensions',[2,1], ...
'NumInputPorts',2, ...
'ShowLegend',true, ...
'Title',['Original vs. Compressed Audio (top)' ...
' and Compressor Gain in dB (bottom)']);
while ~isDone(audioreader)
x = audioreader();
y = compexp(x);
audiowriter(y);
x1 = x(:,1);
y1 = y(:,1);
scope([x1,y1]);
end
release(audioreader)
release(compexp)
release(audiowriter)
release(scope)

Answers (1)

Jimmy Lapierre
Jimmy Lapierre on 24 Nov 2020
I suspect that compexp returns a row vector instead of a column vector, so it looks like 1024 channels. Try pre-allocating y before your loop in compexp (i.e. y = zeros(x)) so that it's a column like x.
  3 Comments
Walter Roberson
Walter Roberson on 26 Nov 2020
Your g is a scalar coming out of your function.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!