Daniel@0: function varargout = mirplay(a,varargin) Daniel@0: % mirplay(a) plays audio signal, envelope, or pitches. Daniel@0: % If a is an envelope, what is actually played is a white noise of Daniel@0: % same envelope. Daniel@0: % If a is a mirpitch object, pitches are played using sinusoids. Daniel@0: % Optional arguments: Daniel@0: % mirplay(...,'Channel',i) plays the channel(s) of rank(s) indicated by Daniel@0: % the array i. Daniel@0: % mirplay(...,'Segment',k) plays the segment(s) of rank(s) indicated by Daniel@0: % the array k. Daniel@0: % mirplay(...,'Sequence',l) plays the sequence(s) of rank(s) indicated Daniel@0: % by the array l. Daniel@0: % mirplay(...,'Increasing',d) plays the sequences in increasing order Daniel@0: % of d, which could be either an array or a mirscalar data. Daniel@0: % mirplay(...,'Decreasing',d) plays the sequences in decreasing order Daniel@0: % of d, which could be either an array or a mirscalar data. Daniel@0: % mirplay(...,'Every',s) plays every s sequence, where s is a number Daniel@0: % indicating the step between sequences. Daniel@0: % mirplay(...,'Burst',0) toggles off the burst sound between Daniel@0: % segments. Daniel@0: % Example: mirplay(mirenvelope('Folder'),... Daniel@0: % 'increasing', mirrms('Folder'),... Daniel@0: % 'every',5) Daniel@0: Daniel@0: if ischar(a) Daniel@0: varargout = mirplay(miraudio(a),varargin{:}); Daniel@0: elseif isscalar(a) Daniel@0: ch.key = 'Channel'; Daniel@0: ch.type = 'Integer'; Daniel@0: ch.default = 0; Daniel@0: option.ch = ch; Daniel@0: Daniel@0: sg.key = 'Segment'; Daniel@0: sg.type = 'Integer'; Daniel@0: sg.default = 0; Daniel@0: option.sg = sg; Daniel@0: Daniel@0: se.key = 'Sequence'; Daniel@0: se.type = 'Integer'; Daniel@0: se.default = 0; Daniel@0: option.se = se; Daniel@0: Daniel@0: inc.key = 'Increasing'; Daniel@0: inc.type = 'MIRtb'; Daniel@0: option.inc = inc; Daniel@0: Daniel@0: dec.key = 'Decreasing'; Daniel@0: dec.type = 'MIRtb'; Daniel@0: option.dec = dec; Daniel@0: Daniel@0: every.key = 'Every'; Daniel@0: every.type = 'Integer'; Daniel@0: option.every = every; Daniel@0: Daniel@0: burst.key = 'Burst'; Daniel@0: burst.type = 'Boolean'; Daniel@0: burst.default = 1; Daniel@0: option.burst = burst; Daniel@0: Daniel@0: specif.option = option; Daniel@0: Daniel@0: specif.eachchunk = 'Normal'; Daniel@0: Daniel@0: varargout = mirfunction(@mirplay,a,varargin,nargout,specif,@init,@main); Daniel@0: if nargout == 0 Daniel@0: varargout = {}; Daniel@0: end Daniel@0: else Daniel@0: mirerror('mirplay','You cannot play this type of object.') Daniel@0: end Daniel@0: Daniel@0: Daniel@0: function [x type] = init(x,option) Daniel@0: type = ''; Daniel@0: Daniel@0: Daniel@0: function noargout = main(a,option,postoption) Daniel@0: if iscell(a) Daniel@0: a = a{1}; Daniel@0: end Daniel@0: d = get(a,'Data'); Daniel@0: if isa(a,'mirpitch') Daniel@0: amp = get(a,'Amplitude'); Daniel@0: end Daniel@0: f = get(a,'Sampling'); Daniel@0: n = get(a,'Name'); Daniel@0: c = get(a,'Channels'); Daniel@0: fp = get(a,'FramePos'); Daniel@0: if not(option.se) Daniel@0: if length(d)>1 Daniel@0: if isfield(option,'inc') Daniel@0: [unused order] = sort(mirgetdata(option.inc)); Daniel@0: elseif isfield(option,'dec') Daniel@0: [unused order] = sort(mirgetdata(option.dec),'descend'); Daniel@0: else Daniel@0: order = 1:length(d); Daniel@0: end Daniel@0: if isfield(option,'every') Daniel@0: order = order(1:option.every:end); Daniel@0: end Daniel@0: else Daniel@0: order = 1; Daniel@0: end Daniel@0: else Daniel@0: order = option.se; Daniel@0: end Daniel@0: if not(isempty(order)) Daniel@0: for k = order(:)' Daniel@0: display(['Playing analysis of file: ' n{k}]) Daniel@0: dk = d{k}; Daniel@0: if not(iscell(dk)) Daniel@0: dk = {dk}; Daniel@0: end Daniel@0: if option.ch Daniel@0: if isempty(c{k}) Daniel@0: chk = option.ch; Daniel@0: else Daniel@0: [unused unused chk] = intersect(option.ch,c{k}); Daniel@0: end Daniel@0: else Daniel@0: chk = 1:size(dk{1},3); Daniel@0: end Daniel@0: if isempty(chk) Daniel@0: display('No channel to play.'); Daniel@0: end Daniel@0: for l = chk Daniel@0: if chk(end)>1 Daniel@0: display([' Playing channel #' num2str(l)]); Daniel@0: end Daniel@0: if option.sg Daniel@0: sgk = option.sg(find(option.sg<=length(dk))); Daniel@0: else Daniel@0: sgk = 1:length(dk); Daniel@0: end Daniel@0: for i = sgk Daniel@0: if sgk(end)>1 Daniel@0: display([' Playing segment #' num2str(i)]) Daniel@0: end Daniel@0: di = dk{i}; Daniel@0: if isa(a,'mirpitch') Daniel@0: ampi = amp{k}{i}; Daniel@0: end Daniel@0: synth = zeros(1,ceil((fp{k}{i}(end)-fp{k}{i}(1))*44100)+1); Daniel@0: for j = 1:size(di,2) Daniel@0: if iscell(di) Daniel@0: dj = di{j}; Daniel@0: else Daniel@0: dj = di(:,j); Daniel@0: end Daniel@0: dj(isnan(dj)) = 0; Daniel@0: if isa(a,'mirpitch') Daniel@0: ampj = zeros(size(dj)); Daniel@0: if iscell(ampi) Daniel@0: ampj(1:size(ampi{j})) = ampi{j}; Daniel@0: else Daniel@0: ampj(1:size(ampi(:,j))) = ampi(:,j); Daniel@0: end Daniel@0: end Daniel@0: if not(isempty(dj)) Daniel@0: k1 = floor((fp{k}{i}(1,j)-fp{k}{i}(1))*44100)+1; Daniel@0: k2 = floor((fp{k}{i}(2,j)-fp{k}{i}(1))*44100)+1; Daniel@0: if isa(a,'mirpitch') Daniel@0: ampj = repmat(ampj,1,k2-k1+1); Daniel@0: else Daniel@0: ampj = ones(size(dj),k2-k1+1); Daniel@0: end Daniel@0: synth(k1:k2) = synth(k1:k2) ... Daniel@0: + sum(ampj.*sin(2*pi*dj*(0:k2-k1)/44100),1) ... Daniel@0: .*hann(k2-k1+1)'; Daniel@0: %plot((ampj.*sin(2*pi*dj*(0:k2-k1)/44100))') Daniel@0: %drawnow Daniel@0: end Daniel@0: end Daniel@0: soundsc(synth,44100); Daniel@0: if option.burst && sgk(end)>1 Daniel@0: sound(rand(1,10)) Daniel@0: end Daniel@0: %pause(0.5) Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: noargout = {};