0

我正在寻找一个循环系统(受Marc Rebillet的启发,看看他),并且正在努力让 2 种声音自己播放。

我已经开始使用 sounddevice,因为它是我发现的第一个音频模块,但我不确定是否有其他更好的音频模块。

import time

def record(duration): # records a few seconds of audio
    print('recording in:')
    print('3')
    time.sleep(1)
    print('2')
    time.sleep(1)
    print('1')
    time.sleep(1)
    print('recording')
    record = sd.rec(int(duration * fs), samplerate=48000, channels=2)
    sd.wait()
    print('done\n\n')
    return record

duration = 3.5  # seconds

recordOne = record(duration)
recordTwo = record(duration)

while True: # repeats both pieces of audio
    sd.play(recordOne, fs)
    sd.play(recordTwo, fs)
    sd.wait()

这段代码最终只播放了 recordTwo,并没有将它们叠加在一起。同样,我希望能够同时播放多个声音。感谢您的任何帮助!

4

1 回答 1

1

您是否尝试过多线程解决方案?

import time
import threading


def record(duration): # records a few seconds of audio
    print('recording in:')

    for t in range(3, 0, -1):
        print(t)
        time.sleep(1)

    print('recording')
    print("mock record process...")
    start = time.time()
    time.sleep(10)
    print(f'done, {time.time() - start} seconds total.\n\n')
    return record


duration = 3.5  # seconds
recordOne = record(duration)
recordTwo = record(duration)


def play(record_piece):
    print("paly the sound")
    time.sleep(duration)


while True: # repeats both pieces of audio
    t1 = threading.Thread(target=play, args=(recordOne,))
    t2 = threading.Thread(target=play, args=(recordTwo,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()

或者您可以尝试在播放之前找到一些可以组合两个音轨的库。

我阅读了sounddevice的文档,它提供了一种播放方法,你试过了吗?

回放

在此处输入图像描述

于 2019-08-25T12:38:04.963 回答