将音频转换为整数然后再转换回音频
Turning Audio into integers then back into Audio
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
,它也给了我整数,但我不太确定如何将它们变成 back.If 有另一个模块我愿意那样做。我确定 numpy 可能有一些东西。
尝试使用 write
函数从 scipy.io.wavfile
到 .wav
文件。
from scipy.io.wavfile import write
write('test.wav', sample_rate, scaled)
#write("filename.wav", int - sample rate, data)
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
,它也给了我整数,但我不太确定如何将它们变成 back.If 有另一个模块我愿意那样做。我确定 numpy 可能有一些东西。
尝试使用 write
函数从 scipy.io.wavfile
到 .wav
文件。
from scipy.io.wavfile import write
write('test.wav', sample_rate, scaled)
#write("filename.wav", int - sample rate, data)