How can i make an Audio File loop continuously?

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

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일

0 개 추천

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

Hey Michael, first thanks for ur quick Answer. I'v checked my file and im sure that the file does not have that 1ms silence in it, this comes from Matlab, when loading the file to replay it. i have read the articles and i still can't find a solution for my problems.
(The code u'v posted about reading the file before the loop and keep it in memory as a variable doesnt work correctly, it feels like the sound gets cut 1000 time per sec.)
So what i want to achieve is a perfect looping of my audio file without this cut? is this Possible to do?
i scoped the output of my Code to better visualize the problem.
Thanks in advance!
Michael
Michael 2022년 8월 27일
Can you upload the .wav file?
Here is the problem: This seems to be related to the audio file reader reaching the end of the file. As you can see in the code below, when it gets to the end and has to loop back to the beginning on the next iteration throug the loop, it takes 1.88 ms rather than the 0.22-0.23 ms that it typically takes. Not sure on a solution yet....
Fs = 48000;
dt = 1/Fs;
t = 0:dt:3;
y = sin(440*2*pi*t);
audiowrite('Sine_wave.wav',y,Fs)
%Input = dsp.AudioFileReader('Sine_wave.wav',SamplesPerFrame=1024,PlayCount=5);
fileReader = dsp.AudioFileReader('Sine_wave.wav',PlayCount=2);
deviceWriter = audioDeviceWriter('SampleRate',fileReader.SampleRate);
i= 1;
while ~isDone(fileReader)
[mySignal, eof(i)] = fileReader();
time(i) = toc;
deviceWriter(mySignal);
tic
i = i+1
end
release(fileReader)
release(deviceWriter)
find(eof==true)
% ans =
%
% 141 282
format long
time(138:145)'
% ans =
%
% 0.000224349000000 <===== 138
% 0.000222358000000 <===== 139
% 0.000227631000000 <===== 140
% 0.000228241000000 <===== 141 (End of file
% 0.001844279000000 <===== 142
% 0.000227445000000 <===== 143
% 0.000227968000000 <===== 144
% 0.000235695000000 <===== 145
Michael
Michael 2022년 8월 27일
편집: Michael 2022년 8월 28일
Here you go. This will load the file in memory and then loop through that vector rather than reading the file:
[y Fs] = audioread('Sine_wave.wav');
totalSamps = numel(y);
seg = 0;
while true
inds = 1+mod((0:1023)+1024*seg,totalSamps);
deviceWriter(y(inds));
seg = seg + 1;
end
Matlab_Beginner
Matlab_Beginner 2022년 8월 28일
편집: Matlab_Beginner 2022년 8월 28일
Hello Michael, Thank u so much for ur effort that ur putting in here. It loops perfect!!!
One last question, as u know the while loop in this Code is infinite. Is it possible to stop it with a keystroke while matlab is running? for exemple with ESC Key.
Thanks a lot in advance
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.
Thanks a lot!

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

추가 답변 (0개)

카테고리

질문:

2022년 8월 27일

댓글:

2022년 8월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by