使用 iPhone sdk 进行文本转语音

发布于 2024-11-03 09:23:13 字数 3535 浏览 2 评论 0原文

我对使用一些输入文本生成语音非常陌生。我用下面的例子尝试了一下,并且成功了。演讲是男人的声音,但我需要把它变成女人的声音。可以处理这个吗?各位可以请建议我我们可以更改哪些设置吗?

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>


@interface FliteTTS : NSObject <AVAudioPlayerDelegate> 
{
    //NSData *soundObj;     // doesn't work yet - see note in FliteTTS.m
    AVAudioPlayer* audioPlayer;
}

// Use these:
-(void)speakText:(NSString *)text;
-(void)stopTalking;
-(void)setPitch:(float)pitch variance:(float)variance speed:(float)speed;
-(void)setVoice:(NSString *)voicename;
@end


#import "FliteTTS.h"
#import "flite.h"

cst_voice *register_cmu_us_kal();
cst_voice *register_cmu_us_kal16();
cst_voice *register_cmu_us_rms();
cst_voice *register_cmu_us_awb();
cst_voice *register_cmu_us_slt();
cst_voice *register_usenglish();
cst_wave *sound;
cst_voice *voice;

@implementation FliteTTS

-(id)init
{
    self = [super init];
    flite_init();
    // Set a default voice
    //voice = register_cmu_us_kal();
    //voice = register_cmu_us_kal16();
    //voice = register_cmu_us_rms();
    //voice = register_cmu_us_awb();
    //voice = register_cmu_us_slt();
    [self setVoice:@"cmu_us_kal"];
    return self;
}

-(void)speakText:(NSString *)text
{
    NSMutableString *cleanString;
    cleanString = [NSMutableString stringWithString:@""];
    if([text length] > 1)
    {
        int x = 0;
        while (x < [text length])
        {
            unichar ch = [text characterAtIndex:x];
            [cleanString appendFormat:@"%c", ch];
            x++;
        }
    }
    if(cleanString == nil)
    {       // string is empty
        cleanString = [NSMutableString stringWithString:@""];
    }
    sound = flite_text_to_wave([cleanString UTF8String], voice);




    NSArray *filePaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,   NSUserDomainMask, YES);
    NSString *recordingDirectory = [filePaths objectAtIndex: 0];
    // Pick a file name
    NSString *tempFilePath = [NSString stringWithFormat: @"%@/%s", recordingDirectory, "temp.wav"];
    printf("\n TempFilePath:%s",[tempFilePath UTF8String]);

    // save wave to disk
    char *path; 
    path = (char*)[tempFilePath UTF8String];
    cst_wave_save_riff(sound, path);


    // Play the sound back.
    NSError *err;
    [audioPlayer stop];
    audioPlayer =  [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL  fileURLWithPath:tempFilePath] error:&err];
    [audioPlayer setDelegate:self];
    [audioPlayer prepareToPlay];
    [audioPlayer play];
    // Remove file
    [[NSFileManager defaultManager] removeItemAtPath:tempFilePath error:nil];

}

-(void)setPitch:(float)pitch variance:(float)variance speed:(float)speed
{
    feat_set_float(voice->features,"int_f0_target_mean", pitch);
    feat_set_float(voice->features,"int_f0_target_stddev",variance);
    feat_set_float(voice->features,"duration_stretch",speed); 
}

-(void)setVoice:(NSString *)voicename
{
    if([voicename isEqualToString:@"cmu_us_kal"]) {
        voice = register_cmu_us_kal();
    }
    else if([voicename isEqualToString:@"cmu_us_kal16"]) {
        voice = register_cmu_us_kal16();
    }
    else if([voicename isEqualToString:@"cmu_us_rms"]) {
        voice = register_cmu_us_rms();
    }
    else if([voicename isEqualToString:@"cmu_us_awb"]) {
        voice = register_cmu_us_awb();
    }
    else if([voicename isEqualToString:@"cmu_us_slt"]) {
        voice = register_cmu_us_slt();
    }

}

-(void)stopTalking
{
    [audioPlayer stop];
}

@end

提前致谢, 塞卡尔

I am very new to generating an speech using some input text. I tried it with the following example and I succeeded. The speech is a man's voice But I need to make it as woman's. Is it possible to handle this?. And guys can you please suggest me that what are the settings that we can change in this.

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>


@interface FliteTTS : NSObject <AVAudioPlayerDelegate> 
{
    //NSData *soundObj;     // doesn't work yet - see note in FliteTTS.m
    AVAudioPlayer* audioPlayer;
}

// Use these:
-(void)speakText:(NSString *)text;
-(void)stopTalking;
-(void)setPitch:(float)pitch variance:(float)variance speed:(float)speed;
-(void)setVoice:(NSString *)voicename;
@end


#import "FliteTTS.h"
#import "flite.h"

cst_voice *register_cmu_us_kal();
cst_voice *register_cmu_us_kal16();
cst_voice *register_cmu_us_rms();
cst_voice *register_cmu_us_awb();
cst_voice *register_cmu_us_slt();
cst_voice *register_usenglish();
cst_wave *sound;
cst_voice *voice;

@implementation FliteTTS

-(id)init
{
    self = [super init];
    flite_init();
    // Set a default voice
    //voice = register_cmu_us_kal();
    //voice = register_cmu_us_kal16();
    //voice = register_cmu_us_rms();
    //voice = register_cmu_us_awb();
    //voice = register_cmu_us_slt();
    [self setVoice:@"cmu_us_kal"];
    return self;
}

-(void)speakText:(NSString *)text
{
    NSMutableString *cleanString;
    cleanString = [NSMutableString stringWithString:@""];
    if([text length] > 1)
    {
        int x = 0;
        while (x < [text length])
        {
            unichar ch = [text characterAtIndex:x];
            [cleanString appendFormat:@"%c", ch];
            x++;
        }
    }
    if(cleanString == nil)
    {       // string is empty
        cleanString = [NSMutableString stringWithString:@""];
    }
    sound = flite_text_to_wave([cleanString UTF8String], voice);




    NSArray *filePaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,   NSUserDomainMask, YES);
    NSString *recordingDirectory = [filePaths objectAtIndex: 0];
    // Pick a file name
    NSString *tempFilePath = [NSString stringWithFormat: @"%@/%s", recordingDirectory, "temp.wav"];
    printf("\n TempFilePath:%s",[tempFilePath UTF8String]);

    // save wave to disk
    char *path; 
    path = (char*)[tempFilePath UTF8String];
    cst_wave_save_riff(sound, path);


    // Play the sound back.
    NSError *err;
    [audioPlayer stop];
    audioPlayer =  [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL  fileURLWithPath:tempFilePath] error:&err];
    [audioPlayer setDelegate:self];
    [audioPlayer prepareToPlay];
    [audioPlayer play];
    // Remove file
    [[NSFileManager defaultManager] removeItemAtPath:tempFilePath error:nil];

}

-(void)setPitch:(float)pitch variance:(float)variance speed:(float)speed
{
    feat_set_float(voice->features,"int_f0_target_mean", pitch);
    feat_set_float(voice->features,"int_f0_target_stddev",variance);
    feat_set_float(voice->features,"duration_stretch",speed); 
}

-(void)setVoice:(NSString *)voicename
{
    if([voicename isEqualToString:@"cmu_us_kal"]) {
        voice = register_cmu_us_kal();
    }
    else if([voicename isEqualToString:@"cmu_us_kal16"]) {
        voice = register_cmu_us_kal16();
    }
    else if([voicename isEqualToString:@"cmu_us_rms"]) {
        voice = register_cmu_us_rms();
    }
    else if([voicename isEqualToString:@"cmu_us_awb"]) {
        voice = register_cmu_us_awb();
    }
    else if([voicename isEqualToString:@"cmu_us_slt"]) {
        voice = register_cmu_us_slt();
    }

}

-(void)stopTalking
{
    [audioPlayer stop];
}

@end

Thanks in Advance,
Sekhar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薯片软お妹 2024-11-10 09:23:13

调用setVoice:@"cmu_us_slt"设置女声。对于带有美国口音的男声,请调用 setVoice:@"cmu_us_rms"

Call setVoice:@"cmu_us_slt" to set the female voice. For a male voice with a US accent, call setVoice:@"cmu_us_rms".

海拔太高太耀眼 2024-11-10 09:23:13

如果我从 这里 它有很好的解释。

Arabic (Saudi Arabia) - ar-SA
Chinese (China) - zh-CN
Chinese (Hong Kong SAR China) - zh-HK
Chinese (Taiwan) - zh-TW
Czech (Czech Republic) - cs-CZ
Danish (Denmark) - da-DK
Dutch (Belgium) - nl-BE
Dutch (Netherlands) - nl-NL
English (Australia) - en-AU
English (Ireland) - en-IE
English (South Africa) - en-ZA
English (United Kingdom) - en-GB
English (United States) - en-US
Finnish (Finland) - fi-FI
French (Canada) - fr-CA
French (France) - fr-FR
German (Germany) - de-DE
Greek (Greece) - el-GR
Hindi (India) - hi-IN
Hungarian (Hungary) - hu-HU
Indonesian (Indonesia) - id-ID
Italian (Italy) - it-IT
Japanese (Japan) - ja-JP
Korean (South Korea) - ko-KR
Norwegian (Norway) - no-NO
Polish (Poland) - pl-PL
Portuguese (Brazil) - pt-BR
Portuguese (Portugal) - pt-PT
Romanian (Romania) - ro-RO
Russian (Russia) - ru-RU
Slovak (Slovakia) - sk-SK
Spanish (Mexico) - es-MX
Spanish (Spain) - es-ES
Swedish (Sweden) - sv-SE
Thai (Thailand) - th-TH
Turkish (Turkey) - tr-TR

希望这对你有帮助

If yI've found Good Article for AVSpeechSynthesizer (iOS 7) from here It has nice explanation.

Arabic (Saudi Arabia) - ar-SA
Chinese (China) - zh-CN
Chinese (Hong Kong SAR China) - zh-HK
Chinese (Taiwan) - zh-TW
Czech (Czech Republic) - cs-CZ
Danish (Denmark) - da-DK
Dutch (Belgium) - nl-BE
Dutch (Netherlands) - nl-NL
English (Australia) - en-AU
English (Ireland) - en-IE
English (South Africa) - en-ZA
English (United Kingdom) - en-GB
English (United States) - en-US
Finnish (Finland) - fi-FI
French (Canada) - fr-CA
French (France) - fr-FR
German (Germany) - de-DE
Greek (Greece) - el-GR
Hindi (India) - hi-IN
Hungarian (Hungary) - hu-HU
Indonesian (Indonesia) - id-ID
Italian (Italy) - it-IT
Japanese (Japan) - ja-JP
Korean (South Korea) - ko-KR
Norwegian (Norway) - no-NO
Polish (Poland) - pl-PL
Portuguese (Brazil) - pt-BR
Portuguese (Portugal) - pt-PT
Romanian (Romania) - ro-RO
Russian (Russia) - ru-RU
Slovak (Slovakia) - sk-SK
Spanish (Mexico) - es-MX
Spanish (Spain) - es-ES
Swedish (Sweden) - sv-SE
Thai (Thailand) - th-TH
Turkish (Turkey) - tr-TR

hope this will help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文