如何在不写入文件的情况下将 pyaudio 帧转换为 wav 格式?

Art*_*rev 8 python audio wav pyaudio

我想使用 pyaudio 和 IBM Bluemix 服务实现简单的语音转文本工具。目前我需要录制音频,将其保存到磁盘,然后再次加载以将其发送到 Bluemix。

RATE=44100
RECORD_SECONDS = 10
CHUNKSIZE = 1024

# initialize portaudio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE,     input=True, frames_per_buffer=CHUNKSIZE)

frames = [] # A python-list of chunks(numpy.ndarray)
print("Please speak!")

for _ in range(0, int(RATE / CHUNKSIZE * RECORD_SECONDS)):
    data = stream.read(CHUNKSIZE)
    frames.append(np.fromstring(data, dtype=np.int16))

#Convert the list of numpy-arrays into a 1D array (column-wise)
numpydata = np.hstack(frames)

# close stream
stream.stop_stream()
stream.close()
p.terminate()

# save audio to disk
wav.write('out.wav',RATE,numpydata)

# Open audio file(.wav) in wave format 
audio = open('/home/dolorousrtur/Documents/Projects/Capstone/out.wav', 'rb') 

# send audio to bluemix service
headers={'Content-Type': 'audio/wav'} 
r = requests.post(url, data=audio, headers=headers, auth=(username, password)) 
Run Code Online (Sandbox Code Playgroud)

如何将 pyaudio 帧转换为 wav 格式而不将它们写入磁盘?

小智 1

这是一个对我有用的例子。如果将录制的音频放入对象中speech_recognition AudioData,可以使用一些方法将其转换为各种音频格式(例如get_wav_data()get_aiff_data()get_flac_data()、 等)。请参阅此处:speech_recognition AudioData

import pyaudio
import speech_recognition
from time import sleep


class Recorder():

    sampling_rate = 44100
    num_channels = 2
    sample_width = 4 # The width of each sample in bytes. Each group of ``sample_width`` bytes represents a single audio sample. 

    def pyaudio_stream_callback(self, in_data, frame_count, time_info, status):
        self.raw_audio_bytes_array.extend(in_data)
        return (in_data, pyaudio.paContinue)

    def start_recording(self):

        self.raw_audio_bytes_array = bytearray()

        pa = pyaudio.PyAudio()
        self.pyaudio_stream = pa.open(format=pyaudio.paInt16,
                                      channels=self.num_channels,
                                      rate=self.sampling_rate,
                                      input=True,
                                      stream_callback=self.pyaudio_stream_callback)

        self.pyaudio_stream.start_stream()

    def stop_recording(self):

        self.pyaudio_stream.stop_stream()
        self.pyaudio_stream.close()

        speech_recognition_audio_data = speech_recognition.AudioData(self.raw_audio_bytes_array,
                                                                     self.sampling_rate,
                                                                     self.sample_width)
        return speech_recognition_audio_data


if __name__ == '__main__':

    recorder = Recorder()

    # start recording
    recorder.start_recording()

    # say something interesting...
    sleep(3)

    # stop recording
    speech_recognition_audio_data = recorder.stop_recording()

    # convert the audio represented by the ``AudioData`` object to
    # a byte string representing the contents of a WAV file
    wav_data = speech_recognition_audio_data.get_wav_data()
Run Code Online (Sandbox Code Playgroud)