Skip to content

Commit 3c24e0e

Browse files
author
litongmacos
committed
fix http server
1 parent 602fc6b commit 3c24e0e

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

inference_handler.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using json = nlohmann::json;
88

99
struct whisper_print_user_data {
10-
const whisper_local_stream_params *params;
10+
const whisper_params *params;
1111

1212
const std::vector<std::vector<float>> *pcmf32s;
1313
int progress_prev;
@@ -149,7 +149,7 @@ void whisper_print_segment_callback(struct whisper_context *ctx, struct whisper_
149149
}
150150

151151
std::string
152-
output_str(struct whisper_context *ctx, const whisper_local_stream_params &params, std::vector<std::vector<float>> pcmf32s) {
152+
output_str(struct whisper_context *ctx, const whisper_params &params, std::vector<std::vector<float>> pcmf32s) {
153153
std::stringstream result;
154154
const int n_segments = whisper_full_n_segments(ctx);
155155
for (int i = 0; i < n_segments; ++i) {
@@ -178,7 +178,7 @@ void whisper_print_progress_callback(struct whisper_context * /*ctx*/, struct wh
178178
}
179179
}
180180

181-
void getReqParameters(const Request &req, whisper_local_stream_params &params) {
181+
void getReqParameters(const Request &req, whisper_params &params) {
182182
// user model configu.has_fileion
183183
if (req.has_file("offset-t")) {
184184
params.offset_t_ms = std::stoi(req.get_file_value("offset-t").content);
@@ -204,9 +204,9 @@ void getReqParameters(const Request &req, whisper_local_stream_params &params) {
204204
}
205205

206206

207-
void getReqParameters(const Request &request, whisper_local_stream_params &params);
207+
void getReqParameters(const Request &request, whisper_params &params);
208208

209-
void handleInference(const Request &req, Response &res, std::mutex &whisper_mutex, whisper_local_stream_params &params,
209+
void handleInference(const Request &req, Response &res, std::mutex &whisper_mutex, whisper_params &params,
210210
whisper_context *ctx, char *arg_audio_file) {
211211
// aquire whisper model mutex lock
212212
whisper_mutex.lock();

inference_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
using namespace httplib;
77

8-
void handleInference(const Request &req, Response &res, std::mutex &whisper_mutex, whisper_local_stream_params &params,
8+
void handleInference(const Request &req, Response &res, std::mutex &whisper_mutex, whisper_params &params,
99
whisper_context *ctx, char *arg_audio_file);

public/index.html

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,39 @@
77
<body>
88
<button id="startRecord">开始录音</button>
99
<button id="stopRecord" disabled>停止录音</button>
10+
<button id="sendData" disabled>发送数据</button>
11+
<audio id="audioPlayer" controls></audio>
12+
<a id="downloadLink" style="display: none;">下载录音</a>
1013
<pre id="response"></pre>
1114

1215
<script>
1316
let mediaRecorder;
1417
let audioChunks = [];
18+
let audioBlob;
1519

16-
document.getElementById("startRecord").addEventListener("click", async () => {
17-
const stream = await navigator.mediaDevices.getUserMedia({audio: true, video: false});
18-
mediaRecorder = new MediaRecorder(stream);
19-
mediaRecorder.start();
20+
document.getElementById("startRecord").addEventListener("click", () => {
21+
navigator.mediaDevices.getUserMedia({ audio: { sampleRate: 16000 } })
22+
.then(stream => {
23+
mediaRecorder = new MediaRecorder(stream);
24+
mediaRecorder.start();
2025

21-
mediaRecorder.ondataavailable = (event) => {
22-
audioChunks.push(event.data);
23-
};
26+
mediaRecorder.ondataavailable = event => {
27+
audioChunks.push(event.data);
28+
};
2429

25-
mediaRecorder.onstop = () => {
26-
const audioBlob = new Blob(audioChunks, {type: 'audio/wav'});
27-
sendData(audioBlob);
28-
audioChunks = [];
29-
};
30+
mediaRecorder.onstop = () => {
31+
audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
32+
audioChunks = [];
3033

31-
setTimeout(() => {
32-
mediaRecorder.stop();
33-
}, 30000); // Stop recording after 30 seconds
34+
const audioUrl = URL.createObjectURL(audioBlob);
35+
document.getElementById("audioPlayer").src = audioUrl;
36+
document.getElementById("downloadLink").href = audioUrl;
37+
document.getElementById("downloadLink").download = "recording.wav";
38+
document.getElementById("downloadLink").style.display = 'block';
39+
40+
document.getElementById("sendData").disabled = false;
41+
};
42+
});
3443

3544
document.getElementById("startRecord").disabled = true;
3645
document.getElementById("stopRecord").disabled = false;
@@ -42,13 +51,13 @@
4251
document.getElementById("stopRecord").disabled = true;
4352
});
4453

45-
function sendData(audioBlob) {
54+
document.getElementById("sendData").addEventListener("click", () => {
4655
const formData = new FormData();
4756
formData.append("file", audioBlob);
4857
formData.append("temperature", "0.2");
4958
formData.append("response-format", "json");
5059

51-
fetch("http://127.0.0.1:8080/inference", {
60+
fetch("/inference", {
5261
method: "POST",
5362
body: formData,
5463
})
@@ -59,7 +68,7 @@
5968
.catch(error => {
6069
console.error("Error:", error);
6170
});
62-
}
71+
});
6372
</script>
6473
</body>
6574
</html>

server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ bool is_file_exist(const char *fileName) {
1919
}
2020

2121
int main(int argc, char **argv) {
22-
whisper_local_stream_params params;
22+
whisper_params params;
2323
server_params sparams;
2424

2525
std::mutex whisper_mutex;

0 commit comments

Comments
 (0)