swift tts
2.1.2 Error if built with macOS
このパッケージには、Avfoundation/AvspeechSynthesizerのTTSの一部の周りの非常に簡単なラッパーが含まれており、テキストからスピーチを簡単に使用できるようにします。
SwiftTTS async awaitとswiftの並行性を使用して、いくつかのAsyncStreamSwiftTTSDependency上記のライブラリ周辺のラッパーは、ポイントフリーの依存関係ライブラリまたは複合アーキテクチャ(TCA)で行われたプロジェクトとの統合を促進します。SwiftTTSCombineこのパッケージでは、OGライブラリがまだ利用可能ですspeak(String) -> Void単純な文字列でTTSを使用するだけで、この方法を呼び出しますisSpeaking() -> AsyncStream<Bool> - 発話がいつ聞こえ始め、いつ停止したかを知るspeakingProgress() -> AsyncStream<Double> - 0から1までの進捗を知るためにrateRatio() -> Floatレートを設定してTTSエンジンを遅くするか加速しますsetRateRatio(Float) -> Voidレートを設定してTTSエンジンを遅くするか、加速しますvoice() -> AVSpeechSynthesisVoice? -TTSエンジンの音声、デフォルトでは、それはen-GBの声ですsetVoice(AVSpeechSynthesisVoice) -> Void TTSエンジンの音声を設定しますimport SwiftTTS
let tts = SwiftTTS . live
tts . speak ( " Hello World! " )
Task {
for await isSpeaking in tts . isSpeaking ( ) {
print ( " TTS is currently ( isSpeaking ? " speaking " : " not speaking " ) " )
}
}
Task {
for await progress in tts . speakingProgress ( ) {
print ( " Progress: ( Int ( progress * 100 ) ) % " )
}
}
tts . setRateRatio ( 3 / 4 )
tts . speak ( " Hello World! But slower " ) @Dependency(.tts) var tts Reducerに追加すると、上記のすべての機能にアクセスできます。
import ComposableArchitecture
import Foundation
import SwiftTTSDependency
public struct TTS : ReducerProtocol {
public struct State : Equatable {
public var text = " "
public var isSpeaking = false
public var speakingProgress = 1.0
public var rateRatio : Float = 1.0
public init (
text : String = " " ,
isSpeaking : Bool = false ,
speakingProgress : Double = 1.0 ,
rateRatio : Float = 1.0
) {
self . text = text
self . isSpeaking = isSpeaking
self . speakingProgress = speakingProgress
self . rateRatio = rateRatio
}
}
public enum Action : Equatable {
case changeRateRatio ( Float )
case speak
case startSpeaking
case stopSpeaking
case changeSpeakingProgress ( Double )
}
@ Dependency ( . tts ) var tts
public init ( ) { }
public var body : some ReducerProtocol < State , Action > {
Reduce { state , action in
switch action {
case let . changeRateRatio ( rateRatio ) :
state . rateRatio = rateRatio
tts . setRateRatio ( rateRatio )
return . none
case . speak :
tts . speak ( state . text )
return . run { send in
for await isSpeaking in tts . isSpeaking ( ) {
if isSpeaking {
await send ( . startSpeaking )
} else {
await send ( . stopSpeaking )
}
}
}
case . startSpeaking :
state . isSpeaking = true
return . run { send in
for await progress in tts . speakingProgress ( ) {
await send ( . changeSpeakingProgress ( progress ) )
}
}
case . stopSpeaking :
state . isSpeaking = false
return . none
case let . changeSpeakingProgress ( speakingProgress ) :
state . speakingProgress = speakingProgress
return . none
}
}
}
} TTSEngineオブジェクトをインスタンス化/挿入できます。この動作があります
func speak(string: String) :単純な文字列でTTSを使用するだけで、この方法を呼び出しますisSpeakingPublisherを購読して、発話がいつ聞こえ始め、いつ停止したかを知るspeakingProgressPublisherを購読して、0から1までの進捗状況を知るvar rateRatio: Float :レートを設定してTTSエンジンを遅くするか加速しますvar voice: AVSpeechSynthesisVoice? :TTSエンジンの音声を設定します。デフォルトでは、 en-GBの声ですimport Combine
import SwiftTTSCombine
let engine : TTSEngine = SwiftTTSCombine . Engine ( )
var cancellables = Set < AnyCancellable > ( )
engine . speak ( string : " Hello World! " )
engine . isSpeakingPublisher
. sink { isSpeaking in
print ( " TTS is currently ( isSpeaking ? " speaking " : " not speaking " ) " )
}
. store ( in : & cancellables )
engine . speakingProgressPublisher
. sink { progress in
print ( " Progress: ( Int ( progress * 100 ) ) % " )
}
. store ( in : & cancellables )
engine . rateRatio = 3 / 4
engine . speak ( string : " Hello World! But slower " ) swifttts libsをパッケージ依存関係として追加することにより、Xcodeプロジェクトに追加できます。
Package.swiftを編集して、利用可能な3つの中に必要なライブラリの1つを追加します。
let package = Package (
...
dependencies : [
. package ( url : " https://github.com/renaudjenny/swift-tts " , from : " 2.0.0 " ) ,
...
] ,
targets : [
. target (
name : " <Your project name> " ,
dependencies : [
. product ( name : " SwiftTTS " , package : " swift-tts " ) , // <-- Modern concurrency
. product ( name : " SwiftTTSDependency " , package : " swift-tts " ) , // <-- Point-Free Dependencies library wrapper
. product ( name : " SwiftTTSCombine " , package : " swift-tts " ) , // <-- Combine wrapper
] ) ,
...
]
)