Created
October 12, 2011 15:55
-
-
Save unwiredben/1281621 to your computer and use it in GitHub Desktop.
Updated sdltts.c with background playback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************************************/ | |
/* */ | |
/* Language Technologies Institute */ | |
/* Carnegie Mellon University */ | |
/* Copyright (c) 2001 */ | |
/* All Rights Reserved. */ | |
/* */ | |
/* Fahrzin Hemmati */ | |
/* Copyright (c) 2011 */ | |
/* */ | |
/* Permission is hereby granted, free of charge, to use and distribute */ | |
/* this software and its documentation without restriction, including */ | |
/* without limitation the rights to use, copy, modify, merge, publish, */ | |
/* distribute, sublicense, and/or sell copies of this work, and to */ | |
/* permit persons to whom this work is furnished to do so, subject to */ | |
/* the following conditions: */ | |
/* 1. The code must retain the above copyright notice, this list of */ | |
/* conditions and the following disclaimer. */ | |
/* 2. Any modifications must be clearly marked as such. */ | |
/* 3. Original authors' names are not deleted. */ | |
/* 4. The authors' names are not used to endorse or promote products */ | |
/* derived from this software without specific prior written */ | |
/* permission. */ | |
/* 5. Modifications must be sent back to the author at their provided */ | |
/* email address. */ | |
/* */ | |
/* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */ | |
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ | |
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ | |
/* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */ | |
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ | |
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ | |
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ | |
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ | |
/* THIS SOFTWARE. */ | |
/* */ | |
/*************************************************************************/ | |
/* Author: Fahrzin Hemmati ([email protected]) */ | |
/* Date: October 2011 */ | |
/*************************************************************************/ | |
/* */ | |
/* Flite as a Hybrid PDK plugin */ | |
/* */ | |
/*************************************************************************/ | |
#include "flite.h" | |
#include "flite_version.h" | |
#include "SDL.h" | |
#include "SDL_mixer.h" | |
#include "PDL.h" | |
#include | |
extern cst_val *flite_set_voice_list(void); | |
void playText(const char * text) { | |
cst_wave * w; | |
w = flite_text_to_wave(text, flite_voice_select(NULL)); | |
if(Mix_OpenAudio(w->sample_rate, AUDIO_S16SYS, w->num_channels, 4096)) | |
exit(1); | |
Mix_Chunk * sound = Mix_QuickLoad_RAW((Uint8*)w->samples,w->num_samples*sizeof(short)); | |
if(sound == NULL) | |
exit(2); | |
int channel = Mix_PlayChannel(-1, sound, 0); | |
if(channel == -1) | |
exit(3); | |
while(Mix_Playing(channel) != 0); | |
Mix_FreeChunk(sound); | |
Mix_CloseAudio(); | |
} | |
PDL_bool playAudio(PDL_JSParameters * parms) { | |
const char * text = PDL_GetJSParamString(parms,0); | |
/* since we don't process this in the method thread, instead post a | |
* SDL event that will be received in the main thread and used to | |
* launch the code. */ | |
SDL_Event event; | |
event.user.type = SDL_USEREVENT; | |
event.user.code = 0; | |
event.user.data1 = strdup(text); | |
SDL_PushEvent(&event); | |
PDL_JSReply(parms, "{}"); | |
return 0; | |
} | |
void TTS_Init() { | |
flite_init(); | |
flite_voice_list = flite_set_voice_list(); | |
} | |
int main(int argc, char ** argv) { | |
if(argc==2) | |
{ | |
// don't do SDL loop | |
flite_init(); | |
flite_voice_list = flite_set_voice_list(); | |
playText(argv[1]); | |
exit(0); | |
return 0; | |
} | |
// Initializes the video subsystem | |
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0) { | |
exit(1); | |
} | |
PDL_Init(0); | |
TTS_Init(); | |
atexit(SDL_Quit); | |
atexit(PDL_Quit); | |
PDL_RegisterJSHandler("playAudio",playAudio); | |
PDL_JSRegistrationComplete(); | |
PDL_CallJS("ready", NULL, 0); | |
SDL_Event Event; | |
while (1) { | |
SDL_WaitEvent(&Event); | |
if(Event.type == SDL_USER && Event.code == 0) { | |
playText((const char *)event.user.data1); | |
PDL_CallJS("playbackComplete", NULL, 0); | |
free(event.user.data1); | |
} | |
if(Event.type == SDL_QUIT) exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment