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
use std::ffi::{CString, CStr};

use crate::rawrsnoboy::root as rsnowboy;

#[derive(Debug, Copy, Clone)]
pub struct SnowboyDetect {
  rsnowboy_detect: *mut rsnowboy::RSnowboyDetect
}

unsafe impl Send for SnowboyDetect {}
unsafe impl Sync for SnowboyDetect {}

impl SnowboyDetect {
  pub fn new<S>(resource_filename: S, model_str: S) -> Self where S: AsRef<str> {
    unsafe {
      let rsnowboy_detect = rsnowboy::detect_create(
        CString::new(resource_filename.as_ref()).unwrap().as_ptr(), //
        CString::new(model_str.as_ref()).unwrap().as_ptr(),
      );
      Self {
        rsnowboy_detect
      }
    }
  }

  pub fn reset(&self) -> bool {
    unsafe {
      rsnowboy::detect_reset(self.rsnowboy_detect)
    }
  }

  pub fn run_char_detection(&self, data: *const ::std::os::raw::c_char, is_end: bool) -> i32 {
    unsafe {
      rsnowboy::detect_run_char_detection(self.rsnowboy_detect, data)
    }
  }

  pub fn run_float_array_detection(&self, data: *const f32, array_length: ::std::os::raw::c_int, is_end: bool) -> i32 {
    unsafe {
      rsnowboy::detect_run_float_array_detection(self.rsnowboy_detect, data, array_length, is_end)
    }
  }

  pub fn run_short_array_detection(&self, data: *const i16, array_length: ::std::os::raw::c_int, is_end: bool) -> i32 {
    unsafe {
      rsnowboy::detect_run_short_array_detection(self.rsnowboy_detect, data, array_length, is_end)
    }
  }

  pub fn run_integer_array_detection(&self, data: *const i32, array_length: ::std::os::raw::c_int, is_end: bool) -> i32 {
    unsafe {
      rsnowboy::detect_run_integer_array_detection(self.rsnowboy_detect, data, array_length, is_end)
    }
  }

  pub fn set_sensitivity<S>(&self, sensitivity_str: S) where S: AsRef<str> {
    unsafe {
      rsnowboy::detect_set_sensitivity(self.rsnowboy_detect,
                                       CString::new(sensitivity_str.as_ref()).unwrap().as_ptr())
    }
  }

  pub fn get_sensitivity(&self) -> String {
    unsafe {
      let ret = rsnowboy::detect_get_sensitivity(self.rsnowboy_detect);
      let cstr = CStr::from_ptr(ret);
      String::from_utf8_lossy(cstr.to_bytes()).to_string()
    }
  }

  pub fn set_audio_gain(&self, audio_gain: f32) {
    unsafe {
      rsnowboy::detect_set_audio_gain(self.rsnowboy_detect, audio_gain)
    }
  }

  pub fn update_model(&self) {
    unsafe {
      rsnowboy::detect_update_model(self.rsnowboy_detect)
    }
  }

  pub fn num_hotwords(&self) -> i32 {
    unsafe {
      rsnowboy::detect_num_hotwords(self.rsnowboy_detect)
    }
  }

  pub fn apply_frontend(&self, apply_frontend: bool) {
    unsafe {
      rsnowboy::detect_apply_frontend(self.rsnowboy_detect, apply_frontend)
    }
  }

  pub fn sample_rate(&self) -> i32 {
    unsafe {
      rsnowboy::detect_sample_rate(self.rsnowboy_detect)
    }
  }

  pub fn num_channels(&self) -> i32 {
    unsafe {
      rsnowboy::detect_num_channels(self.rsnowboy_detect)
    }
  }

  pub fn bits_per_sample(&self) -> i32 {
    unsafe {
      rsnowboy::detect_bits_per_sample(self.rsnowboy_detect)
    }
  }

  pub fn destroy(&self) {
    unsafe {
      rsnowboy::detect_destroy(self.rsnowboy_detect)
    }
  }

}

#[derive(Debug, Copy, Clone)]
pub struct SnowboyVad {
  rsnowboy_vad: *mut rsnowboy::RSnowboyVad
}

impl SnowboyVad {
  pub fn new<S>(resource_filename: S) -> Self where S: AsRef<str> {
    unsafe {
      let rsnowboy_vad =
        rsnowboy::vad_create(
          CString::new(resource_filename.as_ref()).unwrap().as_ptr());
      Self {
        rsnowboy_vad
      }
    }
  }

  pub fn reset(&self) -> bool {
    unsafe {
      rsnowboy::vad_reset(self.rsnowboy_vad)
    }
  }

  pub fn run_char(&self, data: *const ::std::os::raw::c_char) -> i32 {
    unsafe {
      rsnowboy::vad_run_char(self.rsnowboy_vad, data)
    }
  }

  pub fn run_float_array(&self, data: *const f32, array_length: ::std::os::raw::c_int, is_end: bool) -> i32 {
    unsafe {
      rsnowboy::vad_run_float_array(self.rsnowboy_vad, data, array_length, is_end)
    }
  }

  pub fn run_short_array(&self, data: *const i16, array_length: ::std::os::raw::c_int, is_end: bool) -> i32 {
    unsafe {
      rsnowboy::vad_run_short_array(self.rsnowboy_vad, data, array_length, is_end)
    }
  }

  pub fn run_integer_array(&self, data: *const i32, array_length: ::std::os::raw::c_int, is_end: bool) -> i32 {
    unsafe {
      rsnowboy::vad_run_integer_array(self.rsnowboy_vad, data, array_length, is_end)
    }
  }

  pub fn set_audio_gain(&self, audio_gain: f32) {
    unsafe {
      rsnowboy::vad_set_audio_gain(self.rsnowboy_vad, audio_gain)
    }
  }

  pub fn apply_frontend(&self, apply_frontend: bool) {
    unsafe {
      rsnowboy::vad_apply_frontend(self.rsnowboy_vad, apply_frontend)
    }
  }

  pub fn sample_rate(&self) -> i32 {
    unsafe {
      rsnowboy::vad_sample_rate(self.rsnowboy_vad)
    }
  }

  pub fn num_channels(&self) -> i32 {
    unsafe {
      rsnowboy::vad_num_channels(self.rsnowboy_vad)
    }
  }

  pub fn bits_per_sample(&self) -> i32 {
    unsafe {
      rsnowboy::vad_bits_per_sample(self.rsnowboy_vad)
    }
  }

  pub fn destroy(&self) {
    unsafe {
      rsnowboy::vad_destroy(self.rsnowboy_vad)
    }
  }

}