I am an electrical engineering student interested in designing a guitar pedal which utilizes digital signal processing. I am currently simulating real-time audio output in Matlab. I simply have an audio file that I am reading, and then trying to output the audio file through my computer's speaker by looping over sequential segments of it. My goal is to have the speaker output of the segmented audio file sound identical to if I just played the entire audio file without looping through segments of it. The reason I am doing this is because eventually I want to upload code to a microcontroller that takes in a real-time guitar signal, performs some filtering/processing, and outputs that filtered signal in real time without much noticeable delay. With this Matlab code, I am simulating just outputting a real time signal without even doing any audio processing yet.
The problem I am facing is that there is an annoying periodic clicking noise as the audio plays, and I think it's because of the discontinuities between the segments of the audio. However, my computer is really fast, and I thought the discontinuities would not be very noticeable if my speaker is rapidly outputting segments of the audio, but I was wrong. And what I'm worried about more is that if I upload some code onto a microcontroller, say an ESP32, the clock would be even slower and the discontinuities would be more exaggerated. Not to mention the added delay from any filtering or various signal processing algorithms I'd need if I wanted to make a good guitar pedal.
How do I make the audio output sound continuous without increasing the size of the audio segments a large amount? I cannot have the audio segments be too long for reasons of my guitar pedal idea.
Here is the code:
clear sound
clear
clc
[y,Fs] = audioread("I Say a Little Prayer - Fingerstyle.mp3");
size_y = size(y);
y1 = transpose(y(1:size_y(1),1)); %storing audio into a vector
segment_length = 5000;
number_of_segments = 100;
for k = 0:1:number_of_segments
N1 = 1 + segment_length*k; %beginning of segment
N2 = (segment_length + 1) + segment_length*k; %end of segment
y1_segment = y1(N1:N2);
out = y1_segment; %file being played
filename = 'audio.wav';
audiowrite(filename,out,Fs);
sound(out,Fs);
pause(length(y1_segment)/Fs); %making sure the segment is played before playing another
end
So far I have tried messing with the duration of the "pause" function at the bottom. I thought that maybe if I make that pause time shorter, then the discontinuities wouldn't be as large and the clicking sound would go away. That didn't seem to work for me though, all that seems to happen is the audio signal sounds sped up and still has the clicking issue.
Another thing I tried was hard coding the end value of each segment to be the same as the first value of the following segment, such that there is no sharp slope between adjacent segments. That didn't seem to have much of an effect, so I increased the number of identical values at the end and beginning of adjacent segments. That didn't work either, it just made the audio sound distorted, which makes sense since I am altering the contents of the signal.
Another thing I'm wondering is if perhaps the "sound" function in Matlab has some inherent delay that I can do nothing about.