Showing posts with label IPhone - development - Audio. Show all posts
Showing posts with label IPhone - development - Audio. Show all posts

Monday, August 16, 2010

Text to speech using VSSpeechSynthesizer

The VSSpeechSynthesizer is the text to speech API in the private VoiceServices framework for IPhone OS 4.0. Appearently, it only available for IPhone 3GS or above. As the VoiceServices is not a public framework, application that is dependent on it may not be allow to publish to Apple appstore.

It is quite a powerful API for text to speech, however it is not very well documented as it is in a private framework. To test out the API follow the following steps:

1. Create the VSSpeechSynthesizer.h file and add it into the project.

Code:
#import  <foundation/foundation.h>

@interface VSSpeechSynthesizer : NSObject
{
}

+ (id)availableLanguageCodes;
+ (BOOL)isSystemSpeaking;
- (id)startSpeakingString:(id)string;
- (id)startSpeakingString:(id)string toURL:(id)url;
- (id)startSpeakingString:(id)string toURL:(id)url withLanguageCode:(id)code;
- (float)rate;             // default rate: 1
- (id)setRate:(float)rate;
- (float)pitch;           // default pitch: 0.5
- (id)setPitch:(float)pitch;
- (float)volume;       // default volume: 0.8
- (id)setVolume:(float)volume;
@end

2. Create and initiate the VSSpeechSynthesizer object in the appropiate area of the code.

Code:
VSSpeechSynthesizer *speech = [[NSClassFromString(@"VSSpeechSynthesizer") alloc] init];

3. Adjust the pitch, rate or volume accordingly.

Eg:
[speech setRate:(float)1.0];

4. Create the speech in the appropiate area of the code.

Eg:
[speech startSpeakingString:@"Hello world, how are you"];

5. Include the private VoiceServices into the frameworks

Eg:

Depending on where you install the IPhone SDK, the location of the VoiceServices framework may varies. Usually the VoiceServices framework is in the following directory:
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk
/System/Library/PrivateFrameworks/VoiceServices.framework/VoiceServices

*Note: The VoiceServies framework only available in the iPhoneOS platform, hence it can only build and run on the actual iPhone device. It can not be run and test on the simulator.







Sample code:

Friday, June 11, 2010

Audio and Sound

SystemSound API
  • For short sounds only, less than 5 sec
  • for .caf .aif or .wav only
  • No control and immediate playback
      Steps to coding
        1. Register the sound
        2. Play the sound
        3. Optionally can get call back when finished

      Eg:
              NSURL *url = [[NSBundle mainBundle] pathForResource: @"audiofilename" ofType: @"ext"];  
              SystemSoundID id;
              AudioServicesCreateSystemSoundID((CFURLRef)url, &id);
              AudioServicesPlaySystemSound(id);   // to play the sound file

AVAudioPlayer Class
  • High level simple API for playing back long sound (>5sec)
  • Support play, stop, loop, pause, seek
  • Support many more sound formats
     Steps to coding
  • Include the AVFoundation header into the .h file. Eg:
             #import <AVFoundation/AVFoundation.h>

Create AVAudioPlayer from file URL or data, Eg:
             AVAudioPlayer *player;
             NSString *path = [[NSBundle mainBundle] pathForResource: @"audiofilename" ofType: @"ext"];
             NSURL *url = [NSURL fileURLWithPath: path];
             player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error: NULL];

  • Start or stop player as needed. Eg:
             if   (!player.playing) {
                   [player play];
             } else {
                   [player pause];
             }

  • Add AVAudionFoundation Frameworks objects code to the project. Eg:
             In Xcode, under Groups and file Section, Under Frameworks, add ->
            Exsisting Frameworks. Select the AVaudioFoundation.framework to add to the
            Frameworks section.