File: speech.mm

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (171 lines) | stat: -rw-r--r-- 2,682 bytes parent folder | download | duplicates (2)
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
#ifdef FS2_SPEECH

#import <AppKit/AppKit.h>
#import <AppKit/NSSpeechSynthesizer.h>

#include "globalincs/pstypes.h"
#include "utils/unicode.h"


static NSSpeechSynthesizer *synth = nil;
static bool Speech_init = false;


bool speech_init()
{
	if (Speech_init) {
		return true;
	}

	synth = [[NSSpeechSynthesizer alloc] init];

	Speech_init = true;

	return true;
}

void speech_deinit()
{
	if ( !Speech_init ) {
		return;
	}

	[synth release];
	synth = nil;

	Speech_init = false;
}

bool speech_play(const char *text)
{
	if ( !Speech_init ) {
		return false;
	}

	if ( !text || !strlen(text) ) {
		nprintf(("Speech", "Not playing speech because passed text is null.\n"));
		return false;
	}

	SCP_string work_buffer;

	bool saw_dollar = false;
	for (auto ch : unicode::codepoint_range(text)) {
		if (ch == UNICODE_CHAR('$')) {
			// Skip $ escape sequences which appear in briefing text
			saw_dollar = true;
			continue;
		} else if (saw_dollar) {
			saw_dollar = false;
			continue;
		}

		unicode::encode(ch, std::back_inserter(work_buffer));
	}

	if (work_buffer.empty()) {
		return false;
	}

	[synth startSpeakingString:
		[NSString stringWithUTF8String:
			work_buffer.c_str()
		]
	];

	return true;
}

bool speech_pause()
{
	if ( !Speech_init ) {
		return false;
	}

	[synth pauseSpeakingAtBoundary:NSSpeechImmediateBoundary];

	return true;
}

bool speech_resume()
{
	if ( !Speech_init ) {
		return false;
	}

	[synth continueSpeaking];

	return true;
}

bool speech_stop()
{
	if ( !Speech_init ) {
		return false;
	}

	[synth stopSpeaking];

	return true;
}

bool speech_set_volume(unsigned short volume)
{
	if ( !Speech_init ) {
		return false;
	}

	float vol = volume * 0.01f;

	[synth
		setObject: [NSNumber numberWithFloat:vol]
		forProperty:NSSpeechVolumeProperty error:nil
	];

	return true;
}

bool speech_set_voice(int voice)
{
	if ( !Speech_init ) {
		return false;
	}

	NSArray *voices = [NSSpeechSynthesizer availableVoices];

	if ( (voice < 0) || (voice >= [voices count]) ) {
		nprintf(("Speech", "Attempting to set voice to invalid value (%d)\n", voice));
		return false;
	}

	[synth setVoice: [voices objectAtIndex:voice]];

	return true;
}

bool speech_is_speaking()
{
	if ( !Speech_init ) {
		return false;
	}

	return [synth isSpeaking];
}

SCP_vector<SCP_string> speech_enumerate_voices()
{
	NSArray *voices = [NSSpeechSynthesizer availableVoices];

	SCP_vector<SCP_string> fsoVoices;

	for (NSString *voiceIdentifier in voices) {
		NSDictionary *attributes = [NSSpeechSynthesizer attributesForVoice:voiceIdentifier];
		NSString *name = [attributes objectForKey:NSVoiceName];

		fsoVoices.push_back([name UTF8String]);
	}

	return fsoVoices;
}

#endif