How to set timer to execute a function

조회 수: 12 (최근 30일)
BeestMann
BeestMann 2016년 3월 3일
댓글: Geoff Hayes 2016년 3월 15일
Okay so this is what I'm trying to do. I'll put whatever I'm struggling with in parenthesis.
  • MATLAB loads pre-recorded WAVE file (I can use Audioread but I need more help with that)
  • MATLAB reads that pre-recorded WAVE file (Once, again, how?)
  • If time is more than 60 seconds from initial, play WAVE file (how do I set this time thing)
  • else if time is less than 60 seconds, do nothing (how do I set this time thing)
Thanks guys :)

답변 (1개)

Geoff Hayes
Geoff Hayes 2016년 3월 15일
Beestmann - audioread can be used to read the audio data from file as
filename = 'myAudioFile.wav';
[y,Fs] = audioread(filename);
Now, you can use the audio player to load (and later play) the audio as
player = audioplayer(y,Fs);
We want to play the audio with a fixed delay, so we create a function that our timer will call
function playAudio(hObject, eventdata, audioPlayer)
play(audioPlayer);
We can ignore the first two input parameters as they are only there because we are going to call this function from a timer (which will populate them with the handle to the timer and perhaps some event data (which is typically empty)).
Now we create a (one-off) timer to play the audio file in sixty seconds as
audioFilename = 'myAudioFile.wav';
[y,Fs] = audioread(filename);
player = audioplayer(y,Fs);
hTimer = timer('Name','MyAudioTimer', ...
'StartDelay', 60, ...
'TimerFcn',{@playAudio, player});
start(hTimer);
Try the above and see what happens!
  댓글 수: 2
BeestMann
BeestMann 2016년 3월 15일
편집: BeestMann 2016년 3월 15일
function BRUHHH = playAudio(hObject, eventdata, audioPlayer)
play(audioPlayer);
Filename = 'imperial_march.wav';
[y,Fs] = audioread(Filename);
player = audioplayer(y,Fs);
hTimer = timer('imperial_march.wav','MyAudioTimer', ...
'StartDelay', 60, ...
'TimerFcn',{@playAudio, player});
start(hTimer);
end
So this is what I put on the function tab. I saved it as "BRUHHH.m" - what did I do wrong? MATLAB is saying that there aren't enough input arguments on lines 2 and I can't figure out what to put in 'Name'
Also, there are too many input arguments for the audioread part? What's wrong with that? Thanks :)
Geoff Hayes
Geoff Hayes 2016년 3월 15일
You've combined playAudio function with the code that creates the timer. They are separate. The playAudio function just calls play on the audioPlayer object. The other code,
audioFilename = 'myAudioFile.wav';
[y,Fs] = audioread(filename);
player = audioplayer(y,Fs);
hTimer = timer('Name','MyAudioTimer', ...
'StartDelay', 60, ...
'TimerFcn',{@playAudio, player});
start(hTimer);
occurs outside of this function.
Also, the property of the timer is Name so you can't change it to the name of the wav file. Look to the documentation.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by