My goal is to record my voice through the laptop mic and simultaneously adding an effect to it, in python. What I need is similar to a music effects pedal where you connect
First, the problem you posed (being able to tile audio samples while automatically removing the quiet space between them) is not one that can be solved with threading. You need to analyze the recorded sound to determine where there is or is not silence, or simply allow the user to specify when recording should end. You can accomplish the latter with a simple loop:
In this simple example, there is no benefit to using threading.
The method suggested, to record and simultaneously play back, seems like a solution to a different problem, one that is much more complex. In this case, there are two major difficulties:
To expand on Luke's answer:
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
In your code is where you commit to a certain time of recording. If you wrote a function "isSilent
" that can determine if a chunk is silent, your code might change to:
while len(frames) <= 0 or not isSilent(frames[-1]):
data = stream.read(CHUNK)
frames.append(data)
If "isSilent
" is to hard to write or if it is to computationally slow you can wait for user input. KeyboardInterrupt
is a first hack to play with this method:
try:
while true:
data = stream.read(CHUNK)
frames.append(data)
except KeyboardInterrupt:
pass
This is a hack, and not the right way to look for user input in production, but it will let you start experimenting with this. you will want to find or make a stopButtonHasBeenPressed
function.
while not stopButtonHasBeenPressed():
data = stream.read(CHUNK)
frames.append(data)