WebSpeech: support pitch change
authorcfleizach@apple.com <cfleizach@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 1 Mar 2013 00:21:46 +0000 (00:21 +0000)
committercfleizach@apple.com <cfleizach@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 1 Mar 2013 00:21:46 +0000 (00:21 +0000)
https://bugs.webkit.org/show_bug.cgi?id=107348

Reviewed by Beth Dakin.

Implement a policy for the Mac synthesizer to convert from the utterance's pitch rate
to something sensible for the platform.

* platform/mac/PlatformSpeechSynthesizerMac.mm:
(-[WebSpeechSynthesisWrapper initWithSpeechSynthesizer:WebCore::]):
(-[WebSpeechSynthesisWrapper convertPitchToNSSpeechValue:]):
(-[WebSpeechSynthesisWrapper updateBasePitchForSynthesizer]):
(-[WebSpeechSynthesisWrapper speakUtterance:WebCore::]):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@144392 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/mac/PlatformSpeechSynthesizerMac.mm

index e57db13..8c83195 100644 (file)
@@ -1,3 +1,19 @@
+2013-02-28  Chris Fleizach  <cfleizach@apple.com>
+
+        WebSpeech: support pitch change
+        https://bugs.webkit.org/show_bug.cgi?id=107348
+
+        Reviewed by Beth Dakin.
+
+        Implement a policy for the Mac synthesizer to convert from the utterance's pitch rate
+        to something sensible for the platform.
+
+        * platform/mac/PlatformSpeechSynthesizerMac.mm:
+        (-[WebSpeechSynthesisWrapper initWithSpeechSynthesizer:WebCore::]):
+        (-[WebSpeechSynthesisWrapper convertPitchToNSSpeechValue:]):
+        (-[WebSpeechSynthesisWrapper updateBasePitchForSynthesizer]):
+        (-[WebSpeechSynthesisWrapper speakUtterance:WebCore::]):
+
 2013-02-28   Vineet Chaudhary  <rgf748@motorola.com>
 
         Unreviewed. Bindings test results update after r144376.
index 43cf771..29863ab 100644 (file)
@@ -40,6 +40,7 @@
     const WebCore::PlatformSpeechSynthesisUtterance* m_utterance;
     
     RetainPtr<NSSpeechSynthesizer> m_synthesizer;
+    float m_basePitch;
 }
 
 - (WebSpeechSynthesisWrapper *)initWithSpeechSynthesizer:(WebCore::PlatformSpeechSynthesizer *)synthesizer;
@@ -55,6 +56,7 @@
         return nil;
     
     m_synthesizerObject = synthesizer;
+    [self updateBasePitchForSynthesizer];
     return self;
 }
 
     return 200.0f * rate;
 }
 
+- (float)convertPitchToNSSpeechValue:(float)pitch
+{
+    // This allows the base pitch to range from 0% - 200% of the normal pitch.
+    return m_basePitch * pitch;
+}
+
+- (void)updateBasePitchForSynthesizer
+{
+    // Reset the base pitch whenever we change voices, since the base pitch is different for each voice.
+    [m_synthesizer setObject:nil forProperty:NSSpeechResetProperty error:nil];
+    m_basePitch = [[m_synthesizer objectForProperty:NSSpeechPitchBaseProperty error:nil] floatValue];
+}
+
 - (void)speakUtterance:(const WebCore::PlatformSpeechSynthesisUtterance *)utterance
 {
     // When speak is called we should not have an existing speech utterance outstanding.
 
     // Don't set the voice unless necessary. There's a bug in NSSpeechSynthesizer such that
     // setting the voice for the first time will cause the first speechDone callback to report it was unsuccessful.
-    if (![[m_synthesizer voice] isEqualToString:voiceURI])
+    BOOL updatePitch = NO;
+    if (![[m_synthesizer voice] isEqualToString:voiceURI]) {
         [m_synthesizer setVoice:voiceURI];
+        // Reset the base pitch whenever we change voices.
+        updatePitch = YES;
+    }
+    
+    if (m_basePitch == 0 || updatePitch)
+        [self updateBasePitchForSynthesizer];    
     
+    [m_synthesizer setObject:[NSNumber numberWithFloat:[self convertPitchToNSSpeechValue:utterance->pitch()]] forProperty:NSSpeechPitchBaseProperty error:nil];
     [m_synthesizer setRate:[self convertRateToWPM:utterance->rate()]];
     [m_synthesizer setVolume:utterance->volume()];