This commit is contained in:
joylink_fanyuhong 2024-05-14 10:42:47 +08:00
commit 25bae04dcb
6 changed files with 67 additions and 44 deletions

Binary file not shown.

View File

@ -0,0 +1,19 @@
[remap]
importer="mp3"
type="AudioStreamMP3"
uid="uid://dlxo0ao1kf67d"
path="res://.godot/imported/correct.mp3-a182f39b133e3af6125dca80877bfa41.mp3str"
[deps]
source_file="res://Assets/training_speech/correct.mp3"
dest_files=["res://.godot/imported/correct.mp3-a182f39b133e3af6125dca80877bfa41.mp3str"]
[params]
loop=false
loop_offset=0
bpm=0
beat_count=0
bar_beats=4

View File

@ -1,5 +1,6 @@
extends Node
## 语音识别成功信号
signal speech_recognition_successed
## 录音效果器
@ -7,6 +8,17 @@ var effect: AudioEffectRecord
## 录音捕获效果器(用于判断录音音量)
var capture: AudioEffectCapture
## 待语音识别的文本
var targetText: String
## 音量最小阈值
const VolumeMin = 0.03
## 长时间没有说话阈值
const LongTimeNoVoice = 1
var hasVoice = false
var novoiceTime = 0
## 语音识别成功回复音效
var reply_correct = preload("res://Assets/training_speech/correct.mp3")
func _ready():
# We get the index of the "Record" bus.
@ -14,6 +26,7 @@ func _ready():
# And use it to retrieve its first effect, which has been defined
# as an "AudioEffectRecord" resource.
effect = AudioServer.get_bus_effect(idx, 0)
# 音频数据捕获,用于判断录音音量从而判断是否有声音输入
capture = AudioServer.get_bus_effect(idx, 1)
## 启动录音
@ -28,8 +41,11 @@ func stopRecord():
if effect.is_recording_active():
effect.set_recording_active(false)
var audio_sd = preload("res://Assets/training_speech/sd.mp3")
var audio_zlgzmhfzc = preload("res://Assets/training_speech/zlgzmhfzc.mp3")
## 重启录音
func restartRecord():
effect.set_recording_active(false)
effect.set_recording_active(true)
## 播放回复
@ -52,58 +68,49 @@ func play_reply(reply):
## 录音并语音识别检查
## PS: 是协程函数外部如果关心结果需await
func speech_record_check(keywords):
func speech_record_check(text: String):
print("录音采样频率: ", AudioServer.get_mix_rate())
targetText = text
startRecord()
$Timer.start()
await speech_recognition_successed
print("识别成功,结束")
$Timer.stop()
stopRecord()
## 长时间没有说话阈值
const VolumeMin = 0.05
const LongTimeNoVoice = 1.5
var novoiceTime = 0
## 定时处理录音并识别的逻辑
func _on_timer_timeout():
if effect.is_recording_active():
print(capture.get_buffer_length_frames(), ", ", capture.get_discarded_frames(), ", ", capture.get_frames_available())
var buf = capture.get_buffer(capture.get_frames_available())
var hasvoice = false
var soundDetected = false
for vec in buf:
if vec.x > VolumeMin or vec.y > VolumeMin:
#print("Left channel volume = ", vec.x, ", Right volume = ", vec.y)
hasvoice = true
if hasvoice:
if novoiceTime > LongTimeNoVoice:
novoiceTime = 0
## 重新开始录音
stopRecord()
startRecord()
soundDetected = true
# 检测到声音处理
if soundDetected:
hasVoice = true
novoiceTime = 0
# 未检测到声音处理
else:
novoiceTime += $Timer.wait_time
if novoiceTime >= LongTimeNoVoice:
print("长时间没有说话: ", novoiceTime)
if hasVoice and novoiceTime >= LongTimeNoVoice:
var rcd = effect.get_recording()
stopRecord()
await play_reply(rcd)
if rcd == null:
return
print("音频时长: ", rcd.get_length())
restartRecord()
#await play_reply(rcd)
if rcd.get_length() > 5:
await play_reply(reply_correct)
speech_recognition_successed.emit()
else:
startRecord()
#if effect.is_recording_active():
#recording = effect.get_recording()
#print(recording.data.size(), ", ", recording.format, ", ", recording.loop_mode, ", ", recording.get_length())
#stopRecord()
#var player = AudioStreamPlayer.new()
#add_child(player)
#player.stream = recording
#player.play()
#player.finished.connect( _on_audio_play_finished.bind(player))
#startRecord()
#else:
#startRecord()
hasVoice = false
# 长时间无语音输入,重启录音
if novoiceTime >= LongTimeNoVoice:
print("长时间无声音,重启录音")
restartRecord()
novoiceTime = 0
func _on_audio_play_finished(player):
print(player, "播放完成")
player.stop()
player.queue_free()
pass

View File

@ -22,8 +22,8 @@ driver/mix_rate.web=16000
[display]
window/size/viewport_width=1920
window/size/viewport_height=1080
window/size/viewport_width=1600
window/size/viewport_height=900
window/size/initial_position_type=0
window/stretch/mode="viewport"

View File

@ -16,6 +16,4 @@ func _ready():
func _on_training_desc_dialog_start_training():
await $VoiceCommunication.play_reply(a_xt_pslgbztmwx)
#await $VoiceCommunication.play_reply(a_zby_ztmjczc)
#await $VoiceCommunication.play_reply(a_sd)
await $VoiceCommunication.speech_record_check([])
await $VoiceCommunication.speech_record_check("值班员上行2号门关门故障使用LCB关闭站台门")

View File

@ -10,7 +10,6 @@ trainingName = "站台单档滑动门关门故障处置"
trainingDesc = "列车关门作业,突发单个站台门未正常关闭。"
[node name="TrainingDescDialog" parent="." instance=ExtResource("2_oc00d")]
size = Vector2i(400, 300)
[node name="VoiceCommunication" parent="." instance=ExtResource("3_ceo1a")]