使用pyaudio生成振幅越来越大的音调。

问题描述 投票:0回答:1

我试图生成一个类似于我用audacity创建的音色,振幅从0.1增加到1,使用pyaudio。

enter image description here

到目前为止,我还没有成功地想出如何在pyaudio中做到这一点。我可以产生一个正弦波,但不能创建一个增加振幅的。

我的目标是使用raspberry pi 4,如果这有帮助的话。

编辑--这是我目前所尝试的。基本上,我试着把正弦波和三角波的前半部分结合起来。

import pyaudio
import numpy as np
import wave
from scipy import signal
p = pyaudio.PyAudio()

#amp 5000
FORMAT = pyaudio.paInt16 
filename = 'test8.wav'
chunk = 1024
channels = 1
sample_format = pyaudio.paInt16
def excitation(freq=60 ,duration=9, amplitude=1, fs=44100):
    p = pyaudio.PyAudio()
    #Sine wave generation
    seconds = duration
    sine = (amplitude*np.sin(2*np.pi*np.arange(fs*duration)*freq/fs)).astype(np.float32)


    t = np.linspace(0, duration*2, fs*duration)
    triangle = signal.sawtooth(2 * np.pi * (1/(duration*2)) * t)
    ramp = amplitude* 2 *triangle[0:(len(sine))]

    samples = sine + ramp

    stream = p.open(format=pyaudio.paFloat32,
                channels=1,
                rate=fs,
                output=True,
                input = True)

    frames = []  # Initialize array to store frames
    stream.write(samples)


    # Store data in chunks for 3 seconds
    for i in range(0, int(fs / chunk * seconds)):
        data = stream.read(chunk)
        frames.append(data)

    # Stop and close the stream 
    stream.stop_stream()
    stream.close()
    # Terminate the PortAudio interface
    p.terminate()

    # Save the recorded data as a WAV file
    wf = wave.open(filename, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(p.get_sample_size(sample_format))
    wf.setframerate(fs)
    wf.writeframes(b''.join(frames))
    wf.close()

    return 

excitation()

python audio raspberry-pi signal-processing pyaudio
1个回答
0
投票

如果你能提前生成音频,你可以简单地应用一个不断增加的增益作为一个乘数,作为一个后处理步骤的每个样本。

samples = generate_samples()
for i in range(0, len(samples)):
  samples[i] *= i/len(samples)  

你也可以在numpy中这样做,如下所示。

samples = generate_samples() #returns a np.array
ramp = np.arange(0,1,1/len(samples))
samples *= ramp
© www.soinside.com 2019 - 2024. All rights reserved.