The following code was easy to program. The difficult stuff was getting it working in the briefings, ingame, techroom etc.
I had originally intended to keep this exclusive to my exe (a non fs2_open project) but since I developed it with help from the community (mainly Karajorma) I feel I have to make it public source now.
I need a coder to contact me for the rest of the code to make it work in freespace. Do not attempt to put it in yourself, that was the tricky bit, much better to have a nice easy copy and paste session. Its your to use or not as you wish.
Anyone wanting to use this for a non freespace project I would prefer to be consulted but at the very least I want credit for my code.
I hope someone is willing to put this in, its not a difficult job and you dont need to know anything about sound or anything.
My e-mail address is in my profile, use it.
- RT
fsspeech.h
/*
* Code created by Thomas Whittaker (RT) for a Freespace 2 source code project
*
* You may not sell or otherwise commercially exploit the source or things you
* created based on the source.
*
*/
#ifndef _FSSPEECH_H_
#define _FSSPEECH_H_
enum
{
FSSPEECH_FROM_TECHROOM,
FSSPEECH_FROM_BRIEFING,
FSSPEECH_FROM_INGAME,
FSSPEECH_FROM_MAX
};
bool fsspeech_init();
void fsspeech_deinit();
void fsspeech_play(int type, char *text);
void fsspeech_stop();
void fsspeech_pause(bool playing);
void fsspeech_start_buffer();
void fsspeech_stuff_buffer(char *text);
void fsspeech_play_buffer(int type);
#endif
fsspeech.cpp
/*
* Code created by Thomas Whittaker (RT) for a Freespace 2 source code project
*
* You may not sell or otherwise commercially exploit the source or things you
* created based on the source.
*
*/
#include "speech.h"
#include "osregistry.h"
#include "fsspeech.h"
const int MAX_SPEECH_BUFFER_LEN = 4096;
bool FSSpeech_play_from[FSSPEECH_FROM_MAX];
char *FSSpeech_play_id[FSSPEECH_FROM_MAX] =
{
"SpeechTechroom",
"SpeechBriefings",
"SpeechIngame"
};
char Speech_buffer[MAX_SPEECH_BUFFER_LEN] = "";
int Speech_buffer_len;
bool fsspeech_init()
{
if(speech_init() == false) {
return false;
}
// Get the settings from the registry
for(int i = 0; i < FSSPEECH_FROM_MAX; i++) {
FSSpeech_play_from[i] =
os_config_read_uint(NULL, FSSpeech_play_id[i], 0) ? true : false;
}
return true;
}
void fsspeech_deinit()
{
speech_deinit();
}
void fsspeech_play(int type, char *text)
{
if(type >= FSSPEECH_FROM_MAX) return;
if(FSSpeech_play_from[type] == false) return;
speech_play(text);
}
void fsspeech_stop()
{
speech_stop();
}
void fsspeech_pause(bool playing)
{
if(playing) {
speech_pause();
} else {
speech_resume();
}
}
void fsspeech_start_buffer()
{
Speech_buffer_len = 0;
Speech_buffer[0] = '\0';
}
void fsspeech_stuff_buffer(char *text)
{
int len = strlen(text);
if(Speech_buffer_len + len < MAX_SPEECH_BUFFER_LEN) {
strcat(Speech_buffer, text);
}
Speech_buffer_len += len;
}
void fsspeech_play_buffer(int type)
{
fsspeech_play(type, Speech_buffer);
}
speech.h
/*
* Code created by Thomas Whittaker (RT) for a Freespace 2 source code project
*
* You may not sell or otherwise commercially exploit the source or things you
* created based on the source.
*
*/
#ifndef _SPEECH_H_
#define _SPEECH_H_
const int MAX_SPEECH_CHAR_LEN = 10000;
bool speech_init();
void speech_deinit();
bool speech_play(char *text);
bool speech_play(unsigned short *text);
bool speech_pause();
bool speech_resume();
bool speech_stop();
#endif
speech.cpp
/*
* Code created by Thomas Whittaker (RT) for a Freespace 2 source code project
*
* You may not sell or otherwise commercially exploit the source or things you
* created based on the source.
*
*/
#include "stdafx.h"
#include
#include
#include "speech.h"
ISpVoice *Voice_device;
bool Speech_init = false;
unsigned short Conversion_buffer[MAX_SPEECH_CHAR_LEN];
bool speech_init()
{
::CoInitialize(NULL);
Speech_init = SUCCEEDED(CoCreateInstance(
CLSID_SpVoice,
NULL,
CLSCTX_ALL,
IID_ISpVoice,
(void **)&Voice_device));
return Speech_init;
}
void speech_deinit()
{
if(Speech_init == false) return;
Voice_device->Release();
::CoUninitialize();
}
bool speech_play(char *text)
{
if(Speech_init == false) return true;
if(text == NULL) return false;
int len = strlen(text);
if(len > (MAX_SPEECH_CHAR_LEN - 1)) {
len = MAX_SPEECH_CHAR_LEN - 1;
}
for(int i = 0; i < len; i++) {
Conversion_buffer[i] = (unsigned short) text[i];
}
Conversion_buffer[i] = '\0';
return speech_play(Conversion_buffer);
}
bool speech_play(unsigned short *text)
{
if(Speech_init == false) return true;
if(text == NULL) return false;
speech_stop();
return SUCCEEDED(Voice_device->Speak(text, SPF_ASYNC, NULL));
}
bool speech_pause()
{
if(Speech_init == false) return true;
return SUCCEEDED(Voice_device->Pause());
}
bool speech_resume()
{
if(Speech_init == false) return true;
return SUCCEEDED(Voice_device->Resume());
}
bool speech_stop()
{
if(Speech_init == false) return true;
return SUCCEEDED(Voice_device->Speak( NULL, SPF_PURGEBEFORESPEAK, NULL ));
}