This video shows how to record sound and modulate it by varying the sample rate of the recorded sound in MATLAB 2017b using App designer.
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
Source Code:
classdef Sound_Recorder_MATLAB < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
RecordButton matlab.ui.control.Button
PlayButton matlab.ui.control.Button
DurationLabel matlab.ui.control.Label
DurationTextArea matlab.ui.control.TextArea
SpeedKnobLabel matlab.ui.control.Label
SpeedKnob matlab.ui.control.Knob
SlowLabel matlab.ui.control.Label
FastLabel matlab.ui.control.Label
end% Callbacks that handle component events
methods (Access = private)% Button pushed function: RecordButton
function RecordButtonPushed(app, event)audioObject = audiorecorder;
duration = str2double(app.DurationTextArea.Value{1});
msgbox(‘Recording Started’);
recordblocking(audioObject, duration);
msgbox(‘Recording Stopped’);
assignin(‘base’, ‘audioObject’, audioObject);
end
% Button pushed function: PlayButton
function PlayButtonPushed(app, event)
audioObject = evalin(‘base’, ‘audioObject’);y = getaudiodata(audioObject);
Fs = audioObject.SampleRate;multiplier = app.SpeedKnob.Value;
sound(y, Fs*multiplier);
% play(audioObject)
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 = ‘UI Figure’;% Create RecordButton
app.RecordButton = uibutton(app.UIFigure, ‘push’);
app.RecordButton.ButtonPushedFcn = createCallbackFcn(app, @RecordButtonPushed, true);
app.RecordButton.Position = [271 400 100 22];
app.RecordButton.Text = ‘Record’;% Create PlayButton
app.PlayButton = uibutton(app.UIFigure, ‘push’);
app.PlayButton.ButtonPushedFcn = createCallbackFcn(app, @PlayButtonPushed, true);
app.PlayButton.Position = [271 66 100 22];
app.PlayButton.Text = ‘Play’;% Create DurationLabel
app.DurationLabel = uilabel(app.UIFigure);
app.DurationLabel.HorizontalAlignment = ‘right’;
app.DurationLabel.VerticalAlignment = ‘top’;
app.DurationLabel.Position = [9 418 58 15];
app.DurationLabel.Text = ‘Duration :’;% Create DurationTextArea
app.DurationTextArea = uitextarea(app.UIFigure);
app.DurationTextArea.HorizontalAlignment = ‘right’;
app.DurationTextArea.Position = [82 416 69 19];
app.DurationTextArea.Value = {‘5’};% Create SpeedKnobLabel
app.SpeedKnobLabel = uilabel(app.UIFigure);
app.SpeedKnobLabel.HorizontalAlignment = ‘center’;
app.SpeedKnobLabel.VerticalAlignment = ‘top’;
app.SpeedKnobLabel.Position = [307 174 41 15];
app.SpeedKnobLabel.Text = ‘Speed’;% Create SpeedKnob
app.SpeedKnob = uiknob(app.UIFigure, ‘continuous’);
app.SpeedKnob.Limits = [0 2];
app.SpeedKnob.Position = [297 223 60 60];
app.SpeedKnob.Value = 1;% Create SlowLabel
app.SlowLabel = uilabel(app.UIFigure);
app.SlowLabel.VerticalAlignment = ‘top’;
app.SlowLabel.Position = [210 246 32 15];
app.SlowLabel.Text = ‘Slow’;% Create FastLabel
app.FastLabel = uilabel(app.UIFigure);
app.FastLabel.VerticalAlignment = ‘top’;
app.FastLabel.Position = [411 246 28 15];
app.FastLabel.Text = ‘Fast’;% 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 = Sound_Recorder_MATLAB% 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