Guest User

bummer

a guest
Feb 14th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. /////////////
  2. // .h
  3. /////////////
  4.  
  5. //
  6. // AppDelegate.h
  7. // SpeakLine
  8. //
  9. // Created by Michael Crook on 12/01/13.
  10. // Copyright (c) 2013 Michael Crook. All rights reserved.
  11. //
  12.  
  13. #import <Cocoa/Cocoa.h>
  14.  
  15. @interface AppDelegate : NSObject <NSApplicationDelegate, NSSpeechSynthesizerDelegate, NSTableViewDelegate>
  16. {
  17. NSArray *_voices;
  18. NSSpeechSynthesizer *_speechSynth;
  19. }
  20.  
  21. @property (assign) IBOutlet NSWindow *window;
  22. @property (weak) IBOutlet NSTextField *textField;
  23. @property (weak) IBOutlet NSButton *stopBtn;
  24. @property (weak) IBOutlet NSButton *speekBtn;
  25. @property (weak) IBOutlet NSTableView *tableView;
  26.  
  27. - (IBAction)stopIt:(id)sender;
  28.  
  29. - (IBAction)sayIt:(id)sender;
  30.  
  31. @end
  32.  
  33.  
  34. /////////////
  35. // .m
  36. /////////////
  37.  
  38. //
  39. // AppDelegate.m
  40. // SpeakLine
  41. //
  42. // Created by Michael Crook on 12/01/13.
  43. // Copyright (c) 2013 Michael Crook. All rights reserved.
  44. //
  45.  
  46. #import "AppDelegate.h"
  47.  
  48. @implementation AppDelegate
  49.  
  50. @synthesize window = _window;
  51. @synthesize textField = _textField;
  52. @synthesize tableView = _tableView;
  53.  
  54. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  55. {
  56. // Insert code here to initialize your application
  57. }
  58.  
  59. - (id)init
  60. {
  61. self = [super init];
  62. if(self)
  63. {
  64. NSLog(@"AppDelegate initiating");
  65.  
  66. // initiate speach synth.
  67. _speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:Nil];
  68. }
  69.  
  70. [_speechSynth setDelegate:self];
  71. [_tableView setDelegate:self];
  72. [_stopBtn setEnabled:FALSE];
  73. [_speekBtn setEnabled:TRUE];
  74.  
  75. _voices = [NSSpeechSynthesizer availableVoices];
  76.  
  77. return (self);
  78. }
  79.  
  80. - (NSInteger)numberOfRowsInTableView:(NSTableView *)tv
  81. {
  82. return (NSInteger)[_voices count];
  83. }
  84.  
  85. - (id)tableView:(NSTableView *)tv
  86. objectValueForTableColumn:(NSTableColumn *)tableColumn
  87. row:(NSInteger)row
  88. {
  89. NSString *v = [_voices objectAtIndex:row];
  90. NSDictionary *dict = [NSSpeechSynthesizer attributesForVoice:v];
  91. return [dict objectForKey:NSVoiceName];
  92. }
  93.  
  94. - (void)tableViewSelectionDidChange:(NSNotification *)notification
  95. {
  96. NSInteger row = [_tableView selectedRow];
  97. if (row == –1)
  98. {
  99. return;
  100. }
  101. NSString *selectedVoice = [_voices objectAtIndex:row];
  102. [_speechSynth setVoice:selectedVoice];
  103. NSLog(@"new voice = %@", selectedVoice);
  104. }
  105.  
  106. - (void)speechSynthesizer:(NSSpeechSynthesizer *)sender
  107. didFinishSpeaking:(BOOL)finishedSpeaking
  108. {
  109. NSLog(@"finishedSpeaking = %d", finishedSpeaking);
  110. [_stopBtn setEnabled:FALSE];
  111. [_speekBtn setEnabled:TRUE];
  112. }
  113.  
  114. -(void)speechSynthesizer:(NSSpeechSynthesizer *)sender
  115. willSpeakWord:(NSRange)characterRange ofString:(NSString *)string
  116. {
  117. [_stopBtn setEnabled:TRUE];
  118. }
  119.  
  120. - (IBAction)stopIt:(id)sender
  121. {
  122. NSLog(@"Stopping Speaking");
  123.  
  124. // stop talking
  125. [_speechSynth stopSpeaking];
  126. }
  127.  
  128. - (IBAction)sayIt:(id)sender
  129. {
  130. NSString *string = [_textField stringValue];
  131.  
  132. // is the string 0 long
  133. if([string length] == 0)
  134. {
  135. NSLog(@"string from %@ is of zero length", _textField);
  136. }
  137. else
  138. {
  139. [_speechSynth startSpeakingString:string];
  140. NSLog(@"Started speaking: %@", string);
  141. }
  142. }
  143.  
  144. @end
Advertisement
Add Comment
Please, Sign In to add comment