Skip to content

Commit

Permalink
[Cocoa] Add implementation for voiceschanged event of SpeechSynthesis…
Browse files Browse the repository at this point in the history
… API

https://bugs.webkit.org/show_bug.cgi?id=260228
rdar://113933356

Reviewed by Chris Fleizach.

Adopt new AVSpeechSynthesis API to receive notification about changes in available voices list, and fire voiceschanged
event based on that.

* Source/WTF/wtf/PlatformHave.h:
* Source/WebCore/PAL/pal/cocoa/AVFoundationSoftLink.h:
* Source/WebCore/PAL/pal/cocoa/AVFoundationSoftLink.mm:
* Source/WebCore/platform/PlatformSpeechSynthesizer.cpp:
(WebCore::PlatformSpeechSynthesizer::resetVoiceList):
(WebCore::PlatformSpeechSynthesizer::voicesDidChange):
* Source/WebCore/platform/PlatformSpeechSynthesizer.h:
* Source/WebCore/platform/cocoa/PlatformSpeechSynthesizerCocoa.mm:
(-[WebSpeechSynthesisWrapper initWithSpeechSynthesizer:]):
(-[WebSpeechSynthesisWrapper availableVoicesDidChange]):

Canonical link: https://commits.webkit.org/267210@main
  • Loading branch information
szewai committed Aug 24, 2023
1 parent 2bec7b6 commit 5dad017
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Source/WTF/wtf/PlatformHave.h
Original file line number Diff line number Diff line change
Expand Up @@ -1500,3 +1500,11 @@
|| PLATFORM(VISION)
#define HAVE_UI_USER_INTERFACE_IDIOM_VISION 1
#endif

#if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000) \
|| (PLATFORM(MACCATALYST) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000) \
|| (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000) \
|| (PLATFORM(APPLETV) && __TV_OS_VERSION_MAX_ALLOWED >= 170000) \
|| (PLATFORM(WATCHOS) && __WATCH_OS_VERSION_MIN_REQUIRED >= 100000)
#define HAVE_AVSPEECHSYNTHESIS_VOICES_CHANGE_NOTIFICATION 1
#endif
5 changes: 5 additions & 0 deletions Source/WebCore/PAL/pal/cocoa/AVFoundationSoftLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,9 @@ SOFT_LINK_CONSTANT_FOR_HEADER(PAL, AVFoundation, AVAudioSessionPortCarAudio, NSS
#define AVAudioSessionPortCarAudio PAL::get_AVFoundation_AVAudioSessionPortCarAudio()
#endif // HAVE(AVAUDIOSESSION)

#if HAVE(AVSPEECHSYNTHESIS_VOICES_CHANGE_NOTIFICATION)
SOFT_LINK_CONSTANT_FOR_HEADER(PAL, AVFoundation, AVSpeechSynthesisAvailableVoicesDidChangeNotification, NSString *)
#define AVSpeechSynthesisAvailableVoicesDidChangeNotification PAL::get_AVFoundation_AVSpeechSynthesisAvailableVoicesDidChangeNotification()
#endif // HAVE(AVSPEECHSYNTHESIS_VOICES_CHANGE_NOTIFICATION)

#endif // USE(AVFOUNDATION)
1 change: 1 addition & 0 deletions Source/WebCore/PAL/pal/cocoa/AVFoundationSoftLink.mm
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ static BOOL justReturnsNO()
SOFT_LINK_CONSTANT_FOR_SOURCE_WITH_EXPORT(PAL, AVFoundation, AVSampleBufferDisplayLayerFailedToDecodeNotificationErrorKey, NSString*, PAL_EXPORT)
SOFT_LINK_CONSTANT_FOR_SOURCE_WITH_EXPORT(PAL, AVFoundation, AVSampleBufferAudioRendererWasFlushedAutomaticallyNotification, NSString*, PAL_EXPORT)
SOFT_LINK_CONSTANT_FOR_SOURCE_WITH_EXPORT(PAL, AVFoundation, AVSampleBufferAudioRendererFlushTimeKey, NSString*, PAL_EXPORT)
SOFT_LINK_CONSTANT_FOR_SOURCE_WITH_EXPORT(PAL, AVFoundation, AVSpeechSynthesisAvailableVoicesDidChangeNotification, NSString *, PAL_EXPORT)
SOFT_LINK_CONSTANT_FOR_SOURCE_WITH_EXPORT(PAL, AVFoundation, AVStreamDataParserContentKeyRequestProtocolVersionsKey, NSString *, PAL_EXPORT)
SOFT_LINK_CONSTANT_FOR_SOURCE_WITH_EXPORT(PAL, AVFoundation, AVStreamingKeyDeliveryContentKeyType, NSString *, PAL_EXPORT)
SOFT_LINK_CONSTANT_MAY_FAIL_FOR_SOURCE_WITH_EXPORT(PAL, AVFoundation, AVURLAssetAllowableTypeCategoriesKey, NSString *, PAL_EXPORT)
Expand Down
15 changes: 15 additions & 0 deletions Source/WebCore/platform/PlatformSpeechSynthesizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ const Vector<RefPtr<PlatformSpeechSynthesisVoice>>& PlatformSpeechSynthesizer::v
return m_voiceList;
}

void PlatformSpeechSynthesizer::resetVoiceList()
{
if (!m_voiceListIsInitialized)
return;

m_voiceListIsInitialized = false;
m_voiceList.clear();
}

void PlatformSpeechSynthesizer::voicesDidChange()
{
resetVoiceList();
m_speechSynthesizerClient.voicesDidChange();
}

} // namespace WebCore

#endif // ENABLE(SPEECH_SYNTHESIS)
2 changes: 2 additions & 0 deletions Source/WebCore/platform/PlatformSpeechSynthesizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class WEBCORE_EXPORT PlatformSpeechSynthesizer : public RefCounted<PlatformSpeec
virtual void resume();
virtual void cancel();
virtual void resetState();
virtual void voicesDidChange();

PlatformSpeechSynthesizerClient& client() const { return m_speechSynthesizerClient; }

Expand All @@ -86,6 +87,7 @@ class WEBCORE_EXPORT PlatformSpeechSynthesizer : public RefCounted<PlatformSpeec

private:
virtual void initializeVoiceList();
virtual void resetVoiceList();

bool m_voiceListIsInitialized { false };
PlatformSpeechSynthesizerClient& m_speechSynthesizerClient;
Expand Down
14 changes: 14 additions & 0 deletions Source/WebCore/platform/cocoa/PlatformSpeechSynthesizerCocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,23 @@ - (WebSpeechSynthesisWrapper *)initWithSpeechSynthesizer:(WebCore::PlatformSpeec
return nil;

m_synthesizerObject = synthesizer;

#if HAVE(AVSPEECHSYNTHESIS_VOICES_CHANGE_NOTIFICATION)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(availableVoicesDidChange) name:AVSpeechSynthesisAvailableVoicesDidChangeNotification object:nil];
#endif

return self;
}

#if HAVE(AVSPEECHSYNTHESIS_VOICES_CHANGE_NOTIFICATION)

- (void)availableVoicesDidChange
{
m_synthesizerObject->voicesDidChange();
}

#endif

- (float)mapSpeechRateToPlatformRate:(float)rate
{
// WebSpeech says to go from .1 -> 10 (default 1)
Expand Down

0 comments on commit 5dad017

Please sign in to comment.