contrib: soxr: fix build on WIN32
[vlc.git] / modules / text_renderer / nsspeechsynthesizer.m
blob32d2512321093d09b7ffc1a67af78ffea80626df
1 /*****************************************************************************
2  * nsspeechsynthesizer.m: Simple text to Speech renderer for Mac OS X
3  *****************************************************************************
4  * Copyright (C) 2015 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne # videolan # org>
8  *
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.
13  *
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.
18  *
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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
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 *);
46 vlc_module_begin ()
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)
53 vlc_module_end ()
55 struct filter_sys_t
57     NSSpeechSynthesizer *speechSynthesizer;
58     NSString *currentLocale;
59     NSString *lastString;
62 static int  Create (vlc_object_t *p_this)
64     filter_t *p_filter = (filter_t *)p_this;
65     filter_sys_t *p_sys;
67     p_filter->p_sys = p_sys = malloc(sizeof(filter_sys_t));
68     if (!p_sys)
69         return VLC_ENOMEM;
71     p_sys->currentLocale = p_sys->lastString = @"";
72     p_sys->speechSynthesizer = [[NSSpeechSynthesizer alloc] init];
74     p_filter->pf_render = RenderText;
76     return VLC_SUCCESS;
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;
94     free(p_sys);
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)
106     @autoreleasepool {
107         filter_sys_t *p_sys = p_filter->p_sys;
108         text_segment_t *p_segment = p_region_in->p_text;
110         if (!p_segment)
111             return VLC_EGENERIC;
113         for ( const text_segment_t *s = p_segment; s != NULL; s = s->p_next ) {
114             if ( !s->psz_text )
115                 continue;
117             if (strlen(s->psz_text) == 0)
118                 continue;
120             NSString *stringToSpeech = [NSString stringWithUTF8String:s->psz_text];
122             if ([p_sys->lastString isEqualToString:stringToSpeech])
123                 continue;
125             if ([stringToSpeech isEqualToString:@"\n"])
126                 continue;
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"])
148                                 continue;
149                             [p_sys->speechSynthesizer setVoice: [voices objectAtIndex:i]];
150                             break;
151                         }
152                     }
153                 }
154             }
156             [p_sys->speechSynthesizer startSpeakingString:stringToSpeech];
157         }
159         return VLC_SUCCESS;
160     }