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
#![deny(unsafe_op_in_unsafe_fn)]
use std::thread;
use std::time::Duration;
use icrate::ns_string;
use icrate::objc2::rc::{Id, Owned};
use icrate::objc2::{extern_class, msg_send, msg_send_id, ClassType};
use icrate::Foundation::{NSObject, NSString};
#[cfg(target_os = "macos")]
mod appkit {
use icrate::objc2::rc::Shared;
use icrate::Foundation::NSCopying;
use super::*;
#[link(name = "AppKit", kind = "framework")]
extern "C" {}
extern_class!(
pub struct Synthesizer;
unsafe impl ClassType for Synthesizer {
type Super = NSObject;
const NAME: &'static str = "NSSpeechSynthesizer";
}
);
impl Synthesizer {
pub fn new() -> Id<Self, Owned> {
unsafe { msg_send_id![Self::class(), new] }
}
fn set_rate(&mut self, rate: f32) {
unsafe { msg_send![self, setRate: rate] }
}
fn set_volume(&mut self, volume: f32) {
unsafe { msg_send![self, setVolume: volume] }
}
fn start_speaking(&mut self, s: &NSString) {
unsafe { msg_send![self, startSpeakingString: s] }
}
pub fn speak(&mut self, utterance: &Utterance) {
self.set_rate(90.0 + (utterance.rate * (360.0 - 90.0)));
self.set_volume(utterance.volume);
self.start_speaking(&utterance.string);
}
pub fn is_speaking(&self) -> bool {
unsafe { msg_send![self, isSpeaking] }
}
}
pub struct Utterance {
rate: f32,
volume: f32,
string: Id<NSString, Shared>,
}
impl Utterance {
pub fn new(string: &NSString) -> Self {
Self {
rate: 0.5,
volume: 1.0,
string: string.copy(),
}
}
pub fn set_rate(&mut self, rate: f32) {
self.rate = rate;
}
pub fn set_volume(&mut self, volume: f32) {
self.volume = volume;
}
}
}
#[cfg(not(target_os = "macos"))]
mod avfaudio {
use super::*;
#[link(name = "AVFoundation", kind = "framework")]
extern "C" {}
extern_class!(
#[derive(Debug)]
pub struct Synthesizer;
unsafe impl ClassType for Synthesizer {
type Super = NSObject;
const NAME: &'static str = "AVSpeechSynthesizer";
}
);
impl Synthesizer {
pub fn new() -> Id<Self, Owned> {
unsafe { msg_send_id![Self::class(), new] }
}
pub fn speak(&mut self, utterance: &Utterance) {
unsafe { msg_send![self, speakUtterance: utterance] }
}
pub fn is_speaking(&self) -> bool {
unsafe { msg_send![self, isSpeaking] }
}
}
extern_class!(
#[derive(Debug)]
pub struct Utterance;
unsafe impl ClassType for Utterance {
type Super = NSObject;
const NAME: &'static str = "AVSpeechUtterance";
}
);
impl Utterance {
pub fn new(string: &NSString) -> Id<Self, Owned> {
unsafe { msg_send_id![Self::alloc(), initWithString: string] }
}
pub fn set_rate(&mut self, rate: f32) {
unsafe { msg_send![self, setRate: rate] }
}
pub fn set_volume(&mut self, volume: f32) {
unsafe { msg_send![self, setVolume: volume] }
}
}
}
#[cfg(target_os = "macos")]
use appkit::{Synthesizer, Utterance};
#[cfg(not(target_os = "macos"))]
use avfaudio::{Synthesizer, Utterance};
fn main() {
let mut synthesizer = Synthesizer::new();
let mut utterance = Utterance::new(ns_string!("Hello from Rust!"));
utterance.set_rate(0.5);
utterance.set_volume(0.5);
synthesizer.speak(&utterance);
thread::sleep(Duration::from_millis(1000));
while synthesizer.is_speaking() {
thread::sleep(Duration::from_millis(100));
}
}