image thumbnail

wavplayer

by

 

01 Oct 2012 (Updated )

An interactive sound (.wav) player for MATLAB using the built-in audioplayer function.

wavplayer(varargin)
function wavplayer(varargin)
%---------------------------------------------------------------------------------------------------%
%                                           % wavplayer %                                           % 
%---------------------------------------------------------------------------------------------------%
  
  % Color Palette
    color01 = [127 127 127]./255;
    color02 = [063 063 063]./255;
    color03 = [255 192 000]./255;
    color04 = [000 176 080]./255;
    color05 = [059 089 182]./255;
    color06 = [192 081 058]./255;
    color07 = [000 000 000]./255;
    color08 = [247 127 000]./255;
    color09 = [200 200 200]./255;
    
  % Window Dimensions
    screenSize = get(0,'ScreenSize');
    ht.fig = 292; wt.fig = 512.5;
    xt.fig = screenSize(3)/2-wt.fig/2; yt.fig = screenSize(4)/2-ht.fig/2;
    pos.fig = [xt.fig yt.fig wt.fig ht.fig];
    
  % Figure
    wav.fig = figure(...
                     'Color',color01,...
                     'CloseRequestFcn',@closeFcn,...
                     'DockControls','off',...
                     'Menubar','none',...
                     'Name','wavplayer',...
                     'Numbertitle','off',...
                     'Position',pos.fig,...
                     'Resize','off',...
                     'WindowStyle','normal',...
                     'Visible','off');
                 
  % Panel
    ht.pn1 = 272.5; wt.pn1 = 495; pos.pn1 = [10 10 wt.pn1 ht.pn1];
    wav.pn1 = uipanel(...
                     'BackgroundColor',color02,...
                     'BorderType','none',...
                     'Parent',wav.fig,...
                     'Units','pixels',...
                     'Position',pos.pn1);
                 
    ht.pn2 = 190; wt.pn2 = 482.5; pos.pn2 = [10 90 wt.pn2 ht.pn2];
    wav.pn2 = uipanel(...
                     'BackgroundColor',color02,...
                     'BorderType','none',...
                     'Parent',wav.fig,...
                     'Units','pixels',...
                     'Position',pos.pn2);
                 
  % Axes
    ht.ax1 = 120; wt.ax1 = 522; pos.ax1 = [11.25 60 wt.ax1 ht.ax1];
    wav.ax1 = axes(...
                     'Box','on',...
                     'Color',color07,...
                     'HandleVisibility','callback',...
                     'Parent',wav.pn2,...
                     'Units','pixels',...
                     'XTickLabel',[],...
                     'YTickLabel',[],...
                     'Position',pos.ax1);
    
  % Progress Bar
    ht.pr8 = 15; wt.pr8 = 517.5; pos.pr8 = [-1 127.5 wt.pr8 ht.pr8];
    lbl = '00 : 00 . 000'; [prh08,wav.pr08] = prog(lbl,pos.pr8,color01);
    set(wav.pr08,'Parent',wav.fig);
                 
  % Buttons  
    % Button 'OPEN'
    ht.pb1 = 50; wt.pb1 = 110; pos.pb1 = [11.5 11.5 wt.pb1 ht.pb1];
    lbl1 = '<HTML><FONT face="Arial Bold" color="rgb(0,0,0)" size=4>';
    lbl2 = '<center>&#9679;</center>OPEN'; lbl3 = '</FONT></HTML>';
    lbl = strcat(lbl1,lbl2,lbl3); [pbh01,wav.pb01] = push(lbl,pos.pb1,color03);
    set(wav.pb01,'Parent',wav.pn1); set(pbh01,'ActionPerformedCallback',@ugetwav)
                 
    % Button 'PLAY'
    ht.pb2 = 50; wt.pb2 = 110; pos.pb2 = [132.5 11.5 wt.pb2 ht.pb2];
    lbl1 = '<HTML><FONT face="Arial Bold" color="rgb(0,0,0)" size=4>';
    lbl2 = '<center>&#9658;</center>PLAY'; lbl3 = '</FONT></HTML>';
    lbl = strcat(lbl1,lbl2,lbl3); [pbh02,wav.pb02] = push(lbl,pos.pb2,color04);
    set(wav.pb02,'Parent',wav.pn1); set(pbh02,'ActionPerformedCallback',@playsnd)

    % Button 'PAUSE'
    ht.pb3 = 50; wt.pb3 = 110; pos.pb3 = [253.5 11.5 wt.pb3 ht.pb3];
    lbl1 = '<HTML><FONT face="Arial Bold" color="rgb(0,0,0)" size=4>';
    lbl2 = '<center>&#8214;</center>PAUSE'; lbl3 = '</FONT></HTML>';
    lbl = strcat(lbl1,lbl2,lbl3); [pbh03,wav.pb03] = push(lbl,pos.pb3,color05);
    set(wav.pb03,'Parent',wav.pn1); set(pbh03,'ActionPerformedCallback',@pausesnd)

    % Button 'STOP'
    ht.pb4 = 50; wt.pb4 = 110; pos.pb4 = [374.5 11.5 wt.pb4 ht.pb4];
    lbl1 = '<HTML><FONT face="Arial Bold" color="rgb(0,0,0)" size=4>';
    lbl2 = '<center>&#9632;</center>STOP'; lbl3 = '</FONT></HTML>';
    lbl = strcat(lbl1,lbl2,lbl3); [pbh04,wav.pb04] = push(lbl,pos.pb4,color06);
    set(wav.pb04,'Parent',wav.pn1); set(pbh04,'ActionPerformedCallback',@stopsnd)
    
    % Button 'Pitch DOWN'
    ht.pb5 = 40; wt.pb5 = 110; pos.pb5 = [11.5 71.5 wt.pb5 ht.pb5];
    lbl1 = '<HTML><FONT face="Arial Bold" color="rgb(0,0,0)" size=5>';
    lbl2 = '<center>&#9837;'; lbl3 = '</FONT></HTML>';
    lbl = strcat(lbl1,lbl2,lbl3); [pbh05,wav.pb05] = push(lbl,pos.pb5,color09);
    set(wav.pb05,'Parent',wav.pn1); set(pbh05,'ActionPerformedCallback',@pitchD)
                 
    % Button 'Pitch UP'
    ht.pb6 = 40; wt.pb6 = 110; pos.pb6 = [132.5 71.5 wt.pb6 ht.pb6];
    lbl1 = '<HTML><FONT face="Arial Bold" color="rgb(0,0,0)" size=5>';
    lbl2 = '<center>&#9839;'; lbl3 = '</FONT></HTML>';
    lbl = strcat(lbl1,lbl2,lbl3); [pbh06,wav.pb06] = push(lbl,pos.pb6,color09);
    set(wav.pb06,'Parent',wav.pn1); set(pbh06,'ActionPerformedCallback',@pitchU)

    % Button 'Volume UP'
    ht.pb7 = 40; wt.pb7 = 110; pos.pb7 = [253.5 71.5 wt.pb7 ht.pb7];
    lbl1 = '<HTML><FONT face="Arial Bold" color="rgb(0,0,0)" size=5>';
    lbl2 = '<center>&#9650;'; lbl3 = '</FONT></HTML>';
    lbl = strcat(lbl1,lbl2,lbl3); [pbh07,wav.pb07] = push(lbl,pos.pb7,color09);
    set(wav.pb07,'Parent',wav.pn1); set(pbh07,'ActionPerformedCallback',@volU)

    % Button 'Volume DOWN'
    ht.pb8 = 40; wt.pb8 = 110; pos.pb8 = [374.5 71.5 wt.pb8 ht.pb8];
    lbl1 = '<HTML><FONT face="Arial Bold" color="rgb(0,0,0)" size=5>';
    lbl2 = '<center>&#9660;'; lbl3 = '</FONT></HTML>';
    lbl = strcat(lbl1,lbl2,lbl3); [pbh08,wav.pb08] = push(lbl,pos.pb8,color09);
    set(wav.pb08,'Parent',wav.pn1); set(pbh08,'ActionPerformedCallback',@volD)
    
    [pbh02.Enable,pbh03.Enable,pbh04.Enable] = deal(0);
    [pbh05.Enable,pbh06.Enable,pbh07.Enable,pbh08.Enable] = deal(0);
           
  % Callbacks
    function [jbh,hContainer] = push(lbl,pos,clr)
        import javax.swing.* java.awt.*
        jb = JButton(lbl);
        set(jb,'Background',(clr))
        jb.setCursor(java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jb.setContentAreaFilled(0)
        jb.setFocusable(true)
        [jbh,hContainer] = javacomponent(jb,pos);
    end

    function [jph,hContainer] = prog(lbl,pos,clr)
        jProgressBar = javax.swing.JProgressBar;
        set(jProgressBar,'Background',color01)
        set(jProgressBar,'Foreground',clr)
        jProgressBar.setValue(100);
        jProgressBar.setBorderPainted(0);
        jProgressBar.setStringPainted(true);
        jProgressBar.setString(lbl);
        [jph,hContainer] = javacomponent(jProgressBar,pos);
    end

    function closeFcn(varargin)
        global player
        if isempty(player)
            closereq;
        else
            stop(player)
            closereq;
        end
    end

    function ugetwav(varargin)
        global wavfile player fs cs ts rt pt
        try
            [fname,floc] = uigetfile('*.wav','Select the .wav file to play');
            if fname == 0
                error('No file selected!!!')
            else
                [wavfile,fs] = wavread(strcat(floc,fname));
                player = audioplayer(wavfile,fs);
                cs = get(player,'CurrentSample');
                ts = get(player,'TotalSample');
                rt = cs/fs; pt = cs/ts;
                str = formatTimeFcn(rt);
                set(prh08,'String',str,'Value',pt*100)
                pbh02.Enable = 1;
            end
        catch err
            if fname == 0
                errordlg('No file selected!!!')
            else
                rethrow(err)
            end
        end
    end

  % Playback Functions
    function playsnd(varargin)
        global wavfile player bpush b cs ts pt rt fs
        [pbh03.Enable,pbh04.Enable,pbh05.Enable] = deal(1);
        [pbh06.Enable,pbh07.Enable,pbh08.Enable] = deal(1);
        if isempty(wavfile)
            ugetwav
        else
            play(player)
            b = length(wavfile); set(player,'StartFcn',@startFcn,'StopFcn',@pauseFcn)
            for k = 1:1:b
                if isplaying(player)
                    x = 10*(k-1)+1; y = 10*(k);
                    refresh; pause(1/16); fftwav = fft(wavfile);
                    p = bar(abs(fftwav(x:y)),'Parent',wav.ax1); axis on; grid on
                    set(p,'FaceColor',color08,'LineWidth',2);
                    set(wav.ax1,'XTick',[1 2 3 4 5 6 7 8 9])
                    set(wav.ax1,'XTickLabel',[],'YTickLabel',[]);
                    set(wav.ax1,'Color',color07,'GridLineStyle','-','Box','off')
                    cs = get(player,'CurrentSample');
                    ts = get(player,'TotalSample');
                    pt = cs/ts; set(prh08,'Value',pt*100)
                    rt = cs/fs; set(prh08,'String',formatTimeFcn(rt))
                else
                    bpush = k;
                    refresh
                    break
                end
            end
        end
    end

    function pausesnd(varargin)
        global player
        pause(player)
        set(pbh02,'ActionPerformedCallback',@resumesnd)
    end

    function resumesnd(varargin)
        global player bpush b wavfile cs ts pt rt fs
        set(pbh02,'ActionPerformedCallback',@playsnd)
        resume(player)
        if ~isempty(bpush)
            for k = bpush:1:b
                if isplaying(player)
                    x = 10*(k-1)+1; y = 10*(k);
                    refresh; pause(1/16); fftwav = fft(wavfile);
                    p = bar(abs(fftwav(x:y)),'Parent',wav.ax1); axis on; grid on
                    set(p,'FaceColor',color08,'LineWidth',2);
                    set(wav.ax1,'XTick',[1 2 3 4 5 6 7 8 9])
                    set(wav.ax1,'XTickLabel',[],'YTickLabel',[]);
                    set(wav.ax1,'Color',color07,'GridLineStyle','-','Box','off')
                    cs = get(player,'CurrentSample');
                    ts = get(player,'TotalSample');
                    pt = cs/ts; set(prh08,'Value',pt*100)
                    rt = cs/fs; set(prh08,'String',formatTimeFcn(rt))
                else
                    refresh
                    break
                end
            end
        end
    end

    function stopsnd(varargin)
        global player
        stop(player)
    end

    function startFcn(varargin)
    end

    function pauseFcn(varargin)
    end

    function pitchU(varargin)
        global wavfile fs player bpush b cs ts pt rt
        if isempty(wavfile)
            ugetwav
        else
            fs = fs*2;
            player = audioplayer(wavfile,fs);
            play(player)
            b = length(wavfile); set(player,'StartFcn',@startFcn,'StopFcn',@pauseFcn)
            for k = 1:1:b
                if isplaying(player)
                    x = 10*(k-1)+1; y = 10*(k);
                    refresh; pause(1/16); fftwav = fft(wavfile);
                    p = bar(abs(fftwav(x:y)),'Parent',wav.ax1); axis on; grid on
                    set(p,'FaceColor',color08,'LineWidth',2);
                    set(wav.ax1,'XTick',[1 2 3 4 5 6 7 8 9])
                    set(wav.ax1,'XTickLabel',[],'YTickLabel',[]);
                    set(wav.ax1,'Color',color07,'GridLineStyle','-','Box','off')
                    cs = get(player,'CurrentSample');
                    ts = get(player,'TotalSample');
                    pt = cs/ts; set(prh08,'Value',pt*100)
                    rt = cs/fs; set(prh08,'String',formatTimeFcn(rt))
                else
                    bpush = k;
                    refresh
                    break
                end
            end
        end
    end

    function pitchD(varargin)
        global wavfile fs player bpush b cs ts pt rt
        if isempty(wavfile)
            ugetwav
        else
            fs = fs/2;
            player = audioplayer(wavfile,fs);
            play(player)
            b = length(wavfile); set(player,'StartFcn',@startFcn,'StopFcn',@pauseFcn)
            for k = 1:1:b
                if isplaying(player)
                    x = 10*(k-1)+1; y = 10*(k);
                    refresh; pause(1/16); fftwav = fft(wavfile);
                    p = bar(abs(fftwav(x:y)),'Parent',wav.ax1); axis on; grid on
                    set(p,'FaceColor',color08,'LineWidth',2);
                    set(wav.ax1,'XTick',[1 2 3 4 5 6 7 8 9])
                    set(wav.ax1,'XTickLabel',[],'YTickLabel',[]);
                    set(wav.ax1,'Color',color07,'GridLineStyle','-','Box','off')
                    cs = get(player,'CurrentSample');
                    ts = get(player,'TotalSample');
                    pt = cs/ts; set(prh08,'Value',pt*100)
                    rt = cs/fs; set(prh08,'String',formatTimeFcn(rt))
                else
                    bpush = k;
                    refresh
                    break
                end
            end
        end
    end

    function volU(varargin)
        global wavfile fs player bpush b rt cs ts pt
        if isempty(wavfile)
            ugetwav
        else
            wavfile = wavfile*2;
            player = audioplayer(wavfile,fs);
            play(player)
            b = length(wavfile); set(player,'StartFcn',@startFcn,'StopFcn',@pauseFcn)
            for k = 1:1:b
                if isplaying(player)
                    x = 10*(k-1)+1; y = 10*(k);
                    refresh; pause(1/16); fftwav = fft(wavfile);
                    p = bar(abs(fftwav(x:y)),'Parent',wav.ax1); axis on; grid on
                    set(p,'FaceColor',color08,'LineWidth',2);
                    set(wav.ax1,'XTick',[1 2 3 4 5 6 7 8 9])
                    set(wav.ax1,'XTickLabel',[],'YTickLabel',[]);
                    set(wav.ax1,'Color',color07,'GridLineStyle','-','Box','off')
                    cs = get(player,'CurrentSample');
                    ts = get(player,'TotalSample');
                    pt = cs/ts; set(prh08,'Value',pt*100)
                    rt = cs/fs; set(prh08,'String',formatTimeFcn(rt))
                else
                    bpush = k;
                    refresh
                    break
                end
            end
            cs = get(player,'CurrentSample');
            rt = cs/fs;
        end
    end

    function volD(varargin)
        global wavfile fs player bpush b cs ts pt rt
        if isempty(wavfile)
            ugetwav
        else
            wavfile = wavfile/2;
            player = audioplayer(wavfile,fs);
            play(player)
            b = length(wavfile); set(player,'StartFcn',@startFcn,'StopFcn',@pauseFcn)
            for k = 1:1:b
                if isplaying(player)
                    x = 10*(k-1)+1; y = 10*(k);
                    refresh; pause(1/16); fftwav = fft(wavfile);
                    p = bar(abs(fftwav(x:y)),'Parent',wav.ax1); axis on; grid on
                    set(p,'FaceColor',color08,'LineWidth',2);
                    set(wav.ax1,'XTick',[1 2 3 4 5 6 7 8 9])
                    set(wav.ax1,'XTickLabel',[],'YTickLabel',[]);
                    set(wav.ax1,'Color',color07,'GridLineStyle','-','Box','off')
                    cs = get(player,'CurrentSample');
                    ts = get(player,'TotalSample');
                    pt = cs/ts; set(prh08,'Value',pt*100)
                    rt = cs/fs; set(prh08,'String',formatTimeFcn(rt))
                else
                    bpush = k;
                    refresh
                    break
                end
            end
        end
    end

  % Time Format
    function str = formatTimeFcn(float_time)
        float_time = abs(float_time);
        hrs = floor(float_time/3600);
        mins = floor(float_time/60 - 60*hrs);
        secs = floor(float_time - 60*(mins + 60*hrs));
        dsec = floor((float_time - 60*(mins + 60*hrs) - secs)*1000);
        m = sprintf('%1.0f : ',mins);
        s = sprintf('%1.0f . ',secs);
        d = sprintf('%1.0f',dsec);
        if mins < 10
            m = sprintf('0%1.0f : ',mins);
        end
        if secs < 10
            s = sprintf('0%1.0f . ',secs);
        end
        if dsec < 100
            if float_time >= 120
                d = sprintf('000');
            else
                if dsec < 10
                d = sprintf('00%1.0f',dsec);
                else
                d = sprintf('0%1.0f',dsec);
                end
            end
        end
        str = [m s d];
    end
        
  % Make GUI visible    
    set(wav.fig,'Visible','on')
end

Contact us