TextToSpeech
@available(iOS 13.0, *)
@objc
public class TextToSpeech : NSObject
This is the client entry point for the Spokestack Text to Speech (TTS) system. It provides the capability to synthesize textual input, and speak back the synthesis as audio system output. The synthesis and speech occur on asynchronous blocks so as to not block the client while it performs network and audio system activities.
When inititalized, the TTS system communicates with the client either via a delegate that receive events, or via a publisher-subscriber pattern.
// assume that self implements the TextToSpeechDelegate protocol.
let configuration = SpeechConfiguration()
let tts = TextToSpeech(self, configuration: configuration)
let input = TextToSpeechInput()
input.text = "Hello world!"
tts.synthesize(input) // synthesize the provided default text input using the default synthetic voice and api key.
tts.speak(input) // synthesize the same input as above, and play back the result using the default audio system.
Using the TTS system requires setting an API client identifier (SpeechConfiguration.apiId
) and API client secret (SpeechConfiguration.apiSecret
) , which are used to cryptographically sign and meter TTS API usage.
-
Delegate that receives TTS events.
Declaration
Swift
public var delegates: [SpokestackDelegate]
-
Initializes a new text to speech instance without a delegate.
Warning
An instance initialized this way is expected to use the pub/subCombine
interface, not the delegate interface, when callingsynthesize
.Declaration
Swift
@objc public init(configuration: SpeechConfiguration) throws
Parameters
configuration
Speech configuration parameters.
-
Initializes a new text to speech instance.
Declaration
Swift
@objc public init(_ delegates: [SpokestackDelegate], configuration: SpeechConfiguration)
Parameters
delegate
Delegate that receives text to speech events.
configuration
Speech configuration parameters.
-
Synthesize speech using the provided input parameters and speech configuration, and play back the result using the default audio system.
Playback is provided as a convenience for the client. The client is responsible for coordinating the audio system resources and utilization required by
SpeechPipeline
and/or other media playback. TheTextToSpeechDelegate.didBeginSpeaking
andTextToSpeechDelegate.didFinishSpeaking
callbacks may be utilized for this purpose.The
TextToSpeech
class handles all memory management for the playback components it utilizes.Note
Playback will begin immediately after the synthesis results are received and sufficiently buffered.Warning
AVAudioSession.Category
andAVAudioSession.CategoryOptions
must be set by the client to compatible settings that allow for playback through the desired audio sytem ouputs.Declaration
Swift
@objc public func speak(_ input: TextToSpeechInput)
Parameters
input
Parameters that specify the speech to synthesize.
-
Stops playback of the current synthesis result.
Declaration
Swift
@objc public func stopSpeaking()
-
Synthesize speech using the provided input parameters and speech configuration. A successful synthesis will return a URL to the streaming audio container of synthesized speech to the
TextToSpeech
‘sdelegate
.Note
The URL will be invalidated within 60 seconds of generation.Declaration
Swift
@objc public func synthesize(_ input: TextToSpeechInput)
Parameters
input
Parameters that specify the speech to synthesize.
-
Synthesize speech using the provided list of inputs. A successful set of synthesises returns a list of synthesis results.
Declaration
Swift
public func synthesize(_ inputs: Array<TextToSpeechInput>) -> AnyPublisher<[TextToSpeechResult], Error>
Parameters
inputs
Array
ofTextToSpeechInput
Return Value
AnyPublisher<[TextToSpeechResult], Error>
-
Internal function that must be public for Objective-C compatibility reasons.
Warning
Client should never call this function.Declaration
Swift
@available(*, deprecated, message: "Internal function that must be public for Objective-C compatibility reasons. Client should never call this function.") override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)