gpt4 book ai didi

iphone - 录制声音并以改变的音高播放

转载 作者:可可西里 更新时间:2023-11-01 04:44:37 30 4
gpt4 key购买 nike

我必须实现一个 iphone 应用程序,它会在您开始说话时录制用户的声音,并改变录制声音的音高并播放它。在 AVAudiorecorder 的帮助下,我能够在检测到声音时录制音频,并且使用 Dirac 库我改变了录制声音的音高。这种方法的问题是输出声音足够嘈杂。我得到了使用 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");
}
}

最佳答案

  1. 如何检测声音?
    我在教程的帮助下解决了我的第一个问题..这是链接,链接:http://mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/
    在这里,我们可以很容易地理解检测到一些噪音时的声音记录。

  2. 如何改变录制声音的音高并回放。
    我在改变声音的音高时遇到的第二个问题。因为我们只能在 AVAudioRecorder 的帮助下录制声音,所以我们不能通过它来改变音调。
    为此,我使用了一个外部库 DIRACHere is the link for the Dirac library.
    这带来了一些示例项目(适用于移动应用程序和桌面应用程序),关于应用程序的狄拉克库的实现。

我发现另一种解决这个问题的方法是通过 Dirac 实现 Cocoas2D、Cocos Denshion。因为上述过程不适用于我的应用程序。 Here is the link for implementing this (关于改变音高和播放录制声音的示例项目)。

我找到了 another link与录音有关。

关于iphone - 录制声音并以改变的音高播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10549317/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com