speech.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //! Accessibility: speech synthesis for the title's narration. No voice is
  2. //! installed here, and every stream is valid WAVE audio of no length, so
  3. //! narration is silent.
  4. // shortcut: silent speech. The host's engine (SAPI through COM, or WinRT
  5. // speech synthesis) needs COM bindings this crate has only in diagnostics
  6. // builds, plus voice enumeration and SSML the title sends (slot 0xa8); the
  7. // title then plays the empty stream and goes on.
  8. #![allow(clippy::not_unsafe_ptr_arg_deref)]
  9. use std::ffi::{c_char, c_void};
  10. use super::xgameruntime::{
  11. Guid, HRESULT, Interface, Methods, Object, S_OK, add_ref, missing, put, text,
  12. };
  13. /// Closed captions, high contrast, speech to text and synthesis.
  14. pub const PROVIDER: Guid = Guid::new(
  15. 0x3e24_1977,
  16. 0x6237,
  17. 0x41e9,
  18. [0x85, 0x59, 0x10, 0x2c, 0x2d, 0x99, 0x83, 0xf1],
  19. );
  20. /// Unchanged since 2020.
  21. pub const IID: Guid = Guid::new(
  22. 0xd722_b373,
  23. 0x8c4d,
  24. 0x4692,
  25. [0x9e, 0x51, 0xd6, 0xad, 0x9b, 0x37, 0xaa, 0x7d],
  26. );
  27. pub static OBJECT: Object = Object::new("accessibility", &VTABLE);
  28. pub const INTERFACES: &[Interface] = &[(PROVIDER, IID, &OBJECT)];
  29. /// A WAVE file of 16 kHz mono 16-bit PCM with no samples: RIFF size 36,
  30. /// a 16-byte format chunk (PCM, 1 channel, 16,000 Hz, 32,000 bytes/s,
  31. /// block 2, 16 bits), an empty data chunk.
  32. const SILENCE: [u8; 44] =
  33. *b"RIFF\x24\0\0\0WAVEfmt \x10\0\0\0\x01\0\x01\0\x80\x3e\0\0\0\x7d\0\0\x02\0\x10\0data\0\0\0\0";
  34. /// Synthesizers and streams hold nothing, so their handles are fixed
  35. /// addresses and closing them is free.
  36. static SYNTHESIZER: u8 = 0;
  37. static STREAM: u8 = 0;
  38. type This = *const Object;
  39. /// No voice is installed, so the callback never runs.
  40. extern "system" fn enumerate_voices(
  41. _: This,
  42. _context: *mut c_void,
  43. _callback: *const c_void,
  44. ) -> HRESULT {
  45. S_OK
  46. }
  47. extern "system" fn create(_: This, out: *mut *const u8) -> HRESULT {
  48. put(out, &raw const SYNTHESIZER)
  49. }
  50. extern "system" fn close(_: This, _: *const u8) -> HRESULT {
  51. S_OK
  52. }
  53. extern "system" fn set_default_voice(_: This, _: *const u8) -> HRESULT {
  54. S_OK
  55. }
  56. extern "system" fn set_custom_voice(_: This, _: *const u8, _voice: *const c_char) -> HRESULT {
  57. S_OK
  58. }
  59. extern "system" fn create_stream(
  60. _: This,
  61. _: *const u8,
  62. words: *const c_char,
  63. out: *mut *const u8,
  64. ) -> HRESULT {
  65. tracing::debug!(text = text(words).unwrap_or_default(), "speech (silent)");
  66. put(out, &raw const STREAM)
  67. }
  68. extern "system" fn stream_size(_: This, _: *const u8, out: *mut usize) -> HRESULT {
  69. put(out, SILENCE.len())
  70. }
  71. extern "system" fn stream_data(
  72. _: This,
  73. _: *const u8,
  74. size: usize,
  75. buffer: *mut u8,
  76. used: *mut usize,
  77. ) -> HRESULT {
  78. if buffer.is_null() || size < SILENCE.len() {
  79. return super::xasync::E_NOT_SUFFICIENT_BUFFER;
  80. }
  81. // SAFETY: the buffer holds `size` bytes, checked above.
  82. unsafe { buffer.copy_from_nonoverlapping(SILENCE.as_ptr(), SILENCE.len()) };
  83. if !used.is_null() {
  84. // SAFETY: checked non-null.
  85. unsafe { used.write(SILENCE.len()) };
  86. }
  87. S_OK
  88. }
  89. static VTABLE: Methods<22> = Methods([
  90. missing::<0> as *const (),
  91. add_ref as *const (),
  92. add_ref as *const (),
  93. missing::<3> as *const (), // 0x18 closed captions
  94. missing::<4> as *const (), // 0x20
  95. missing::<5> as *const (), // 0x28 high contrast
  96. missing::<6> as *const (), // 0x30 speech to text
  97. missing::<7> as *const (), // 0x38
  98. enumerate_voices as *const (), // 0x40
  99. create as *const (), // 0x48
  100. close as *const (), // 0x50
  101. set_default_voice as *const (), // 0x58
  102. set_custom_voice as *const (), // 0x60
  103. create_stream as *const (), // 0x68 from text
  104. close as *const (), // 0x70 a stream
  105. stream_size as *const (), // 0x78
  106. stream_data as *const (), // 0x80
  107. missing::<17> as *const (), // 0x88 speech to text
  108. missing::<18> as *const (), // 0x90
  109. missing::<19> as *const (), // 0x98
  110. missing::<20> as *const (), // 0xa0
  111. create_stream as *const (), // 0xa8 from SSML
  112. ]);
  113. #[cfg(test)]
  114. mod tests {
  115. use super::*;
  116. use crate::modules::xgameruntime::method;
  117. #[test]
  118. fn streams_are_empty_wave_files() {
  119. let this: This = &OBJECT;
  120. type Create = extern "system" fn(This, *mut *const u8) -> HRESULT;
  121. type Stream = extern "system" fn(This, *const u8, *const c_char, *mut *const u8) -> HRESULT;
  122. type Size = extern "system" fn(This, *const u8, *mut usize) -> HRESULT;
  123. type Data = extern "system" fn(This, *const u8, usize, *mut u8, *mut usize) -> HRESULT;
  124. let (mut synth, mut stream, mut size) = (std::ptr::null(), std::ptr::null(), 0);
  125. assert_eq!(method::<Create>(&OBJECT, 0x48)(this, &mut synth), S_OK);
  126. let say = method::<Stream>(&OBJECT, 0x68);
  127. assert_eq!(say(this, synth, c"Hello".as_ptr(), &mut stream), S_OK);
  128. assert_eq!(method::<Size>(&OBJECT, 0x78)(this, stream, &mut size), S_OK);
  129. let mut wav = vec![0u8; size];
  130. let mut used = 0;
  131. let data = method::<Data>(&OBJECT, 0x80);
  132. assert_eq!(data(this, stream, size, wav.as_mut_ptr(), &mut used), S_OK);
  133. assert_eq!(
  134. (&wav[..4], &wav[8..16], &wav[36..40]),
  135. (&b"RIFF"[..], &b"WAVEfmt "[..], &b"data"[..])
  136. );
  137. assert_eq!(u32::from_le_bytes(wav[24..28].try_into().unwrap()), 16_000);
  138. assert_eq!(used, 44);
  139. }
  140. }