1 /*****************************************************************************
2 * nsspeechsynthesizer.m: Simple text to Speech renderer for Mac OS X
3 *****************************************************************************
4 * Copyright (C) 2015 VLC authors and VideoLAN
7 * Authors: Felix Paul Kühne <fkuehne # videolan # org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include <vlc_subpicture.h>
37 #import <Cocoa/Cocoa.h>
39 static int Create (vlc_object_t *);
40 static void Destroy(vlc_object_t *);
41 static int RenderText(filter_t *,
42 subpicture_region_t *,
43 subpicture_region_t *,
44 const vlc_fourcc_t *);
47 set_description(N_("Speech synthesis for Mac OS X"))
48 set_category(CAT_VIDEO)
49 set_subcategory(SUBCAT_VIDEO_SUBPIC)
51 set_capability("text renderer", 0)
52 set_callbacks(Create, Destroy)
55 typedef struct filter_sys_t
57 NSSpeechSynthesizer *speechSynthesizer;
58 NSString *currentLocale;
62 static int Create (vlc_object_t *p_this)
64 filter_t *p_filter = (filter_t *)p_this;
67 p_filter->p_sys = p_sys = malloc(sizeof(filter_sys_t));
71 p_sys->currentLocale = p_sys->lastString = @"";
72 p_sys->speechSynthesizer = [[NSSpeechSynthesizer alloc] init];
74 p_filter->pf_render = RenderText;
79 static void Destroy(vlc_object_t *p_this)
81 filter_t *p_filter = (filter_t *)p_this;
82 filter_sys_t *p_sys = p_filter->p_sys;
84 [p_sys->speechSynthesizer stopSpeaking];
85 [p_sys->speechSynthesizer release];
86 p_sys->speechSynthesizer = nil;
88 [p_sys->lastString release];
89 p_sys->lastString = nil;
91 [p_sys->currentLocale release];
92 p_sys->currentLocale = nil;
97 static NSString * languageCodeForString(NSString *string) {
98 return (NSString *)CFStringTokenizerCopyBestStringLanguage((CFStringRef)string, CFRangeMake(0, [string length]));
101 static int RenderText(filter_t *p_filter,
102 subpicture_region_t *p_region_out,
103 subpicture_region_t *p_region_in,
104 const vlc_fourcc_t *p_chroma_list)
107 filter_sys_t *p_sys = p_filter->p_sys;
108 text_segment_t *p_segment = p_region_in->p_text;
113 for ( const text_segment_t *s = p_segment; s != NULL; s = s->p_next ) {
117 if (strlen(s->psz_text) == 0)
120 NSString *stringToSpeech = [NSString stringWithUTF8String:s->psz_text];
122 if ([p_sys->lastString isEqualToString:stringToSpeech])
125 if ([stringToSpeech isEqualToString:@"\n"])
128 p_sys->lastString = [stringToSpeech retain];
130 msg_Dbg(p_filter, "Speaking '%s'", [stringToSpeech UTF8String]);
132 NSString *detectedLocale = languageCodeForString(stringToSpeech);
133 if (detectedLocale != nil) {
134 if (![detectedLocale isEqualToString:p_sys->currentLocale]) {
135 p_sys->currentLocale = [detectedLocale retain];
136 msg_Dbg(p_filter, "switching speaker locale to '%s'", [p_sys->currentLocale UTF8String]);
137 NSArray *voices = [NSSpeechSynthesizer availableVoices];
138 NSUInteger count = voices.count;
139 NSRange range = NSMakeRange(0, 2);
141 for (NSUInteger i = 0; i < count; i++) {
142 NSDictionary *voiceAttributes = [NSSpeechSynthesizer attributesForVoice: [voices objectAtIndex:i]];
143 NSString *voiceLanguage = [voiceAttributes objectForKey:@"VoiceLanguage"];
144 if ([p_sys->currentLocale isEqualToString:[voiceLanguage substringWithRange:range]]) {
145 NSString *voiceName = [voiceAttributes objectForKey:@"VoiceName"];
146 msg_Dbg(p_filter, "switched to voice '%s'", [voiceName UTF8String]);
147 if ([voiceName isEqualToString:@"Agnes"] || [voiceName isEqualToString:@"Albert"])
149 [p_sys->speechSynthesizer setVoice: [voices objectAtIndex:i]];
156 [p_sys->speechSynthesizer startSpeakingString:stringToSpeech];