如何使用pyaudio设置音频流的音量?

时间:2019-01-11 16:27:41

标签: python pyaudio

我正在为工作构建声音化程序,我需要进行音频流传输。我有2个代码,第一个是滤波器“ oct3_filter”,它具有3个输入:信号,频率和音量。第二个是音频流,其编码类似于带有pyaudio和回调函数的类,以在流式传输之前处理我的输入信号。 我在audiostream类中构建了一个函数来修改音量,但是它不起作用,我也不知道为什么。你能帮我一下吗?

# -*- coding: utf-8 -*-
"""
Created on Thu Dec 27 14:44:54 2018

@author: William
"""

import wave
import pyaudio
from utility import byteToPCM, floatToPCM, pcmToFloat
from Filters import oct3_filter

class AudioPlayer:
    """ Player audio qui filtre ou non le signal d'entrée """

    def __init__(self, filename, buffer_size, filtering):

        self.filename = filename
        self.buffer_size = buffer_size
        self.filtering = filtering
        self.wf = wave.open(self.filename, 'rb')
        self.f_rate = self.wf.getframerate()
        self.samp_width = self.wf.getsampwidth()
        self.n_channels = self.wf.getnchannels()
        self.p = pyaudio.PyAudio()


    def callback(self, in_data, frameCount, timeInfo, statusFlags):

        data = self.wf.readframes(frameCount)
        if type(data) == type(''):
                data = str.encode(data)
        if len(data) == 0:
            return data, pyaudio.paComplete 

        if self.filtering == 'n':
            filtered = pcmToFloat(byteToPCM(data, self.samp_width))
        elif self.filtering == 'y':
            filtered = oct3_filter(pcmToFloat(byteToPCM(data, self.samp_width)), self.f_rate, self.gain)

        return bytes(floatToPCM(filtered)), pyaudio.paContinue


    def play(self):
        """Start the audio stream playing"""
        self.stream = self.p.open(
                format = self.p.get_format_from_width(self.samp_width),
                channels = self.n_channels,
                rate = self.f_rate,
                frames_per_buffer = self.buffer_size,
                output = True,
                stream_callback = self.callback
                )
        if self.stream:
            self.stream.start_stream()


    def pause(self):
        """Stop the audio stream playing"""
        if self.stream:
            self.stream.stop_stream()


    def stop(self):
        """Clean-up everything"""
        self.pause()
        self.stream.close()
        self.p.terminate()

    def param(self):
        if self.stream:
            print('Output latency :', self.stream.get_output_latency())
            print('The number of frames that can be read without waiting is :', self.stream.get_read_available())

    def gainGlobal(self, valeur):
        """ Set the sound volume"""
        self.gain = int(valeur)

当我调用AudioPlayer时:AudioPlayer(filename,buffer_size,'y')我遇到了该错误:AttributeError:'AudioPlayer'对象没有属性'gain'

非常感谢您的帮助

0 个答案:

没有答案