I am attempting to edit a song as the song is playing. So far, I have successfully created a flow that does almost what I desire. I will share how it works and the problem that brought me here for advice.
I take in a song as an array, y, and sample rate, sr. Then, a data feed that provides a new ping every 5 seconds (denoted below as arguments=[]). I use that ping to edit my song during playback. Then, repeat every 5 seconds.
import librosa as lr
y, sr = lr.load(song)
arguments=[] # Populated from external source.
for arg in arguments:
end += some_interval
if end > y.size: ... # breaks out of the loop if the song is over.
x, sr = song_edit(y[front:end],sr, arg)
sd.play(x, sr, blocking=True)
front=end
Problem:
- There is a slight delay that occurs at the end of one 5s clip of music and the next. In music, this is fundamentally problematic.
Solutions I've considered:
- Since blocking=True in sd.play, the code is stopping while the whole clip is playing. Therefore, the delay must be solvable.
- I've considered threading or multiprocessing. Is this the best approach here? My understanding is that threading would not work because my argument is not predetermined.
- What am I missing?
Edit: I tested this:
#x, sr = song_edit(y[front:end],sr, arg)
sd.play(y[front:end], sr, blocking=True)
The delay is still there! Therefore, the delay must be caused by the iteration of the loop or an inherent delay in sd.play.