|
| 1 | +#import <Foundation/Foundation.h> |
| 2 | +#import <AVFAudio/AVSpeechSynthesis.h> |
| 3 | + |
| 4 | +void* avsynth_create(void) { |
| 5 | + @try { |
| 6 | + AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init]; |
| 7 | + return (void *)CFBridgingRetain(synth); |
| 8 | + } @catch (NSException *e) { |
| 9 | + return NULL; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +void avsynth_destroy(void *handle) { |
| 14 | + if (!handle) return; |
| 15 | + @try { |
| 16 | + AVSpeechSynthesizer *synth = (__bridge_transfer AVSpeechSynthesizer *)handle; |
| 17 | + synth = nil; |
| 18 | + } @catch (NSException *e) {} |
| 19 | +} |
| 20 | + |
| 21 | +void avsynth_speak(void *handle, const char *text, const char *voice_id, |
| 22 | + float rate, float pitch, float volume) { |
| 23 | + if (!handle || !text) return; |
| 24 | + @try { |
| 25 | + AVSpeechSynthesizer *synth = (__bridge AVSpeechSynthesizer *)handle; |
| 26 | + NSString *nsText = [NSString stringWithUTF8String:text]; |
| 27 | + if (!nsText) return; |
| 28 | + AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:nsText]; |
| 29 | + utterance.rate = rate < 0.1f ? 0.1f : (rate > 10.0f ? 10.0f : rate); |
| 30 | + utterance.pitchMultiplier = pitch < 0.5f ? 0.5f : (pitch > 2.0f ? 2.0f : pitch); |
| 31 | + utterance.volume = volume < 0.0f ? 0.0f : (volume > 1.0f ? 1.0f : volume); |
| 32 | + if (voice_id && voice_id[0] != '\0') { |
| 33 | + NSString *nsVoiceId = [NSString stringWithUTF8String:voice_id]; |
| 34 | + AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithIdentifier:nsVoiceId]; |
| 35 | + if (voice) utterance.voice = voice; |
| 36 | + } |
| 37 | + [synth speakUtterance:utterance]; |
| 38 | + } @catch (NSException *e) {} |
| 39 | +} |
| 40 | + |
| 41 | +void avsynth_stop(void *handle) { |
| 42 | + if (!handle) return; |
| 43 | + @try { |
| 44 | + AVSpeechSynthesizer *synth = (__bridge AVSpeechSynthesizer *)handle; |
| 45 | + [synth stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; |
| 46 | + } @catch (NSException *e) {} |
| 47 | +} |
| 48 | + |
| 49 | +void avsynth_pause(void *handle) { |
| 50 | + if (!handle) return; |
| 51 | + @try { |
| 52 | + AVSpeechSynthesizer *synth = (__bridge AVSpeechSynthesizer *)handle; |
| 53 | + [synth pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate]; |
| 54 | + } @catch (NSException *e) {} |
| 55 | +} |
| 56 | + |
| 57 | +void avsynth_resume(void *handle) { |
| 58 | + if (!handle) return; |
| 59 | + @try { |
| 60 | + AVSpeechSynthesizer *synth = (__bridge AVSpeechSynthesizer *)handle; |
| 61 | + [synth continueSpeaking]; |
| 62 | + } @catch (NSException *e) {} |
| 63 | +} |
| 64 | + |
| 65 | +int avsynth_voice_count(void *handle) { |
| 66 | + if (!handle) return 0; |
| 67 | + @try { |
| 68 | + NSArray<AVSpeechSynthesisVoice *> *voices = [AVSpeechSynthesisVoice speechVoices]; |
| 69 | + return (int)[voices count]; |
| 70 | + } @catch (NSException *e) { |
| 71 | + return 0; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +int avsynth_get_voice(void *handle, int index, |
| 76 | + char *id_buf, int id_buf_len, |
| 77 | + char *name_buf, int name_buf_len, |
| 78 | + char *lang_buf, int lang_buf_len) { |
| 79 | + if (!handle) return -1; |
| 80 | + @try { |
| 81 | + NSArray<AVSpeechSynthesisVoice *> *voices = [AVSpeechSynthesisVoice speechVoices]; |
| 82 | + if (index < 0 || index >= (int)[voices count]) return -1; |
| 83 | + AVSpeechSynthesisVoice *voice = voices[index]; |
| 84 | + |
| 85 | + NSString *identifier = voice.identifier ?: @""; |
| 86 | + NSString *name = voice.name ?: @""; |
| 87 | + NSString *language = voice.language ?: @""; |
| 88 | + |
| 89 | + strncpy(id_buf, [identifier UTF8String], id_buf_len - 1); |
| 90 | + id_buf[id_buf_len - 1] = '\0'; |
| 91 | + strncpy(name_buf, [name UTF8String], name_buf_len - 1); |
| 92 | + name_buf[name_buf_len - 1] = '\0'; |
| 93 | + strncpy(lang_buf, [language UTF8String], lang_buf_len - 1); |
| 94 | + lang_buf[lang_buf_len - 1] = '\0'; |
| 95 | + return 0; |
| 96 | + } @catch (NSException *e) { |
| 97 | + return -1; |
| 98 | + } |
| 99 | +} |
0 commit comments