Spaces:
Running
Running
examples : add "command" tool (#171)
Browse files- .gitignore +1 -0
- Makefile +4 -1
- README.md +26 -25
- examples/CMakeLists.txt +1 -0
- examples/command/CMakeLists.txt +7 -0
- examples/command/README.md +26 -0
- examples/command/command.cpp +646 -0
- examples/main/README.md +23 -24
- examples/main/main.cpp +1 -1
.gitignore
CHANGED
|
@@ -13,6 +13,7 @@ build-sanitize-thread/
|
|
| 13 |
|
| 14 |
main
|
| 15 |
stream
|
|
|
|
| 16 |
bench
|
| 17 |
sync.sh
|
| 18 |
compile_commands.json
|
|
|
|
| 13 |
|
| 14 |
main
|
| 15 |
stream
|
| 16 |
+
command
|
| 17 |
bench
|
| 18 |
sync.sh
|
| 19 |
compile_commands.json
|
Makefile
CHANGED
|
@@ -134,7 +134,7 @@ libwhisper.so: ggml.o whisper.o
|
|
| 134 |
$(CXX) $(CXXFLAGS) -shared -o libwhisper.so ggml.o whisper.o $(LDFLAGS)
|
| 135 |
|
| 136 |
clean:
|
| 137 |
-
rm -f *.o main stream bench libwhisper.a libwhisper.so
|
| 138 |
|
| 139 |
#
|
| 140 |
# Examples
|
|
@@ -149,6 +149,9 @@ main: examples/main/main.cpp ggml.o whisper.o
|
|
| 149 |
stream: examples/stream/stream.cpp ggml.o whisper.o
|
| 150 |
$(CXX) $(CXXFLAGS) examples/stream/stream.cpp ggml.o whisper.o -o stream $(CC_SDL) $(LDFLAGS)
|
| 151 |
|
|
|
|
|
|
|
|
|
|
| 152 |
bench: examples/bench/bench.cpp ggml.o whisper.o
|
| 153 |
$(CXX) $(CXXFLAGS) examples/bench/bench.cpp ggml.o whisper.o -o bench $(LDFLAGS)
|
| 154 |
|
|
|
|
| 134 |
$(CXX) $(CXXFLAGS) -shared -o libwhisper.so ggml.o whisper.o $(LDFLAGS)
|
| 135 |
|
| 136 |
clean:
|
| 137 |
+
rm -f *.o main stream command bench libwhisper.a libwhisper.so
|
| 138 |
|
| 139 |
#
|
| 140 |
# Examples
|
|
|
|
| 149 |
stream: examples/stream/stream.cpp ggml.o whisper.o
|
| 150 |
$(CXX) $(CXXFLAGS) examples/stream/stream.cpp ggml.o whisper.o -o stream $(CC_SDL) $(LDFLAGS)
|
| 151 |
|
| 152 |
+
command: examples/command/command.cpp ggml.o whisper.o
|
| 153 |
+
$(CXX) $(CXXFLAGS) examples/command/command.cpp ggml.o whisper.o -o command $(CC_SDL) $(LDFLAGS)
|
| 154 |
+
|
| 155 |
bench: examples/bench/bench.cpp ggml.o whisper.o
|
| 156 |
$(CXX) $(CXXFLAGS) examples/bench/bench.cpp ggml.o whisper.o -o bench $(LDFLAGS)
|
| 157 |
|
README.md
CHANGED
|
@@ -98,26 +98,27 @@ c++ -I. -I./examples -O3 -std=c++11 -pthread examples/main/main.cpp whisper.o gg
|
|
| 98 |
usage: ./main [options] file0.wav file1.wav ...
|
| 99 |
|
| 100 |
options:
|
| 101 |
-
-h, --help
|
| 102 |
-
-t N, --threads N number of threads to use during computation
|
| 103 |
-
-p N, --processors N
|
| 104 |
-
-ot N, --offset-t N
|
| 105 |
-
-on N, --offset-n N
|
| 106 |
-
-
|
| 107 |
-
-
|
| 108 |
-
-
|
| 109 |
-
-
|
| 110 |
-
|
| 111 |
-
-
|
| 112 |
-
-
|
| 113 |
-
-
|
| 114 |
-
-
|
| 115 |
-
-
|
| 116 |
-
-
|
| 117 |
-
-
|
| 118 |
-
-
|
| 119 |
-
-
|
| 120 |
-
-
|
|
|
|
| 121 |
|
| 122 |
bash ./models/download-ggml-model.sh base.en
|
| 123 |
Downloading ggml model base.en ...
|
|
@@ -149,13 +150,13 @@ whisper_model_load: n_text_layer = 6
|
|
| 149 |
whisper_model_load: n_mels = 80
|
| 150 |
whisper_model_load: f16 = 1
|
| 151 |
whisper_model_load: type = 2
|
| 152 |
-
whisper_model_load: mem_required = 670.00 MB
|
| 153 |
whisper_model_load: adding 1607 extra tokens
|
| 154 |
-
whisper_model_load:
|
| 155 |
-
whisper_model_load:
|
| 156 |
-
whisper_model_load:
|
|
|
|
| 157 |
|
| 158 |
-
system_info: n_threads = 4 / 10 | AVX2 = 0 | AVX512 = 0 | NEON = 1 | FP16_VA = 1 | WASM_SIMD = 0 | BLAS = 1 |
|
| 159 |
|
| 160 |
main: processing 'samples/jfk.wav' (176000 samples, 11.0 sec), 4 threads, 1 processors, lang = en, task = transcribe, timestamps = 1 ...
|
| 161 |
|
|
|
|
| 98 |
usage: ./main [options] file0.wav file1.wav ...
|
| 99 |
|
| 100 |
options:
|
| 101 |
+
-h, --help [default] show this help message and exit
|
| 102 |
+
-t N, --threads N [4 ] number of threads to use during computation
|
| 103 |
+
-p N, --processors N [1 ] number of processors to use during computation
|
| 104 |
+
-ot N, --offset-t N [0 ] time offset in milliseconds
|
| 105 |
+
-on N, --offset-n N [0 ] segment index offset
|
| 106 |
+
-d N, --duration N [0 ] duration of audio to process in milliseconds
|
| 107 |
+
-mc N, --max-context N [-1 ] maximum number of text context tokens to store
|
| 108 |
+
-ml N, --max-len N [0 ] maximum segment length in characters
|
| 109 |
+
-wt N, --word-thold N [0.01 ] word timestamp probability threshold
|
| 110 |
+
-su, --speed-up [false ] speed up audio by x2 (reduced accuracy)
|
| 111 |
+
-tr, --translate [false ] translate from source language to english
|
| 112 |
+
-otxt, --output-txt [false ] output result in a text file
|
| 113 |
+
-ovtt, --output-vtt [false ] output result in a vtt file
|
| 114 |
+
-osrt, --output-srt [false ] output result in a srt file
|
| 115 |
+
-owts, --output-words [false ] output script for generating karaoke video
|
| 116 |
+
-ps, --print-special [false ] print special tokens
|
| 117 |
+
-pc, --print-colors [false ] print colors
|
| 118 |
+
-nt, --no-timestamps [true ] do not print timestamps
|
| 119 |
+
-l LANG, --language LANG [en ] spoken language
|
| 120 |
+
-m FNAME, --model FNAME [models/ggml-base.en.bin] model path
|
| 121 |
+
-f FNAME, --file FNAME [ ] input WAV file path
|
| 122 |
|
| 123 |
bash ./models/download-ggml-model.sh base.en
|
| 124 |
Downloading ggml model base.en ...
|
|
|
|
| 150 |
whisper_model_load: n_mels = 80
|
| 151 |
whisper_model_load: f16 = 1
|
| 152 |
whisper_model_load: type = 2
|
|
|
|
| 153 |
whisper_model_load: adding 1607 extra tokens
|
| 154 |
+
whisper_model_load: mem_required = 506.00 MB
|
| 155 |
+
whisper_model_load: ggml ctx size = 140.60 MB
|
| 156 |
+
whisper_model_load: memory size = 22.83 MB
|
| 157 |
+
whisper_model_load: model size = 140.54 MB
|
| 158 |
|
| 159 |
+
system_info: n_threads = 4 / 10 | AVX = 0 | AVX2 = 0 | AVX512 = 0 | NEON = 1 | FP16_VA = 1 | WASM_SIMD = 0 | BLAS = 1 |
|
| 160 |
|
| 161 |
main: processing 'samples/jfk.wav' (176000 samples, 11.0 sec), 4 threads, 1 processors, lang = en, task = transcribe, timestamps = 1 ...
|
| 162 |
|
examples/CMakeLists.txt
CHANGED
|
@@ -24,5 +24,6 @@ if (EMSCRIPTEN)
|
|
| 24 |
else()
|
| 25 |
add_subdirectory(main)
|
| 26 |
add_subdirectory(stream)
|
|
|
|
| 27 |
add_subdirectory(bench)
|
| 28 |
endif()
|
|
|
|
| 24 |
else()
|
| 25 |
add_subdirectory(main)
|
| 26 |
add_subdirectory(stream)
|
| 27 |
+
add_subdirectory(command)
|
| 28 |
add_subdirectory(bench)
|
| 29 |
endif()
|
examples/command/CMakeLists.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
if (WHISPER_SUPPORT_SDL2)
|
| 2 |
+
# command
|
| 3 |
+
set(TARGET command)
|
| 4 |
+
add_executable(${TARGET} command.cpp)
|
| 5 |
+
target_include_directories(${TARGET} PRIVATE ${SDL2_INCLUDE_DIRS})
|
| 6 |
+
target_link_libraries(${TARGET} PRIVATE whisper ${SDL2_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
|
| 7 |
+
endif ()
|
examples/command/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# command
|
| 2 |
+
|
| 3 |
+
This is a basic Voice Assistant example that accepts voice commands from the microphone.
|
| 4 |
+
More info is available in [issue #171](https://github.com/ggerganov/whisper.cpp/issues/171).
|
| 5 |
+
|
| 6 |
+
```java
|
| 7 |
+
# Run with default arguments and small model
|
| 8 |
+
./command -m ./models/ggml-small.en.bin -t 8
|
| 9 |
+
|
| 10 |
+
# On Raspberry Pi, use tiny or base models + "-ac 768" for better performance
|
| 11 |
+
./bin/command -m ../models/ggml-tiny.en.bin -ac 768
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
## Building
|
| 15 |
+
|
| 16 |
+
The `command` tool depends on SDL2 library to capture audio from the microphone. You can build it like this:
|
| 17 |
+
|
| 18 |
+
```bash
|
| 19 |
+
# Install SDL2 on Linux
|
| 20 |
+
sudo apt-get install libsdl2-dev
|
| 21 |
+
|
| 22 |
+
# Install SDL2 on Mac OS
|
| 23 |
+
brew install sdl2
|
| 24 |
+
|
| 25 |
+
make command
|
| 26 |
+
```
|
examples/command/command.cpp
ADDED
|
@@ -0,0 +1,646 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Voice assistant example
|
| 2 |
+
//
|
| 3 |
+
// Speak short text commands to the microphone.
|
| 4 |
+
// This program will detect your voice command and convert them to text.
|
| 5 |
+
//
|
| 6 |
+
// ref: https://github.com/ggerganov/whisper.cpp/issues/171
|
| 7 |
+
//
|
| 8 |
+
|
| 9 |
+
#include "whisper.h"
|
| 10 |
+
|
| 11 |
+
#include <SDL.h>
|
| 12 |
+
#include <SDL_audio.h>
|
| 13 |
+
|
| 14 |
+
#include <cassert>
|
| 15 |
+
#include <cstdio>
|
| 16 |
+
#include <string>
|
| 17 |
+
#include <thread>
|
| 18 |
+
#include <vector>
|
| 19 |
+
#include <fstream>
|
| 20 |
+
#include <regex>
|
| 21 |
+
|
| 22 |
+
// command-line parameters
|
| 23 |
+
struct whisper_params {
|
| 24 |
+
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
|
| 25 |
+
int32_t prompt_ms = 5000;
|
| 26 |
+
int32_t command_ms = 4000;
|
| 27 |
+
int32_t capture_id = -1;
|
| 28 |
+
int32_t max_tokens = 32;
|
| 29 |
+
int32_t audio_ctx = 0;
|
| 30 |
+
|
| 31 |
+
float vad_thold = 0.6f;
|
| 32 |
+
float freq_thold = 100.0f;
|
| 33 |
+
|
| 34 |
+
bool speed_up = false;
|
| 35 |
+
bool translate = false;
|
| 36 |
+
bool no_context = true;
|
| 37 |
+
bool print_special = false;
|
| 38 |
+
bool print_energy = false;
|
| 39 |
+
bool no_timestamps = true;
|
| 40 |
+
|
| 41 |
+
std::string language = "en";
|
| 42 |
+
std::string model = "models/ggml-base.en.bin";
|
| 43 |
+
std::string fname_out = "";
|
| 44 |
+
};
|
| 45 |
+
|
| 46 |
+
void whisper_print_usage(int argc, char ** argv, const whisper_params & params);
|
| 47 |
+
|
| 48 |
+
bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
|
| 49 |
+
for (int i = 1; i < argc; i++) {
|
| 50 |
+
std::string arg = argv[i];
|
| 51 |
+
|
| 52 |
+
if (arg == "-h" || arg == "--help") {
|
| 53 |
+
whisper_print_usage(argc, argv, params);
|
| 54 |
+
exit(0);
|
| 55 |
+
}
|
| 56 |
+
else if (arg == "-t" || arg == "--threads") { params.n_threads = std::stoi(argv[++i]); }
|
| 57 |
+
else if (arg == "-pms" || arg == "--prompt-ms") { params.prompt_ms = std::stoi(argv[++i]); }
|
| 58 |
+
else if (arg == "-cms" || arg == "--command-ms") { params.command_ms = std::stoi(argv[++i]); }
|
| 59 |
+
else if (arg == "-c" || arg == "--capture") { params.capture_id = std::stoi(argv[++i]); }
|
| 60 |
+
else if (arg == "-mt" || arg == "--max-tokens") { params.max_tokens = std::stoi(argv[++i]); }
|
| 61 |
+
else if (arg == "-ac" || arg == "--audio-ctx") { params.audio_ctx = std::stoi(argv[++i]); }
|
| 62 |
+
else if (arg == "-vth" || arg == "--vad-thold") { params.vad_thold = std::stof(argv[++i]); }
|
| 63 |
+
else if (arg == "-fth" || arg == "--freq-thold") { params.freq_thold = std::stof(argv[++i]); }
|
| 64 |
+
else if (arg == "-su" || arg == "--speed-up") { params.speed_up = true; }
|
| 65 |
+
else if (arg == "-tr" || arg == "--translate") { params.translate = true; }
|
| 66 |
+
else if (arg == "-ps" || arg == "--print-special") { params.print_special = true; }
|
| 67 |
+
else if (arg == "-pe" || arg == "--print-energy") { params.print_energy = true; }
|
| 68 |
+
else if (arg == "-l" || arg == "--language") { params.language = argv[++i]; }
|
| 69 |
+
else if (arg == "-m" || arg == "--model") { params.model = argv[++i]; }
|
| 70 |
+
else if (arg == "-f" || arg == "--file") { params.fname_out = argv[++i]; }
|
| 71 |
+
else {
|
| 72 |
+
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
| 73 |
+
whisper_print_usage(argc, argv, params);
|
| 74 |
+
exit(0);
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
return true;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
void whisper_print_usage(int argc, char ** argv, const whisper_params & params) {
|
| 82 |
+
fprintf(stderr, "\n");
|
| 83 |
+
fprintf(stderr, "usage: %s [options]\n", argv[0]);
|
| 84 |
+
fprintf(stderr, "\n");
|
| 85 |
+
fprintf(stderr, "options:\n");
|
| 86 |
+
fprintf(stderr, " -h, --help [default] show this help message and exit\n");
|
| 87 |
+
fprintf(stderr, " -t N, --threads N [%-7d] number of threads to use during computation\n", params.n_threads);
|
| 88 |
+
fprintf(stderr, " -pms N, --prompt-ms N [%-7d] prompt duration in milliseconds\n", params.prompt_ms);
|
| 89 |
+
fprintf(stderr, " -cms N, --command-ms N [%-7d] command duration in milliseconds\n", params.command_ms);
|
| 90 |
+
fprintf(stderr, " -c ID, --capture ID [%-7d] capture device ID\n", params.capture_id);
|
| 91 |
+
fprintf(stderr, " -mt N, --max-tokens N [%-7d] maximum number of tokens per audio chunk\n", params.max_tokens);
|
| 92 |
+
fprintf(stderr, " -ac N, --audio-ctx N [%-7d] audio context size (0 - all)\n", params.audio_ctx);
|
| 93 |
+
fprintf(stderr, " -vth N, --vad-thold N [%-7.2f] voice activity detection threshold\n", params.vad_thold);
|
| 94 |
+
fprintf(stderr, " -fth N, --freq-thold N [%-7.2f] high-pass frequency cutoff\n", params.freq_thold);
|
| 95 |
+
fprintf(stderr, " -su, --speed-up [%-7s] speed up audio by x2 (reduced accuracy)\n", params.speed_up ? "true" : "false");
|
| 96 |
+
fprintf(stderr, " -tr, --translate [%-7s] translate from source language to english\n", params.translate ? "true" : "false");
|
| 97 |
+
fprintf(stderr, " -ps, --print-special [%-7s] print special tokens\n", params.print_special ? "true" : "false");
|
| 98 |
+
fprintf(stderr, " -pe, --print-energy [%-7s] print sound energy (for debugging)\n", params.print_energy ? "true" : "false");
|
| 99 |
+
fprintf(stderr, " -l LANG, --language LANG [%-7s] spoken language\n", params.language.c_str());
|
| 100 |
+
fprintf(stderr, " -m FNAME, --model FNAME [%-7s] model path\n", params.model.c_str());
|
| 101 |
+
fprintf(stderr, " -f FNAME, --file FNAME [%-7s] text output file name\n", params.fname_out.c_str());
|
| 102 |
+
fprintf(stderr, "\n");
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
//
|
| 106 |
+
// SDL Audio capture
|
| 107 |
+
//
|
| 108 |
+
|
| 109 |
+
class audio_async {
|
| 110 |
+
public:
|
| 111 |
+
audio_async(int len_ms) {
|
| 112 |
+
m_len_ms = len_ms;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
bool init(int capture_id, int sample_rate);
|
| 116 |
+
|
| 117 |
+
// start capturing audio via the provided SDL callback
|
| 118 |
+
// keep last len_ms seconds of audio in a circular buffer
|
| 119 |
+
bool resume();
|
| 120 |
+
bool pause();
|
| 121 |
+
bool clear();
|
| 122 |
+
|
| 123 |
+
// callback to be called by SDL
|
| 124 |
+
void callback(uint8_t * stream, int len);
|
| 125 |
+
|
| 126 |
+
// get audio data from the circular buffer
|
| 127 |
+
void get(int ms, std::vector<float> & audio);
|
| 128 |
+
|
| 129 |
+
private:
|
| 130 |
+
SDL_AudioDeviceID m_dev_id_in = 0;
|
| 131 |
+
|
| 132 |
+
int m_len_ms = 0;
|
| 133 |
+
int m_sample_rate = 0;
|
| 134 |
+
|
| 135 |
+
bool m_running = false;
|
| 136 |
+
std::mutex m_mutex;
|
| 137 |
+
|
| 138 |
+
std::vector<float> m_audio;
|
| 139 |
+
std::vector<float> m_audio_new;
|
| 140 |
+
size_t m_audio_pos = 0;
|
| 141 |
+
size_t m_audio_len = 0;
|
| 142 |
+
};
|
| 143 |
+
|
| 144 |
+
bool audio_async::init(int capture_id, int sample_rate) {
|
| 145 |
+
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
| 146 |
+
|
| 147 |
+
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
|
| 148 |
+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
| 149 |
+
return false;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
SDL_SetHintWithPriority(SDL_HINT_AUDIO_RESAMPLING_MODE, "medium", SDL_HINT_OVERRIDE);
|
| 153 |
+
|
| 154 |
+
{
|
| 155 |
+
int nDevices = SDL_GetNumAudioDevices(SDL_TRUE);
|
| 156 |
+
fprintf(stderr, "%s: found %d capture devices:\n", __func__, nDevices);
|
| 157 |
+
for (int i = 0; i < nDevices; i++) {
|
| 158 |
+
fprintf(stderr, "%s: - Capture device #%d: '%s'\n", __func__, i, SDL_GetAudioDeviceName(i, SDL_TRUE));
|
| 159 |
+
}
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
SDL_AudioSpec capture_spec_requested;
|
| 163 |
+
SDL_AudioSpec capture_spec_obtained;
|
| 164 |
+
|
| 165 |
+
SDL_zero(capture_spec_requested);
|
| 166 |
+
SDL_zero(capture_spec_obtained);
|
| 167 |
+
|
| 168 |
+
capture_spec_requested.freq = sample_rate;
|
| 169 |
+
capture_spec_requested.format = AUDIO_F32;
|
| 170 |
+
capture_spec_requested.channels = 1;
|
| 171 |
+
capture_spec_requested.samples = 1024;
|
| 172 |
+
capture_spec_requested.callback = [](void * userdata, uint8_t * stream, int len) {
|
| 173 |
+
audio_async * audio = (audio_async *) userdata;
|
| 174 |
+
audio->callback(stream, len);
|
| 175 |
+
};
|
| 176 |
+
capture_spec_requested.userdata = this;
|
| 177 |
+
|
| 178 |
+
if (capture_id >= 0) {
|
| 179 |
+
fprintf(stderr, "%s: attempt to open capture device %d : '%s' ...\n", __func__, capture_id, SDL_GetAudioDeviceName(capture_id, SDL_TRUE));
|
| 180 |
+
m_dev_id_in = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(capture_id, SDL_TRUE), SDL_TRUE, &capture_spec_requested, &capture_spec_obtained, 0);
|
| 181 |
+
} else {
|
| 182 |
+
fprintf(stderr, "%s: attempt to open default capture device ...\n", __func__);
|
| 183 |
+
m_dev_id_in = SDL_OpenAudioDevice(nullptr, SDL_TRUE, &capture_spec_requested, &capture_spec_obtained, 0);
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
if (!m_dev_id_in) {
|
| 187 |
+
fprintf(stderr, "%s: couldn't open an audio device for capture: %s!\n", __func__, SDL_GetError());
|
| 188 |
+
m_dev_id_in = 0;
|
| 189 |
+
|
| 190 |
+
return false;
|
| 191 |
+
} else {
|
| 192 |
+
fprintf(stderr, "%s: obtained spec for input device (SDL Id = %d):\n", __func__, m_dev_id_in);
|
| 193 |
+
fprintf(stderr, "%s: - sample rate: %d\n", __func__, capture_spec_obtained.freq);
|
| 194 |
+
fprintf(stderr, "%s: - format: %d (required: %d)\n", __func__, capture_spec_obtained.format,
|
| 195 |
+
capture_spec_requested.format);
|
| 196 |
+
fprintf(stderr, "%s: - channels: %d (required: %d)\n", __func__, capture_spec_obtained.channels,
|
| 197 |
+
capture_spec_requested.channels);
|
| 198 |
+
fprintf(stderr, "%s: - samples per frame: %d\n", __func__, capture_spec_obtained.samples);
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
m_sample_rate = capture_spec_obtained.freq;
|
| 202 |
+
|
| 203 |
+
m_audio.resize((m_sample_rate*m_len_ms)/1000);
|
| 204 |
+
|
| 205 |
+
return true;
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
bool audio_async::resume() {
|
| 209 |
+
if (!m_dev_id_in) {
|
| 210 |
+
fprintf(stderr, "%s: no audio device to resume!\n", __func__);
|
| 211 |
+
return false;
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
if (m_running) {
|
| 215 |
+
fprintf(stderr, "%s: already running!\n", __func__);
|
| 216 |
+
return false;
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
SDL_PauseAudioDevice(m_dev_id_in, 0);
|
| 220 |
+
|
| 221 |
+
m_running = true;
|
| 222 |
+
|
| 223 |
+
return true;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
bool audio_async::pause() {
|
| 227 |
+
if (!m_dev_id_in) {
|
| 228 |
+
fprintf(stderr, "%s: no audio device to pause!\n", __func__);
|
| 229 |
+
return false;
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
if (!m_running) {
|
| 233 |
+
fprintf(stderr, "%s: already paused!\n", __func__);
|
| 234 |
+
return false;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
SDL_PauseAudioDevice(m_dev_id_in, 1);
|
| 238 |
+
|
| 239 |
+
m_running = false;
|
| 240 |
+
|
| 241 |
+
return true;
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
bool audio_async::clear() {
|
| 245 |
+
if (!m_dev_id_in) {
|
| 246 |
+
fprintf(stderr, "%s: no audio device to clear!\n", __func__);
|
| 247 |
+
return false;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
if (!m_running) {
|
| 251 |
+
fprintf(stderr, "%s: not running!\n", __func__);
|
| 252 |
+
return false;
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
{
|
| 256 |
+
std::lock_guard<std::mutex> lock(m_mutex);
|
| 257 |
+
|
| 258 |
+
m_audio_pos = 0;
|
| 259 |
+
m_audio_len = 0;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
return true;
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
// callback to be called by SDL
|
| 266 |
+
void audio_async::callback(uint8_t * stream, int len) {
|
| 267 |
+
if (!m_running) {
|
| 268 |
+
return;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
const size_t n_samples = len / sizeof(float);
|
| 272 |
+
|
| 273 |
+
m_audio_new.resize(n_samples);
|
| 274 |
+
memcpy(m_audio_new.data(), stream, n_samples * sizeof(float));
|
| 275 |
+
|
| 276 |
+
//fprintf(stderr, "%s: %zu samples, pos %zu, len %zu\n", __func__, n_samples, m_audio_pos, m_audio_len);
|
| 277 |
+
|
| 278 |
+
{
|
| 279 |
+
std::lock_guard<std::mutex> lock(m_mutex);
|
| 280 |
+
|
| 281 |
+
if (m_audio_pos + n_samples > m_audio.size()) {
|
| 282 |
+
const size_t n0 = m_audio.size() - m_audio_pos;
|
| 283 |
+
|
| 284 |
+
memcpy(&m_audio[m_audio_pos], stream, n0 * sizeof(float));
|
| 285 |
+
memcpy(&m_audio[0], &stream[n0], (n_samples - n0) * sizeof(float));
|
| 286 |
+
|
| 287 |
+
m_audio_pos = (m_audio_pos + n_samples) % m_audio.size();
|
| 288 |
+
m_audio_len = m_audio.size();
|
| 289 |
+
} else {
|
| 290 |
+
memcpy(&m_audio[m_audio_pos], stream, n_samples * sizeof(float));
|
| 291 |
+
|
| 292 |
+
m_audio_pos = (m_audio_pos + n_samples) % m_audio.size();
|
| 293 |
+
m_audio_len = std::min(m_audio_len + n_samples, m_audio.size());
|
| 294 |
+
}
|
| 295 |
+
}
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
void audio_async::get(int ms, std::vector<float> & result) {
|
| 299 |
+
if (!m_dev_id_in) {
|
| 300 |
+
fprintf(stderr, "%s: no audio device to get audio from!\n", __func__);
|
| 301 |
+
return;
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
if (!m_running) {
|
| 305 |
+
fprintf(stderr, "%s: not running!\n", __func__);
|
| 306 |
+
return;
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
result.clear();
|
| 310 |
+
|
| 311 |
+
{
|
| 312 |
+
std::lock_guard<std::mutex> lock(m_mutex);
|
| 313 |
+
|
| 314 |
+
if (ms <= 0) {
|
| 315 |
+
ms = m_len_ms;
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
size_t n_samples = (m_sample_rate * ms) / 1000;
|
| 319 |
+
if (n_samples > m_audio_len) {
|
| 320 |
+
n_samples = m_audio_len;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
result.resize(n_samples);
|
| 324 |
+
|
| 325 |
+
int s0 = m_audio_pos - n_samples;
|
| 326 |
+
if (s0 < 0) {
|
| 327 |
+
s0 += m_audio.size();
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
if (s0 + n_samples > m_audio.size()) {
|
| 331 |
+
const size_t n0 = m_audio.size() - s0;
|
| 332 |
+
|
| 333 |
+
memcpy(result.data(), &m_audio[s0], n0 * sizeof(float));
|
| 334 |
+
memcpy(&result[n0], &m_audio[0], (n_samples - n0) * sizeof(float));
|
| 335 |
+
} else {
|
| 336 |
+
memcpy(result.data(), &m_audio[s0], n_samples * sizeof(float));
|
| 337 |
+
}
|
| 338 |
+
}
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
///////////////////////////
|
| 342 |
+
|
| 343 |
+
std::string trim(const std::string & s) {
|
| 344 |
+
std::regex e("^\\s+|\\s+$");
|
| 345 |
+
return std::regex_replace(s, e, "");
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
void high_pass_filter(std::vector<float> & data, float cutoff, float sample_rate) {
|
| 349 |
+
const float rc = 1.0f / (2.0f * M_PI * cutoff);
|
| 350 |
+
const float dt = 1.0f / sample_rate;
|
| 351 |
+
const float alpha = dt / (rc + dt);
|
| 352 |
+
|
| 353 |
+
float y = data[0];
|
| 354 |
+
|
| 355 |
+
for (size_t i = 1; i < data.size(); i++) {
|
| 356 |
+
y = alpha * (y + data[i] - data[i - 1]);
|
| 357 |
+
data[i] = y;
|
| 358 |
+
}
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
bool vad_simple(std::vector<float> & pcmf32, int sample_rate, int last_ms, float vad_thold, float freq_thold, bool verbose) {
|
| 362 |
+
const int n_samples = pcmf32.size();
|
| 363 |
+
const int n_samples_last = (sample_rate * last_ms) / 1000;
|
| 364 |
+
|
| 365 |
+
if (n_samples_last >= n_samples) {
|
| 366 |
+
// not enough samples - assume no speech
|
| 367 |
+
return false;
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
if (freq_thold > 0.0f) {
|
| 371 |
+
high_pass_filter(pcmf32, freq_thold, sample_rate);
|
| 372 |
+
}
|
| 373 |
+
|
| 374 |
+
float energy_all = 0.0f;
|
| 375 |
+
float energy_last = 0.0f;
|
| 376 |
+
|
| 377 |
+
for (size_t i = 0; i < n_samples; i++) {
|
| 378 |
+
energy_all += fabsf(pcmf32[i]);
|
| 379 |
+
|
| 380 |
+
if (i >= n_samples - n_samples_last) {
|
| 381 |
+
energy_last += fabsf(pcmf32[i]);
|
| 382 |
+
}
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
energy_all /= n_samples;
|
| 386 |
+
energy_last /= n_samples_last;
|
| 387 |
+
|
| 388 |
+
if (verbose) {
|
| 389 |
+
fprintf(stderr, "%s: energy_all: %f, energy_last: %f, vad_thold: %f, freq_thold: %f\n", __func__, energy_all, energy_last, vad_thold, freq_thold);
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
if (energy_last > vad_thold*energy_all) {
|
| 393 |
+
return false;
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
return true;
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
std::string transcribe(whisper_context * ctx, const whisper_params & params, const std::vector<float> & pcmf32, float & prob, int64_t & t_ms) {
|
| 400 |
+
const auto t_start = std::chrono::high_resolution_clock::now();
|
| 401 |
+
|
| 402 |
+
prob = 0.0f;
|
| 403 |
+
t_ms = 0;
|
| 404 |
+
|
| 405 |
+
whisper_full_params wparams = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
|
| 406 |
+
|
| 407 |
+
wparams.print_progress = false;
|
| 408 |
+
wparams.print_special = params.print_special;
|
| 409 |
+
wparams.print_realtime = false;
|
| 410 |
+
wparams.print_timestamps = !params.no_timestamps;
|
| 411 |
+
wparams.translate = params.translate;
|
| 412 |
+
wparams.no_context = true;
|
| 413 |
+
wparams.single_segment = true;
|
| 414 |
+
wparams.max_tokens = params.max_tokens;
|
| 415 |
+
wparams.language = params.language.c_str();
|
| 416 |
+
wparams.n_threads = params.n_threads;
|
| 417 |
+
|
| 418 |
+
wparams.audio_ctx = params.audio_ctx;
|
| 419 |
+
wparams.speed_up = params.speed_up;
|
| 420 |
+
|
| 421 |
+
if (whisper_full(ctx, wparams, pcmf32.data(), pcmf32.size()) != 0) {
|
| 422 |
+
return "";
|
| 423 |
+
}
|
| 424 |
+
|
| 425 |
+
int prob_n = 0;
|
| 426 |
+
std::string result;
|
| 427 |
+
|
| 428 |
+
const int n_segments = whisper_full_n_segments(ctx);
|
| 429 |
+
for (int i = 0; i < n_segments; ++i) {
|
| 430 |
+
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 431 |
+
|
| 432 |
+
result += text;
|
| 433 |
+
|
| 434 |
+
const int n_tokens = whisper_full_n_tokens(ctx, i);
|
| 435 |
+
for (int j = 0; j < n_tokens; ++j) {
|
| 436 |
+
const auto token = whisper_full_get_token_data(ctx, i, j);
|
| 437 |
+
|
| 438 |
+
prob += token.p;
|
| 439 |
+
++prob_n;
|
| 440 |
+
}
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
if (prob_n > 0) {
|
| 444 |
+
prob /= prob_n;
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
const auto t_end = std::chrono::high_resolution_clock::now();
|
| 448 |
+
t_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t_end - t_start).count();
|
| 449 |
+
|
| 450 |
+
return result;
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
// compute similarity between two strings using Levenshtein distance
|
| 454 |
+
float similarity(const std::string & s0, const std::string & s1) {
|
| 455 |
+
const size_t len0 = s0.size() + 1;
|
| 456 |
+
const size_t len1 = s1.size() + 1;
|
| 457 |
+
|
| 458 |
+
std::vector<int> col(len1, 0);
|
| 459 |
+
std::vector<int> prevCol(len1, 0);
|
| 460 |
+
|
| 461 |
+
for (size_t i = 0; i < len1; i++) {
|
| 462 |
+
prevCol[i] = i;
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
for (size_t i = 0; i < len0; i++) {
|
| 466 |
+
col[0] = i;
|
| 467 |
+
for (size_t j = 1; j < len1; j++) {
|
| 468 |
+
col[j] = std::min(std::min(1 + col[j - 1], 1 + prevCol[j]), prevCol[j - 1] + (s0[i - 1] == s1[j - 1] ? 0 : 1));
|
| 469 |
+
}
|
| 470 |
+
col.swap(prevCol);
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
const float dist = prevCol[len1 - 1];
|
| 474 |
+
|
| 475 |
+
return 1.0f - (dist / std::max(s0.size(), s1.size()));
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
int main(int argc, char ** argv) {
|
| 479 |
+
whisper_params params;
|
| 480 |
+
|
| 481 |
+
if (whisper_params_parse(argc, argv, params) == false) {
|
| 482 |
+
return 1;
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
if (whisper_lang_id(params.language.c_str()) == -1) {
|
| 486 |
+
fprintf(stderr, "error: unknown language '%s'\n", params.language.c_str());
|
| 487 |
+
whisper_print_usage(argc, argv, params);
|
| 488 |
+
exit(0);
|
| 489 |
+
}
|
| 490 |
+
|
| 491 |
+
// whisper init
|
| 492 |
+
|
| 493 |
+
struct whisper_context * ctx = whisper_init(params.model.c_str());
|
| 494 |
+
|
| 495 |
+
// print some info about the processing
|
| 496 |
+
{
|
| 497 |
+
fprintf(stderr, "\n");
|
| 498 |
+
if (!whisper_is_multilingual(ctx)) {
|
| 499 |
+
if (params.language != "en" || params.translate) {
|
| 500 |
+
params.language = "en";
|
| 501 |
+
params.translate = false;
|
| 502 |
+
fprintf(stderr, "%s: WARNING: model is not multilingual, ignoring language and translation options\n", __func__);
|
| 503 |
+
}
|
| 504 |
+
}
|
| 505 |
+
fprintf(stderr, "%s: processing, %d threads, lang = %s, task = %s, timestamps = %d ...\n",
|
| 506 |
+
__func__,
|
| 507 |
+
params.n_threads,
|
| 508 |
+
params.language.c_str(),
|
| 509 |
+
params.translate ? "translate" : "transcribe",
|
| 510 |
+
params.no_timestamps ? 0 : 1);
|
| 511 |
+
|
| 512 |
+
fprintf(stderr, "\n");
|
| 513 |
+
}
|
| 514 |
+
|
| 515 |
+
|
| 516 |
+
// init audio
|
| 517 |
+
|
| 518 |
+
audio_async audio(30*1000);
|
| 519 |
+
if (!audio.init(params.capture_id, WHISPER_SAMPLE_RATE)) {
|
| 520 |
+
fprintf(stderr, "%s: audio.init() failed!\n", __func__);
|
| 521 |
+
return 1;
|
| 522 |
+
}
|
| 523 |
+
|
| 524 |
+
audio.resume();
|
| 525 |
+
|
| 526 |
+
bool is_running = true;
|
| 527 |
+
bool have_prompt = false;
|
| 528 |
+
bool ask_prompt = true;
|
| 529 |
+
|
| 530 |
+
float prob0 = 0.0f;
|
| 531 |
+
float prob = 0.0f;
|
| 532 |
+
|
| 533 |
+
std::vector<float> pcmf32_cur;
|
| 534 |
+
std::vector<float> pcmf32_prompt;
|
| 535 |
+
|
| 536 |
+
const std::string k_prompt = "Ok Whisper, start listening for commands.";
|
| 537 |
+
|
| 538 |
+
// main loop
|
| 539 |
+
while (is_running) {
|
| 540 |
+
// handle Ctrl + C
|
| 541 |
+
{
|
| 542 |
+
SDL_Event event;
|
| 543 |
+
while (SDL_PollEvent(&event)) {
|
| 544 |
+
switch (event.type) {
|
| 545 |
+
case SDL_QUIT:
|
| 546 |
+
{
|
| 547 |
+
is_running = false;
|
| 548 |
+
} break;
|
| 549 |
+
default:
|
| 550 |
+
break;
|
| 551 |
+
}
|
| 552 |
+
}
|
| 553 |
+
|
| 554 |
+
if (!is_running) {
|
| 555 |
+
break;
|
| 556 |
+
}
|
| 557 |
+
}
|
| 558 |
+
|
| 559 |
+
// delay
|
| 560 |
+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
| 561 |
+
|
| 562 |
+
if (ask_prompt) {
|
| 563 |
+
fprintf(stdout, "\n");
|
| 564 |
+
fprintf(stdout, "%s: Say the following phrase: '%s'\n", __func__, k_prompt.c_str());
|
| 565 |
+
fprintf(stdout, "\n");
|
| 566 |
+
|
| 567 |
+
ask_prompt = false;
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
int64_t t_ms = 0;
|
| 571 |
+
|
| 572 |
+
{
|
| 573 |
+
audio.get(2000, pcmf32_cur);
|
| 574 |
+
|
| 575 |
+
if (vad_simple(pcmf32_cur, WHISPER_SAMPLE_RATE, 1000, params.vad_thold, params.freq_thold, params.print_energy)) {
|
| 576 |
+
fprintf(stdout, "%s: Speech detected!\n", __func__);
|
| 577 |
+
|
| 578 |
+
if (!have_prompt) {
|
| 579 |
+
audio.get(params.prompt_ms, pcmf32_cur);
|
| 580 |
+
|
| 581 |
+
const auto txt = ::trim(::transcribe(ctx, params, pcmf32_cur, prob0, t_ms));
|
| 582 |
+
|
| 583 |
+
fprintf(stdout, "%s: Heard '%s', (prob = %6.3f, t = %d ms)\n", __func__, txt.c_str(), prob0, (int) t_ms);
|
| 584 |
+
|
| 585 |
+
const float sim = similarity(txt, k_prompt);
|
| 586 |
+
|
| 587 |
+
if (txt.length() < 0.8*k_prompt.length() || txt.length() > 1.2*k_prompt.length() || sim < 0.8f) {
|
| 588 |
+
fprintf(stdout, "%s: WARNING: prompt not recognized, try again\n", __func__);
|
| 589 |
+
ask_prompt = true;
|
| 590 |
+
} else {
|
| 591 |
+
fprintf(stdout, "\n");
|
| 592 |
+
fprintf(stdout, "%s: The prompt has been recognized!\n", __func__);
|
| 593 |
+
fprintf(stdout, "%s: Waiting for voice commands ...\n", __func__);
|
| 594 |
+
fprintf(stdout, "\n");
|
| 595 |
+
|
| 596 |
+
// save the audio for the prompt
|
| 597 |
+
pcmf32_prompt = pcmf32_cur;
|
| 598 |
+
have_prompt = true;
|
| 599 |
+
}
|
| 600 |
+
} else {
|
| 601 |
+
audio.get(params.command_ms, pcmf32_cur);
|
| 602 |
+
|
| 603 |
+
// prepend the prompt audio
|
| 604 |
+
pcmf32_cur.insert(pcmf32_cur.begin(), pcmf32_prompt.begin(), pcmf32_prompt.end());
|
| 605 |
+
|
| 606 |
+
const auto txt = ::trim(::transcribe(ctx, params, pcmf32_cur, prob, t_ms));
|
| 607 |
+
|
| 608 |
+
printf("prob0 = %6.3f, prob = %6.3f, t = %d ms\n", prob0, prob, (int) t_ms);
|
| 609 |
+
prob = 100.0f*(prob - prob0);
|
| 610 |
+
|
| 611 |
+
//fprintf(stdout, "%s: heard '%s'\n", __func__, txt.c_str());
|
| 612 |
+
|
| 613 |
+
// find the prompt in the text
|
| 614 |
+
float best_sim = 0.0f;
|
| 615 |
+
size_t best_len = 0;
|
| 616 |
+
for (int n = 0.8*k_prompt.size(); n <= 1.2*k_prompt.size(); ++n) {
|
| 617 |
+
const auto prompt = txt.substr(0, n);
|
| 618 |
+
|
| 619 |
+
const float sim = similarity(prompt, k_prompt);
|
| 620 |
+
|
| 621 |
+
//fprintf(stderr, "%s: prompt = '%s', sim = %f\n", __func__, prompt.c_str(), sim);
|
| 622 |
+
|
| 623 |
+
if (sim > best_sim) {
|
| 624 |
+
best_sim = sim;
|
| 625 |
+
best_len = n;
|
| 626 |
+
}
|
| 627 |
+
}
|
| 628 |
+
|
| 629 |
+
const std::string command = ::trim(txt.substr(best_len));
|
| 630 |
+
|
| 631 |
+
fprintf(stdout, "%s: Command '%s', (prob = %6.3f, t = %d ms)\n", __func__, command.c_str(), prob, (int) t_ms);
|
| 632 |
+
fprintf(stdout, "\n");
|
| 633 |
+
}
|
| 634 |
+
|
| 635 |
+
audio.clear();
|
| 636 |
+
}
|
| 637 |
+
}
|
| 638 |
+
}
|
| 639 |
+
|
| 640 |
+
audio.pause();
|
| 641 |
+
|
| 642 |
+
whisper_print_timings(ctx);
|
| 643 |
+
whisper_free(ctx);
|
| 644 |
+
|
| 645 |
+
return 0;
|
| 646 |
+
}
|
examples/main/README.md
CHANGED
|
@@ -6,29 +6,28 @@ It can be used as a reference for using the `whisper.cpp` library in other proje
|
|
| 6 |
```
|
| 7 |
./main -h
|
| 8 |
|
| 9 |
-
usage: ./
|
| 10 |
-
|
| 11 |
-
-h, --help show this help message and exit
|
| 12 |
-
-s SEED, --seed SEED RNG seed (default: -1)
|
| 13 |
-
-t N, --threads N number of threads to use during computation (default: 4)
|
| 14 |
-
-p N, --processors N number of processors to use during computation (default: 1)
|
| 15 |
-
-ot N, --offset-t N time offset in milliseconds (default: 0)
|
| 16 |
-
-on N, --offset-n N segment index offset (default: 0)
|
| 17 |
-
-mc N, --max-context N maximum number of text context tokens to store (default: max)
|
| 18 |
-
-ml N, --max-len N maximum segment length in characters (default: 0)
|
| 19 |
-
-wt N, --word-thold N word timestamp probability threshold (default: 0.010000)
|
| 20 |
-
-v, --verbose verbose output
|
| 21 |
-
--translate translate from source language to english
|
| 22 |
-
-otxt, --output-txt output result in a text file
|
| 23 |
-
-ovtt, --output-vtt output result in a vtt file
|
| 24 |
-
-osrt, --output-srt output result in a srt file
|
| 25 |
-
-owts, --output-words output script for generating karaoke video
|
| 26 |
-
-ps, --print_special print special tokens
|
| 27 |
-
-pc, --print_colors print colors
|
| 28 |
-
-nt, --no_timestamps do not print timestamps
|
| 29 |
-
-l LANG, --language LANG spoken language (default: en)
|
| 30 |
-
-m FNAME, --model FNAME model path (default: models/ggml-base.en.bin)
|
| 31 |
-
-f FNAME, --file FNAME input WAV file path
|
| 32 |
-
-h, --help show this help message and exit
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
```
|
|
|
|
| 6 |
```
|
| 7 |
./main -h
|
| 8 |
|
| 9 |
+
usage: ./main [options] file0.wav file1.wav ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
options:
|
| 12 |
+
-h, --help [default] show this help message and exit
|
| 13 |
+
-t N, --threads N [4 ] number of threads to use during computation
|
| 14 |
+
-p N, --processors N [1 ] number of processors to use during computation
|
| 15 |
+
-ot N, --offset-t N [0 ] time offset in milliseconds
|
| 16 |
+
-on N, --offset-n N [0 ] segment index offset
|
| 17 |
+
-d N, --duration N [0 ] duration of audio to process in milliseconds
|
| 18 |
+
-mc N, --max-context N [-1 ] maximum number of text context tokens to store
|
| 19 |
+
-ml N, --max-len N [0 ] maximum segment length in characters
|
| 20 |
+
-wt N, --word-thold N [0.01 ] word timestamp probability threshold
|
| 21 |
+
-su, --speed-up [false ] speed up audio by x2 (reduced accuracy)
|
| 22 |
+
-tr, --translate [false ] translate from source language to english
|
| 23 |
+
-otxt, --output-txt [false ] output result in a text file
|
| 24 |
+
-ovtt, --output-vtt [false ] output result in a vtt file
|
| 25 |
+
-osrt, --output-srt [false ] output result in a srt file
|
| 26 |
+
-owts, --output-words [false ] output script for generating karaoke video
|
| 27 |
+
-ps, --print-special [false ] print special tokens
|
| 28 |
+
-pc, --print-colors [false ] print colors
|
| 29 |
+
-nt, --no-timestamps [true ] do not print timestamps
|
| 30 |
+
-l LANG, --language LANG [en ] spoken language
|
| 31 |
+
-m FNAME, --model FNAME [models/ggml-base.en.bin] model path
|
| 32 |
+
-f FNAME, --file FNAME [ ] input WAV file path
|
| 33 |
```
|
examples/main/main.cpp
CHANGED
|
@@ -132,7 +132,7 @@ void whisper_print_usage(int argc, char ** argv, const whisper_params & params)
|
|
| 132 |
fprintf(stderr, " -d N, --duration N [%-7d] duration of audio to process in milliseconds\n", params.duration_ms);
|
| 133 |
fprintf(stderr, " -mc N, --max-context N [%-7d] maximum number of text context tokens to store\n", params.max_context);
|
| 134 |
fprintf(stderr, " -ml N, --max-len N [%-7d] maximum segment length in characters\n", params.max_len);
|
| 135 |
-
fprintf(stderr, " -wt N, --word-thold N [%-
|
| 136 |
fprintf(stderr, " -su, --speed-up [%-7s] speed up audio by x2 (reduced accuracy)\n", params.speed_up ? "true" : "false");
|
| 137 |
fprintf(stderr, " -tr, --translate [%-7s] translate from source language to english\n", params.translate ? "true" : "false");
|
| 138 |
fprintf(stderr, " -otxt, --output-txt [%-7s] output result in a text file\n", params.output_txt ? "true" : "false");
|
|
|
|
| 132 |
fprintf(stderr, " -d N, --duration N [%-7d] duration of audio to process in milliseconds\n", params.duration_ms);
|
| 133 |
fprintf(stderr, " -mc N, --max-context N [%-7d] maximum number of text context tokens to store\n", params.max_context);
|
| 134 |
fprintf(stderr, " -ml N, --max-len N [%-7d] maximum segment length in characters\n", params.max_len);
|
| 135 |
+
fprintf(stderr, " -wt N, --word-thold N [%-7.2f] word timestamp probability threshold\n", params.word_thold);
|
| 136 |
fprintf(stderr, " -su, --speed-up [%-7s] speed up audio by x2 (reduced accuracy)\n", params.speed_up ? "true" : "false");
|
| 137 |
fprintf(stderr, " -tr, --translate [%-7s] translate from source language to english\n", params.translate ? "true" : "false");
|
| 138 |
fprintf(stderr, " -otxt, --output-txt [%-7s] output result in a text file\n", params.output_txt ? "true" : "false");
|