swift tts
2.1.2 Error if built with macOS
이 패키지에는 AVFoundation/AvSpeechSynthesizer의 TTS 부분 주변의 매우 간단한 포장지가 포함되어있어 텍스트를 쉽게 사용할 수 있습니다.
SwiftTTS await async 동시성을 사용한 신속한 비동기 AsyncStreamSwiftTTSDependency Point-Free Dependencies Library와의 통합 또는 Composable Architecture (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 ADD 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 " ) 패키지 종속성으로 추가하여 Xcode 프로젝트에 Swifttts Libs를 추가 할 수 있습니다.
Package.swift 를 편집하십시오.
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
] ) ,
...
]
)