### Install KokoroSwift using Swift Package Manager Source: https://github.com/mlalma/kokoro-ios/blob/main/README.md This snippet shows how to add the KokoroSwift library to your project's dependencies using Swift Package Manager. Ensure you have the correct URL and version specified. ```swift dependencies: [ .package(url: "https://github.com/mlalma/kokoro-ios.git", from: "1.0.0") ] .target( name: "YourTarget", dependencies: [ .product(name: "KokoroSwift", package: "kokoro-ios") ] ) ``` -------------------------------- ### Integrate KokoroSwift with Swift Package Manager Source: https://context7.com/mlalma/kokoro-ios/llms.txt Add the KokoroSwift library to your project using Swift Package Manager by specifying the repository URL and version in your Package.swift file. This setup requires iOS 18.0+ or macOS 15.0+. ```swift import PackageDescription let package = Package( name: "MyTTSApp", platforms: [ .iOS(.v18), .macOS(.v15) ], dependencies: [ .package(url: "https://github.com/mlalma/kokoro-ios.git", from: "1.0.0") ], targets: [ .executableTarget( name: "MyTTSApp", dependencies: [ .product(name: "KokoroSwift", package: "kokoro-ios") ] ) ] ) ``` -------------------------------- ### Kokoro-iOS TTS Constants for Configuration Source: https://context7.com/mlalma/kokoro-ios/llms.txt Explains the use of KokoroTTS.Constants for accessing essential configuration values such as the maximum token count for input text and the audio sampling rate. Includes example functions for validating text length and calculating audio duration. ```swift import KokoroSwift // Maximum number of input tokens allowed let maxTokens = KokoroTTS.Constants.maxTokenCount // 510 tokens // Audio sample rate for generated audio let sampleRate = KokoroTTS.Constants.samplingRate // 24000 Hz // Example: Validate text length before generation func validateAndGenerate(text: String, tts: KokoroTTS, voice: MLXArray) throws -> [Float] { // Rough estimate: ~4 characters per token on average let estimatedTokens = text.count / 4 if estimatedTokens > KokoroTTS.Constants.maxTokenCount { // Split long text into chunks print("Text may be too long, consider splitting") } do { let (audio, _) = try tts.generateAudio( voice: voice, language: .enUS, text: text ) return audio } catch KokoroTTS.KokoroTTSError.tooManyTokens { throw NSError(domain: "TTS", code: 1, userInfo: [ NSLocalizedDescriptionKey: "Input text exceeds maximum token limit of \(KokoroTTS.Constants.maxTokenCount)" ]) } } // Calculate audio duration from samples func calculateDuration(samples: [Float]) -> Double { return Double(samples.count) / Double(KokoroTTS.Constants.samplingRate) } ``` -------------------------------- ### Initialize KokoroTTS Engine in Swift Source: https://context7.com/mlalma/kokoro-ios/llms.txt Initializes the KokoroTTS engine, the main entry point for text-to-speech synthesis. It requires a path to the model weights and a selection for the G2P processor. Model files must be downloaded separately. The engine supports both Misaki and eSpeak NG G2P processors. ```swift import KokoroSwift import MLX // Initialize the TTS engine with model path and G2P processor let modelPath = URL(fileURLWithPath: "/path/to/kokoro-model.safetensors") let tts = KokoroTTS(modelPath: modelPath, g2p: .misaki) // Alternative: Use eSpeak NG G2P processor (if compiled with eSpeakNGSwift) // let tts = KokoroTTS(modelPath: modelPath, g2p: .eSpeakNG) // TTS engine is now ready for audio generation // Model weights are loaded and preprocessed automatically print("TTS engine initialized successfully") ``` -------------------------------- ### Generate Speech using KokoroTTS in Swift Source: https://github.com/mlalma/kokoro-ios/blob/main/README.md This Swift code demonstrates how to initialize the KokoroTTS engine and generate audio from text. It requires a model path, a G2P processor (like .misaki), a voice embedding, and the text to synthesize. ```swift import KokoroSwift // Initialize the TTS engine let modelPath = URL(fileURLWithPath: "path/to/your/model") let tts = KokoroTTS(modelPath: modelPath, g2p: .misaki) // Generate speech let voiceEmbedding = ... // See KokoroTestApp on how to get a voice style as an `MLXArray` let text = "Hello, this is a test of Kokoro TTS." let audioBuffer = try tts.generateAudio(voice: voiceEmbedding, language: .enUS, text: text) // audioBuffer now contains the synthesized speech ``` -------------------------------- ### Debug Audio Output with AudioUtils in Kokoro-iOS Source: https://context7.com/mlalma/kokoro-ios/llms.txt Illustrates how to use the `AudioUtils` class, available only in DEBUG builds, to save generated audio samples to a WAV file. This is useful for debugging and verifying the output of the TTS engine. ```swift #if DEBUG import KokoroSwift import Foundation // Generate audio let (audioSamples, _) = try tts.generateAudio( voice: voiceEmbedding, language: .enUS, text: "This is a test recording." ) // Save to WAV file for debugging let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] let outputURL = documentsPath.appendingPathComponent("test_output.wav") try AudioUtils.writeWavFile( samples: audioSamples, sampleRate: Double(KokoroTTS.Constants.samplingRate), // 24000.0 fileURL: outputURL ) print("Audio saved to: \(outputURL.path)") // Verify file was created let fileSize = try FileManager.default.attributesOfItem(atPath: outputURL.path)[.size] as? Int ?? 0 print("File size: \(fileSize) bytes") #endif ``` -------------------------------- ### Select G2P Engine for Kokoro-iOS TTS Source: https://context7.com/mlalma/kokoro-ios/llms.txt Demonstrates how to select between different Grapheme-to-Phoneme (G2P) engines within KokoroSwift. MisakiSwift is the default and recommended engine for English, while eSpeakNG offers multi-language support but requires an additional dependency. ```swift import KokoroSwift // Available G2P engines let misakiEngine = G2P.misaki // Default, optimized for English let espeakEngine = G2P.eSpeakNG // Alternative, multi-language support // MisakiSwift G2P (recommended) // - Optimized for English text // - Handles contractions, abbreviations, numbers // - Returns word-level tokens with timestamps let tts = KokoroTTS(modelPath: modelPath, g2p: .misaki) // eSpeak NG G2P (optional, requires eSpeakNGSwift dependency) // - Supports multiple languages // - Different phoneme set // Note: Requires uncommenting the eSpeakNGSwift dependency in Package.swift // let tts = KokoroTTS(modelPath: modelPath, g2p: .eSpeakNG) ``` -------------------------------- ### Implement Text-to-Speech Service with KokoroSwift and MLX Source: https://context7.com/mlalma/kokoro-ios/llms.txt A Swift class demonstrating the usage of KokoroSwift for text-to-speech generation, integrated with the MLX framework. It includes initialization with model and voice paths, and a method to convert text into audio data. ```swift import KokoroSwift import MLX // Complete usage example class TTSService { private var tts: KokoroTTS? private var voiceEmbedding: MLXArray? func initialize(modelPath: URL, voicePath: URL) throws { tts = KokoroTTS(modelPath: modelPath, g2p: .misaki) voiceEmbedding = try MLX.loadArray(url: voicePath) } func speak(_ text: String, speed: Float = 1.0) throws -> [Float] { guard let tts = tts, let voice = voiceEmbedding else { throw NSError(domain: "TTS", code: 0, userInfo: nil) } let (audio, _) = try tts.generateAudio( voice: voice, language: .enUS, text: text, speed: speed ) return audio } } ``` -------------------------------- ### Select Language for Speech Synthesis in Swift Source: https://context7.com/mlalma/kokoro-ios/llms.txt Demonstrates how to select the appropriate language for text-to-speech synthesis using the `Language` enum. Supported options include US English (`.enUS`) and British English (`.enGB`), which affect phoneme generation for accurate pronunciation. The voice style is independently controlled by the voice embedding. ```swift import KokoroSwift // Available language options let usEnglish = Language.enUS // American English pronunciation let ukEnglish = Language.enGB // British English pronunciation let noLanguage = Language.none // Language-independent (not recommended) // Language affects phoneme generation, not voice style // Voice style is controlled by the voice embedding parameter // Example: Generate same text with different pronunciations let text = "The colour of the aluminium is quite nice." // US English: "color", "aluminum" let (usAudio, _) = try tts.generateAudio( voice: voiceEmbedding, language: .enUS, text: text ) // British English: "colour", "aluminium" let (ukAudio, _) = try tts.generateAudio( voice: voiceEmbedding, language: .enGB, text: text ) ``` -------------------------------- ### Swift TTS Manager for Audio Generation and Timestamp Extraction Source: https://context7.com/mlalma/kokoro-ios/llms.txt Manages Text-to-Speech (TTS) synthesis using KokoroSwift. It initializes the TTS engine with model and voice embeddings, generates audio data from text, and extracts word timestamps for highlighting. Dependencies include KokoroSwift, MLX, and AVFoundation. Input is text, language, and speed; output is audio data and word timestamps. ```swift import KokoroSwift import MLX import AVFoundation class KokoroTTSManager { private let tts: KokoroTTS private let voiceEmbedding: MLXArray private var audioPlayer: AVAudioPlayer? init(modelURL: URL, voiceURL: URL) throws { // Initialize TTS engine self.tts = KokoroTTS(modelPath: modelURL, g2p: .misaki) // Load voice embedding self.voiceEmbedding = try MLX.loadArray(url: voiceURL) } /// Generates speech and returns audio data with word timestamps func synthesize( text: String, language: Language = .enUS, speed: Float = 1.0 ) throws -> (audioData: Data, timestamps: [(word: String, start: Double, end: Double)]) { // Generate audio let (samples, tokens) = try tts.generateAudio( voice: voiceEmbedding, language: language, text: text, speed: speed ) // Convert samples to audio data let audioData = samples.withUnsafeBytes { Data($0) } // Extract timestamps for word highlighting var timestamps: [(word: String, start: Double, end: Double)] = [] if let tokens = tokens { for token in tokens { if let start = token.start_ts, let end = token.end_ts { timestamps.append((word: "", start: start, end: end)) } } } return (audioData, timestamps) } /// Generates speech at different speeds for comparison func compareSpeedsDemo(text: String) throws -> [String: [Float]] { var results: [String: [Float]] = [:] // Slow speed (0.75x) let (slowAudio, _) = try tts.generateAudio( voice: voiceEmbedding, language: .enUS, text: text, speed: 0.75 ) results["slow"] = slowAudio // Normal speed (1.0x) let (normalAudio, _) = try tts.generateAudio( voice: voiceEmbedding, language: .enUS, text: text, speed: 1.0 ) results["normal"] = normalAudio // Fast speed (1.5x) let (fastAudio, _) = try tts.generateAudio( voice: voiceEmbedding, language: .enUS, text: text, speed: 1.5 ) results["fast"] = fastAudio return results } } // Usage let manager = try KokoroTTSManager( modelURL: Bundle.main.url(forResource: "kokoro", withExtension: "safetensors")!, voiceURL: Bundle.main.url(forResource: "voice_af", withExtension: "npy")! ) let (audio, timestamps) = try manager.synthesize( text: "Welcome to Kokoro text to speech. This library provides high quality audio synthesis.", language: .enUS, speed: 1.0 ) print("Audio size: \(audio.count) bytes") print("Word timestamps: \(timestamps.count)") ``` -------------------------------- ### Generate Speech Audio with KokoroSwift in Swift Source: https://context7.com/mlalma/kokoro-ios/llms.txt Generates speech audio from input text using the primary `generateAudio` method. This method requires a voice embedding (style vector), language selection, the input text, and an optional speech speed parameter. It returns an array of audio samples and optional token timestamps for synchronization. ```swift import KokoroSwift import MLX // Initialize TTS engine let modelPath = URL(fileURLWithPath: "/path/to/kokoro-model.safetensors") let tts = KokoroTTS(modelPath: modelPath, g2p: .misaki) // Load voice embedding (style vector) from your application bundle // Voice embeddings define speaker characteristics and style let voiceData = try Data(contentsOf: URL(fileURLWithPath: "/path/to/voice.npy")) let voiceEmbedding: MLXArray = try MLX.loadArray(data: voiceData) // Generate audio with US English pronunciation let text = "Hello! This is a demonstration of Kokoro text to speech synthesis." let (audioSamples, tokens) = try tts.generateAudio( voice: voiceEmbedding, language: .enUS, // Use .enGB for British English text: text, speed: 1.0 // 1.0 = normal, 0.8 = slower, 1.2 = faster ) // audioSamples contains raw audio at 24kHz sample rate print("Generated \(audioSamples.count) audio samples") print("Audio duration: \(Double(audioSamples.count) / 24000.0) seconds") // Access token timestamps if available (new in v1.0.8) if let tokens = tokens { for token in tokens { if let start = token.start_ts, let end = token.end_ts { print("Token: \(start)s - \(end)s") } } } ``` -------------------------------- ### Error Handling in Kokoro-iOS TTS Source: https://context7.com/mlalma/kokoro-ios/llms.txt Provides a robust error handling strategy for KokoroTTS operations. It demonstrates how to catch specific errors like `tooManyTokens`, `processorNotInitialized`, and `unsupportedLanguage`, returning a `Result` type for cleaner error management. ```swift import KokoroSwift // Handle TTS-specific errors func generateSpeechSafely(tts: KokoroTTS, voice: MLXArray, text: String) -> Result<[Float], Error> { do { let (audio, _) = try tts.generateAudio( voice: voice, language: .enUS, text: text ) return .success(audio) } catch KokoroTTS.KokoroTTSError.tooManyTokens { // Input text was too long (>510 tokens) return .failure(NSError( domain: "KokoroTTS", code: 1, userInfo: [NSLocalizedDescriptionKey: "Text too long, please split into smaller chunks"] )) } catch G2PProcessorError.processorNotInitialized { // G2P processor not properly initialized return .failure(NSError( domain: "KokoroTTS", code: 2, userInfo: [NSLocalizedDescriptionKey: "G2P processor failed to initialize"] )) } catch G2PProcessorError.unsupportedLanguage { // Requested language not supported return .failure(NSError( domain: "KokoroTTS", code: 3, userInfo: [NSLocalizedDescriptionKey: "Language not supported by G2P processor"] )) } catch { return .failure(error) } } // Usage let result = generateSpeechSafely(tts: tts, voice: voiceEmbedding, text: longText) switch result { case .success(let audio): print("Generated \(audio.count) samples") case .failure(let error): print("Error: \(error.localizedDescription)") } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.