view listeningTest/playSound.m @ 15:24be5e9ce25b tip

Update README
author Brecht De Man <brecht.deman@bcu.ac.uk>
date Thu, 20 Sep 2018 12:23:20 +0200
parents 4fd284285159
children
line wrap: on
line source
function playSound(snd)

global soundID;

if isempty(snd) % play no sound (= stop) by calling playSound([])
    if ~isempty(soundID)
        stop(soundID);
    end
    
else
    if isempty(snd.vect) % read sound (if ~DOWNLOAD)
        if isempty(snd.path)
            snd.path=cd;
        end
        
        [svect,sFs] = audioread([snd.path snd.name '.wav']);
        curSample = get(soundID,'CurrentSample');
        if length(curSample)<1 % if empty vector (first time)
            curSample = 1;
        else if curSample > length(svect)-0.5*sFs % if near end
                curSample = 1;
            end
        end
        soundID = audioplayer(svect,sFs, 24); % 24 bit, default device
        stop(soundID);
        play(soundID, curSample);
        
        
        % CLEAN UP
    else
        curSample = get(soundID,'CurrentSample');
        if length(curSample)<1 % if empty vector (first time)
            curSample = 1;
        else if curSample > length(snd.vect)-25000 % if near end
                curSample = 1;
            end
        end
        soundID = audioplayer(snd.vect,snd.fs, 24); % 24 bit, default device
        stop(soundID);
        play(soundID, curSample);
    end
    
    % TODO after this: button back to green?
    
end