You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.6 KiB
96 lines
2.6 KiB
4 years ago
|
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
||
|
#
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
#
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
#
|
||
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
7 years ago
|
"""Client-end for the ASR demo."""
|
||
|
import argparse
|
||
4 years ago
|
import sys
|
||
|
|
||
|
import keyboard
|
||
7 years ago
|
import pyaudio
|
||
|
|
||
3 years ago
|
from paddlespeech.s2t.utils.socket_server import socket_send
|
||
4 years ago
|
|
||
7 years ago
|
parser = argparse.ArgumentParser(description=__doc__)
|
||
|
parser.add_argument(
|
||
|
"--host_ip",
|
||
|
default="localhost",
|
||
|
type=str,
|
||
|
help="Server IP address. (default: %(default)s)")
|
||
|
parser.add_argument(
|
||
|
"--host_port",
|
||
|
default=8086,
|
||
|
type=int,
|
||
|
help="Server Port. (default: %(default)s)")
|
||
|
args = parser.parse_args()
|
||
7 years ago
|
|
||
|
is_recording = False
|
||
|
enable_trigger_record = True
|
||
|
|
||
|
|
||
5 years ago
|
def on_press_release(x):
|
||
|
"""Keyboard callback function."""
|
||
7 years ago
|
global is_recording, enable_trigger_record
|
||
5 years ago
|
press = keyboard.KeyboardEvent('down', 28, 'space')
|
||
|
release = keyboard.KeyboardEvent('up', 28, 'space')
|
||
|
if x.event_type == 'down' and x.name == press.name:
|
||
7 years ago
|
if (not is_recording) and enable_trigger_record:
|
||
|
sys.stdout.write("Start Recording ... ")
|
||
|
sys.stdout.flush()
|
||
|
is_recording = True
|
||
5 years ago
|
if x.event_type == 'up' and x.name == release.name:
|
||
4 years ago
|
if is_recording:
|
||
7 years ago
|
is_recording = False
|
||
|
|
||
|
|
||
|
data_list = []
|
||
|
|
||
|
|
||
|
def callback(in_data, frame_count, time_info, status):
|
||
7 years ago
|
"""Audio recorder's stream callback function."""
|
||
7 years ago
|
global data_list, is_recording, enable_trigger_record
|
||
|
if is_recording:
|
||
|
data_list.append(in_data)
|
||
|
enable_trigger_record = False
|
||
|
elif len(data_list) > 0:
|
||
4 years ago
|
socket_send(args.host_ip, args.host_port, ''.join(data_list))
|
||
7 years ago
|
data_list = []
|
||
|
enable_trigger_record = True
|
||
|
return (in_data, pyaudio.paContinue)
|
||
|
|
||
|
|
||
|
def main():
|
||
7 years ago
|
# prepare audio recorder
|
||
7 years ago
|
p = pyaudio.PyAudio()
|
||
|
stream = p.open(
|
||
4 years ago
|
format=pyaudio.paInt16,
|
||
7 years ago
|
channels=1,
|
||
|
rate=16000,
|
||
|
input=True,
|
||
|
stream_callback=callback)
|
||
|
stream.start_stream()
|
||
|
|
||
7 years ago
|
# prepare keyboard listener
|
||
5 years ago
|
while (1):
|
||
|
keyboard.hook(on_press_release)
|
||
|
if keyboard.record('esc'):
|
||
|
break
|
||
7 years ago
|
|
||
7 years ago
|
# close up
|
||
7 years ago
|
stream.stop_stream()
|
||
|
stream.close()
|
||
|
p.terminate()
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|