Simultaneous record audio from mic and play it back with effect in python

后端 未结 2 1117
你的背包
你的背包 2020-12-30 18:40

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

2条回答
  •  情歌与酒
    2020-12-30 18:56

    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)
    

提交回复
热议问题