问题描述
我正在从录制的音频样本中读取字节。我想将帧变量中的字节转换为 .wav 文件,我想将其存储在变量中,以便我可以访问它而无需将其存储在文件中。下面的代码只是将记录的数据存储到一个名为frames的变量中。
from playsound import playsound
from random import randrange
import pyttsx3
from datetime import datetime
import pyaudio
import speech_recognition as sr
import requests
import wave
import numpy as np
import sounddevice as sd
import math
import time
import os
import struct
def voiceDetection():
SoundThreshHold = 50
TimeoutLength = 5
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 16000
def rms(data):
count = len(data)/2
format = "%dh"%(count)
shorts = struct.unpack( format,data )
sum_squares = 0.0
for sample in shorts:
n = sample * (1.0/32768)
sum_squares += n*n
return math.sqrt( sum_squares / count)*1000
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=chunk)
currentTime = time.time()
end = time.time() + TimeoutLength
frames = []
while currentTime < end:
currentTime = time.time()
data = stream.read(chunk)
if rms(data) >= SoundThreshHold:
end = time.time() + TimeoutLength
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
return frames
print(voiceDetection())
希望得到任何帮助。新年快乐!
解决方法
Python 对此 BytesIO 有一个通用机制。
BytesIO 允许您创建一个内存文件流,您可以像读取和写入文件系统上的文件一样对其进行读写。
如果您只想将数据作为数组获取,this question has a solution
通常,当您在 Python 中处理声音/数字数据时,您需要了解如何将数据放入 NumPy 数组以进行处理。大多数库/工具包都适用于 NumPy 数组。