### Install MisakiSwift via Swift Package Manager Source: https://github.com/mlalma/misakiswift/blob/main/README.md Add the library to your project's dependencies in Package.swift. ```swift dependencies: [ .package(url: "https://github.com/mlalma/MisakiSwift", from: "1.0.1") ] ``` -------------------------------- ### Integrate MisakiSwift via Swift Package Manager Source: https://context7.com/mlalma/misakiswift/llms.txt Shows the Package.swift configuration and a wrapper class implementation for TTS engines. ```swift // Package.swift import PackageDescription let package = Package( name: "MyTTSApp", platforms: [ .iOS(.v18), .macOS(.v15) ], dependencies: [ .package(url: "https://github.com/mlalma/MisakiSwift", from: "1.0.1") ], targets: [ .target( name: "MyTTSApp", dependencies: ["MisakiSwift"] ) ] ) // Usage in your Swift file import MisakiSwift class TTSEngine { private let g2p: EnglishG2P init(useBritishEnglish: Bool = false) { self.g2p = EnglishG2P(british: useBritishEnglish) } func convertToPhonemes(_ text: String) -> String { let (phonemes, _) = g2p.phonemize(text: text) return phonemes } func convertWithTokens(_ text: String) -> (String, [MToken]) { return g2p.phonemize(text: text) } } // Initialize and use let tts = TTSEngine(useBritishEnglish: false) let phonemes = tts.convertToPhonemes("Welcome to the application!") print(phonemes) ``` -------------------------------- ### Initialize EnglishG2P and Convert Text Source: https://context7.com/mlalma/misakiswift/llms.txt The EnglishG2P class is the primary interface for phonemization. Use the british parameter to toggle between American and British English pronunciation models. ```swift import MisakiSwift // Initialize for American English pronunciation let g2pAmerican = EnglishG2P(british: false) // Initialize for British English pronunciation let g2pBritish = EnglishG2P(british: true) // Basic text to phoneme conversion let (phonemes, tokens) = g2pAmerican.phonemize(text: "Hello world!") print(phonemes) // Output: "həlˈO wˈɜɹld!" // British English conversion let (britishPhonemes, _) = g2pBritish.phonemize(text: "Hello world!") print(britishPhonemes) // Output: "həlˈəʊ wˈɜːld!" // Complex sentence with various elements let complexText = "The price is $50 and we need to process 1,234 items by January 15th, 2024." let (result, _) = g2pAmerican.phonemize(text: complexText) print(result) // Converts currency, numbers, dates, and ordinals to phonemes automatically ``` -------------------------------- ### Convert Text to Phonemes Source: https://github.com/mlalma/misakiswift/blob/main/README.md Initialize the EnglishG2P converter and use the phonemize method to generate phonetic representations. ```swift import MisakiSwift // Create G2P converter (british = false for American English) let g2p = EnglishG2P(british: false) // Convert text to phonemes let (phonemes, tokens) = g2p.phonemize(text: "Hello world!") print(phonemes) // "həlˈO wˈɜɹld!" ``` -------------------------------- ### Phonemize text and access token metadata Source: https://context7.com/mlalma/misakiswift/llms.txt Demonstrates how to retrieve phonemes and MToken metadata for word-level timing and analysis. ```swift import MisakiSwift let g2p = EnglishG2P(british: false) let (phonemes, tokens) = g2p.phonemize(text: "Hello world!") // Access token metadata for token in tokens { print("Text: \(token.text)") print("Phonemes: \(token.phonemes ?? "nil")") print("Whitespace: '\(token.whitespace)'") print("POS Tag: \(token.tag?.rawValue ?? "unknown")") print("Rating: \(token._.rating ?? 0)") // Rating indicates confidence: // 4-5 = dictionary match (high confidence) // 3 = silver dictionary or acronym // 1 = neural network fallback print("---") } // Use tokens for word-level timing in TTS applications let wordTimings = tokens.enumerated().map { (index, token) in return (word: token.text, phoneme: token.phonemes ?? "") } print(wordTimings) ``` -------------------------------- ### Handle Currency and Number Normalization Source: https://context7.com/mlalma/misakiswift/llms.txt The library automatically converts currencies, years, and ordinals into spoken word equivalents before phonemization. ```swift import MisakiSwift let g2p = EnglishG2P(british: false) // Currency conversion - American English let (dollarPhonemes, _) = g2p.phonemize(text: "The total is $100.") print(dollarPhonemes) // Converts to "one hundred dollars" then to phonemes // Currency with cents let (centsPhonemes, _) = g2p.phonemize(text: "That costs $29.99.") print(centsPhonemes) // Handles dollars and cents separately // British currency handling let g2pBritish = EnglishG2P(british: true) let (poundPhonemes, _) = g2pBritish.phonemize(text: "It costs £50 or €45.") print(poundPhonemes) // Converts pounds and euros to phonemes // Year formatting let (yearPhonemes, _) = g2p.phonemize(text: "The year 2024 will be great.") print(yearPhonemes) // Years are spoken as "twenty twenty-four" // Ordinal numbers let (ordinalPhonemes, _) = g2p.phonemize(text: "This is the 21st century.") print(ordinalPhonemes) // Ordinals converted to "twenty-first" ``` -------------------------------- ### Apply Custom Phoneme Overrides Source: https://context7.com/mlalma/misakiswift/llms.txt Use Markdown-like link syntax to force specific pronunciations or stress patterns for words, bypassing dictionary and neural network lookups. ```swift import MisakiSwift let g2p = EnglishG2P(british: false) // Override pronunciation for brand names or technical terms let text = "[Misaki](/misˈɑki/) is a G2P engine designed for [Kokoro](/kˈOkəɹO/) models." let (phonemes, _) = g2p.phonemize(text: text) print(phonemes) // Output: "misˈɑki ɪz ɐ ʤˈitəpˈi ˈɛnʤən dəzˈInd fɔɹ kˈOkəɹO mˈɑdᵊlz." // Override with stress control using numeric values // [word](0.5) - secondary stress // [word](1) - primary stress // [word](-0.5) - reduce stress let stressText = "[important](1) information" let (stressPhonemes, _) = g2p.phonemize(text: stressText) print(stressPhonemes) // Custom pronunciation for proper nouns let nameText = "Contact [Nguyen](/wɪn/) for assistance." let (namePhonemes, _) = g2p.phonemize(text: nameText) print(namePhonemes) ``` -------------------------------- ### Apply Custom Phoneme Overrides Source: https://github.com/mlalma/misakiswift/blob/main/README.md Use Markdown-style syntax within the input string to force specific pronunciations. ```swift let g2p = EnglishG2P(british: false) let text = "[Misaki](/misˈɑki/) is a G2P engine designed for [Kokoro](/kˈOkəɹO/) models." let (phonemes, _) = g2p.phonemize(text: text) // "misˈɑki ɪz ɐ ʤˈitəpˈi ˈɛnʤən dəzˈInd fɔɹ kˈOkəɹO mˈɑdᵊlz." ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.