bing-rs 0.0.4

Rust bindings to the Bing API
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
use std::ffi::CString;
use std::marker::Send;
use std::mem;
use std::os::raw::{c_char, c_double, c_int, c_void};
use std::ptr;
use std::sync::{Arc, Mutex};

use speech::websocket::*;
use speech::*;

#[no_mangle]
pub struct BingSpeech {
    handle: Speech,
}

#[no_mangle]
pub struct BingSpeechWebsocket {
    handle: Websocket,
}

#[no_mangle]
#[repr(C)]
pub struct BingSpeechWebsocketHandler {
    on_turn_start: fn(),
    on_turn_end: fn(),
    on_speech_start: fn(),
    on_speech_end: fn(),
    on_speech_hypothesis: fn(BingSpeechHypothesis),
    on_speech_phrase: fn(BingSpeechPhrase),
}

#[no_mangle]
#[repr(C)]
pub struct BingSpeechResult {
    pub confidence: c_double,
    pub lexical: *mut c_char,
    pub itn: *mut c_char,
    pub masked_itn: *mut c_char,
    pub display: *mut c_char,
}

#[no_mangle]
#[repr(C)]
pub struct BingSpeechPhrase {
    recognition_status: *mut c_char,
    display_text: *mut c_char,
    offset: c_double,
    duration: c_double,
    nbest: *mut BingSpeechResult,
    nbest_count: c_int,
}

#[no_mangle]
#[repr(C)]
pub struct BingSpeechHypothesis {
    text: *mut c_char,
    offset: c_double,
    duration: c_double,
}

unsafe impl Send for BingSpeechWebsocketHandler {}

#[no_mangle]
#[repr(C)]
pub struct BingSpeechHandler {
    c_handler: Arc<Mutex<BingSpeechWebsocketHandler>>,
}

fn nbest_to_c(nbest: &[DetailedPhraseItem]) -> Vec<BingSpeechResult> {
    nbest
        .iter()
        .map(|result| BingSpeechResult {
            confidence: result.confidence,
            lexical: to_c_string(&result.lexical),
            itn: to_c_string(&result.itn),
            masked_itn: to_c_string(&result.masked_itn),
            display: to_c_string(&result.display),
        })
        .collect()
}

fn to_c_string(s: &str) -> *mut c_char {
    CString::new(s).unwrap().into_raw()
}

impl Handler for BingSpeechHandler {
    fn on_turn_start(&mut self) {
        let handler = self.c_handler.lock().unwrap();
        let f: extern "C" fn() = unsafe { mem::transmute(handler.on_turn_start) };
        f();
    }

    fn on_turn_end(&mut self) {
        let handler = self.c_handler.lock().unwrap();
        let f: extern "C" fn() = unsafe { mem::transmute(handler.on_turn_end) };
        f();
    }

    fn on_speech_start(&mut self) {
        let handler = self.c_handler.lock().unwrap();
        let f: extern "C" fn() = unsafe { mem::transmute(handler.on_speech_start) };
        f();
    }

    fn on_speech_end(&mut self) {
        let handler = self.c_handler.lock().unwrap();
        let f: extern "C" fn() = unsafe { mem::transmute(handler.on_speech_end) };
        f();
    }

    fn on_speech_hypothesis(&mut self, hypothesis: Hypothesis) {
        let handler = self.c_handler.lock().unwrap();
        let f: extern "C" fn(BingSpeechHypothesis) =
            unsafe { mem::transmute(handler.on_speech_hypothesis) };
        f(BingSpeechHypothesis {
            text: to_c_string(&hypothesis.text),
            offset: hypothesis.offset,
            duration: hypothesis.duration,
        });
    }

    fn on_speech_phrase(&mut self, phrase: Phrase) {
        let handler = self.c_handler.lock().unwrap();
        let f: extern "C" fn(BingSpeechPhrase) =
            unsafe { mem::transmute(handler.on_speech_phrase) };
        let phrase = match phrase {
            Phrase::Simple(simple) => BingSpeechPhrase {
                recognition_status: to_c_string(&simple.recognition_status),
                display_text: to_c_string(&simple.display_text),
                offset: simple.offset,
                duration: simple.duration,
                nbest: ptr::null_mut(),
                nbest_count: 0,
            },
            Phrase::Detailed(detailed) => {
                let nbest_count = detailed.nbest.len() as i32;
                let mut nbest = nbest_to_c(&detailed.nbest);
                let phrase = BingSpeechPhrase {
                    recognition_status: to_c_string(&detailed.recognition_status),
                    display_text: ptr::null_mut(),
                    offset: detailed.offset,
                    duration: detailed.duration,
                    nbest: nbest.as_mut_ptr(),
                    nbest_count,
                };
                mem::forget(nbest);
                phrase
            }
            Phrase::Silence(silence) => BingSpeechPhrase {
                recognition_status: to_c_string(&silence.recognition_status),
                display_text: ptr::null_mut(),
                offset: silence.offset,
                duration: silence.duration,
                nbest: ptr::null_mut(),
                nbest_count: 0,
            },
            Phrase::Unknown => BingSpeechPhrase {
                recognition_status: to_c_string("Unknown"),
                display_text: ptr::null_mut(),
                offset: 0.0,
                duration: 0.0,
                nbest: ptr::null_mut(),
                nbest_count: 0,
            },
        };
        f(phrase);
    }
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_new(subscription_key: *mut c_char) -> *mut BingSpeech {
    let subscription_key = CString::from_raw(subscription_key).into_string().unwrap();
    let bing_speech = Box::new(BingSpeech {
        handle: Speech::new(&subscription_key).unwrap(),
    });

    mem::forget(subscription_key);
    Box::into_raw(bing_speech)
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_free(bing_speech: *mut BingSpeech) {
    Box::from_raw(bing_speech);
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_set_custom_speech(
    bing_speech: *mut BingSpeech,
    is_custom_speech: c_int,
) {
    (*bing_speech)
        .handle
        .set_custom_speech(is_custom_speech > 0);
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_set_endpoint_id(
    bing_speech: *mut BingSpeech,
    endpoint_id: *mut c_char,
) {
    let endpoint_id = CString::from_raw(endpoint_id).into_string().unwrap();
    (*bing_speech).handle.set_endpoint_id(&endpoint_id);
    mem::forget(endpoint_id);
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_fetch_token(bing_speech: *mut BingSpeech) -> *mut c_char {
    let result = (*bing_speech).handle.fetch_token();
    match result {
        Ok((_, _, Some(token))) => to_c_string(&token),
        _ => ptr::null_mut(),
    }
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_auto_fetch_token(bing_speech: *mut BingSpeech) {
    (*bing_speech).handle.auto_fetch_token();
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_recognize(
    bing_speech: *mut BingSpeech,
    c_audio: *const c_void,
    c_audio_len: c_int,
    c_mode: c_int,
    c_language: c_int,
    c_format: c_int,
    c_phrase: *mut BingSpeechPhrase,
) -> c_int {
    let audio: Vec<u8> = Vec::from_raw_parts(c_audio as *mut u8, c_audio_len as usize, c_audio_len as usize);
    let (mode, ok) = mode_from_c(c_mode, c_language);
    if ok != 0 {
        return ok;
    }

    let format =
        if c_format > 0 {
            Format::Detailed
        } else {
            Format::Simple
        };

    let audio_1 = audio.clone();
    mem::forget(audio);

    if let Ok((_, _, Some(phrase))) = (*bing_speech).handle.recognize(audio_1, &mode, &format) {
        match phrase {
            Phrase::Simple(simple) => {
                (*c_phrase).recognition_status = to_c_string(&simple.recognition_status);
                (*c_phrase).display_text = to_c_string(&simple.display_text);
                (*c_phrase).offset = simple.offset;
                (*c_phrase).duration = simple.duration;
                (*c_phrase).nbest = ptr::null_mut();
                (*c_phrase).nbest_count = 0;
            },
            Phrase::Detailed(detailed) => {
                let mut nbest = nbest_to_c(&detailed.nbest);
                let nbest_count = detailed.nbest.len() as i32;
                (*c_phrase).recognition_status = to_c_string(&detailed.recognition_status);
                (*c_phrase).display_text = ptr::null_mut();
                (*c_phrase).offset = detailed.offset;
                (*c_phrase).duration = detailed.duration;
                (*c_phrase).nbest = nbest.as_mut_ptr();
                (*c_phrase).nbest_count = nbest_count;
                mem::forget(nbest);
            }
            Phrase::Silence(silence) => {
                (*c_phrase).recognition_status = to_c_string(&silence.recognition_status);
                (*c_phrase).display_text = ptr::null_mut();
                (*c_phrase).offset = silence.offset;
                (*c_phrase).duration = silence.duration;
                (*c_phrase).nbest = ptr::null_mut();
                (*c_phrase).nbest_count = 0;
            },
            Phrase::Unknown => {
                (*c_phrase).recognition_status = to_c_string("Unknown");
                (*c_phrase).display_text = ptr::null_mut();
                (*c_phrase).offset = 0.0;
                (*c_phrase).duration = 0.0;
                (*c_phrase).nbest = ptr::null_mut();
                (*c_phrase).nbest_count = 0;
            },
        };
        0
    } else {
        1
    }
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_synthesize(bing_speech: *mut BingSpeech, c_text: *mut c_char, c_font: c_int, c_output: *mut *mut c_void, c_output_len: *mut c_int) {
    let text = CString::from_raw(c_text).into_string().unwrap();
    if let Ok((_, _, Some(mut data))) = (*bing_speech).handle.synthesize(&text, font_from_c(c_font)) {
        *c_output_len = data.len() as i32;
        *c_output = data.as_mut_ptr() as *mut c_void;
        mem::forget(data);
    } else {
        *c_output_len = 0;
        *c_output = ptr::null_mut();
    }
    mem::forget(text);
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_websocket_new() -> *mut BingSpeechWebsocket {
    let handle = Websocket::new();
    let websocket = Box::new(BingSpeechWebsocket { handle });
    Box::into_raw(websocket)
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_websocket_connect(
    c_speech: *mut BingSpeech,
    c_websocket: *mut BingSpeechWebsocket,
    c_mode: c_int,
    c_language: c_int,
    c_format: c_int,
    c_is_custom_speech: c_int,
    c_endpoint_id: *mut c_char,
    handler: BingSpeechWebsocketHandler,
) -> c_int {
    let (mode, ok) = mode_from_c(c_mode, c_language);
    if ok != 0 {
        return ok;
    }
    let format = format_from_c(c_format);
    let is_custom_speech = c_is_custom_speech > 0;
    let endpoint_id = if is_custom_speech {
        CString::from_raw(c_endpoint_id).into_string().unwrap()
    } else {
        "".to_string()
    };

    // Connect to Websocket
    let result = (*c_websocket).handle.connect(
        (*c_speech).handle.token.clone(),
        &mode,
        &format,
        is_custom_speech,
        &endpoint_id,
        Arc::new(Mutex::new(BingSpeechHandler {
            c_handler: Arc::new(Mutex::new(handler)),
        })),
    );
    mem::forget(endpoint_id);

    match result {
        Ok(_) => 0,
        Err(err) => {
            error!("{}", err);
            1
        }
    }
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_websocket_disconnect(
    c_websocket: *mut BingSpeechWebsocket,
) -> c_int {
    let result = (*c_websocket).handle.disconnect();

    match result {
        Ok(_) => 0,
        Err(err) => {
            error!("{}", err);
            1
        }
    }
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_websocket_free(
    c_websocket: *mut BingSpeechWebsocket,
) {
    Box::from_raw(c_websocket);
}

#[no_mangle]
pub unsafe extern "C" fn bing_speech_websocket_audio(
    handle: *mut BingSpeechWebsocket,
    audio: *const u8,
    audio_size: usize,
) -> i32 {
    const BUFFER_SIZE: usize = 4096;

    let audio: Vec<u8> = Vec::from_raw_parts(audio as *mut u8, audio_size, audio_size);
    let mut i = 0;

    while i < audio_size {
        let j = if audio_size - i < BUFFER_SIZE {
            audio_size
        } else {
            i + BUFFER_SIZE
        };

        // Send audio data to Bing Speech
        let result = (*handle).handle.audio(&audio[i..j].to_vec());
        if let Err(err) = result {
            error!("{}", err);
            return 2;
        }

        i = j;
    }

    mem::forget(audio);

    0
}

fn mode_from_c(c_mode: c_int, c_language: c_int) -> (Mode, c_int) {
    match c_mode {
        0 => match c_language {
            0 => (
                Mode::Interactive(InteractiveDictationLanguage::ArabicEgypt),
                0,
            ),
            1 => (
                Mode::Interactive(InteractiveDictationLanguage::CatalanSpain),
                0,
            ),
            2 => (
                Mode::Interactive(InteractiveDictationLanguage::ChineseChina),
                0,
            ),
            3 => (
                Mode::Interactive(InteractiveDictationLanguage::ChineseHongKong),
                0,
            ),
            4 => (
                Mode::Interactive(InteractiveDictationLanguage::ChineseTaiwan),
                0,
            ),
            5 => (
                Mode::Interactive(InteractiveDictationLanguage::DanishDenmark),
                0,
            ),
            6 => (
                Mode::Interactive(InteractiveDictationLanguage::DutchNetherlands),
                0,
            ),
            7 => (
                Mode::Interactive(InteractiveDictationLanguage::EnglishAustralia),
                0,
            ),
            8 => (
                Mode::Interactive(InteractiveDictationLanguage::EnglishCanada),
                0,
            ),
            9 => (
                Mode::Interactive(InteractiveDictationLanguage::EnglishIndia),
                0,
            ),
            10 => (
                Mode::Interactive(InteractiveDictationLanguage::EnglishNewZealand),
                0,
            ),
            11 => (
                Mode::Interactive(InteractiveDictationLanguage::EnglishUnitedKingdom),
                0,
            ),
            12 => (
                Mode::Interactive(InteractiveDictationLanguage::EnglishUnitedStates),
                0,
            ),
            13 => (
                Mode::Interactive(InteractiveDictationLanguage::FinnishFinland),
                0,
            ),
            14 => (
                Mode::Interactive(InteractiveDictationLanguage::FrenchCanada),
                0,
            ),
            15 => (
                Mode::Interactive(InteractiveDictationLanguage::FrenchFrance),
                0,
            ),
            16 => (
                Mode::Interactive(InteractiveDictationLanguage::GermanGermany),
                0,
            ),
            17 => (
                Mode::Interactive(InteractiveDictationLanguage::HindiIndia),
                0,
            ),
            18 => (
                Mode::Interactive(InteractiveDictationLanguage::ItalianItaly),
                0,
            ),
            19 => (
                Mode::Interactive(InteractiveDictationLanguage::JapaneseJapan),
                0,
            ),
            20 => (
                Mode::Interactive(InteractiveDictationLanguage::KoreanKorea),
                0,
            ),
            21 => (
                Mode::Interactive(InteractiveDictationLanguage::NorwegianNorway),
                0,
            ),
            22 => (
                Mode::Interactive(InteractiveDictationLanguage::PolishPoland),
                0,
            ),
            23 => (
                Mode::Interactive(InteractiveDictationLanguage::PortugueseBrazil),
                0,
            ),
            24 => (
                Mode::Interactive(InteractiveDictationLanguage::PortuguesePortugal),
                0,
            ),
            25 => (
                Mode::Interactive(InteractiveDictationLanguage::RussianRussia),
                0,
            ),
            26 => (
                Mode::Interactive(InteractiveDictationLanguage::SpanishMexico),
                0,
            ),
            27 => (
                Mode::Interactive(InteractiveDictationLanguage::SpanishSpain),
                0,
            ),
            28 => (
                Mode::Interactive(InteractiveDictationLanguage::SwedishSweden),
                0,
            ),
            _ => (
                Mode::Interactive(InteractiveDictationLanguage::EnglishUnitedStates),
                1,
            ),
        },
        1 => match c_language {
            0 => (
                Mode::Dictation(InteractiveDictationLanguage::ArabicEgypt),
                0,
            ),
            1 => (
                Mode::Dictation(InteractiveDictationLanguage::CatalanSpain),
                0,
            ),
            2 => (
                Mode::Dictation(InteractiveDictationLanguage::ChineseChina),
                0,
            ),
            3 => (
                Mode::Dictation(InteractiveDictationLanguage::ChineseHongKong),
                0,
            ),
            4 => (
                Mode::Dictation(InteractiveDictationLanguage::ChineseTaiwan),
                0,
            ),
            5 => (
                Mode::Dictation(InteractiveDictationLanguage::DanishDenmark),
                0,
            ),
            6 => (
                Mode::Dictation(InteractiveDictationLanguage::DutchNetherlands),
                0,
            ),
            7 => (
                Mode::Dictation(InteractiveDictationLanguage::EnglishAustralia),
                0,
            ),
            8 => (
                Mode::Dictation(InteractiveDictationLanguage::EnglishCanada),
                0,
            ),
            9 => (
                Mode::Dictation(InteractiveDictationLanguage::EnglishIndia),
                0,
            ),
            10 => (
                Mode::Dictation(InteractiveDictationLanguage::EnglishNewZealand),
                0,
            ),
            11 => (
                Mode::Dictation(InteractiveDictationLanguage::EnglishUnitedKingdom),
                0,
            ),
            12 => (
                Mode::Dictation(InteractiveDictationLanguage::EnglishUnitedStates),
                0,
            ),
            13 => (
                Mode::Dictation(InteractiveDictationLanguage::FinnishFinland),
                0,
            ),
            14 => (
                Mode::Dictation(InteractiveDictationLanguage::FrenchCanada),
                0,
            ),
            15 => (
                Mode::Dictation(InteractiveDictationLanguage::FrenchFrance),
                0,
            ),
            16 => (
                Mode::Dictation(InteractiveDictationLanguage::GermanGermany),
                0,
            ),
            17 => (Mode::Dictation(InteractiveDictationLanguage::HindiIndia), 0),
            18 => (
                Mode::Dictation(InteractiveDictationLanguage::ItalianItaly),
                0,
            ),
            19 => (
                Mode::Dictation(InteractiveDictationLanguage::JapaneseJapan),
                0,
            ),
            20 => (
                Mode::Dictation(InteractiveDictationLanguage::KoreanKorea),
                0,
            ),
            21 => (
                Mode::Dictation(InteractiveDictationLanguage::NorwegianNorway),
                0,
            ),
            22 => (
                Mode::Dictation(InteractiveDictationLanguage::PolishPoland),
                0,
            ),
            23 => (
                Mode::Dictation(InteractiveDictationLanguage::PortugueseBrazil),
                0,
            ),
            24 => (
                Mode::Dictation(InteractiveDictationLanguage::PortuguesePortugal),
                0,
            ),
            25 => (
                Mode::Dictation(InteractiveDictationLanguage::RussianRussia),
                0,
            ),
            26 => (
                Mode::Dictation(InteractiveDictationLanguage::SpanishMexico),
                0,
            ),
            27 => (
                Mode::Dictation(InteractiveDictationLanguage::SpanishSpain),
                0,
            ),
            28 => (
                Mode::Dictation(InteractiveDictationLanguage::SwedishSweden),
                0,
            ),
            _ => (
                Mode::Dictation(InteractiveDictationLanguage::EnglishUnitedStates),
                1,
            ),
        },
        2 => match c_language {
            0 => (Mode::Conversation(ConversationLanguage::ArabicEgypt), 0),
            1 => (Mode::Conversation(ConversationLanguage::ChineseChina), 0),
            2 => (
                Mode::Conversation(ConversationLanguage::EnglishUnitedStates),
                0,
            ),
            3 => (Mode::Conversation(ConversationLanguage::FrenchFrance), 0),
            4 => (Mode::Conversation(ConversationLanguage::GermanGermany), 0),
            5 => (Mode::Conversation(ConversationLanguage::ItalianItaly), 0),
            6 => (Mode::Conversation(ConversationLanguage::JapaneseJapan), 0),
            7 => (
                Mode::Conversation(ConversationLanguage::PortugueseBrazil),
                0,
            ),
            8 => (Mode::Conversation(ConversationLanguage::RussianRussia), 0),
            9 => (Mode::Conversation(ConversationLanguage::SpanishSpain), 0),
            _ => (
                Mode::Conversation(ConversationLanguage::EnglishUnitedStates),
                1,
            ),
        },
        _ => (
            Mode::Interactive(InteractiveDictationLanguage::EnglishUnitedStates),
            0,
        ),
    }
}

fn format_from_c(c_format: c_int) -> Format {
    match c_format {
        0 => Format::Simple,
        _ => Format::Detailed,
    }
}

fn font_from_c(c_font: c_int) -> &'static voice::Font {
    match c_font {
        0 => voice::ar_eg::HODA,
        1 => voice::ar_sa::NAAYF,
        2 => voice::bg_bg::IVAN,
        3 => voice::ca_es::HERENA_RUS,
        4 => voice::ca_cz::JAKUB,
        5 => voice::da_dk::HELLE_RUS,
        6 => voice::de_at::MICHAEL,
        7 => voice::de_ch::KARSTEN,
        8 => voice::de_de::HEDDA,
        9 => voice::de_de::HEDDA_RUS,
        10 => voice::de_de::STEFAN_APOLLO,
        11 => voice::el_gr::STEFANOS,
        12 => voice::en_au::CATHERINE,
        13 => voice::en_au::HAYLEY_RUS,
        14 => voice::en_ca::LINDA,
        15 => voice::en_ca::HEATHER_RUS,
        16 => voice::en_gb::SUSAN_APOLLO,
        17 => voice::en_gb::HAZEL_RUS,
        18 => voice::en_gb::GEORGE_APOLLO,
        19 => voice::en_ie::SEAN,
        20 => voice::en_in::HEERA_APOLLO,
        21 => voice::en_in::PRIYA_RUS,
        22 => voice::en_in::RAVI_APOLLO,
        23 => voice::en_us::ZIRA_RUS,
        24 => voice::en_us::JESSA_RUS,
        25 => voice::en_us::BENJAMIN_RUS,
        26 => voice::es_es::LAURA_APOLLO,
        27 => voice::es_es::HELENA_RUS,
        28 => voice::es_es::PABLO_APOLLO,
        29 => voice::es_mx::HILDA_RUS,
        30 => voice::es_mx::RAUL_APOLLO,
        31 => voice::fi_fi::HEIDI_RUS,
        32 => voice::fr_ca::CAROLINE,
        33 => voice::fr_ca::HARMONIE_RUS,
        34 => voice::fr_ch::GUILLAUME,
        35 => voice::fr_fr::JULIE_APOLLO,
        36 => voice::fr_fr::HORTENSE_RUS,
        37 => voice::fr_fr::PAUL_APOLLO,
        38 => voice::he_il::ASAF,
        39 => voice::hi_in::KALPANA_APOLLO,
        40 => voice::hi_in::KALPANA,
        41 => voice::hi_in::HEMANT,
        42 => voice::hr_hr::MATEJ,
        43 => voice::hu_hu::SZABOLCS,
        44 => voice::id_id::ANDIKA,
        45 => voice::it_it::COSIMA_APOLLO,
        46 => voice::ja_jp::AYUMI_APOLLO,
        47 => voice::ja_jp::ICHIRO_APOLLO,
        48 => voice::ja_jp::HARUKA_RUS,
        49 => voice::ja_jp::LUCIA_RUS,
        50 => voice::ja_jp::EKATERINA_RUS,
        51 => voice::ko_kr::HEAMI_RUS,
        52 => voice::ms_my::RIZWAN,
        53 => voice::nb_no::HULDA_RUS,
        54 => voice::nl_nl::HANNA_RUS,
        55 => voice::pt_br::HELOISA_RUS,
        56 => voice::pt_br::DANIEL_APOLLO,
        57 => voice::ro_ro::ANDREI,
        58 => voice::ru_ru::IRINA_APOLLO,
        59 => voice::ru_ru::PAVEL_APOLLO,
        60 => voice::sk_sk::FILIP,
        61 => voice::sv_se::HEDVIG_RUS,
        62 => voice::ta_in::VALLUVAR,
        63 => voice::th_th::PATTARA,
        64 => voice::tr_tr::SEDA_RUS,
        65 => voice::vi_vn::AN,
        66 => voice::zh_cn::HUIHUI_RUS,
        67 => voice::zh_cn::YAOYAO_APOLLO,
        68 => voice::zh_cn::KANGKANG_APOLLO,
        69 => voice::zh_hk::TRACY_APOLLO,
        70 => voice::zh_hk::TRACY_RUS,
        71 => voice::zh_hk::DANNY_APOLLO,
        72 => voice::zh_tw::YATING_APOLLO,
        73 => voice::zh_tw::HANHAN_RUS,
        74 => voice::zh_tw::ZHIWEI_APOLLO,
        _ => voice::en_us::JESSA_RUS,
    }
}

#[no_mangle]
pub static MODE_INTERACTIVE: i32 = 0;
#[no_mangle]
pub static MODE_DICTATION: i32 = 1;
#[no_mangle]
pub static MODE_CONVERSATION: i32 = 2;

#[no_mangle]
pub static LANGUAGE_ARABIC_EGYPT: i32 = 0;
#[no_mangle]
pub static LANGUAGE_CATALAN_SPAIN: i32 = 1;
#[no_mangle]
pub static LANGUAGE_CHINESE_CHINA: i32 = 2;
#[no_mangle]
pub static LANGUAGE_CHINESE_HONG_KONG: i32 = 3;
#[no_mangle]
pub static LANGUAGE_CHINESE_TAIWAN: i32 = 4;
#[no_mangle]
pub static LANGUAGE_DANISH_DENMARK: i32 = 5;
#[no_mangle]
pub static LANGUAGE_DUTCH_NETHERLANDS: i32 = 6;
#[no_mangle]
pub static LANGUAGE_ENGLISH_AUSTRALIA: i32 = 7;
#[no_mangle]
pub static LANGUAGE_ENGLISH_CANADA: i32 = 8;
#[no_mangle]
pub static LANGUAGE_ENGLISH_INDIA: i32 = 9;
#[no_mangle]
pub static LANGUAGE_ENGLISH_NEW_ZEALAND: i32 = 10;
#[no_mangle]
pub static LANGUAGE_ENGLISH_UNITED_KINGDOM: i32 = 11;
#[no_mangle]
pub static LANGUAGE_ENGLISH_UNITED_STATES: i32 = 12;
#[no_mangle]
pub static LANGUAGE_FINNISH_FINLAND: i32 = 13;
#[no_mangle]
pub static LANGUAGE_FRENCH_CANADA: i32 = 14;
#[no_mangle]
pub static LANGUAGE_FRENCH_FRANCE: i32 = 15;
#[no_mangle]
pub static LANGUAGE_GERMAN_GERMANY: i32 = 16;
#[no_mangle]
pub static LANGUAGE_HINDI_INDIA: i32 = 17;
#[no_mangle]
pub static LANGUAGE_ITALIAN_ITALY: i32 = 18;
#[no_mangle]
pub static LANGUAGE_JAPANESE_JAPAN: i32 = 19;
#[no_mangle]
pub static LANGUAGE_KOREAN_KOREA: i32 = 20;
#[no_mangle]
pub static LANGUAGE_NORWEGIAN_NORWAY: i32 = 21;
#[no_mangle]
pub static LANGUAGE_POLISH_POLAND: i32 = 22;
#[no_mangle]
pub static LANGUAGE_PORTUGUESE_BRAZIL: i32 = 23;
#[no_mangle]
pub static LANGUAGE_PORTUGUESE_PORTUGAL: i32 = 24;
#[no_mangle]
pub static LANGUAGE_RUSSIAN_RUSSIA: i32 = 25;
#[no_mangle]
pub static LANGUAGE_SPANISH_MEXICO: i32 = 26;
#[no_mangle]
pub static LANGUAGE_SPANISH_SPAIN: i32 = 27;
#[no_mangle]
pub static LANGUAGE_SWEDISH_SWEDEN: i32 = 28;

#[no_mangle]
pub static FORMAT_SIMPLE: i32 = 0;
#[no_mangle]
pub static FORMAT_DETAILED: i32 = 1;

#[no_mangle]
pub static VOICE_FONT_AR_EG_HODA           : i32 = 0;
#[no_mangle]
pub static VOICE_FONT_AR_SA_NAAYF          : i32 = 1;
#[no_mangle]
pub static VOICE_FONT_BG_BG_IVAN           : i32 = 2;
#[no_mangle]
pub static VOICE_FONT_CA_ES_HERENA_RUS     : i32 = 3;
#[no_mangle]
pub static VOICE_FONT_CA_CZ_JAKUB          : i32 = 4;
#[no_mangle]
pub static VOICE_FONT_DA_DK_HELLE_RUS      : i32 = 5;
#[no_mangle]
pub static VOICE_FONT_DE_AT_MICHAEL        : i32 = 6;
#[no_mangle]
pub static VOICE_FONT_DE_CH_KARSTEN        : i32 = 7;
#[no_mangle]
pub static VOICE_FONT_DE_DE_HEDDA          : i32 = 8;
#[no_mangle]
pub static VOICE_FONT_DE_DE_HEDDA_RUS      : i32 = 9;
#[no_mangle]
pub static VOICE_FONT_DE_DE_STEFAN_APOLLO  : i32 = 10;
#[no_mangle]
pub static VOICE_FONT_EL_GR_STEFANOS       : i32 = 11;
#[no_mangle]
pub static VOICE_FONT_EN_AU_CATHERINE      : i32 = 12;
#[no_mangle]
pub static VOICE_FONT_EN_AU_HAYLEY_RUS     : i32 = 13;
#[no_mangle]
pub static VOICE_FONT_EN_CA_LINDA          : i32 = 14;
#[no_mangle]
pub static VOICE_FONT_EN_CA_HEATHER_RUS    : i32 = 15;
#[no_mangle]
pub static VOICE_FONT_EN_GB_SUSAN_APOLLO   : i32 = 16;
#[no_mangle]
pub static VOICE_FONT_EN_GB_HAZEL_RUS      : i32 = 17;
#[no_mangle]
pub static VOICE_FONT_EN_GB_GEORGE_APOLLO  : i32 = 18;
#[no_mangle]
pub static VOICE_FONT_EN_IE_SEAN           : i32 = 19;
#[no_mangle]
pub static VOICE_FONT_EN_IN_HEERA_APOLLO   : i32 = 20;
#[no_mangle]
pub static VOICE_FONT_EN_IN_PRIYA_RUS      : i32 = 21;
#[no_mangle]
pub static VOICE_FONT_EN_IN_RAVI_APOLLO    : i32 = 22;
#[no_mangle]
pub static VOICE_FONT_EN_US_ZIRA_RUS       : i32 = 23;
#[no_mangle]
pub static VOICE_FONT_EN_US_JESSA_RUS      : i32 = 24;
#[no_mangle]
pub static VOICE_FONT_EN_US_BENJAMIN_RUS   : i32 = 25;
#[no_mangle]
pub static VOICE_FONT_ES_ES_LAURA_APOLLO   : i32 = 26;
#[no_mangle]
pub static VOICE_FONT_ES_ES_HELENA_RUS     : i32 = 27;
#[no_mangle]
pub static VOICE_FONT_ES_ES_PABLO_APOLLO   : i32 = 28;
#[no_mangle]
pub static VOICE_FONT_ES_MX_HILDA_RUS      : i32 = 29;
#[no_mangle]
pub static VOICE_FONT_ES_MX_RAUL_APOLLO    : i32 = 30;
#[no_mangle]
pub static VOICE_FONT_FI_FI_HEIDI_RUS      : i32 = 31;
#[no_mangle]
pub static VOICE_FONT_FR_CA_CAROLINE       : i32 = 32;
#[no_mangle]
pub static VOICE_FONT_FR_CA_HARMONIE_RUS   : i32 = 33;
#[no_mangle]
pub static VOICE_FONT_FR_CH_GUILLAUME      : i32 = 34;
#[no_mangle]
pub static VOICE_FONT_FR_FR_JULIE_APOLLO   : i32 = 35;
#[no_mangle]
pub static VOICE_FONT_FR_FR_HORTENSE_RUS   : i32 = 36;
#[no_mangle]
pub static VOICE_FONT_FR_FR_PAUL_APOLLO    : i32 = 37;
#[no_mangle]
pub static VOICE_FONT_HE_IL_ASAF           : i32 = 38;
#[no_mangle]
pub static VOICE_FONT_HI_IN_KALPANA_APOLLO : i32 = 39;
#[no_mangle]
pub static VOICE_FONT_HI_IN_KALPANA        : i32 = 40;
#[no_mangle]
pub static VOICE_FONT_HI_IN_HEMANT         : i32 = 41;
#[no_mangle]
pub static VOICE_FONT_HR_HR_MATEJ          : i32 = 42;
#[no_mangle]
pub static VOICE_FONT_HU_HU_SZABOLCS       : i32 = 43;
#[no_mangle]
pub static VOICE_FONT_ID_ID_ANDIKA         : i32 = 44;
#[no_mangle]
pub static VOICE_FONT_IT_IT_COSIMA_APOLLO  : i32 = 45;
#[no_mangle]
pub static VOICE_FONT_JA_JP_AYUMI_APOLLO   : i32 = 46;
#[no_mangle]
pub static VOICE_FONT_JA_JP_ICHIRO_APOLLO  : i32 = 47;
#[no_mangle]
pub static VOICE_FONT_JA_JP_HARUKA_RUS     : i32 = 48;
#[no_mangle]
pub static VOICE_FONT_JA_JP_LUCIA_RUS      : i32 = 49;
#[no_mangle]
pub static VOICE_FONT_JA_JP_EKATERINA_RUS  : i32 = 50;
#[no_mangle]
pub static VOICE_FONT_KO_KR_HEAMI_RUS      : i32 = 51;
#[no_mangle]
pub static VOICE_FONT_MS_MY_RIZWAN         : i32 = 52;
#[no_mangle]
pub static VOICE_FONT_NB_NO_HULDA_RUS      : i32 = 53;
#[no_mangle]
pub static VOICE_FONT_NL_NL_HANNA_RUS      : i32 = 54;
#[no_mangle]
pub static VOICE_FONT_PT_BR_HELOISA_RUS    : i32 = 55;
#[no_mangle]
pub static VOICE_FONT_PT_BR_DANIEL_APOLLO  : i32 = 56;
#[no_mangle]
pub static VOICE_FONT_RO_RO_ANDREI         : i32 = 57;
#[no_mangle]
pub static VOICE_FONT_RU_RU_IRINA_APOLLO   : i32 = 58;
#[no_mangle]
pub static VOICE_FONT_RU_RU_PAVEL_APOLLO   : i32 = 59;
#[no_mangle]
pub static VOICE_FONT_SK_SK_FILIP          : i32 = 60;
#[no_mangle]
pub static VOICE_FONT_SV_SE_HEDVIG_RUS     : i32 = 61;
#[no_mangle]
pub static VOICE_FONT_TA_IN_VALLUVAR       : i32 = 62;
#[no_mangle]
pub static VOICE_FONT_TH_TH_PATTARA        : i32 = 63;
#[no_mangle]
pub static VOICE_FONT_TR_TR_SEDA_RUS       : i32 = 64;
#[no_mangle]
pub static VOICE_FONT_VI_VN_AN             : i32 = 65;
#[no_mangle]
pub static VOICE_FONT_ZH_CN_HUIHUI_RUS     : i32 = 66;
#[no_mangle]
pub static VOICE_FONT_ZH_CN_YAOYAO_APOLLO  : i32 = 67;
#[no_mangle]
pub static VOICE_FONT_ZH_CN_KANGKANG_APOLLO: i32 = 68;
#[no_mangle]
pub static VOICE_FONT_ZH_HK_TRACY_APOLLO   : i32 = 69;
#[no_mangle]
pub static VOICE_FONT_ZH_HK_TRACY_RUS      : i32 = 70;
#[no_mangle]
pub static VOICE_FONT_ZH_HK_DANNY_APOLLO   : i32 = 71;
#[no_mangle]
pub static VOICE_FONT_ZH_TW_YATING_APOLLO  : i32 = 72;
#[no_mangle]
pub static VOICE_FONT_ZH_TW_HANHAN_RUS     : i32 = 73;
#[no_mangle]
pub static VOICE_FONT_ZH_TW_ZHIWEI_APOLLO  : i32 = 74;