added language autodiscovery

This commit is contained in:
Jeena 2014-11-13 23:00:28 +01:00
parent 08b0ec0840
commit dabbb4304d
8 changed files with 868 additions and 1163 deletions

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,8 @@
/* Begin PBXBuildFile section */
1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1DDD58140DA1D0A300B32029 /* MainMenu.xib */; };
1F6C8C521A06A13D004D992E /* pause.png in Resources */ = {isa = PBXBuildFile; fileRef = 1F6C8C501A06A13D004D992E /* pause.png */; };
1F6C8C531A06A13D004D992E /* play.png in Resources */ = {isa = PBXBuildFile; fileRef = 1F6C8C511A06A13D004D992E /* play.png */; };
1FA38C49124D27C8004B8852 /* play-icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 1FA38C48124D27C8004B8852 /* play-icon.png */; };
1FA38CD3124D4349004B8852 /* Icon.icns in Resources */ = {isa = PBXBuildFile; fileRef = 1FA38CD2124D4349004B8852 /* Icon.icns */; };
1FF67B3E12948FDE00CA07A3 /* SpeakerApplication.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FF67B3D12948FDE00CA07A3 /* SpeakerApplication.m */; };
@ -22,6 +24,8 @@
1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
13E42FB307B3F0F600E4EEF1 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; };
1DDD58150DA1D0A300B32029 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; };
1F6C8C501A06A13D004D992E /* pause.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = pause.png; sourceTree = "<group>"; };
1F6C8C511A06A13D004D992E /* play.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = play.png; sourceTree = "<group>"; };
1FA38C48124D27C8004B8852 /* play-icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "play-icon.png"; sourceTree = "<group>"; };
1FA38CD2124D4349004B8852 /* Icon.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = Icon.icns; sourceTree = "<group>"; };
1FF67B3C12948FDE00CA07A3 /* SpeakerApplication.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SpeakerApplication.h; sourceTree = "<group>"; };
@ -109,6 +113,8 @@
29B97317FDCFA39411CA2CEA /* Resources */ = {
isa = PBXGroup;
children = (
1F6C8C501A06A13D004D992E /* pause.png */,
1F6C8C511A06A13D004D992E /* play.png */,
1FA38CD2124D4349004B8852 /* Icon.icns */,
1FA38C48124D27C8004B8852 /* play-icon.png */,
8D1107310486CEB800E47090 /* Speaker-Info.plist */,
@ -153,6 +159,8 @@
/* Begin PBXProject section */
29B97313FDCFA39411CA2CEA /* Project object */ = {
isa = PBXProject;
attributes = {
};
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "Speaker" */;
compatibilityVersion = "Xcode 3.1";
developmentRegion = English;
@ -179,8 +187,10 @@
files = (
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */,
1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */,
1F6C8C521A06A13D004D992E /* pause.png in Resources */,
1FA38C49124D27C8004B8852 /* play-icon.png in Resources */,
1FA38CD3124D4349004B8852 /* Icon.icns in Resources */,
1F6C8C531A06A13D004D992E /* play.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View file

@ -10,29 +10,29 @@
<string>Speaker</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>B3FBBA97-D123-44B8-9654-E8F7B4DE23C0</key>
<key>84929CFFB1C126DC09C07F4A0C98501C72BCAF7B</key>
<string>ssh://github.com/jeena/Speaker.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>Speaker.xcodeproj/project.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>B3FBBA97-D123-44B8-9654-E8F7B4DE23C0</key>
<key>84929CFFB1C126DC09C07F4A0C98501C72BCAF7B</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>ssh://github.com/jeena/Speaker.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>B3FBBA97-D123-44B8-9654-E8F7B4DE23C0</string>
<string>84929CFFB1C126DC09C07F4A0C98501C72BCAF7B</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>B3FBBA97-D123-44B8-9654-E8F7B4DE23C0</string>
<string>84929CFFB1C126DC09C07F4A0C98501C72BCAF7B</string>
<key>IDESourceControlWCCName</key>
<string>Speaker</string>
</dict>

View file

@ -16,12 +16,14 @@
NSSpeechSynthesizer *synth;
NSRange oldRange;
BOOL isNewLocation;
NSArray *languages;
}
@property (weak) IBOutlet NSWindow *window;
@property (strong, nonatomic) IBOutlet NSTextView *textView;
@property (weak) IBOutlet NSMenu *languageMenu;
@property (weak) IBOutlet NSPopUpButton *languageMenuPopupButton;
@property (weak) IBOutlet NSToolbarItem *speakButton;
-(IBAction)speakAction:(id)sender;
-(IBAction)seekForward:(id)sender;
@ -33,4 +35,7 @@
- (void)changeLanguage:(id)sender;
- (void)changeVoiceGender:(id)sender;
- (NSString *)findLanguageFromString:(NSString *)text;
- (void)setVoiceForLanguage:(NSString *)language;
@end

View file

@ -15,6 +15,7 @@
@implementation SpeakerAppDelegate
@synthesize languageMenu;
@synthesize languageMenuPopupButton;
@synthesize speakButton;
@synthesize window, textView;
@ -24,6 +25,7 @@
synth.delegate = self;
oldRange = NSMakeRange(-1, -1);
isNewLocation = YES;
languages = nil;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *string = [defaults objectForKey:@"text"];
@ -41,51 +43,64 @@
- (void)initLanugageMenu
{
NSString *startName = [[NSUserDefaults standardUserDefaults] stringForKey:@"languageVoiceName"];
NSMenuItem *startItem = nil;
NSLocale *currentLocale = [NSLocale currentLocale];
NSArray *voices = [NSSpeechSynthesizer availableVoices];
[self.languageMenu removeAllItems];
NSMutableArray *langs = [NSMutableArray arrayWithCapacity:[voices count]];
NSMutableArray *defaultVoices = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"defaultVoices"];
for (NSInteger i = 0; i < [voices count]; i++)
{
NSDictionary *dict = [NSSpeechSynthesizer attributesForVoice:[voices objectAtIndex:i]];
NSString *countryString = [currentLocale displayNameForKey:NSLocaleIdentifier value:[dict objectForKey:@"VoiceLocaleIdentifier"]];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[dict objectForKey:@"VoiceName"] action:@selector(changeLanguage:) keyEquivalent:@""];
NSMenuItem *country = [self.languageMenu itemWithTitle:countryString];
if (!country) {
country = [[NSMenuItem alloc] initWithTitle:countryString action:nil keyEquivalent:@""];
NSMenu *submenu = [[NSMenu alloc] init];
[country setSubmenu:submenu];
[self.languageMenu addItem:country];
[langs addObject:[[[dict objectForKey:@"VoiceLanguage"] componentsSeparatedByString:@"-"] objectAtIndex:0]];
}
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[dict objectForKey:@"VoiceName"] action:@selector(changeLanguage:) keyEquivalent:@""];
item.tag = i;
[[country submenu] addItem:item];
if ([defaultVoices indexOfObjectIdenticalTo:item.title] != NSNotFound) {
item.state = NSOnState;
}
if ((![[NSUserDefaults standardUserDefaults] objectForKey:@"languageVoiceIndex"] && [[dict objectForKey:@"VoiceName"] isEqualToString:@"Alex"]) || [item.title isEqualToString:startName])
{
startItem = item;
//startItem = item;
}
}
[self changeLanguage:startItem];
languages = langs;
}
- (void)changeLanguage:(id)sender
{
NSMenuItem *item = (NSMenuItem *)sender;
// Only if the user doesn't chose the same as it already was.
if (item.menu != self.languageMenu) {
NSString *voice = [[NSSpeechSynthesizer attributesForVoice:[[NSSpeechSynthesizer availableVoices] objectAtIndex:item.tag]] objectForKey:@"VoiceIdentifier"];
[synth setVoice:voice];
[[NSUserDefaults standardUserDefaults] setObject:item.title forKey:@"languageVoiceName"];
[self.languageMenuPopupButton setTitle:item.title];
NSMenuItem *parent = item.parentItem;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *defaultVoices = [defaults mutableArrayValueForKey:@"defaultVoices"];
for (NSMenuItem *child in parent.submenu.itemArray) {
if (child.state == NSOnState) {
[defaultVoices removeObjectIdenticalTo:child.title];
child.state = NSOffState;
}
}
item.state = NSOnState;
[defaultVoices addObject:item.title];
[defaults synchronize];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
@ -98,8 +113,10 @@
-(IBAction)speakAction:(id)sender {
if ([synth isSpeaking]) {
[self stopSpeaking];
speakButton.image = [NSImage imageNamed:@"play.png"];
} else {
[self startSpeaking];
speakButton.image = [NSImage imageNamed:@"pause.png"];
}
}
@ -130,6 +147,9 @@
}
[synth stopSpeaking];
[self setVoiceForLanguage:[self findLanguageFromString:text]];
[synth startSpeakingString:text];
oldRange = range;
@ -170,5 +190,39 @@
[textView setEditable:YES];
}
- (NSString *)findLanguageFromString:(NSString *)text
{
NSArray *tagschemes = [NSArray arrayWithObjects:NSLinguisticTagSchemeLanguage, nil];
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
[tagger setString:text];
NSString *language = [tagger tagAtIndex:0 scheme:NSLinguisticTagSchemeLanguage tokenRange:NULL sentenceRange:NULL];
return language;
}
- (void)setVoiceForLanguage:(NSString *)language
{
NSMenuItem *item;
NSMenuItem *languageItem;
for (NSInteger i = 0; i < [languages count]; i++)
{
if ([[languages objectAtIndex:i] isEqualToString:language]) {
languageItem = [[self.languageMenu itemArray] objectAtIndex:i];
for (item in [languageItem.submenu itemArray]) {
if (item.state == NSOnState) {
break;
}
}
item = [[languageItem.submenu itemArray] objectAtIndex:0];
}
}
if (languageItem && item) {
NSString *voice = [[NSSpeechSynthesizer attributesForVoice:[[NSSpeechSynthesizer availableVoices] objectAtIndex:item.tag]] objectForKey:@"VoiceIdentifier"];
[synth setVoice:voice];
[self.languageMenuPopupButton selectItem:languageItem];
}
}
@end

BIN
pause.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 250 B

BIN
play.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 377 B