NoPlagiarism

Untitled

Jun 9th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 17.57 KB | None | 0 0
  1. classdef DebugApp < matlab.apps.AppBase
  2.  
  3.     % Properties that correspond to app components
  4.     properties (Access = public)
  5.         UIFigure               matlab.ui.Figure
  6.         AChordButton           matlab.ui.control.Button
  7.         CChordButton           matlab.ui.control.Button
  8.         AmChordButton          matlab.ui.control.Button
  9.         DChordButton           matlab.ui.control.Button
  10.         D7ChordButton          matlab.ui.control.Button
  11.         EButton_2              matlab.ui.control.Button
  12.         AButton                matlab.ui.control.Button
  13.         DButton                matlab.ui.control.Button
  14.         GButton                matlab.ui.control.Button
  15.         BButton                matlab.ui.control.Button
  16.         EButton_7              matlab.ui.control.Button
  17.         EChordButton           matlab.ui.control.Button
  18.         EmChordButton          matlab.ui.control.Button
  19.         FChordButton           matlab.ui.control.Button
  20.         GChordButton           matlab.ui.control.Button
  21.         VolumeSliderLabel      matlab.ui.control.Label
  22.         VolumeSlider           matlab.ui.control.Slider
  23.         SustainSliderLabel     matlab.ui.control.Label
  24.         SustainSlider          matlab.ui.control.Slider
  25.         DistortionSwitchLabel  matlab.ui.control.Label
  26.         DistortionSwitch       matlab.ui.control.RockerSwitch
  27.         TremoloSwitchLabel     matlab.ui.control.Label
  28.         TremoloSwitch          matlab.ui.control.RockerSwitch
  29.         WahWahSwitchLabel      matlab.ui.control.Label
  30.         WahWahSwitch           matlab.ui.control.RockerSwitch
  31.     end
  32.  
  33.    
  34.     properties (Access = private)
  35.         Fs = 44100;
  36.         Volume = 0.5;
  37.         Gain = 0.498;
  38.         Distortion = -1;
  39.         Tremolo = -1;
  40.         WahWah = -1;
  41.        
  42.         E2 = 534;
  43.         A2 = 400;
  44.         D3 = 299;
  45.         G3 = 224;
  46.         B3 = 177;
  47.         E4 = 133;
  48.        
  49.         %Chord A
  50.         AE3 = 267;
  51.         AA3 = 199;
  52.         ACS4 = 158;
  53.        
  54.         %Chord Am
  55.         AmC4 = 167;
  56.        
  57.         %Chord C
  58.         CC3 = 335;
  59.        
  60.         %Chord D
  61.         DD4 = 149;
  62.         DFS4 = 118;
  63.        
  64.         %Chord E
  65.         EB2 = 356;
  66.         EAb3 = 211;
  67.        
  68.         %Chord F
  69.         FF3 = 252;
  70.         FF4 = 125;
  71.        
  72.         %Chord G
  73.         GG2 = 445;
  74.         GG4 = 224;
  75.        
  76.        
  77.        
  78.        
  79.     end
  80.     methods (Access = private)
  81.        
  82.         function Pluck(app, ~, N, g)
  83.             if ~exist('g','var')
  84.                 g = 0;
  85.             end
  86.             duration = 500; %100
  87.             gain = app.Gain - g;
  88.            
  89.             x = 2*randi([0 1], fix(N), 1)-1; %Random noise burst
  90.             x(x>0) = x(x>0)-(1-app.Volume); %Change volume
  91.             x(x<0) = x(x<0)+(1-app.Volume);
  92.             rx = x; %Real input
  93.             x = [x; zeros(length(x)*duration, 1)]; %Make input longer
  94.            
  95.             out = [rx; zeros(length(x)*5,1)]; %Initialise empty output channel
  96.             prev = out(length(rx)); %Previous sample value
  97.            
  98.             for n = length(rx)+1:length(out) %Only modify after noise burst
  99.                 %Add lowpass filter and to delayed channel.
  100.                 out(n) = (out(n-length(rx)) + prev)*gain;
  101.                 prev = out(n);
  102.             end
  103.            
  104.             if app.Distortion == 1
  105.                 out = app.Distort(out);
  106.             end
  107.            
  108.             if app.Tremolo == 1
  109.                 out = app.ApplyTremolo(out);
  110.             end
  111.            
  112.             if app.WahWah == 1
  113.                 out = app.ApplyWahWah(out);
  114.             end
  115.          
  116.            
  117.             sound(out, app.Fs);
  118.  
  119.         end
  120.        
  121.         function out = Distort(app, out)
  122.             out = out*50;
  123.             out(out>1) = 1 * app.Volume;
  124.             out(out<-1) = -1 * app.Volume;
  125.         end
  126.        
  127.         function out = ApplyTremolo(app, out)
  128.             t = [1:length(out)] * (1/app.Fs);
  129.             t = t(:);
  130.             LFO = sin(20* t);
  131.            
  132.             out = out.*LFO;
  133.         end
  134.        
  135.         function out = ApplyWahWah(app, out)
  136.             t = [1:length(out)*5] * (1/app.Fs);
  137.             t = t(:);
  138.             LFO = sin(20 * t)*200;
  139.            
  140.             channel = out;
  141.             for n = 1:length(out)
  142.                 if n-fix(LFO(n)) > 1
  143.                     out(n) = out(n) + channel(n-fix(LFO(n)));
  144.                 end
  145.             end
  146.         end
  147.     end
  148.  
  149.     methods (Access = private)
  150.  
  151.         % Button pushed function: EButton_2
  152.         function EButton_2Pushed(app, event)
  153.             app.Pluck(app, app.E2);
  154.         end
  155.  
  156.         % Button pushed function: AButton
  157.         function AButtonPushed(app, event)
  158.             app.Pluck(app, app.A2);
  159.         end
  160.  
  161.         % Button pushed function: DButton
  162.         function DButtonPushed(app, event)
  163.             app.Pluck(app, app.D3);
  164.         end
  165.  
  166.         % Button pushed function: GButton
  167.         function GButtonPushed(app, event)
  168.             app.Pluck(app, app.G3);
  169.         end
  170.  
  171.         % Button pushed function: BButton
  172.         function BButtonPushed(app, event)
  173.             app.Pluck(app, app.B3);
  174.         end
  175.  
  176.         % Button pushed function: EButton_7
  177.         function EButton_7Pushed(app, event)
  178.             app.Pluck(app, app.E4);
  179.         end
  180.  
  181.         % Button pushed function: AChordButton
  182.         function AChordButtonPushed(app, event)
  183.             app.Pluck(app, app.E2, 0.01);
  184.             app.Pluck(app, app.A2);
  185.             app.Pluck(app, app.AE3);
  186.             app.Pluck(app, app.AA3);
  187.             app.Pluck(app, app.ACS4);
  188.             app.Pluck(app, app.E4);
  189.         end
  190.  
  191.         % Button pushed function: AmChordButton
  192.         function AmChordButtonPushed(app, event)
  193.             app.Pluck(app, app.A2);
  194.             app.Pluck(app, app.AE3);
  195.             app.Pluck(app, app.AA3);
  196.             app.Pluck(app, app.AmC4);
  197.             app.Pluck(app, app.E4);
  198.         end
  199.  
  200.         % Button pushed function: CChordButton
  201.         function CChordButtonPushed(app, event)
  202.             app.Pluck(app, app.CC3);
  203.             app.Pluck(app, app.AE3);
  204.             app.Pluck(app, app.G3);
  205.             app.Pluck(app, app.AmC4);
  206.             app.Pluck(app, app.E4);
  207.         end
  208.  
  209.         % Button pushed function: DChordButton
  210.         function DChordButtonPushed(app, event)
  211.             app.Pluck(app, app.A2, 0.01);
  212.             app.Pluck(app, app.D3);
  213.             app.Pluck(app, app.AA3);
  214.             app.Pluck(app, app.DD4);
  215.             app.Pluck(app, app.DFS4);
  216.         end
  217.  
  218.         % Button pushed function: EChordButton
  219.         function EChordButtonPushed(app, event)
  220.             app.Pluck(app, app.E2, 0.01);
  221.             app.Pluck(app, app.EB2);
  222.             app.Pluck(app, app.AE3);
  223.             app.Pluck(app, app.EAb3);
  224.             app.Pluck(app, app.B3);
  225.             app.Pluck(app, app.E4);
  226.         end
  227.  
  228.         % Button pushed function: EmChordButton
  229.         function EmChordButtonPushed(app, event)
  230.             app.Pluck(app, app.E2, 0.01);
  231.             app.Pluck(app, app.EB2);
  232.             app.Pluck(app, app.AE3);
  233.             app.Pluck(app, app.G3);
  234.             app.Pluck(app, app.B3);
  235.             app.Pluck(app, app.E4);
  236.         end
  237.  
  238.         % Button pushed function: FChordButton
  239.         function FChordButtonPushed(app, event)
  240.             app.Pluck(app, app.A2, 0.01);
  241.             app.Pluck(app, app.FF3);
  242.             app.Pluck(app, app.AA3);
  243.             app.Pluck(app, app.AmC4);
  244.             app.Pluck(app, app.FF4);
  245.         end
  246.  
  247.         % Button pushed function: GChordButton
  248.         function GChordButtonPushed(app, event)
  249.             app.Pluck(app, app.GG2, 0.01);
  250.             app.Pluck(app, app.EB2);
  251.             app.Pluck(app, app.D3);
  252.             app.Pluck(app, app.G3);
  253.             app.Pluck(app, app.DD4);
  254.             app.Pluck(app, app.GG4);
  255.         end
  256.  
  257.         % Button pushed function: D7ChordButton
  258.         function D7ChordButtonPushed(app, event)
  259.             app.Pluck(app, app.A2, 0.01);
  260.             app.Pluck(app, app.D3);
  261.             app.Pluck(app, app.AA3);
  262.             app.Pluck(app, app.AmC4);
  263.             app.Pluck(app, app.DFS4);
  264.         end
  265.  
  266.         % Value changed function: VolumeSlider
  267.         function VolumeSliderValueChanged(app, event)
  268.             app.Volume = app.VolumeSlider.Value;
  269.            
  270.         end
  271.  
  272.         % Value changed function: SustainSlider
  273.         function SustainSliderValueChanged(app, event)
  274.             app.Gain = app.SustainSlider.Value;
  275.            
  276.         end
  277.  
  278.         % Value changed function: DistortionSwitch
  279.         function DistortionSwitchValueChanged(app, event)
  280.             app.Distortion = app.Distortion*-1;
  281.            
  282.         end
  283.  
  284.         % Value changed function: TremoloSwitch
  285.         function TremoloSwitchValueChanged(app, event)
  286.             app.Tremolo = app.Tremolo*-1;
  287.            
  288.         end
  289.  
  290.         % Value changed function: WahWahSwitch
  291.         function WahWahSwitchValueChanged(app, event)
  292.             app.WahWah = app.WahWah*-1;
  293.            
  294.         end
  295.     end
  296.  
  297.     % App initialization and construction
  298.     methods (Access = private)
  299.  
  300.         % Create UIFigure and components
  301.         function createComponents(app)
  302.  
  303.             % Create UIFigure
  304.             app.UIFigure = uifigure;
  305.             app.UIFigure.Position = [100 100 648 420];
  306.             app.UIFigure.Name = 'UI Figure';
  307.  
  308.             % Create AChordButton
  309.             app.AChordButton = uibutton(app.UIFigure, 'push');
  310.             app.AChordButton.ButtonPushedFcn = createCallbackFcn(app, @AChordButtonPushed, true);
  311.             app.AChordButton.Position = [34 337 90 56];
  312.             app.AChordButton.Text = 'A Chord';
  313.  
  314.             % Create CChordButton
  315.             app.CChordButton = uibutton(app.UIFigure, 'push');
  316.             app.CChordButton.ButtonPushedFcn = createCallbackFcn(app, @CChordButtonPushed, true);
  317.             app.CChordButton.Position = [232 337 90 56];
  318.             app.CChordButton.Text = 'C Chord';
  319.  
  320.             % Create AmChordButton
  321.             app.AmChordButton = uibutton(app.UIFigure, 'push');
  322.             app.AmChordButton.ButtonPushedFcn = createCallbackFcn(app, @AmChordButtonPushed, true);
  323.             app.AmChordButton.Position = [133 337 90 56];
  324.             app.AmChordButton.Text = 'Am Chord';
  325.  
  326.             % Create DChordButton
  327.             app.DChordButton = uibutton(app.UIFigure, 'push');
  328.             app.DChordButton.ButtonPushedFcn = createCallbackFcn(app, @DChordButtonPushed, true);
  329.             app.DChordButton.Position = [331 337 90 56];
  330.             app.DChordButton.Text = 'D Chord';
  331.  
  332.             % Create D7ChordButton
  333.             app.D7ChordButton = uibutton(app.UIFigure, 'push');
  334.             app.D7ChordButton.ButtonPushedFcn = createCallbackFcn(app, @D7ChordButtonPushed, true);
  335.             app.D7ChordButton.Position = [430 337 90 56];
  336.             app.D7ChordButton.Text = 'D7 Chord';
  337.  
  338.             % Create EButton_2
  339.             app.EButton_2 = uibutton(app.UIFigure, 'push');
  340.             app.EButton_2.ButtonPushedFcn = createCallbackFcn(app, @EButton_2Pushed, true);
  341.             app.EButton_2.Position = [144 219 50 42];
  342.             app.EButton_2.Text = 'E';
  343.  
  344.             % Create AButton
  345.             app.AButton = uibutton(app.UIFigure, 'push');
  346.             app.AButton.ButtonPushedFcn = createCallbackFcn(app, @AButtonPushed, true);
  347.             app.AButton.Position = [209 219 50 42];
  348.             app.AButton.Text = 'A';
  349.  
  350.             % Create DButton
  351.             app.DButton = uibutton(app.UIFigure, 'push');
  352.             app.DButton.ButtonPushedFcn = createCallbackFcn(app, @DButtonPushed, true);
  353.             app.DButton.Position = [274 219 50 42];
  354.             app.DButton.Text = 'D';
  355.  
  356.             % Create GButton
  357.             app.GButton = uibutton(app.UIFigure, 'push');
  358.             app.GButton.ButtonPushedFcn = createCallbackFcn(app, @GButtonPushed, true);
  359.             app.GButton.Position = [339 219 50 42];
  360.             app.GButton.Text = 'G';
  361.  
  362.             % Create BButton
  363.             app.BButton = uibutton(app.UIFigure, 'push');
  364.             app.BButton.ButtonPushedFcn = createCallbackFcn(app, @BButtonPushed, true);
  365.             app.BButton.Position = [404 219 50 42];
  366.             app.BButton.Text = 'B';
  367.  
  368.             % Create EButton_7
  369.             app.EButton_7 = uibutton(app.UIFigure, 'push');
  370.             app.EButton_7.ButtonPushedFcn = createCallbackFcn(app, @EButton_7Pushed, true);
  371.             app.EButton_7.Position = [469 219 50 42];
  372.             app.EButton_7.Text = 'E';
  373.  
  374.             % Create EChordButton
  375.             app.EChordButton = uibutton(app.UIFigure, 'push');
  376.             app.EChordButton.ButtonPushedFcn = createCallbackFcn(app, @EChordButtonPushed, true);
  377.             app.EChordButton.Position = [529 337 90 56];
  378.             app.EChordButton.Text = 'E Chord';
  379.  
  380.             % Create EmChordButton
  381.             app.EmChordButton = uibutton(app.UIFigure, 'push');
  382.             app.EmChordButton.ButtonPushedFcn = createCallbackFcn(app, @EmChordButtonPushed, true);
  383.             app.EmChordButton.Position = [183 270 90 56];
  384.             app.EmChordButton.Text = 'Em Chord';
  385.  
  386.             % Create FChordButton
  387.             app.FChordButton = uibutton(app.UIFigure, 'push');
  388.             app.FChordButton.ButtonPushedFcn = createCallbackFcn(app, @FChordButtonPushed, true);
  389.             app.FChordButton.Position = [282 270 90 56];
  390.             app.FChordButton.Text = 'F Chord';
  391.  
  392.             % Create GChordButton
  393.             app.GChordButton = uibutton(app.UIFigure, 'push');
  394.             app.GChordButton.ButtonPushedFcn = createCallbackFcn(app, @GChordButtonPushed, true);
  395.             app.GChordButton.Position = [380 270 90 56];
  396.             app.GChordButton.Text = 'G Chord';
  397.  
  398.             % Create VolumeSliderLabel
  399.             app.VolumeSliderLabel = uilabel(app.UIFigure);
  400.             app.VolumeSliderLabel.HorizontalAlignment = 'right';
  401.             app.VolumeSliderLabel.Position = [34 60 46 22];
  402.             app.VolumeSliderLabel.Text = 'Volume';
  403.  
  404.             % Create VolumeSlider
  405.             app.VolumeSlider = uislider(app.UIFigure);
  406.             app.VolumeSlider.Limits = [0 1];
  407.             app.VolumeSlider.MajorTicks = [0 1];
  408.             app.VolumeSlider.MajorTickLabels = {'Min', 'Max', ''};
  409.             app.VolumeSlider.ValueChangedFcn = createCallbackFcn(app, @VolumeSliderValueChanged, true);
  410.             app.VolumeSlider.Position = [101 69 506 3];
  411.             app.VolumeSlider.Value = 0.5;
  412.  
  413.             % Create SustainSliderLabel
  414.             app.SustainSliderLabel = uilabel(app.UIFigure);
  415.             app.SustainSliderLabel.HorizontalAlignment = 'right';
  416.             app.SustainSliderLabel.Position = [34 114 46 22];
  417.             app.SustainSliderLabel.Text = 'Sustain';
  418.  
  419.             % Create SustainSlider
  420.             app.SustainSlider = uislider(app.UIFigure);
  421.             app.SustainSlider.Limits = [0.49 0.50005];
  422.             app.SustainSlider.MajorTicks = [0.46 0.50005];
  423.             app.SustainSlider.MajorTickLabels = {'Min', 'Max', ''};
  424.             app.SustainSlider.ValueChangedFcn = createCallbackFcn(app, @SustainSliderValueChanged, true);
  425.             app.SustainSlider.Position = [101 123 505 3];
  426.             app.SustainSlider.Value = 0.498;
  427.  
  428.             % Create DistortionSwitchLabel
  429.             app.DistortionSwitchLabel = uilabel(app.UIFigure);
  430.             app.DistortionSwitchLabel.HorizontalAlignment = 'center';
  431.             app.DistortionSwitchLabel.Position = [447 145 56 22];
  432.             app.DistortionSwitchLabel.Text = 'Distortion';
  433.  
  434.             % Create DistortionSwitch
  435.             app.DistortionSwitch = uiswitch(app.UIFigure, 'rocker');
  436.             app.DistortionSwitch.Orientation = 'horizontal';
  437.             app.DistortionSwitch.ValueChangedFcn = createCallbackFcn(app, @DistortionSwitchValueChanged, true);
  438.             app.DistortionSwitch.Position = [452 175 45 20];
  439.  
  440.             % Create TremoloSwitchLabel
  441.             app.TremoloSwitchLabel = uilabel(app.UIFigure);
  442.             app.TremoloSwitchLabel.HorizontalAlignment = 'center';
  443.             app.TremoloSwitchLabel.Position = [313 145 49 22];
  444.             app.TremoloSwitchLabel.Text = 'Tremolo';
  445.  
  446.             % Create TremoloSwitch
  447.             app.TremoloSwitch = uiswitch(app.UIFigure, 'rocker');
  448.             app.TremoloSwitch.Orientation = 'horizontal';
  449.             app.TremoloSwitch.ValueChangedFcn = createCallbackFcn(app, @TremoloSwitchValueChanged, true);
  450.             app.TremoloSwitch.Position = [314 175 45 20];
  451.  
  452.             % Create WahWahSwitchLabel
  453.             app.WahWahSwitchLabel = uilabel(app.UIFigure);
  454.             app.WahWahSwitchLabel.HorizontalAlignment = 'center';
  455.             app.WahWahSwitchLabel.Position = [160.5 145 58 22];
  456.             app.WahWahSwitchLabel.Text = 'Wah-Wah';
  457.  
  458.             % Create WahWahSwitch
  459.             app.WahWahSwitch = uiswitch(app.UIFigure, 'rocker');
  460.             app.WahWahSwitch.Orientation = 'horizontal';
  461.             app.WahWahSwitch.ValueChangedFcn = createCallbackFcn(app, @WahWahSwitchValueChanged, true);
  462.             app.WahWahSwitch.Position = [166 175 45 20];
  463.         end
  464.     end
  465.  
  466.     methods (Access = public)
  467.  
  468.         % Construct app
  469.         function app = DebugApp
  470.  
  471.             % Create and configure components
  472.             createComponents(app)
  473.  
  474.             % Register the app with App Designer
  475.             registerApp(app, app.UIFigure)
  476.  
  477.             if nargout == 0
  478.                 clear app
  479.             end
  480.         end
  481.  
  482.         % Code that executes before app deletion
  483.         function delete(app)
  484.  
  485.             % Delete UIFigure when app is deleted
  486.             delete(app.UIFigure)
  487.         end
  488.     end
  489. end
Add Comment
Please, Sign In to add comment