snowboy_ros
snowboy-detect.h
Go to the documentation of this file.
1 // include/snowboy-detect.h
2 
3 // Copyright 2016 KITT.AI (author: Guoguo Chen)
4 
5 #ifndef SNOWBOY_INCLUDE_SNOWBOY_DETECT_H_
6 #define SNOWBOY_INCLUDE_SNOWBOY_DETECT_H_
7 
8 #include <memory>
9 #include <string>
10 
11 namespace snowboy {
12 
13 // Forward declaration.
14 struct WaveHeader;
15 class PipelineDetect;
16 
18 //
19 // SnowboyDetect class interface.
20 //
23  public:
24  // Constructor that takes a resource file, and a list of hotword models which
25  // are separated by comma. In the case that more than one hotword exist in the
26  // provided models, RunDetection() will return the index of the hotword, if
27  // the corresponding hotword is triggered.
28  //
29  // CAVEAT: a personal model only contain one hotword, but an universal model
30  // may contain multiple hotwords. It is your responsibility to figure
31  // out the index of the hotword. For example, if your model string is
32  // "foo.pmdl,bar.umdl", where foo.pmdl contains hotword x, bar.umdl
33  // has two hotwords y and z, the indices of different hotwords are as
34  // follows:
35  // x 1
36  // y 2
37  // z 3
38  //
39  // @param [in] resource_filename Filename of resource file.
40  // @param [in] model_str A string of multiple hotword models,
41  // separated by comma.
42  SnowboyDetect(const std::string& resource_filename,
43  const std::string& model_str);
44 
45  // Resets the detection. This class handles voice activity detection (VAD)
46  // internally. But if you have an external VAD, you should call Reset()
47  // whenever you see segment end from your VAD.
48  bool Reset();
49 
50  // Runs hotword detection. Supported audio format is WAVE (with linear PCM,
51  // 8-bits unsigned integer, 16-bits signed integer or 32-bits signed integer).
52  // See SampleRate(), NumChannels() and BitsPerSample() for the required
53  // sampling rate, number of channels and bits per sample values. You are
54  // supposed to provide a small chunk of data (e.g., 0.1 second) each time you
55  // call RunDetection(). Larger chunk usually leads to longer delay, but less
56  // CPU usage.
57  //
58  // Definition of return values:
59  // -2: Silence.
60  // -1: Error.
61  // 0: No event.
62  // 1: Hotword 1 triggered.
63  // 2: Hotword 2 triggered.
64  // ...
65  //
66  // @param [in] data Small chunk of data to be detected. See
67  // above for the supported data format.
68  int RunDetection(const std::string& data);
69 
70  // Various versions of RunDetection() that take different format of audio. If
71  // NumChannels() > 1, e.g., NumChannels() == 2, then the array is as follows:
72  //
73  // d1c1, d1c2, d2c1, d2c2, d3c1, d3c2, ..., dNc1, dNc2
74  //
75  // where d1c1 means data point 1 of channel 1.
76  //
77  // @param [in] data Small chunk of data to be detected. See
78  // above for the supported data format.
79  // @param [in] array_length Length of the data array.
80  int RunDetection(const float* const data, const int array_length);
81  int RunDetection(const int16_t* const data, const int array_length);
82  int RunDetection(const int32_t* const data, const int array_length);
83 
84  // Sets the sensitivity string for the loaded hotwords. A <sensitivity_str> is
85  // a list of floating numbers between 0 and 1, and separated by comma. For
86  // example, if there are 3 loaded hotwords, your string should looks something
87  // like this:
88  // 0.4,0.5,0.8
89  // Make sure you properly align the sensitivity value to the corresponding
90  // hotword.
91  void SetSensitivity(const std::string& sensitivity_str);
92 
93  // Returns the sensitivity string for the current hotwords.
95 
96  // Applied a fixed gain to the input audio. In case you have a very weak
97  // microphone, you can use this function to boost input audio level.
98  void SetAudioGain(const float audio_gain);
99 
100  // Writes the models to the model filenames specified in <model_str> in the
101  // constructor. This overwrites the original model with the latest parameter
102  // setting. You are supposed to call this function if you have updated the
103  // hotword sensitivities through SetSensitivity(), and you would like to store
104  // those values in the model as the default value.
105  void UpdateModel() const;
106 
107  // Returns the number of the loaded hotwords. This helps you to figure the
108  // index of the hotwords.
109  int NumHotwords() const;
110 
111  // If <apply_frontend> is true, then apply frontend audio processing;
112  // otherwise turns the audio processing off.
113  void ApplyFrontend(const bool apply_frontend);
114 
115  // Returns the required sampling rate, number of channels and bits per sample
116  // values for the audio data. You should use this information to set up your
117  // audio capturing interface.
118  int SampleRate() const;
119  int NumChannels() const;
120  int BitsPerSample() const;
121 
122  ~SnowboyDetect();
123 
124  private:
127 };
128 
129 } // namespace snowboy
130 
131 #endif // SNOWBOY_INCLUDE_SNOWBOY_DETECT_H_
std::string
snowboy::SnowboyDetect::SetAudioGain
void SetAudioGain(const float audio_gain)
snowboy::SnowboyDetect::NumHotwords
int NumHotwords() const
snowboy::SnowboyDetect::UpdateModel
void UpdateModel() const
snowboy::SnowboyDetect::Reset
bool Reset()
snowboy::SnowboyDetect::SampleRate
int SampleRate() const
snowboy::SnowboyDetect::detect_pipeline_
std::unique_ptr< PipelineDetect > detect_pipeline_
Definition: snowboy-detect.h:126
snowboy::SnowboyDetect::RunDetection
int RunDetection(const std::string &data)
training_service.data
dictionary data
END OF MODIFY ##################.
Definition: training_service.py:31
snowboy::SnowboyDetect::~SnowboyDetect
~SnowboyDetect()
snowboy
Definition: snowboy-detect.h:11
snowboy::SnowboyDetect::wave_header_
std::unique_ptr< WaveHeader > wave_header_
Definition: snowboy-detect.h:125
snowboy::SnowboyDetect::SetSensitivity
void SetSensitivity(const std::string &sensitivity_str)
memory
snowboy::SnowboyDetect::GetSensitivity
std::string GetSensitivity() const
snowboy::SnowboyDetect::NumChannels
int NumChannels() const
snowboy::SnowboyDetect
Definition: snowboy-detect.h:22
snowboy::SnowboyDetect::ApplyFrontend
void ApplyFrontend(const bool apply_frontend)
std::unique_ptr< WaveHeader >
snowboy::SnowboyDetect::BitsPerSample
int BitsPerSample() const
snowboy::SnowboyDetect::SnowboyDetect
SnowboyDetect(const std::string &resource_filename, const std::string &model_str)
string