How can i make an Audio File loop continuously?

39 ビュー (過去 30 日間)
Matlab_Beginner
Matlab_Beginner 2022 年 8 月 27 日
コメント済み: Matlab_Beginner 2022 年 8 月 28 日
Hello, im trying to make a simple Code, which allows me to loop an Audio File continuously.
i made a endless while loop, so that the Audio gets repeated, but it doesnt reload it and after the first Play, it get silenced.
Second Problem occur when the sound gets repeated via: ('Playcount=5' as an Exemple), i hear a millisecond cut between the replays, and i want to avoid that.
Any Ideas how can i solve this?
Thanks in advance.
Input = dsp.AudioFileReader('Sine_wave.wav',SamplesPerFrame=1024,PlayCount=5);
Output= audioDeviceWriter('SampleRate',Input.SampleRate);
while true
audio = Input();
Output(audio);
end
release(Input);
release(Output);
  2 件のコメント
Walter Roberson
Walter Roberson 2022 年 8 月 27 日
Is it a 1 ms cut? Or would it just happen to be time equivalent to padding the end of the file to meet the SamplesPerFrame boundary?
That is, I wonder if it happens to work like buffer, buffer, buffer, final short buffer + padding to buffer boundary, buffer1, buffer2, ...
Matlab_Beginner
Matlab_Beginner 2022 年 8 月 28 日
編集済み: Matlab_Beginner 2022 年 8 月 28 日
Hey walter, i think that may be the case that this happens to be time equivalent to padding the end of the file to meet the SamplesPerFrame boundary. However Mr Michael has posted a perfect solution. now it loops without any cuts.
Im wondering if i could stop this infinite loop with a keystroke when matlab in running and the sound is going out(Esc, Enter..)?
Thanks in advance!

サインインしてコメントする。

採用された回答

Michael
Michael 2022 年 8 月 27 日
編集済み: Michael 2022 年 8 月 27 日
Have you read this article? Real-Time Audio in MATLAB It looks like your code is similar to what they show for real-time file to audio output, which is good. Have you checked your audio file to be sure the 1ms isn't at the end of the file? I'd graph the samples to check that.
The more likely culprit is an underrun of sample in the buffer. See this article for a discussion on how to reduce underrun errors. https://www.mathworks.com/help/audio/gs/audio-io-buffering-latency-and-throughput.html
One more thing...it looks like you are reading from the file each loop. If the file isnt too big you could do the read before the loop and keep it in memory as a variable.
Input = dsp.AudioFileReader('Sine_wave.wav',SamplesPerFrame=1024,PlayCount=5);
Output= audioDeviceWriter('SampleRate',Input.SampleRate);
Audio = Input()
while true
Output(audio);
end
release(Input);
release(Output);
  7 件のコメント
Michael
Michael 2022 年 8 月 28 日
No problem. See this on stopping execution: https://www.mathworks.com/help/matlab/matlab_env/stop-execution.html
Please don't forget to accept the answer.
Matlab_Beginner
Matlab_Beginner 2022 年 8 月 28 日
Thanks a lot!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAudio I/O and Waveform Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by