繁体   English   中英

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

[英]How to convert pyaudio frames into wav format without writing to a file?

我想使用 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)) 

如何在不将它们写入磁盘的情况下将 pyaudio 帧转换为 wav 格式?

这是一个对我有用的例子。 如果将录制的音频放入speech_recognition AudioData object 中,则有多种方法可用于转换为各种音频格式(例如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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM