Creating Infinite Continuous Smooth Sound

Hi,
I wish to create an infinite continuous sound, with no breaks or any other sound other than the audio itself.
Stopping the sound is not an issue for now.
The sound will be produced for example - from a sinusoidal signal, which is built from a certain frequency and length.
I've seen a few other questions on this topic(e.g. "How to produce a continuous sound?") , but none worked smooth for me, so I hope I'll find a solution here.
The first approach I tried is using the "sound" function, like so:
Fs = 44100; % Sample rate (samples per second)
duration = 3; % Duration of each tone in seconds
frequency = 440; % Frequency of the tone in Hz
t = 0:1/Fs:duration;
tone = sin(2*pi*frequency*t);
while true
sound(tone, Fs);
pause(3)
end
The second approach I tried is using the DSP System Toolbox package, like so:
Fs = 44100; % Sample rate (samples per second)
duration = 3; % Duration of each tone in seconds
frequency = 440; % Frequency of the tone in Hz
t = 0:1/Fs:duration;
y = sin(2*pi*frequency*t);
audio_file_name = 'audio_file.wav';
audiowrite(audio_file_name,y,Fs);
hafr = dsp.AudioFileReader(audio_file_name);
hap = audioDeviceWriter('SampleRate',48000);
while true
while ~isDone(hafr)
audio = step(hafr);
step(hap,audio);
end
reset(hafr)
end
Both of those approaches do produce a continuous sound, but not a smooth one, as in between iterations there is a small intermisson.
I hope there is a simple solution to this.
Thank you!

Answers (1)

Voss
Voss on 20 Feb 2024
Edited: Voss on 20 Feb 2024
In order to avoid that "small intermission" or slight interruption between iterations, the signal must have a smoothly varying phase across the iteration transition. In this case, that can be done by constructing your signal with one fewer sample at the end so that you're not repeating that sample when the next sound starts:
Fs = 44100; % Sample rate (samples per second)
duration = 3; % Duration of each tone in seconds
frequency = 440; % Frequency of the tone in Hz
% t = 0:1/Fs:duration;
t = (0:duration*Fs-1)/Fs; % one fewer sample at the end
tone = sin(2*pi*frequency*t);
while true
sound(tone, Fs);
pause(duration) % (use the duration variable instead of hard-coding 3)
end

4 Comments

To illustrate the problem and solution, I'll plot two iterations' of signals and zoom in to show the transition between them, first using your definition of t and then using mine. You can see that with your t, the last sample of the first iteration is the same as the first sample of the second iteration, whereas with my t, by excluding the last sample, a smooth transition is made as if it were a single continuous signal.
Fs = 44100; % Sample rate (samples per second)
duration = 3; % Duration of each tone in seconds
frequency = 440; % Frequency of the tone in Hz
figure
subplot(2,1,1)
t = 0:1/Fs:duration; % original
tone = sin(2*pi*frequency*t);
plot(t,tone,'o')
hold on
plot(duration+t,tone,'s','MarkerSize',10)
xlim(duration+[-10 10]/Fs)
title('Original: one sample repeats')
subplot(2,1,2)
t = (0:duration*Fs-1)/Fs; % my suggestion
tone = sin(2*pi*frequency*t);
plot(t,tone,'o')
hold on
plot(duration+t,tone,'s','MarkerSize',10)
xlim(duration+[-10 10]/Fs)
title('New: no repeated samples')
Hi, Thank you for the detailed answer.
I tried your solution, but it seems that there is a break in the sound and not completely smooth.
Did you try to sound it?
Thank you.
Voss
Voss on 24 Feb 2024
Edited: Voss on 24 Feb 2024
"Did you try to sound it?"
Yes. There was a break in the sound before and no break after applying my solution.
Please share your current code that exhibits a break in the sound.
@Yuval Blutman: Any updates? Did this solution ultimately work out?

Sign in to comment.

Categories

Find more on Audio Processing Algorithm Design in Help Center and File Exchange

Products

Release

R2023b

Asked:

on 20 Feb 2024

Commented:

on 9 Mar 2024

Community Treasure Hunt

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

Start Hunting!