Lazaro Hernandez asked . 2024-03-20

How to filter music in real time without stopping the music from playing?

Hello, I'm currently working to make a 10-band equalizer. I am using appdesigner. Currently I get the mp3 song using uigetfile then use the audioread function. After which I filter the song and then use audioplayer to play the song. What I would like to do is play the song and filter while the song keeps playing. I don't know if this is possible with MATLAB. I have sliders for the gain of each band and one for volume. I would like to adust the gain of any band and then filter the song but continue playing. Currently I have to stop the song re filter and press play and start the music from the beggining. Is what I'm asking possible and if so could I be pointed in the right direction? My code so far is below as well as a picture of what the app currently looks like. 

 

classdef musicPlayer < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure            matlab.ui.Figure
        SongEditFieldLabel  matlab.ui.control.Label
        SongEditField       matlab.ui.control.EditField
        BrowseButton        matlab.ui.control.Button
        PlayButton          matlab.ui.control.Button
        PauseButton         matlab.ui.control.Button
        StopButton          matlab.ui.control.Button
        VolumeKnobLabel     matlab.ui.control.Label
        VolumeKnob          matlab.ui.control.Knob
        HzSlider_3Label     matlab.ui.control.Label
        HzSlider_3          matlab.ui.control.Slider
        HzSlider_4Label     matlab.ui.control.Label
        HzSlider_4          matlab.ui.control.Slider
        HzSlider_5Label     matlab.ui.control.Label
        HzSlider_5          matlab.ui.control.Slider
        kHzSliderLabel      matlab.ui.control.Label
        kHzSlider           matlab.ui.control.Slider
        kHzSlider_2Label    matlab.ui.control.Label
        kHzSlider_2         matlab.ui.control.Slider
        kHzSlider_3Label    matlab.ui.control.Label
        kHzSlider_3         matlab.ui.control.Slider
        kHzSlider_4Label    matlab.ui.control.Label
        kHzSlider_4         matlab.ui.control.Slider
        kHzSlider_5Label    matlab.ui.control.Label
        kHzSlider_5         matlab.ui.control.Slider
        HipHopButton        matlab.ui.control.Button
        LatinButton         matlab.ui.control.Button
        RockButton          matlab.ui.control.Button
        RBButton            matlab.ui.control.Button
        HzSlider_6Label     matlab.ui.control.Label
        HzSlider_1          matlab.ui.control.Slider
        HzSlider_7Label     matlab.ui.control.Label
        HzSlider_2          matlab.ui.control.Slider
    end

    
    properties (Access = private)
        song;
        path;
        
        y;
        Fs;
        Fn;
        audio;
        
        yFiltered32;
        yFiltered64;
        yFiltered130;
        yFiltered270;
        yFiltered560;
        yFiltered1k;
        yFiltered2k;
        yFiltered4k;
        yFiltered8k;
        yFiltered16k;
        yFiltered;
        yFilteredWithVolume;
        
        paused = 0;
    end
    
    methods (Access = private)
        
        function yFilt = lowpassFirFilt(app, order, Fcutoff)
            a = 1;
            b = fir1(order, Fcutoff/app.Fn, 'low');
            yFilt = filter(b, a, app.y);
        end
        
        function yFilt = bandpassFirFilt(app, order, Fcutoff, Fstopband)
            a = 1;
            b = fir1(order, [Fcutoff/app.Fn Fstopband/app.Fn], 'bandpass');
            yFilt = filter(b, a, app.y);
        end
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Button pushed function: BrowseButton
        function BrowseButtonPushed(app, event)
            [app.song, app.path] = uigetfile({'*.mp3'}, 'File Selector');
            app.SongEditField.Value = app.song;
            
            [app.y, app.Fs] = audioread(app.song);
            app.Fn = app.Fs/2;
        end

        % Button pushed function: PlayButton
        function PlayButtonPushed(app, event)
            if app.paused == 1
                resume(app.audio);
                app.paused = 0;
            elseif app.paused == 0 
                app.yFiltered32 = app.HzSlider_1.Value*app.lowpassFirFilt(4, 32);
                app.yFiltered64 = app.HzSlider_2.Value*app.bandpassFirFilt(4, 33, 64);
                app.yFiltered130 = app.HzSlider_3.Value*app.bandpassFirFilt(4, 65, 130); 
                app.yFiltered270 = app.HzSlider_4.Value*app.bandpassFirFilt(4, 131, 270);
                app.yFiltered560 = app.HzSlider_5.Value*app.bandpassFirFilt(4, 271, 560);
                app.yFiltered1k = app.kHzSlider.Value*app.bandpassFirFilt(4, 561, 1000);
                app.yFiltered2k = app.kHzSlider_2.Value*app.bandpassFirFilt(4, 1001, 2000);
                app.yFiltered4k = app.kHzSlider_3.Value*app.bandpassFirFilt(4, 2001, 4000);
                app.yFiltered8k = app.kHzSlider_4.Value*app.bandpassFirFilt(4, 4001, 8000);
                app.yFiltered16k = app.kHzSlider_5.Value*app.bandpassFirFilt(4, 8001, 16000);
                
                app.yFiltered = app.yFiltered32 + app.yFiltered64 + app.yFiltered130 + app.yFiltered270 + app.yFiltered560 + ...
                    app.yFiltered1k + app.yFiltered2k + app.yFiltered4k + app.yFiltered8k + app.yFiltered16k;
                
                app.yFilteredWithVolume = app.yFiltered*(app.VolumeKnob.Value/10);
                
                app.audio = audioplayer(app.yFilteredWithVolume, app.Fs);
                play(app.audio);
            end
        end

        % Button pushed function: PauseButton
        function PauseButtonPushed(app, event)
            pause(app.audio);
            app.paused = 1;
        end

        % Button pushed function: StopButton
        function StopButtonPushed(app, event)
            stop(app.audio);
        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 1036 761];
            app.UIFigure.Name = 'MATLAB App';

            % Create SongEditFieldLabel
            app.SongEditFieldLabel = uilabel(app.UIFigure);
            app.SongEditFieldLabel.HorizontalAlignment = 'right';
            app.SongEditFieldLabel.Position = [36 722 34 22];
            app.SongEditFieldLabel.Text = 'Song';

            % Create SongEditField
            app.SongEditField = uieditfield(app.UIFigure, 'text');
            app.SongEditField.Position = [85 722 480 22];

            % Create BrowseButton
            app.BrowseButton = uibutton(app.UIFigure, 'push');
            app.BrowseButton.ButtonPushedFcn = createCallbackFcn(app, @BrowseButtonPushed, true);
            app.BrowseButton.Position = [587 722 100 22];
            app.BrowseButton.Text = 'Browse';

            % Create PlayButton
            app.PlayButton = uibutton(app.UIFigure, 'push');
            app.PlayButton.ButtonPushedFcn = createCallbackFcn(app, @PlayButtonPushed, true);
            app.PlayButton.Position = [19 667 100 22];
            app.PlayButton.Text = 'Play';

            % Create PauseButton
            app.PauseButton = uibutton(app.UIFigure, 'push');
            app.PauseButton.ButtonPushedFcn = createCallbackFcn(app, @PauseButtonPushed, true);
            app.PauseButton.Position = [136 667 100 22];
            app.PauseButton.Text = 'Pause';

            % Create StopButton
            app.StopButton = uibutton(app.UIFigure, 'push');
            app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true);
            app.StopButton.Position = [251 667 100 22];
            app.StopButton.Text = 'Stop';

            % Create VolumeKnobLabel
            app.VolumeKnobLabel = uilabel(app.UIFigure);
            app.VolumeKnobLabel.HorizontalAlignment = 'center';
            app.VolumeKnobLabel.Position = [801 607 46 22];
            app.VolumeKnobLabel.Text = 'Volume';

            % Create VolumeKnob
            app.VolumeKnob = uiknob(app.UIFigure, 'continuous');
            app.VolumeKnob.Position = [793 663 60 60];

            % Create HzSlider_3Label
            app.HzSlider_3Label = uilabel(app.UIFigure);
            app.HzSlider_3Label.HorizontalAlignment = 'right';
            app.HzSlider_3Label.Position = [256 342 44 22];
            app.HzSlider_3Label.Text = '130 Hz';

            % Create HzSlider_3
            app.HzSlider_3 = uislider(app.UIFigure);
            app.HzSlider_3.Limits = [-10 10];
            app.HzSlider_3.Orientation = 'vertical';
            app.HzSlider_3.Position = [256 372 3 150];
            app.HzSlider_3.Value = 1;

            % Create HzSlider_4Label
            app.HzSlider_4Label = uilabel(app.UIFigure);
            app.HzSlider_4Label.HorizontalAlignment = 'right';
            app.HzSlider_4Label.Position = [366 341 44 22];
            app.HzSlider_4Label.Text = '270 Hz';

            % Create HzSlider_4
            app.HzSlider_4 = uislider(app.UIFigure);
            app.HzSlider_4.Limits = [-10 10];
            app.HzSlider_4.Orientation = 'vertical';
            app.HzSlider_4.Position = [370 371 3 150];
            app.HzSlider_4.Value = 1;

            % Create HzSlider_5Label
            app.HzSlider_5Label = uilabel(app.UIFigure);
            app.HzSlider_5Label.HorizontalAlignment = 'right';
            app.HzSlider_5Label.Position = [477 344 44 22];
            app.HzSlider_5Label.Text = '560 Hz';

            % Create HzSlider_5
            app.HzSlider_5 = uislider(app.UIFigure);
            app.HzSlider_5.Limits = [-10 10];
            app.HzSlider_5.Orientation = 'vertical';
            app.HzSlider_5.Position = [481 372 3 150];
            app.HzSlider_5.Value = 1;

            % Create kHzSliderLabel
            app.kHzSliderLabel = uilabel(app.UIFigure);
            app.kHzSliderLabel.HorizontalAlignment = 'right';
            app.kHzSliderLabel.Position = [580 347 36 22];
            app.kHzSliderLabel.Text = '1 kHz';

            % Create kHzSlider
            app.kHzSlider = uislider(app.UIFigure);
            app.kHzSlider.Limits = [-10 10];
            app.kHzSlider.Orientation = 'vertical';
            app.kHzSlider.Position = [580 374 3 150];
            app.kHzSlider.Value = 1;

            % Create kHzSlider_2Label
            app.kHzSlider_2Label = uilabel(app.UIFigure);
            app.kHzSlider_2Label.HorizontalAlignment = 'right';
            app.kHzSlider_2Label.Position = [679 348 36 22];
            app.kHzSlider_2Label.Text = '2 kHz';

            % Create kHzSlider_2
            app.kHzSlider_2 = uislider(app.UIFigure);
            app.kHzSlider_2.Limits = [-10 10];
            app.kHzSlider_2.Orientation = 'vertical';
            app.kHzSlider_2.Position = [679 378 3 150];
            app.kHzSlider_2.Value = 1;

            % Create kHzSlider_3Label
            app.kHzSlider_3Label = uilabel(app.UIFigure);
            app.kHzSlider_3Label.HorizontalAlignment = 'right';
            app.kHzSlider_3Label.Position = [768 353 36 22];
            app.kHzSlider_3Label.Text = '4 kHz';

            % Create kHzSlider_3
            app.kHzSlider_3 = uislider(app.UIFigure);
            app.kHzSlider_3.Limits = [-10 10];
            app.kHzSlider_3.Orientation = 'vertical';
            app.kHzSlider_3.Position = [775 377 3 150];
            app.kHzSlider_3.Value = 1;

            % Create kHzSlider_4Label
            app.kHzSlider_4Label = uilabel(app.UIFigure);
            app.kHzSlider_4Label.HorizontalAlignment = 'right';
            app.kHzSlider_4Label.Position = [878 350 36 22];
            app.kHzSlider_4Label.Text = '8 kHz';

            % Create kHzSlider_4
            app.kHzSlider_4 = uislider(app.UIFigure);
            app.kHzSlider_4.Limits = [-10 10];
            app.kHzSlider_4.Orientation = 'vertical';
            app.kHzSlider_4.Position = [881 375 3 150];
            app.kHzSlider_4.Value = 1;

            % Create kHzSlider_5Label
            app.kHzSlider_5Label = uilabel(app.UIFigure);
            app.kHzSlider_5Label.HorizontalAlignment = 'right';
            app.kHzSlider_5Label.Position = [977 353 43 22];
            app.kHzSlider_5Label.Text = '16 kHz';

            % Create kHzSlider_5
            app.kHzSlider_5 = uislider(app.UIFigure);
            app.kHzSlider_5.Limits = [-10 10];
            app.kHzSlider_5.Orientation = 'vertical';
            app.kHzSlider_5.Position = [980 378 3 150];
            app.kHzSlider_5.Value = 1;

            % Create HipHopButton
            app.HipHopButton = uibutton(app.UIFigure, 'push');
            app.HipHopButton.Position = [180 220 100 22];
            app.HipHopButton.Text = 'Hip Hop';

            % Create LatinButton
            app.LatinButton = uibutton(app.UIFigure, 'push');
            app.LatinButton.Position = [385 220 100 22];
            app.LatinButton.Text = 'Latin';

            % Create RockButton
            app.RockButton = uibutton(app.UIFigure, 'push');
            app.RockButton.Position = [793 220 100 22];
            app.RockButton.Text = 'Rock';

            % Create RBButton
            app.RBButton = uibutton(app.UIFigure, 'push');
            app.RBButton.Position = [587 220 100 22];
            app.RBButton.Text = 'R & B';

            % Create HzSlider_6Label
            app.HzSlider_6Label = uilabel(app.UIFigure);
            app.HzSlider_6Label.HorizontalAlignment = 'right';
            app.HzSlider_6Label.Position = [45 342 37 22];
            app.HzSlider_6Label.Text = '32 Hz';

            % Create HzSlider_1
            app.HzSlider_1 = uislider(app.UIFigure);
            app.HzSlider_1.Limits = [-10 10];
            app.HzSlider_1.Orientation = 'vertical';
            app.HzSlider_1.Position = [52 374 3 150];
            app.HzSlider_1.Value = 1;

            % Create HzSlider_7Label
            app.HzSlider_7Label = uilabel(app.UIFigure);
            app.HzSlider_7Label.HorizontalAlignment = 'right';
            app.HzSlider_7Label.Position = [147 340 37 22];
            app.HzSlider_7Label.Text = '64 Hz';

            % Create HzSlider_2
            app.HzSlider_2 = uislider(app.UIFigure);
            app.HzSlider_2.Limits = [-10 10];
            app.HzSlider_2.Orientation = 'vertical';
            app.HzSlider_2.Position = [152 370 3 150];
            app.HzSlider_2.Value = 1;

            % 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 = musicPlayer

            % 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

Signal Processing , Audio Toolbox , Audio Processing Algorithm Design , realtime , filter

Expert Answer

Prashant Kumar answered . 2024-04-27 08:27:44

Lazaro - I don't think that changing the audio player samples is permitted (at least I can't see any functions to get or set the samples) so you would need to stop the player and create a new one...though you may be able to play from where the last one left off. If you pause the player and then get it's CurrentSample property you may be able to use it when starting the next player. For example, to get that sample you could do

 

load handel.mat;
handelObj = audioplayer(y, Fs);
play(handelObj);
pause(1);
pause(handelObj);
atCurrentSample = get(handelObj, 'CurrentSample')
I'm not sure how accurate it is but you could then use atCurrentSample when calling play on your other/new audio player. So the steps would be
  1. Apply filters to audio data samples.
  2. Pause player and get current sample.
  3. Stop player.
  4. Create new player with filtered audio samples.
  5. Play player and pass in atCurrentSample as the start sample.


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.