-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.php
280 lines (266 loc) · 13.4 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
include __DIR__ . '/system/inc.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI语音对话</title>
<link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.min.js"></script>
<script src="./recorder.mp3.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.js"></script>
<style>
body {
background-image: url(https://imgapi.cn/api.php?fl=dongman);
}
/* 移动设备样式 */
@media only screen and (max-width: 600px) {
body {
background-image: url(https://imgapi.cn/api.php?zd=mobile&fl=dongman);
background-size: cover;
}
}
</style>
</head>
<body>
<div id="app" class="container mt-5">
<div class="row">
<div class="col-md-6 offset-md-3 col-sm-12">
<div class="card opacity-75">
<div class="card-body">
<h5 class="card-title">语音对话chatgpt</h5>
<div v-if="status=='load'">
<div class="spinner-grow" role="status">
<span class="visually-hidden">加载中...</span>
</div>
需要一些时间进行思考
</div>
<div v-else-if="status=='play'">
<div class="spinner-grow" role="status">
<span class="visually-hidden">加载中...</span>
</div>
说话中
</div>
<div v-else-if="status=='finish'">
<div class="row">
<div class="d-flex justify-content-center">
<button v-if="cur=='stop'" class="btn btn-primary" @click="startRecording">开始说话</button>
<button v-else class="btn btn-danger" @click="stopRecording">结束说话</button>
</div>
</div>
<div class="form-group">
<label for="type">语音识别</label>
<select class="form-select" id="type" v-model="type">
<option value="1">浏览器识别——低延迟</option>
<option value="2">whisper识别——高精度</option>
</select>
</div>
<div class="form-group" v-if="type==1">
<label for="language">语言类型</label>
<select class="form-select" id="language" v-model="language">
<option value="zh-CN">中文普通话</option>
<option value="zh-HK">中文白话</option>
<option value="en-US">英文</option>
</select>
</div>
<div class="form-group">
<label for="voice">音色选择</label>
<select class="form-control" id="voice" v-model="voice">
<option value="alloy">Alloy</option>
<option value="echo">Echo</option>
<option value="fable">Fable</option>
<option value="onyx">Onyx</option>
<option value="nova">Nova</option>
<option value="shimmer">Shimmer</option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<footer class="fixed-bottom">
<div class="text-center text-light p-3" style="background-color: rgba(0, 0, 0, 0.2);">
© 2023 xy3 |
<a class="text-light" href="https://github.com/xy3xy3/VoiceChatgpt" target="_blank">VoiceChatgpt</a>
</div>
</footer>
<script>
let app = new Vue({
el: '#app',
data: {
status: "finish",
cur: "stop",
msg: null,
rec: null, //mp3录制
language: "zh-CN",
recognition: null, //浏览器识别
recognitionText: "", // 存储语音识别的文本结果
chunks: [],
socket: null,
type: 2,
voice: "echo" // 更改为单选框的绑定属性
},
watch: {
type(newType) {
this.saveUserConfig();
},
language(newLanguage) {
this.recognition.lang = newLanguage; // 设置语言,可以根据需要更改
this.saveUserConfig();
},
voice(newVoice) {
this.saveUserConfig();
}
},
methods: {
saveUserConfig() {
const userConfig = {
type: this.type,
language: this.language,
voice: this.voice
};
localStorage.setItem('userConfig', JSON.stringify(userConfig));
},
startRecording() {
this.cur = 'start';
if (this.type == 1) {
this.recognition.start();
} else {
this.rec.start();
}
},
stopRecording() {
vm = this;
this.cur = 'stop';
if (this.type == 1) {
this.recognition.stop();
console.log("语音识别结果:" + vm.recognitionText);
if (vm.recognitionText == "") {
vm.status = 'finish';
return layer.alert("语音识别失败");
}
vm.status = 'load';
let formData = new FormData();
formData.append('text', vm.recognitionText); // 提交用户选择的音色
formData.append('voice', vm.voice); // 提交用户选择的音色
vm.recognitionText = "";
$.ajax({
url: '/chat.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(response) {
vm.loading = false;
if (response.code === 0) {
// Play audio if code is 0
vm.status = 'play';
let audio = new Audio(response.msg);
audio.play();
// 在音频播放结束时触发相应的操作
audio.addEventListener('ended', function() {
vm.status = 'finish';
// 在这里可以执行其他你想要的操作
});
} else {
vm.status = 'finish';
// Show popup if code is not 0
layer.alert(response.msg);
}
},
error: function(error) {
vm.loading = false;
console.error('Error sending audio to server:', error);
},
dataType: 'json' // Specify the expected data type as JSON
});
} else {
this.rec.stop(function(blob, duration) {
vm.status = 'load';
let formData = new FormData();
formData.append('audio', blob, "recorder.mp3");
formData.append('voice', vm.voice); // 提交用户选择的音色
$.ajax({
url: '/chat.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(response) {
vm.loading = false;
if (response.code === 0) {
// Play audio if code is 0
vm.status = 'play';
let audio = new Audio(response.msg);
audio.play();
// 在音频播放结束时触发相应的操作
audio.addEventListener('ended', function() {
vm.status = 'finish';
// 在这里可以执行其他你想要的操作
});
} else {
vm.status = 'finish';
// Show popup if code is not 0
layer.alert(response.msg);
}
},
error: function(error) {
vm.loading = false;
console.error('Error sending audio to server:', error);
},
dataType: 'json' // Specify the expected data type as JSON
});
});
}
},
},
mounted() {
vm = this
if (localStorage.getItem('userConfig')) {
const userConfig = JSON.parse(localStorage.getItem('userConfig'));
this.type = userConfig.type || this.type;
this.language = userConfig.language || this.language;
this.voice = userConfig.voice || this.voice;
}
set = {
type: "mp3", //输出类型:mp3,wav等,使用一个类型前需要先引入对应的编码引擎
bitRate: 16, //比特率,必须是数字 wav(位):16、8,MP3(单位kbps):8kbps时文件大小1k/s,16kbps 2k/s,录音文件很小
sampleRate: 48000 //采样率,必须是数字,wav格式(8位)文件大小=sampleRate*时间;mp3此项对低比特率文件大小有影响,高比特率几乎无影响。
//wav任意值,mp3取值范围:48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000
}
this.rec = Recorder(set);
this.rec.open(function() { //打开麦克风授权获得相关资源
layer.msg('麦克风权限已获取');
}, function(msg, isUserNotAllow) { //用户拒绝未授权或不支持
layer.msg((isUserNotAllow ? "UserNotAllow," : "") + "无法录音:" + msg);
console.log((isUserNotAllow ? "UserNotAllow," : "") + "无法录音:" + msg);
});
if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) {
this.recognition = new(window.SpeechRecognition || window.webkitSpeechRecognition)();
this.recognition.continuous = true;
this.recognition.interimResults = true;
this.recognition.lang = this.language; // 设置语言,可以根据需要更改
// 在识别到结果时触发
this.recognition.onresult = function(event) {
for (let i = event.resultIndex; i < event.results.length; i++) {
if (event.results[i].isFinal) {
vm.recognitionText += event.results[i][0].transcript;
} else {
vm.recognitionText += event.results[i][0].transcript;
}
}
};
} else {
alert('浏览器不支持语音识别功能');
}
}
});
</script>
</body>
</html>