Clear Filters
Clear Filters

Undefined function 'audioDeviceReader' for input arguments of type 'char'.

4 views (last 30 days)
I get errors at line 49 and 62
classdef app_one < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
InstrumentLabel matlab.ui.control.Label
EditField3 matlab.ui.control.EditField
ResetButton matlab.ui.control.Button
AnalyzeButton matlab.ui.control.Button
RecordButton matlab.ui.control.Button
UIAxes2 matlab.ui.control.UIAxes
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
fs % Description
nob % Description
noc % Description
audio % Description
y % Description
t % Description
nfft % Description
f % Description
ymax % Description
f1 % Description
f2 % Description
xymax % Description
xmax % Description
val % Description
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: RecordButton
function RecordButtonPushed(app, event)
app.EditField3.Value= '-';
plot(app.UIAxes2,0,0);
plot(app.UIAxes,0,0);
app.RecordButton.BackgroundColor='g';
app.fs=48000;
app.nob=16;
app.noc=2;
app.audio=audioDeviceReader('SampleRate', app.fs, app.noc);
app.y=record(app.audio, app.fs * 5)';
app.RecordButton.BackgroundColor='w';
end
% Button pushed function: AnalyzeButton
function AnalyzeButtonPushed(app, event)
app.nfft=2^nextpow2(length(app.y));
app.f=linspace(0,app.fs,app.nfft);
app.y=abs(fft(app.y,app.nfft));
% Find the highest peaks in the frequency spectrum
[pks,locs] = findpeaks(app.y(2:app.nfft/2),'MinPeakHeight',0.1*max(app.y(2:app.nfft/2)),'MinPeakDistance',50);
locs = locs + 1;
% Match peak frequencies with known frequency ranges of different instruments
instrument_names = {'Open Hi-Hat', 'Close Hi-Hat', 'Big Crash', 'Small Crash', 'Splash', 'Ride', 'Ride Bell', 'Floor Tom', 'High Tom', 'Low Tom', 'Bass Drum', 'Snare Drum'};
instrument_freq_ranges = {[7633,7787], [11702,11938], [5038,5140], [3992,4072], [6020,6142], [3524,3596], [3693,3767], [75,77], [166,170], [109,113], [30,80], [400,3000]};
detected_instruments = cell(1,length(locs)); % Preallocate cell array with fixed size
for i = 1:length(locs)
detected_instruments{i} = {};
for j = 1:length(instrument_freq_ranges)
if locs(i) > instrument_freq_ranges{j}(1) && locs(i) < instrument_freq_ranges{j}(2)
detected_instruments{i} = instrument_names{j};
end
end
end
% Display detected instruments
if ~isempty(detected_instruments)
app.EditField3.Value = strjoin(detected_instruments, ', ');
else
app.EditField3.Value = 'Unknown instrument';
end
% Plot frequency spectrum with peak frequencies marked
plot(app.UIAxes2,app.f(2:app.nfft/2),app.y(2:app.nfft/2),'r');
hold(app.UIAxes2, 'on');
plot(app.UIAxes2,app.f(locs),pks,'*b');
hold(app.UIAxes2, 'off');
end
% Button pushed function: ResetButton
function ResetButtonPushed(app, event)
app.EditField3.Value= '-';
plot(app.UIAxes2,0,0);
plot(app.UIAxes,0,0);
clear sound;
close all;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Time Domain')
xlabel(app.UIAxes, 'Time')
ylabel(app.UIAxes, 'Amplitude')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [1 153 313 328];
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Frequency Domain')
xlabel(app.UIAxes2, 'Frequency')
ylabel(app.UIAxes2, 'Amplitude')
zlabel(app.UIAxes2, 'Z')
app.UIAxes2.Position = [329 153 300 328];
% Create RecordButton
app.RecordButton = uibutton(app.UIFigure, 'push');
app.RecordButton.ButtonPushedFcn = createCallbackFcn(app, @RecordButtonPushed, true);
app.RecordButton.Position = [69 98 100 23];
app.RecordButton.Text = 'Record';
% Create AnalyzeButton
app.AnalyzeButton = uibutton(app.UIFigure, 'push');
app.AnalyzeButton.ButtonPushedFcn = createCallbackFcn(app, @AnalyzeButtonPushed, true);
app.AnalyzeButton.Position = [259 97 100 23];
app.AnalyzeButton.Text = 'Analyze';
% Create ResetButton
app.ResetButton = uibutton(app.UIFigure, 'push');
app.ResetButton.ButtonPushedFcn = createCallbackFcn(app, @ResetButtonPushed, true);
app.ResetButton.Position = [438 97 100 23];
app.ResetButton.Text = 'Reset';
% Create EditField3
app.EditField3 = uieditfield(app.UIFigure, 'text');
app.EditField3.Position = [282 38 100 22];
% Create InstrumentLabel
app.InstrumentLabel = uilabel(app.UIFigure);
app.InstrumentLabel.Position = [301 59 62 22];
app.InstrumentLabel.Text = 'Instrument';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app_one
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

Answers (1)

Ajay Gajulapally
Ajay Gajulapally on 1 Mar 2023
Hi Michael,
Invalid argument syntax is being used on line 49
Consider changing the lines from
audioDeviceReader('SampleRate', app.fs, app.noc);
to
audioDeviceReader('SampleRate', app.fs,'NumChannels' app.noc);
if you want the number of channels to be of desired value.
Kindly ensure that "record" function being used at line 50 doesn't accept "audioDeviceReader" object with a time length, instead by each second. Then try to use a for loop to get number of seconds or try to use "audioRecorder" object instead of "audioDeviceReader" through which "record" function can take time as input
Refer to the following MATLAB documentation to know more:

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!