forked from jerrykrinock/ClassesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSQueuedSpeechSynthesizer.m
131 lines (107 loc) · 2.59 KB
/
SSQueuedSpeechSynthesizer.m
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
#import "SSQueuedSpeechSynthesizer.h"
// This is a "true singleton" per Cocoa document "Creating A Singleton Instance", except
// that the single is not created if needed by +sharedSpeaker but must be explicitly created
// by sending +createSharedSpeaker. If +sharedSpeaker is invoked before +createSharedSppeaker
// is run once, it returns nil.
static SSQueuedSpeechSynthesizer *sharedSpeaker = nil ;
@implementation SSQueuedSpeechSynthesizer
- (void)setSynth:(NSSpeechSynthesizer*)synth {
[synth retain] ;
[_synth release] ;
_synth = synth ;
}
- (NSSpeechSynthesizer*)synth {
return _synth;
}
- (void)setQueue:(NSMutableArray*)queue {
[queue retain] ;
[_queue release] ;
_queue = queue ;
}
- (NSMutableArray*)queue {
return _queue ;
}
- (id)init {
if ((self = [super init])) {
NSSpeechSynthesizer* synth = [[NSSpeechSynthesizer alloc] init] ;
[self setSynth:synth] ;
[synth release] ;
[[self synth] setDelegate:self];
NSMutableArray* queue = [[NSMutableArray alloc] init] ;
[self setQueue:queue] ;
[queue release] ;
}
return self ;
}
+ (SSQueuedSpeechSynthesizer*)createSharedSpeaker
{
if (sharedSpeaker == nil) {
sharedSpeaker = [[self alloc] init];
}
return sharedSpeaker;
}
+ (SSQueuedSpeechSynthesizer*)sharedSpeaker
{
return sharedSpeaker;
}
+ (id)allocWithZone:(NSZone *)zone
{
if (sharedSpeaker == nil) {
sharedSpeaker = [super allocWithZone:zone];
}
return sharedSpeaker;
}
- (id)retain
{
return self;
}
- (NSUInteger)retainCount
{
return NSUIntegerMax ; //denotes an object that cannot be released
}
- (void)release
{
//do nothing
}
- (id)autorelease
{
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (void)speak:(NSString*)s1
then:(NSString*)s2
then:(NSString*)s3 {
NSMutableString* whole = [[NSMutableString alloc] init] ;
if (s1) {
[whole appendString:s1] ;
}
if (s2) {
[whole appendString:@" "] ;
[whole appendString:s2] ;
}
if (s3) {
[whole appendString:@" "] ;
[whole appendString:s3] ;
}
if ([[self synth] isSpeaking] || ([[self queue] count] > 0)) {
[[self queue] addObject:whole] ;
}
else {
[[self synth] startSpeakingString:whole] ;
}
[whole release] ;
}
- (void)startSpeakingAndDeleteFromQueue {
[[self synth] startSpeakingString:[[self queue] objectAtIndex:0]] ;
[[self queue] removeObjectAtIndex:0] ;
}
- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking {
NSMutableArray* queue = [self queue] ;
if ([queue count] > 0) {
[self performSelector:@selector(startSpeakingAndDeleteFromQueue) withObject:nil afterDelay:1.0] ;
}
}
@end