gpt4 book ai didi

python - 如何从谷歌语音 api 获取每个话语的结果并将每个音频话语 block 单独保存为 wav 文件?

转载 作者:行者123 更新时间:2023-12-03 14:42:06 28 4
gpt4 key购买 nike

我正在使用下面的 python 脚本从实时流音频输入中获取来自谷歌语音 API 的预测。
问题是,我需要从谷歌语音 API 对每个话语进行预测,然后还将每个话语的音频保存到磁盘。
我不确定如何修改脚本以保存每个话语的实时音频并打印每个话语的结果而不是连续预测。

#!/usr/bin/env python

import os
import re
import sys
import time

from google.cloud import speech
import pyaudio
from six.moves import queue

# Audio recording parameters
STREAMING_LIMIT = 240000 # 4 minutes
SAMPLE_RATE = 16000
CHUNK_SIZE = int(SAMPLE_RATE / 10) # 100ms

api_key = r'path_to_json_file\google.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = api_key

RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[0;33m'


def get_current_time():
"""Return Current Time in MS."""

return int(round(time.time() * 1000))


class ResumableMicrophoneStream:
"""Opens a recording stream as a generator yielding the audio chunks."""

def __init__(self, rate, chunk_size):
self._rate = rate
self.chunk_size = chunk_size
self._num_channels = 1
self._buff = queue.Queue()
self.closed = True
self.start_time = get_current_time()
self.restart_counter = 0
self.audio_input = []
self.last_audio_input = []
self.result_end_time = 0
self.is_final_end_time = 0
self.final_request_end_time = 0
self.bridging_offset = 0
self.last_transcript_was_final = False
self.new_stream = True
self._audio_interface = pyaudio.PyAudio()
self._audio_stream = self._audio_interface.open(
format=pyaudio.paInt16,
channels=self._num_channels,
rate=self._rate,
input=True,
frames_per_buffer=self.chunk_size,
# Run the audio stream asynchronously to fill the buffer object.
# This is necessary so that the input device's buffer doesn't
# overflow while the calling thread makes network requests, etc.
stream_callback=self._fill_buffer,
)

def __enter__(self):

self.closed = False
return self

def __exit__(self, type, value, traceback):

self._audio_stream.stop_stream()
self._audio_stream.close()
self.closed = True
# Signal the generator to terminate so that the client's
# streaming_recognize method will not block the process termination.
self._buff.put(None)
self._audio_interface.terminate()

def _fill_buffer(self, in_data, *args, **kwargs):
"""Continuously collect data from the audio stream, into the buffer."""

self._buff.put(in_data)
return None, pyaudio.paContinue

def generator(self):
"""Stream Audio from microphone to API and to local buffer"""

while not self.closed:
data = []

if self.new_stream and self.last_audio_input:

chunk_time = STREAMING_LIMIT / len(self.last_audio_input)

if chunk_time != 0:

if self.bridging_offset < 0:
self.bridging_offset = 0

if self.bridging_offset > self.final_request_end_time:
self.bridging_offset = self.final_request_end_time

chunks_from_ms = round((self.final_request_end_time -
self.bridging_offset) / chunk_time)

self.bridging_offset = (round((
len(self.last_audio_input) - chunks_from_ms)
* chunk_time))

for i in range(chunks_from_ms, len(self.last_audio_input)):
data.append(self.last_audio_input[i])

self.new_stream = False

# Use a blocking get() to ensure there's at least one chunk of
# data, and stop iteration if the chunk is None, indicating the
# end of the audio stream.
chunk = self._buff.get()
self.audio_input.append(chunk)

if chunk is None:
return
data.append(chunk)
# Now consume whatever other data's still buffered.
while True:
try:
chunk = self._buff.get(block=False)

if chunk is None:
return
data.append(chunk)
self.audio_input.append(chunk)

except queue.Empty:
break

yield b''.join(data)


def listen_print_loop(responses, stream):
"""Iterates through server responses and prints them.
The responses passed is a generator that will block until a response
is provided by the server.
Each response may contain multiple results, and each result may contain
multiple alternatives; Here we
print only the transcription for the top alternative of the top result.
In this case, responses are provided for interim results as well. If the
response is an interim one, print a line feed at the end of it, to allow
the next result to overwrite it, until the response is a final one. For the
final one, print a newline to preserve the finalized transcription.
"""

for response in responses:

if get_current_time() - stream.start_time > STREAMING_LIMIT:
stream.start_time = get_current_time()
break

if not response.results:
continue

result = response.results[0]

if not result.alternatives:
continue

transcript = result.alternatives[0].transcript

result_seconds = 0
result_nanos = 0

if result.result_end_time.seconds:
result_seconds = result.result_end_time.seconds

if result.result_end_time.nanos:
result_nanos = result.result_end_time.nanos

stream.result_end_time = int((result_seconds * 1000)
+ (result_nanos / 1000000))

corrected_time = (stream.result_end_time - stream.bridging_offset
+ (STREAMING_LIMIT * stream.restart_counter))
# Display interim results, but with a carriage return at the end of the
# line, so subsequent lines will overwrite them.

if result.is_final:

sys.stdout.write(GREEN)
sys.stdout.write('\033[K')
sys.stdout.write(str(corrected_time) + ': ' + transcript + '\n')

stream.is_final_end_time = stream.result_end_time
stream.last_transcript_was_final = True

# Exit recognition if any of the transcribed phrases could be
# one of our keywords.
if re.search(r'\b(exit|quit)\b', transcript, re.I):
sys.stdout.write(YELLOW)
sys.stdout.write('Exiting...\n')
stream.closed = True
break

else:
sys.stdout.write(RED)
sys.stdout.write('\033[K')
sys.stdout.write(str(corrected_time) + ': ' + transcript + '\r')

stream.last_transcript_was_final = False


def main():
"""start bidirectional streaming from microphone input to speech API"""

client = speech.SpeechClient()
config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=SAMPLE_RATE,
language_code='en-US',
max_alternatives=1)
streaming_config = speech.types.StreamingRecognitionConfig(
config=config,
interim_results=True)

mic_manager = ResumableMicrophoneStream(SAMPLE_RATE, CHUNK_SIZE)
print(mic_manager.chunk_size)
sys.stdout.write(YELLOW)
sys.stdout.write('\nListening, say "Quit" or "Exit" to stop.\n\n')
sys.stdout.write('End (ms) Transcript Results/Status\n')
sys.stdout.write('=====================================================\n')

with mic_manager as stream:

while not stream.closed:
sys.stdout.write(YELLOW)
sys.stdout.write('\n' + str(
STREAMING_LIMIT * stream.restart_counter) + ': NEW REQUEST\n')

stream.audio_input = []
audio_generator = stream.generator()

requests = (speech.types.StreamingRecognizeRequest(
audio_content=content)for content in audio_generator)

responses = client.streaming_recognize(streaming_config,
requests)

# Now, put the transcription responses to use.
listen_print_loop(responses, stream)

if stream.result_end_time > 0:
stream.final_request_end_time = stream.is_final_end_time
stream.result_end_time = 0
stream.last_audio_input = []
stream.last_audio_input = stream.audio_input
stream.audio_input = []
stream.restart_counter = stream.restart_counter + 1

if not stream.last_transcript_was_final:
sys.stdout.write('\n')
stream.new_stream = True


if __name__ == '__main__':
main()

最佳答案

我很难理解这段代码中发生的所有事情,而且我不想支付许可证来尝试它,但这里有一些想法。也许其他人会发现它们很有用并且可以进一步帮助您。
检测句子的结尾
首先,将句子与语音分开的一个大问题是,并非每个人都遵循相同的句子之间的停顿。有些人会等待更长时间,而另一些人会直接耕种下一个。有些人在句子中也会停顿。如果您使用相对简单的方法(例如尝试检测停顿)来检测音频数据中的句子结尾,则这会使得从音频数据中检测句子的结尾变得困难。
我能想象到的最好方法是使用您从 Google Speech API 返回的解释并在结束标点符号( !?. )上进行拆分。然后,您的问题将减少到将返回的响应与特定的音频数据块相关联。
看起来你可以通过 None回到你的生成器,它已经优雅地结束了,所以应该不会太糟糕。当您决定一个句子结束时,您可能希望保存生成转录本的任何音频数据块。
这可能很难,因为当接收到更多音频时,Google Speech API 可能会追溯决定一个完整的句子实际上不是完整的,而是更长句子的一部分,因此您也需要注意这一点。
保存音频数据
至于保存原始音频数据,一旦您知道哪些块适用于哪些转录,只需将它们全部附加到列表中(例如 list_of_chunks )并使用 wave :

import wave 

with wave.open("foo.wav", 'wb') as f:
f.setnchannels(self._num_channels)
f.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
f.setframerate(self._rate)
f.writeframes(b''.join(list_of_chunks))
您当然必须制作 num_channelsrate如果您在 ResumableMicrophoneStream 之外执行此操作,则可以访问类(class)。

关于python - 如何从谷歌语音 api 获取每个话语的结果并将每个音频话语 block 单独保存为 wav 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63094643/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com