0

sfoster iPhone TTS を使用しています。テキストを音声に変換したい & 文字列を文字に分割したい & テキストと文字の両方が発音されます。しかし、それはテキストと文字を発音していません。私のコードは次のとおりです。

-(void)spellOut:(NSString *)text{

[fliteEngine speakText:text];   
[fliteEngine setPitch:100.0 variance:50.0 speed:2.5];
[fliteEngine setVoice:@"cmu_us_rms"];

/* split a string into an array */

NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[text length]];

for (int i=0; i < [text length]; i++) {
    NSString *ichar  = [NSString stringWithFormat:@"%c", [text characterAtIndex:i]];
    [characters addObject:ichar];
}

/* set letter from each index of the array */

for (int i=0; i<[characters count]; i++) {

    NSString *letter = [characters objectAtIndex:i];

    NSLog(@"%@\n",letter);

    [fliteEngine speakText:letter];
    [fliteEngine setPitch:100.0 variance:50.0 speed:2.5];
    [fliteEngine setVoice:@"cmu_us_rms"];   
  }

}

問題はどこだ?

前もって感謝します。

4

3 に答える 3

1

まず、FliteTTS を開始していないようですので、最初にこれを行ってください

fliteEngine = [[FliteTTS alloc] init];

次に、あなたのコードから知ったように、すべての文字を個別に発音せずにこのコードを使用すると機能します。

-(void)spellOut:(NSString *)text{

[fliteEngine speakText:text];
[fliteEngine setPitch:100.0 variance:50.0 speed:2.5];
[fliteEngine setVoice:@"cmu_us_rms"];

/* split a string into an array */

NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[text length]];

for (int i=0; i < [text length]; i++) 
 {
    NSString *ichar  = [NSString stringWithFormat:@"%c", [text characterAtIndex:i]];
    [characters addObject:ichar];
}


}

お役に立てば幸いです。

于 2013-04-30T09:18:56.590 に答える
0

テキストのスペルアウトにタイマーを使用できます。

.h

NSString *speechText;
int tempVariable;

.m

-(void)spellOut:(NSString *)text{

    speechText=[NSString stringWithFormat:@"%@",text];

    tempVariable=0;

    [fliteEngine speakText:text];   
    [fliteEngine setPitch:100.0 variance:50.0 speed:2.5];
    [fliteEngine setVoice:@"cmu_us_rms"];

    NSTimer *popupTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    NSRunLoop *runner = [NSRunLoop currentRunLoop];
    [runner addTimer:popupTimer forMode: NSDefaultRunLoopMode];
}

-(void)timerAction:(NSTimer *)theTimer {

    if(tempVariable<speechText.length) {

        NSString *letter = [NSString stringWithFormat:@"%c",[speechText characterAtIndex:tempVariable]];

        NSLog(@"%@\n",letter);

        [fliteEngine speakText:letter];
        [fliteEngine setPitch:100.0 variance:50.0 speed:2.5];
        [fliteEngine setVoice:@"cmu_us_rms"];
        tempVariable++;
    } else {

        [theTimer invalidate];
        theTimer=nil;
    }

}
于 2013-04-30T09:29:17.493 に答える
0
- (void)viewDidLoad
{


 @try {

    [super viewDidLoad];

    self.wordToSpeech = @"What do functional foods mean? According to the April 2009 position on functional foods by the American Dietetic Association (ADA), all foods are functional at some level, because they provide nutrients that furnish energy, sustain growth, or maintain and repair vital processes. While the functional food category, per se, is not officially recognized by the Food and Drug Administration, the ADA considers functional foods to be whole foods and fortified, enriched, or enhanced foods that have a potentially beneficial effect on health. Thus a list of functional foods might be as varied as nuts, calcium-fortified orange juice, energy bars, bottled teas and gluten-free foods. While many functional foods—from whole grain breads to wild salmon—provide obvious health benefits, other functional foods like acai berry or brain development foods may make overly optimistic promises. Thus, it’s important to evaluate each functional food on the basis of scientific evidence before you buy into their benefits";

    [self sentenceToSpeech];
}
@catch (NSException *exception)
{
    NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}

}

- (void)sentenceToSpeech{

@try {

    NSString *sentenceToSpeech = @"";

    if (0 == self.totalCountPlayed) {

        sentenceToSpeech = [self.wordToSpeech substringToIndex:100];
        self.totalCountPlayed = 100;

    }
    else{

        NSString *tempString = [self.wordToSpeech substringFromIndex:self.totalCountPlayed];

        if (100 <= [tempString length]) {

            sentenceToSpeech = [tempString substringToIndex:100];
            self.totalCountPlayed += 100;
        }
        else
        {
            sentenceToSpeech = tempString;
            self.totalCountPlayed = 0;
        }
    }

    [self speechUsingGoogleTTS:sentenceToSpeech];
}
@catch (NSException *exception)
{
    NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}

}

- (void)speechUsingGoogleTTS:(NSString *)sentenceToSpeeh{

@try {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.mp3"];

    NSString *urlString = [NSString stringWithFormat:@"http://www.translate.google.com/translate_tts?tl=en&q=%@",sentenceToSpeeh];
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url] ;
    [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1" forHTTPHeaderField:@"User-Agent"];
    NSURLResponse* response = nil;
    NSError* error = nil;
    NSData* data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
    [data writeToFile:path atomically:YES];


    NSError        *err;
    if ([[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                       [NSURL fileURLWithPath:path] error:&err];

        [self.player prepareToPlay];
        [self.player setNumberOfLoops:0];
        [self.player setDelegate:self];
        [self.player play];

    }
}
@catch (NSException *exception)
{
    NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{

@try {

    if (0 != self.totalCountPlayed) {

        [self sentenceToSpeech];
    }
}
@catch (NSException *exception)
{
    NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}
}

このコードを使用すると、これが機能します。

于 2013-04-30T09:12:17.097 に答える