luis_sys 0.4.5

FFI bindings for Microsoft LUIS 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
//! Represents specific audio configuration, such as microphone, file, or custom audio streams.
//!

use crate::speech_api::*;
use crate::{
    error, hr, properties::Properties, DeriveHandle, FlattenProps, Handle,
    Result, SmartHandle, INVALID_HANDLE, NULL_HANDLE,
};
use serde::{Deserialize, Serialize};
use std::{
    ffi::CString,
    io::{Cursor, Read, Write},
    os::raw::{c_char, c_int, c_void},
    slice::{from_raw_parts, from_raw_parts_mut},
    sync::{
        mpsc::{channel, Receiver, Sender, TryRecvError},
        Arc, Weak,
    },
};

/// Creates an audio stream format object with the specified PCM waveformat characteristics.
/// Currently, only WAV / PCM with 16-bit samples, 16 kHz sample rate, and a single channel (Mono) is supported.
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub struct AudioSpec {
    pub rate: u32,
    pub bits: u8,
    pub channels: u8,
}

impl Default for AudioSpec {
    fn default() -> Self {
        AudioSpec {
            rate: 16_000,
            bits: 16,
            channels: 1,
        }
    }
}

impl From<(u32, u8, u8)> for AudioSpec {
    fn from(trio: (u32, u8, u8)) -> Self {
        AudioSpec {
            rate: trio.0,
            bits: trio.1,
            channels: trio.2,
        }
    }
}

DeriveHandle!(
    Audio,
    SPXAUDIOCONFIGHANDLE,
    audio_config_release,
    audio_config_is_handle_valid
);

/// Audio input mode configuration.
pub struct Audio {
    /// Underlying handle.
    handle: SPXAUDIOCONFIGHANDLE,
    /// Placeholder of audio stream.
    stream: Option<Box<dyn AudioStream>>,
    /// Internal properties bag.
    props: Properties,
}

impl Audio {
    fn new(handle: SPXAUDIOCONFIGHANDLE) -> Result<Self> {
        if handle == NULL_HANDLE {
            return Ok(Audio {
                handle,
                props: Properties::new(INVALID_HANDLE),
                stream: None,
            });
        }
        let mut hprops = INVALID_HANDLE;
        hr!(audio_config_get_property_bag(handle, &mut hprops))?;
        Ok(Audio {
            handle,
            props: Properties::new(hprops),
            stream: None,
        })
    }

    /// Create push mode audio input stream.
    pub fn create_push_input(cfg: &AudioSpec) -> Result<Self> {
        let stream = PushAudioInputStream::from_config(cfg)?;
        Self::create_inpput_from_stream(Box::new(stream))
    }

    /// Create pull mode audio input stream.
    pub fn create_pull_input(cfg: &AudioSpec) -> Result<Self> {
        let stream = PullAudioInputStream::from_config(cfg)?;
        Self::create_inpput_from_stream(Box::new(stream))
    }

    /// Create audio output stream.
    pub fn create_output(cfg: &AudioSpec) -> Result<Self> {
        let stream = AudioOutputStream::from_config(cfg)?;
        Self::create_output_from_stream(Box::new(stream))
    }

    /// Create push mode audio input stream.
    pub fn create_push_output(cfg: &AudioSpec) -> Result<Self> {
        let stream = PushAudioOutputStream::from_config(cfg)?;
        Self::create_output_from_stream(Box::new(stream))
    }

    /// Create pull mode audio input stream.
    pub fn create_pull_output(cfg: &AudioSpec) -> Result<Self> {
        let stream = PullAudioOutputStream::from_config(cfg)?;
        Self::create_output_from_stream(Box::new(stream))
    }

    /// Create audio input from wav file. The audio format read from wav file header.
    pub fn create_input_from_wav_file(path: &str) -> Result<Self> {
        let mut handle = INVALID_HANDLE;
        let path = CString::new(path)?;
        hr!(audio_config_create_audio_input_from_wav_file_name(
            &mut handle,
            path.as_ptr(),
        ))?;
        Audio::new(handle)
    }

    /// Convert AudioInputStream to AudioInput. AudioInputStream is kept in AudioInput instance.
    pub fn create_inpput_from_stream(
        stream: Box<dyn AudioStream>,
    ) -> Result<Self> {
        let mut handle = INVALID_HANDLE;
        hr!(audio_config_create_audio_input_from_stream(
            &mut handle,
            stream.handle()
        ))?;
        let mut audio = Audio::new(handle)?;
        audio.stream = Some(stream);
        Ok(audio)
    }

    /// Create audio input from host microphone.
    pub fn create_input_from_microphone() -> Result<Self> {
        let mut handle = INVALID_HANDLE;
        hr!(audio_config_create_audio_input_from_default_microphone(
            &mut handle
        ))?;
        Audio::new(handle)
    }

    /// Create audio output to host speaker.
    pub fn create_output_to_speaker() -> Result<Self> {
        let mut handle = INVALID_HANDLE;
        hr!(audio_config_create_audio_output_from_default_speaker(
            &mut handle
        ))?;
        Audio::new(handle)
    }

    /// Create audio input from host microphone.
    pub fn create_output_to_file(path: &str) -> Result<Self> {
        let mut handle = INVALID_HANDLE;
        hr!(audio_config_create_audio_output_from_wav_file_name(
            &mut handle,
            path.as_ptr() as *const c_char,
        ))?;
        Audio::new(handle)
    }

    /// Convert audio output stream to Audio.
    pub fn create_output_from_stream(
        stream: Box<dyn AudioStream>,
    ) -> Result<Self> {
        let mut handle = INVALID_HANDLE;
        hr!(audio_config_create_audio_output_from_stream(
            &mut handle,
            stream.handle()
        ))?;
        let mut audio = Audio::new(handle)?;
        audio.stream = Some(stream);
        Ok(audio)
    }
}

FlattenProps!(Audio);

impl AudioStream for Audio {
    /// Input audio data via created stream.
    fn write(&mut self, buffer: &mut [u8]) -> Result {
        if let Some(stream) = &mut self.stream {
            stream.write(buffer)
        } else {
            Err(error::IsNothing)
        }
    }

    /// Output audio data via created stream.
    fn read(&mut self, buffer: &mut [u8]) -> Result<usize> {
        if let Some(stream) = &mut self.stream {
            stream.read(buffer)
        } else {
            Err(error::IsNothing)
        }
    }

    /// Close stream and release resource.
    fn close(&mut self) -> Result {
        if let Some(stream) = &mut self.stream {
            stream.close()
        } else {
            Ok(())
        }
    }
}

pub trait AudioStream: Handle {
    /// The main method to stream audio data.
    fn write(&mut self, _buffer: &mut [u8]) -> Result {
        Err(error::Unimplemented)
    }

    /// The main method to stream audio data.
    fn read(&mut self, _buffer: &mut [u8]) -> Result<usize> {
        Err(error::Unimplemented)
    }

    /// Close the stream gracefully.
    fn close(&mut self) -> Result {
        Ok(())
    }
}

// pub struct StreamReader {
//     receiver: Receiver<Vec<u8>>,
//     buffer: Option<Cursor<Vec<u8>>>,
//     closing: bool,
// }

// impl StreamReader {
//     pub fn new(receiver: Receiver<Vec<u8>>) -> Self {
//         StreamReader {
//             receiver,
//             buffer: None,
//             closing: false,
//         }
//     }
// }

// impl Read for StreamReader {
//     fn read(&mut self, buffer: &mut [u8]) -> IoResult<usize> {
//         if self.closing {
//             return Ok(0);
//         }

//         let buf_size = buffer.len();
//         let mut read_size = 0;
//         if let Some(cache) = &mut self.buffer {
//             read_size = cache.read(buffer)?;
//             if read_size < buf_size {
//                 self.buffer = None;
//             } else {
//                 return Ok(read_size);
//             }
//         }

//         while buf_size > read_size {
//             match self.receiver.try_recv() {
//                 Ok(data) => {
//                     let data_size = data.len();
//                     if data_size == 0 {
//                         self.closing = true;
//                         break;
//                     }
//                     let mut cache = Cursor::new(data);
//                     let sz = cache.read(&mut buffer[read_size..])?;
//                     read_size = read_size + sz;
//                     if sz < data_size {
//                         self.buffer = Some(cache);
//                         break;
//                     }
//                 }
//                 Err(TryRecvError::Empty) => (),
//                 Err(TryRecvError::Disconnected) => {
//                     return Err(IoError::from(ErrorKind::ConnectionAborted))
//                 }
//             }
//         }

//         if read_size > 0 {
//             return Ok(read_size);
//         }

//         let cache = self
//             .receiver
//             .recv()
//             .map_err(|err| IoError::new(ErrorKind::BrokenPipe, err))?;
//         if cache.len() == 0 {
//             self.closing = true;
//             return Ok(0);
//         } else {
//             self.buffer = Some(Cursor::new(cache));
//             return self.read(buffer);
//         }
//     }
// }

DeriveHandle!(
    PullAudioInputStream,
    SPXAUDIOSTREAMHANDLE,
    audio_stream_release,
    audio_stream_is_handle_valid
);

/// Pull audio input stream.
pub struct PullAudioInputStream {
    handle: SPXAUDIOSTREAMHANDLE,
    writer: Sender<Vec<u8>>,
    _reader: Arc<Receiver<Vec<u8>>>,
}

impl PullAudioInputStream {
    /// Create push stream according to the format.
    pub fn from_config(cfg: &AudioSpec) -> Result<Self> {
        let af = AudioStreamFormat::from_config(cfg)?;
        let mut hstream = INVALID_HANDLE;
        hr!(audio_stream_create_pull_audio_input_stream(
            &mut hstream,
            af.handle()
        ))?;
        let (writer, reader) = channel();
        let _reader = Arc::new(reader);
        let r = Box::new(Arc::downgrade(&_reader));
        let context = Box::into_raw(r) as *mut c_void;
        hr!(pull_audio_input_stream_set_callbacks(
            hstream,
            context,
            Some(on_stream_read),
            Some(on_stream_close)
        ))?;
        Ok(PullAudioInputStream {
            handle: hstream,
            writer,
            _reader,
        })
    }
}

impl AudioStream for PullAudioInputStream {
    fn write(&mut self, buffer: &mut [u8]) -> Result {
        let buf = buffer.to_owned();
        Ok(self.writer.send(buf)?)
    }

    /// Close the stream gracefully.
    fn close(&mut self) -> Result {
        self.write(&mut [])
    }
}

unsafe extern "C" fn on_stream_close(context: *mut c_void) {
    log::debug!("Pull stream close event fired.");
    if !context.is_null() {
        // Auto release the Box and weak pointer.
        Box::from_raw(context as *mut Weak<Receiver<Vec<u8>>>);
    }
}

unsafe extern "C" fn on_stream_read(
    context: *mut c_void,
    buffer: *mut u8,
    size: u32,
) -> c_int {
    if context.is_null() {
        log::error!("Unknown context with NULL pointer when read stream.");
        return 0;
    }
    let ctx = Box::from_raw(context as *mut Weak<Receiver<Vec<u8>>>);
    let ctx = Box::leak(ctx); // avoid auto release.
    if let Some(r) = ctx.upgrade() {
        let mut buf = from_raw_parts_mut(buffer, size as usize);
        let data = match r.try_recv() {
            Ok(data) => data,
            Err(TryRecvError::Empty) => match r.recv() {
                Ok(data) => data,
                Err(err) => {
                    log::error!("Data read error: {}", err);
                    return 0;
                }
            },
            Err(TryRecvError::Disconnected) => {
                log::error!("Data channel is disconnected!");
                return 0;
            }
        };
        let sz = data.len();
        return if sz == 0 {
            0
        } else if sz > buf.len() {
            log::error!(
                "Read buffer size is too small ({} vs {}) ",
                sz,
                buf.len()
            );
            0
        } else {
            buf.write(&data).unwrap()
        } as c_int;
    }
    log::error!("Cannot get stream reader!");
    return 0;
}

SmartHandle!(
    PushAudioInputStream,
    SPXAUDIOSTREAMHANDLE,
    audio_stream_release,
    audio_stream_is_handle_valid
);

impl PushAudioInputStream {
    /// Create push stream according to the format.
    pub fn from_config(cfg: &AudioSpec) -> Result<Self> {
        let af = AudioStreamFormat::from_config(cfg)?;
        let mut hstream = INVALID_HANDLE;
        hr!(audio_stream_create_push_audio_input_stream(
            &mut hstream,
            af.handle()
        ))?;
        Ok(PushAudioInputStream::new(hstream))
    }
}

impl AudioStream for PushAudioInputStream {
    fn write(&mut self, buffer: &mut [u8]) -> Result {
        let buf = buffer.as_mut_ptr();
        let size = buffer.len();
        hr!(push_audio_input_stream_write(self.handle, buf, size as u32))
    }

    /// Close the stream gracefully.
    fn close(&mut self) -> Result {
        hr!(push_audio_input_stream_close(self.handle()))
    }
}

DeriveHandle!(
    PushAudioOutputStream,
    SPXAUDIOSTREAMHANDLE,
    audio_stream_release,
    audio_stream_is_handle_valid
);

/// Push output stream.
pub struct PushAudioOutputStream {
    handle: SPXAUDIOSTREAMHANDLE,
    _writer: Arc<Sender<Vec<u8>>>,
    reader: Receiver<Vec<u8>>,
    buffer: Option<Cursor<Vec<u8>>>,
}

impl PushAudioOutputStream {
    /// Create push stream according to the format.
    pub fn from_config(cfg: &AudioSpec) -> Result<Self> {
        let af = AudioStreamFormat::from_config(cfg)?;
        let mut hstream = INVALID_HANDLE;
        hr!(audio_stream_create_push_audio_output_stream(
            &mut hstream,
            af.handle()
        ))?;
        let (writer, reader) = channel();
        let _writer = Arc::new(writer);
        let w = Box::new(Arc::downgrade(&_writer));
        let context = Box::into_raw(w) as *mut c_void;
        hr!(push_audio_output_stream_set_callbacks(
            hstream,
            context,
            Some(on_stream_write),
            Some(on_output_stream_close)
        ))?;
        Ok(PushAudioOutputStream {
            handle: hstream,
            _writer,
            reader,
            buffer: None,
        })
    }
}

impl AudioStream for PushAudioOutputStream {
    fn read(&mut self, buffer: &mut [u8]) -> Result<usize> {
        let buf_size = buffer.len();
        let mut read_size = 0;
        if let Some(cache) = &mut self.buffer {
            read_size = cache.read(buffer)?;
            if read_size < buf_size {
                self.buffer = None;
            } else {
                return Ok(read_size);
            }
        }
        while buf_size > read_size {
            match self.reader.try_recv() {
                Ok(data) => {
                    let data_size = data.len();
                    if data_size == 0 {
                        break;
                    }
                    let mut cache = Cursor::new(data);
                    let sz = cache.read(&mut buffer[read_size..])?;
                    read_size = read_size + sz;
                    if sz < data_size {
                        self.buffer = Some(cache);
                        break;
                    }
                }
                Err(TryRecvError::Empty) => (),
                Err(TryRecvError::Disconnected) => {
                    return Err(error::Other("disconnected".to_string()));
                }
            }
        }

        Ok(read_size)
    }
}

unsafe extern "C" fn on_output_stream_close(context: *mut c_void) {
    log::debug!("Output stream close event fired.");
    if !context.is_null() {
        // Auto release the Box and weak pointer.
        Box::from_raw(context as *mut Weak<Sender<Vec<u8>>>);
    }
}

unsafe extern "C" fn on_stream_write(
    context: *mut c_void,
    buffer: *mut u8,
    size: u32,
) -> c_int {
    if context.is_null() {
        log::error!("Unknown context with NULL pointer when write stream.");
        return 0;
    }
    let ctx = Box::from_raw(context as *mut Weak<Sender<Vec<u8>>>);
    let ctx = Box::leak(ctx); // avoid auto release.
    if let Some(s) = ctx.upgrade() {
        let buf = from_raw_parts(buffer, size as usize);
        match s.send(buf.to_owned()) {
            Ok(()) => return size as c_int,
            Err(err) => log::error!("Audio input stream read error: {}", err),
        }
    }
    log::error!("Cannot get stream reader!");
    return 0;
}

SmartHandle!(
    PullAudioOutputStream,
    SPXAUDIOSTREAMHANDLE,
    audio_stream_release,
    audio_stream_is_handle_valid
);

impl PullAudioOutputStream {
    /// Create push stream according to the format.
    pub fn from_config(cfg: &AudioSpec) -> Result<Self> {
        let af = AudioStreamFormat::from_config(cfg)?;
        let mut hstream = INVALID_HANDLE;
        hr!(audio_stream_create_pull_audio_output_stream(
            &mut hstream,
            af.handle()
        ))?;
        Ok(PullAudioOutputStream::new(hstream))
    }
}

impl AudioStream for PullAudioOutputStream {
    fn read(&mut self, buffer: &mut [u8]) -> Result<usize> {
        let buf = buffer.as_mut_ptr();
        let size = buffer.len();
        let mut filled = 0u32;
        let ptr_filled = &mut filled as *mut u32;
        hr!(pull_audio_output_stream_read(
            self.handle,
            buf,
            size as u32,
            ptr_filled
        ))?;
        Ok(filled as usize)
    }
}

SmartHandle!(
    AudioOutputStream,
    SPXAUDIOSTREAMHANDLE,
    audio_stream_release,
    audio_stream_is_handle_valid
);

impl AudioOutputStream {
    /// Create push stream according to the format.
    pub fn from_config(cfg: &AudioSpec) -> Result<Self> {
        let af = AudioStreamFormat::from_config(cfg)?;
        let mut hstream = INVALID_HANDLE;
        hr!(audio_stream_create_pull_audio_output_stream(
            &mut hstream,
            af.handle()
        ))?;
        Ok(AudioOutputStream::new(hstream))
    }
}

impl AudioStream for AudioOutputStream {}

SmartHandle!(
    AudioStreamFormat,
    SPXAUDIOSTREAMFORMATHANDLE,
    audio_stream_format_release,
    audio_stream_format_is_handle_valid
);

impl AudioStreamFormat {
    /// Create by specs.
    pub fn from_config(cfg: &AudioSpec) -> Result<Self> {
        let mut handle = INVALID_HANDLE;
        hr!(audio_stream_format_create_from_waveformat_pcm(
            &mut handle,
            cfg.rate,
            cfg.bits,
            cfg.channels
        ))?;
        Ok(AudioStreamFormat::new(handle))
    }
    /// Creates a memory backed push stream using the default format (16Khz 16bit mono PCM).
    pub fn from_default() -> Self {
        let mut handle = INVALID_HANDLE;
        unsafe {
            audio_stream_format_create_from_default_input(&mut handle);
        }
        AudioStreamFormat { handle }
    }
}