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
/*
* Copyright (c) 2024. The RigelA open source project team and
* its contributors reserve all rights.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
#![doc = include_str!("../README.md")]
//! 尝试更现代化的开源读屏项目:
//! [RigelA](https://gitcode.net/mzdk100/rigela.git)
use libloading::{Library, Symbol};
use std::{
env::var,
io::{Error, ErrorKind},
path::Path,
sync::OnceLock,
};
//noinspection SpellCheckingInspection
#[cfg(target_arch = "x86")]
const LIB_NAME: &str = "ZDSRAPI.dll";
//noinspection SpellCheckingInspection
#[cfg(target_arch = "x86_64")]
const LIB_NAME: &str = "ZDSRAPI_x64.dll";
static LIB: OnceLock<Library> = OnceLock::new();
//noinspection SpellCheckingInspection
/// 初始化语音接口
///
/// # Arguments
///
/// * `r#type`: 0 读屏通道; 1 独立通道
/// * `channel_name`: type为0时忽略,传入NULL; type为1时:独立通道名称, NULL或空字符串时,将使用默认名称"API"
/// * `key_down_interrupt`: TRUE 按键打断; FALSE 按键不打断
///
/// returns: Result<i32, Error>
/// 0: 成功
/// 1: 版本不匹配
///
/// # Examples
///
/// ```
/// use zdsr_api::init_tts;
/// init_tts(0, std::ptr::null(), 0).unwrap();
/// ```
pub fn init_tts(
r#type: i32,
channel_name: *const u16,
key_down_interrupt: i32,
) -> Result<i32, Error> {
let win_dir = var("WINDIR").unwrap();
let zdsr_dir = Path::new(&win_dir)
.parent()
.unwrap()
.join("Program Files (x86)")
.join("zdsr");
// 检查商业版
let mut lib_path = zdsr_dir.join("zdsr").join(LIB_NAME);
if !lib_path.exists() {
// 检查青春版
lib_path = zdsr_dir.join("zdsr_yth").join(LIB_NAME);
if !lib_path.exists() {
return Err(Error::last_os_error());
}
}
let res = unsafe {
let lib = LIB.get_or_init(move || Library::new(lib_path).unwrap());
let func: Symbol<unsafe extern "C" fn(i32, *const u16, i32) -> i32> =
lib.get(b"InitTTS\0").unwrap();
func(r#type, channel_name, key_down_interrupt)
};
Ok(res)
}
//noinspection SpellCheckingInspection
/// 朗读文本
///
/// # Arguments
///
/// * `text`: 要朗读的文本,Unicode
/// * `interrupt`: TRUE:清空排队,立刻打断朗读, FALSE:等待空闲时朗读
///
/// returns: Result<i32, Error>
/// 0: 成功
/// 1: 版本不匹配
/// 2: ZDSR没有运行
///
/// # Examples
///
///```
/// use zdsr_api::speak;
/// // Speak: ABC
/// speak([65u16, 66, 67].as_ptr(), 0).unwrap();
/// ```
pub fn speak(text: *const u16, interrupt: i32) -> Result<i32, Error> {
let Some(lib) = LIB.get() else {
return Err(Error::new(
ErrorKind::Other,
"Can't find or init zdsr library.",
));
};
let res = unsafe {
let func: Symbol<unsafe extern "C" fn(*const u16, i32) -> i32> =
lib.get(b"Speak\0").unwrap();
func(text, interrupt)
};
Ok(res)
}
//noinspection SpellCheckingInspection
/// 获取朗读状态
///
/// returns Result<i32, Error>
/// 1: 版本不匹配
/// 2: ZDSR没有运行
/// 3: 正在朗读
/// 4: 没有朗读
///
/// # examples
///
/// ```
/// use zdsr_api::get_speak_state;
/// get_speak_state().unwrap();
/// ```
pub fn get_speak_state() -> Result<i32, Error> {
let Some(lib) = LIB.get() else {
return Err(Error::new(
ErrorKind::Other,
"Can't find or init zdsr library.",
));
};
let res = unsafe {
let func: Symbol<unsafe extern "C" fn() -> i32> = lib.get(b"GetSpeakState\0").unwrap();
func()
};
Ok(res)
}
//noinspection SpellCheckingInspection
/// 停止朗读
///
/// returns: Result<(), Error>
///
/// # examples
///
/// ```
/// use zdsr_api::stop_speak;
/// stop_speak().unwrap();
/// ```
pub fn stop_speak() -> Result<(), Error> {
let Some(lib) = LIB.get() else {
return Err(Error::new(
ErrorKind::Other,
"Can't find or init zdsr library.",
));
};
let res = unsafe {
let func: Symbol<unsafe extern "C" fn()> = lib.get(b"StopSpeak\0").unwrap();
func()
};
Ok(res)
}
//noinspection SpellCheckingInspection
#[cfg(test)]
mod test_zdsr {
use crate::{get_speak_state, init_tts, speak, stop_speak};
#[test]
fn main() {
assert_eq!(0, init_tts(0, std::ptr::null(), 0).unwrap());
assert_eq!(0, speak([65u16, 66, 67].as_ptr(), 0).unwrap());
assert_eq!(3, get_speak_state().unwrap());
stop_speak().unwrap();
}
}