我必须实现一个iphone应用程序,它将记录用户的声音,当你开始说话,并改变所记录的声音的音高,并播放它。我能够在AVAudiorecorder的帮助下录制声音检测上的音频,并使用狄拉克库我已经改变了录制的声音的音高。这种方法的问题是输出的声音足够嘈杂。我得到了使用SoundEngine的响应,但我没有得到实现它的方法。有没有人能给我解释一下实现这个功能的其他方法?
my code//
-(void)initialSetup
{
count=0;
silenceTime=0;
//[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat: @"%.0f.%@",[NSDate timeIntervalSinceReferenceDate]*1000.0, @"caf"]]];
recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
//recorder = [[ AVAudioRecorder alloc] init];
[recorder setDelegate:self];
[recorder updateMeters];
[recorder prepareToRecord];
//[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
//In Order To Move Sound To The Speaker
//UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
//AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof(audioRouteOverride),&audioRouteOverride);
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"audio.caf"];
recordedTmpFile1 = [NSURL fileURLWithPath:soundFilePath];
recordSetting1 = [[NSMutableDictionary alloc] init];
recordSetting1 = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],AVEncoderAudioQualityKey,
//[NSNumber numberWithInt:kAudioFormatAppleIMA4],AVFormatIDKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,nil];
recorder1 = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile1 settings:recordSetting1 error:&error];
[recorder1 prepareToRecord];
[recorder1 setDelegate:self];
if(recorder)
{
recorder.meteringEnabled = YES;
[recorder record];
double val=[recorder peakPowerForChannel:0];
NSLog(@"The Very First Value Of The Recorder Is=%f",val);
levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.4 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
}
else
{
NSLog(@"error in initilising of the recorder=%@",[error localizedDescription]);
}
}
-(void)levelTimerCallback:(NSTimer *)timer
{
[recorder updateMeters];
const double ALPHA = 0.05;
//NOISE FILERATION ALGORITHMS
double peakPowerForChannel = pow(10,(0.05 *[recorder peakPowerForChannel:0]));
double audioMonitorResults1 = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * audioMonitorResults1;
double audioMonitorResults;
audioMonitorResults= [recorder peakPowerForChannel:0];
NSLog(@"This time only frequency is==>%f",audioMonitorResults1);
//if (audioMonitorResults1 >0.020)
if(audioMonitorResults1 > .05)//the value of audioMonitorResults may be equal to -10 for device
{
[recorder1 updateMeters];
recorder1.meteringEnabled=YES;
//recorder.meteringEnabled=YES;
[recorder1 record];
NSLog(@"SOUND IS DETECTED");
NSLog(@"%f",[recorder1 peakPowerForChannel:0]);
NSLog(@"Recording is going on");
count=1;
silenceTime=0;
}
else
{
NSLog(@"NO SOUND IS DETECTED");
silenceTime=silenceTime+0.3;
if(count==1 && silenceTime>1)
{
[levelTimer invalidate];
[recorder1 stop];
}
}
}
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
NSLog(@"Recorder stop delegate is processing");
if(flag)
{
NSLog(@"Player has finished successfully");
[self playRecording];
}
else
{
NSLog(@"problem in recording.......not recorded");
}
}发布于 2012-06-26 16:35:49
我在tutorial..here的帮助下解决了我的第一个问题,链接是:http://mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/
这里我们可以很容易地理解录音上的一些噪音的检测。
我在改变音高时遇到的第二个问题。因为我们只能在AVAudioRecorder的帮助下录制声音,所以我们不能用它改变音高。
为此,我使用了一个外部库DIRAC。Here is the link for the Dirac library.
这里提供了一些示例项目(针对移动应用程序和桌面应用程序),介绍了如何将dirac库实现到应用程序中。
我找到了另一种通过实现Cocoas2D来解决这个问题的方法,Cocos Denshion和Dirac。因为上面的过程在我的应用程序中不能很好地工作。Here is the link for implementing this (一个关于改变音高和播放录制声音的示例项目)。
我找到了与录音相关的another link。
发布于 2013-06-07 23:06:36
实际上,有一个更简单的解决方案。使用音频播放器的速率功能。它的范围在0.1f - 2.0f之间,其中较高的数字意味着更快、更紧凑的音高,而较低的数字意味着缓慢的拖拽声音(低沉的声音)
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:path] error:&err];
player.volume = 0.4f;
player.enableRate=YES;
[player prepareToPlay];
[player setNumberOfLoops:0];
player.rate=2.0f;
[player play];https://stackoverflow.com/questions/10549317
复制相似问题