我对使用一些输入文本生成语音非常新颖。我试着用下面的例子,我成功了。演讲是一个男人的声音,但我需要把它作为女人的。有没有可能处理这个问题?而且你们可以向我建议我们可以改变的设置是什么。使用iPhone sdk的文本到语音
#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
由于事先 Sekhar
您有四种类型的FLITE图书馆的声音,你可以使用[fliteEngine setVoice:@ “cmu_us_slt”];在设置新语音之后清除所有目标 – kiri 2011-04-22 10:06:18
我也在工作同一类型的项目,我想生成孩子的声音是否有可能使用setVoice属性生成... – Aadil 2011-06-21 05:36:08