1 /*
2  * Code created by Thomas Whittaker (RT) for a FreeSpace 2 source code project
3  *
4  * You may not sell or otherwise commercially exploit the source or things you
5  * created based on the source.
6  *
7 */
8 
9 #include "globalincs/pstypes.h"
10 #include "osapi/osregistry.h"
11 #include "sound/fsspeech.h"
12 #include "sound/speech.h"
13 
14 
15 extern int Cmdline_freespace_no_sound;
16 
17 const size_t MAX_SPEECH_BUFFER_LEN = 4096;
18 
19 static int speech_inited = 0;
20 
21 bool FSSpeech_play_from[FSSPEECH_FROM_MAX];
22 const char *FSSpeech_play_id[FSSPEECH_FROM_MAX] =
23 {
24 	"SpeechTechroom",
25 	"SpeechBriefings",
26 	"SpeechIngame",
27 	"SpeechMulti"
28 };
29 
30 char Speech_buffer[MAX_SPEECH_BUFFER_LEN] = "";
31 size_t  Speech_buffer_len;
32 
fsspeech_init()33 bool fsspeech_init()
34 {
35 	if (speech_inited) {
36 		return true;
37 	}
38 
39 	// if sound is disabled from the cmdline line then don't do speech either
40 	if (Cmdline_freespace_no_sound) {
41 		return false;
42 	}
43 
44 	if(speech_init() == false) {
45 		return false;
46 	}
47 
48 	// Get the settings from the registry
49 	for(int i = 0; i < FSSPEECH_FROM_MAX; i++) {
50 		FSSpeech_play_from[i] =
51 			os_config_read_uint(NULL, FSSpeech_play_id[i], 0) ? true : false;
52 		nprintf(("Speech", "Play %s: %s\n", FSSpeech_play_id[i], FSSpeech_play_from[i] ? "true" : "false"));
53 	}
54 
55 	int volume = os_config_read_uint(NULL, "SpeechVolume", 100);
56 	speech_set_volume((unsigned short) volume);
57 
58 	int voice = os_config_read_uint(NULL, "SpeechVoice", 0);
59 	speech_set_voice(voice);
60 
61 	speech_inited = 1;
62 
63 	return true;
64 }
65 
fsspeech_deinit()66 void fsspeech_deinit()
67 {
68 	if (!speech_inited)
69 		return;
70 
71 	speech_deinit();
72 
73 	speech_inited = 0;
74 }
75 
fsspeech_play(int type,const char * text)76 void fsspeech_play(int type, const char *text)
77 {
78 	if (!speech_inited) {
79 		nprintf(("Speech", "Aborting fsspech_play because speech_inited is false.\n"));
80 		return;
81 	}
82 
83 	if (type >= FSSPEECH_FROM_MAX) {
84 		nprintf(("Speech", "Aborting fsspeech_play because speech type is out of range.\n"));
85 		return;
86 	}
87 
88 	if (type >= 0 && FSSpeech_play_from[type] == false) {
89 		nprintf(("Speech", "Aborting fsspeech_play because we aren't supposed to play from type %s.\n", FSSpeech_play_id[type]));
90 		return;
91 	}
92 
93 	speech_play(text);
94 }
95 
fsspeech_stop()96 void fsspeech_stop()
97 {
98 	if (!speech_inited)
99 		return;
100 
101 	speech_stop();
102 }
103 
fsspeech_pause(bool playing)104 void fsspeech_pause(bool playing)
105 {
106 	if (!speech_inited)
107 		return;
108 
109 	if(playing) {
110 		speech_pause();
111 	} else {
112 		speech_resume();
113 	}
114 }
115 
fsspeech_start_buffer()116 void fsspeech_start_buffer()
117 {
118 	Speech_buffer_len = 0;
119 	Speech_buffer[0] = '\0';
120 }
121 
fsspeech_stuff_buffer(const char * text)122 void fsspeech_stuff_buffer(const char *text)
123 {
124 	if (!speech_inited)
125 		return;
126 
127 	size_t len = strlen(text);
128 
129 	if(Speech_buffer_len + len < MAX_SPEECH_BUFFER_LEN) {
130 		strcat_s(Speech_buffer, text);
131 	}
132 
133 	Speech_buffer_len += len;
134 }
135 
fsspeech_play_buffer(int type)136 void fsspeech_play_buffer(int type)
137 {
138 	if (!speech_inited)
139 		return;
140 
141 	fsspeech_play(type, Speech_buffer);
142 }
143 
144 // Goober5000
fsspeech_play_from(int type)145 bool fsspeech_play_from(int type)
146 {
147 	Assert(type >= 0 && type < FSSPEECH_FROM_MAX);
148 
149 	return (speech_inited && FSSpeech_play_from[type]);
150 }
151 
152 // Goober5000
fsspeech_playing()153 bool fsspeech_playing()
154 {
155 	if (!speech_inited)
156 		return false;
157 
158 	return speech_is_speaking();
159 }
160