espeak-ng/espeak-ng-spm

Can't get phonemes

Closed this issue · 5 comments

1Carl commented

Hi, I'm making a synthesizer app using espeak phonemes. But when I try to get phoneme of text, it always returns Optional(0).

        let options: Int32 = espeakPHONEMES_IPA | 0 
        var text = "My text I want to phonemize"
        var textPtr: UnsafeRawPointer? = UnsafeRawPointer(text)
        let phonemesPtr = espeak_TextToPhonemes(&textPtr, espeakCHARS_UTF8, options)
        if let phonemes = phonemesPtr {
          print(String(cString: phonemes)) // prints "" here
       }

Update

Currently, i can get phoneme on MacOS Monterey. Unfortunately i couldn't figure it out on MacOS Ventura.

PS: Sorry for bad english :)

Sorry, I’m not familiar with espeak phonemizer API. Will try it by myself…

1Carl commented

This works as well.

        let options: Int32 = 19
        var text = "My text I want to phonemize"
        text.utf8CString.withUnsafeBufferPointer { ptr in
            var textPtr: UnsafeRawPointer? = UnsafeRawPointer(ptr.baseAddress)
            let phonemesPtr = espeak_TextToPhonemes(&textPtr, espeakCHARS_UTF8, options)
            if let phonemes = phonemesPtr {
              print(String(cString: phonemes))
            }
        }

@1Carl
Hello, can I view specific usage examples from you? I've recently been trying to invoke text-to-phoneme on macOS and iOS. The version I read during initialization on macOS is 0, and it's throwing an exception. However, on iOS, I'm also encountering an exception due to the absence of the "phontab" file. Have you been able to successfully phonemize text on iOS? Thank for your assistance.

1Carl commented

@cvv-student
Hello,
I created a swift package and added this package as a dependency. Then i created an utility class and added a function. This is how implemented that class.
ps: I'm struggling with this markdown 😢
`
extension espeak_ng_STATUS {
func check() throws {
guard self == ENS_OK else {
var stringBuffer = [CChar](repeating: 0, count: 512)
let str = stringBuffer.withUnsafeMutableBufferPointer { buf in
espeak_ng_GetStatusCodeMessage(self, buf.baseAddress!, buf.count)
return String(cString: buf.baseAddress!)
}
throw NSError(domain: EspeakErrorDomain, code: Int(rawValue), userInfo: [NSLocalizedFailureReasonErrorKey: str])
}
}
}

public class ESPEAK_Phonemizer {
init() {
let documentsDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let root = documentsDirectory
try! EspeakLib.ensureBundleInstalled(inRoot: root)
espeak_ng_InitializePath(root.path)
try! espeak_ng_Initialize(nil).check()
// try espeak_ng_SetVoiceByName(ESPEAKNG_DEFAULT_VOICE).check()
try! espeak_ng_SetVoiceByName("en").check()
}

public func latin_phonemes(_ words: [String]) -> [String] {
    let options: Int32 = espeakPHONEMES_IPA | 19

    return words.map { $0.utf8CString.withUnsafeBufferPointer { ptr in
        var textPtr: UnsafeRawPointer? = UnsafeRawPointer(ptr.baseAddress)
        let phonemesPtr = espeak_TextToPhonemes(&textPtr, espeakCHARS_UTF8, options)
        if let phonemes = phonemesPtr {
            let data = String(cString: phonemes)
            return data
        }

        fatalError("can't phonemizer espeak")
    }}
}

}

`

@1Carl
Using your method, I have also successfully obtained the phoneme. Thank for help.