whisper_ros: whisper.cpp for ROS 2
Loading...
Searching...
No Matches
whisper.hpp
Go to the documentation of this file.
1// MIT License
2
3// Copyright (c) 2023 Miguel Ángel González Santamarta
4
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#ifndef WHISPER_HPP
24#define WHISPER_HPP
25
26#include <string>
27#include <vector>
28
29#include "grammar-parser.h"
30#include "whisper.h"
31
32// whisper logs
33#define WHISPER_LOG_ERROR(text, ...) \
34 fprintf(stderr, "[ERROR] " text "\n", ##__VA_ARGS__)
35#define WHISPER_LOG_WARN(text, ...) \
36 fprintf(stderr, "[WARN] " text "\n", ##__VA_ARGS__)
37#define WHISPER_LOG_INFO(text, ...) \
38 fprintf(stderr, "[INFO] " text "\n", ##__VA_ARGS__)
39
41 std::string text;
42 float prob;
43};
44
45namespace whisper_ros {
46
47class Whisper {
48
49public:
50 Whisper(const std::string &model, const std::string &openvino_encode_device,
51 int n_processors, const struct whisper_context_params &cparams,
52 const struct whisper_full_params &wparams);
53 ~Whisper();
54
55 struct transcription_output transcribe(const std::vector<float> &pcmf32);
56 std::string trim(const std::string &s);
57 std::string timestamp_to_str(int64_t t, bool comma = false);
58
59 bool set_grammar(const std::string grammar, const std::string start_rule,
60 float grammar_penalty);
61 void reset_grammar();
62 void set_init_prompt(const std::string prompt);
63 void reset_init_prompt();
64
65protected:
67 struct whisper_full_params wparams;
68
69 struct whisper_context *ctx;
70 grammar_parser::parse_state grammar_parsed;
71 std::vector<const whisper_grammar_element *> grammar_rules;
72};
73
74} // namespace whisper_ros
75
76#endif
Definition whisper.hpp:47
Whisper(const std::string &model, const std::string &openvino_encode_device, int n_processors, const struct whisper_context_params &cparams, const struct whisper_full_params &wparams)
Definition whisper.cpp:31
grammar_parser::parse_state grammar_parsed
Definition whisper.hpp:70
void reset_grammar()
Definition whisper.cpp:157
struct whisper_full_params wparams
Definition whisper.hpp:67
struct transcription_output transcribe(const std::vector< float > &pcmf32)
Definition whisper.cpp:76
int n_processors
Definition whisper.hpp:66
bool set_grammar(const std::string grammar, const std::string start_rule, float grammar_penalty)
Definition whisper.cpp:137
~Whisper()
Definition whisper.cpp:73
void reset_init_prompt()
Definition whisper.cpp:168
std::string trim(const std::string &s)
Definition whisper.cpp:116
struct whisper_context * ctx
Definition whisper.hpp:69
std::vector< const whisper_grammar_element * > grammar_rules
Definition whisper.hpp:71
std::string timestamp_to_str(int64_t t, bool comma=false)
Definition whisper.cpp:121
void set_init_prompt(const std::string prompt)
Definition whisper.cpp:164
Definition whisper.hpp:45
Definition whisper.hpp:40
std::string text
Definition whisper.hpp:41
float prob
Definition whisper.hpp:42