将音频转换为整数然后再转换为音频

时间:2019-05-24 04:40:03

标签: python numpy struct pyaudio wave

import pyaudio
import struct 
import numpy as np
import wave

 #gather information from data
    n=(1024*4)
    sound = wave.open('test.wav','rb')
    num_sample = sound.getnframes()
    sample_rate = sound.getframerate()
    duration = round(num_sample/sample_rate, 4)

    #instantiate pyaudio and start stream
    p = pyaudio.PyAudio()

    stream = p.open(format = p.get_format_from_width(sound.getsampwidth()),
        channels = sound.getnchannels(),
        rate = sample_rate,
        output = True,
        frames_per_buffer = n)

    #Get data from .wav and turn it into integers
    data = sound.readframes(n)
    data_int = struct.unpack(str(len(data))+'B',data)
    print(data_int_test)

这是我的代码的一部分,其中我从音频中制成了一个整数数组。我将尝试对整数添加减法乘以等,以了解发生了什么。但是,我不知道如何将整数数组转换回音频。我也尝试使用numpy.frombuffer,它也给了我整数,但是我不太确定如何将它们转回原位。如果其他模块有办法,我会愿意这样做。我确定numpy可能有东西。

1 个答案:

答案 0 :(得分:1)

尝试从writescipy.io.wavfile文件使用.wav功能。

from scipy.io.wavfile import write

write('test.wav', sample_rate, scaled)
#write("filename.wav", int - sample rate, data)