CODE:
classdef mid38a < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure PlayButton matlab.ui.control.Button StopButton matlab.ui.control.Button UIAxes matlab.ui.control.UIAxes VolumeSliderLabel matlab.ui.control.Label VolumeSlider matlab.ui.control.Slider FreqSliderLabel matlab.ui.control.Label FreqSlider matlab.ui.control.Slider ReverseButton matlab.ui.control.Button end properties (Access = private) fs = 44100; Freq; music; signal; y; y1; y2; end methods (Access = private) % Code that executes after component creation function startupFcn(app) [app.y1,app.fs] = audioread('Sick Boy.wav',[1 1411200]); app.signal = app.y1; title(app.UIAxes, 'Freq'); xlabel(app.UIAxes, 'Time'); ylabel(app.UIAxes, 'amplitude'); end % Button pushed function: PlayButton function PlayButtonPushed(app, event) app.Freq = app.fs * app.FreqSlider.Value; app.signal = app.y1 *10*exp(app.VolumeSlider.Value); app.music = audioplayer(app.signal,app.Freq); play(app.music,app.Freq); time = (1:length(app.signal))/app.Freq; plot(app.UIAxes, time, app.signal); end % Button pushed function: StopButton function StopButtonPushed(app, event) stop(app.music); end % Value changed function: VolumeSlider function VolumeSliderValueChanged(app, event) value = app.VolumeSlider.Value; end % Button pushed function: ReverseButton function ReverseButtonPushed(app, event) app.Freq = app.fs * app.FreqSlider.Value; app.y2 = app.y1 *10*exp(app.VolumeSlider.Value); app.music= audioplayer(flipud(app.y2), app.Freq); % 播放前後顛倒的音訊波形 play(app.music, app.Freq); time = (1:length( app.y2))/app.Freq; plot(app.UIAxes, time, flipud(app.y2)); end end % App initialization and construction methods (Access = private) % Create UIFigure and components function createComponents(app) % Create UIFigure app.UIFigure = uifigure; app.UIFigure.Position = [100 100 760 576]; app.UIFigure.Name = 'UI Figure'; % Create PlayButton app.PlayButton = uibutton(app.UIFigure, 'push'); app.PlayButton.ButtonPushedFcn = createCallbackFcn(app, @PlayButtonPushed, true); app.PlayButton.BackgroundColor = [0.9608 0.9608 0.9608]; app.PlayButton.FontSize = 36; app.PlayButton.Position = [40 338 123 83]; app.PlayButton.Text = 'Play'; % Create StopButton app.StopButton = uibutton(app.UIFigure, 'push'); app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true); app.StopButton.BackgroundColor = [0.9608 0.9608 0.9608]; app.StopButton.FontSize = 36; app.StopButton.Position = [40 103 123 83]; app.StopButton.Text = 'Stop'; % Create UIAxes app.UIAxes = uiaxes(app.UIFigure); title(app.UIAxes, 'Title') xlabel(app.UIAxes, 'X') ylabel(app.UIAxes, 'Y') app.UIAxes.Position = [191 103 514 318]; % Create VolumeSliderLabel app.VolumeSliderLabel = uilabel(app.UIFigure); app.VolumeSliderLabel.HorizontalAlignment = 'right'; app.VolumeSliderLabel.Position = [62 477 46 22]; app.VolumeSliderLabel.Text = 'Volume'; % Create VolumeSlider app.VolumeSlider = uislider(app.UIFigure); app.VolumeSlider.Limits = [1 5]; app.VolumeSlider.MajorTicks = [1 2 3 4 5 6 7 8 9 10]; app.VolumeSlider.ValueChangedFcn = createCallbackFcn(app, @VolumeSliderValueChanged, true); app.VolumeSlider.Position = [129 486 225 3]; app.VolumeSlider.Value = 1; % Create FreqSliderLabel app.FreqSliderLabel = uilabel(app.UIFigure); app.FreqSliderLabel.HorizontalAlignment = 'right'; app.FreqSliderLabel.Position = [413 477 30 22]; app.FreqSliderLabel.Text = 'Freq'; % Create FreqSlider app.FreqSlider = uislider(app.UIFigure); app.FreqSlider.Limits = [0 3]; app.FreqSlider.MajorTicks = [0 0.5 1 1.5 2 2.5 3]; app.FreqSlider.Position = [464 486 235 3]; app.FreqSlider.Value = 1; % Create ReverseButton app.ReverseButton = uibutton(app.UIFigure, 'push'); app.ReverseButton.ButtonPushedFcn = createCallbackFcn(app, @ReverseButtonPushed, true); app.ReverseButton.FontSize = 28; app.ReverseButton.Position = [40 221 123 83]; app.ReverseButton.Text = 'Reverse'; end end methods (Access = public) % Construct app function app = mid38a % Create and configure components createComponents(app) % Register the app with App Designer registerApp(app, app.UIFigure) % Execute the startup function runStartupFcn(app, @startupFcn) 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 endendDEMO: