classdef audio
    %% Audio object
    properties
        s  %vector containing the audio signal
        fs %sampling frequency 
        nBits % number of bits per sample
        name % string containing the name of the audio file
        format % string containing the format of the audio file
    end
    
    methods
        %% Constructor
        function obj = audio(varargin)
            error(nargchk(1,5,nargin));
            if ischar(varargin{1})
                [~, obj.name obj.format] = fileparts(varargin{1});
                switch obj.format
                    case '.wav'
                        [obj.s obj.fs obj.nBits] = wavread(varargin{1});
                    otherwise
                        error('Unsupported audio format')
                end
            else
                obj.s = varargin{1};
                if nargin>1, obj.fs = varargin{2}; else obj.fs = []; end
                if nargin>2, obj.nBits = varargin{3}; else obj.nBits = []; end
                if nargin>3, obj.name = varargin{4}; else obj.name = []; end
                if nargin>4, obj.format = varargin{5}; else obj.format = []; end
            end
        end
        
        %% Playback functions
        function player = play(obj, player)
            if ~exist('player','var') || isempty(player)
                player = audioplayer(obj.s,obj.fs);
            end
            play(player);
        end
        
        function player = stop(obj, player)
            if ~exist('player','var') || isempty(player)
                player = audioplayer(obj.s,obj.fs);
            end
            stop(player)
        end
        
        function player = pause(obj, player)
            if ~exist('player','var') || isempty(player)
                player = audioplayer(obj.s,obj.fs);
            end
            pause(player)
        end
    end
end
