本文介绍了PyAudio - 如何将波形文件混合成连续流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个非常基本的应用程序,将音频从麦克风传递到扬声器.如 https://people.csail.mit.edu/所述,这对于 pyaudio 来说非常简单休伯特/pyaudio/ .

I want to write a very basic application that passes audio from microphone to speakers. This is very simple with pyaudio as described on https://people.csail.mit.edu/hubert/pyaudio/ .

def passthrough():
    WIDTH = 2
    CHANNELS = 1
    RATE = 44100

    p = pyaudio.PyAudio()

    def callback(in_data, frame_count, time_info, status):

        return (in_data, pyaudio.paContinue)

    stream = p.open(format=p.get_format_from_width(WIDTH),
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    output=True,
                    stream_callback=callback)

    stream.start_stream()

    while stream.is_active():
        time.sleep(0.1)

    stream.stop_stream()
    stream.close()

    p.terminate()

但现在我尝试在事件发生时将波形文件混合到此流中.这就是我现在陷入困境的地方.播放波形文件似乎也很容易.

But now I try to mix a wave file into this stream, when an event occurs. And that's where I am stuck right now. Playing a wave file seems to be easy, too.

def play_wave(wav_file):
    wf = wave.open(wav_file, 'rb')

    sample_width=wf.getsampwidth()
    channels=wf.getnchannels()
    rate=wf.getframerate()
    second=sample_width*channels*rate

    def callback(in_data, frame_count, time_info, status):
        data = wf.readframes(frame_count)
        return (data, pyaudio.paContinue)

    p = pyaudio.PyAudio()

    stream = p.open(format=p.get_format_from_width(sample_width),
                channels=channels,
                rate=int(rate),
                output=True,
                stream_callback=callback)
    stream.start_stream()

    while stream.is_active():
        time.sleep(0.1)

    stream.stop_stream()
    stream.close()
    wf.close()

    p.terminate()

此时,我有两个问题.

  1. 如何将波形输出混合到连续流中
  2. 如何触发 1. 以事件为基础

希望有人能照亮我现在所在的黑暗地下室.

Hope someone can light up the dark basement I am in right now.

假设波形文件具有相同数量的通道和相同的速率,因此无需转换.

Assume the wave file to have same number of channels and same rate, so no conversion necessary.

推荐答案

一位同事提供了以下解决方案,这是一种非常原始的方法,但它有效并且有助于理解 pyaudio 的工作原理.

A colleague provided the below solution, which is a very raw approach, but it works and is good for understanding how this pyaudio stuff works.

import time
import pyaudio
import numpy
WIDTH = 2
CHANNELS = 1
RATE = 44100
p = pyaudio.PyAudio()
SINE_WAVE_FREQUENCY = 440.0 # In Hz
SINE_WAVE_DURATION = 5.0 # In seconds
SINE_WAVE_VOLUME = 0.5
SINE_WAVE = (numpy.sin(2 * numpy.pi * numpy.arange(RATE * SINE_WAVE_DURATION) * SINE_WAVE_FREQUENCY / RATE)).astype(numpy.float32) * SINE_WAVE_VOLUME
def loopback(in_data, frame_count, time_info, status):
        return (in_data, pyaudio.paContinue)
stream = p.open(format=p.get_format_from_width(WIDTH), channels=CHANNELS, rate=RATE, input=True, output=True, stream_callback=loopback)
stream.start_stream()
def playsine():
        sinestream = p.open(format=pyaudio.paFloat32, channels=1, rate=RATE, output=True)
        sinestream.write(SINE_WAVE)
        sinestream.stop_stream()
        sinestream.close()
while True:
        input("Press enter to play a sine wave")
        playsine()

这篇关于PyAudio - 如何将波形文件混合成连续流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:31