录制声音并以改变的音高播放 [英] Record the sound and play it back with changed pitch

查看:125
本文介绍了录制声音并以改变的音高播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现一个iphone应用程序,它会在你开始说话时记录用户的声音,并改变录制声音的音高并播放。我能够在AVAudiorecorder的帮助下录制声音检测音频,并使用Dirac库我改变了录制声音的音高。这种方法的问题是输出声音足够嘈杂。我得到了使用SoundEngine的响应,但我没有得到实现它的方法。任何人都可以解释一下其他任何实现方法吗?

I have to implement a iphone application which will record user's voice as you starts speaking, and change the pitch of the recorded sound and play it back. I am able to record the audio on the detection of the sound by the help of AVAudiorecorder, and using Dirac library I have changed the pitch of recorded sound. The problem with this approach is that the output sound is noisy enough. I got the response for using SoundEngine but I didn't get the way to implement it. Can anyone please explain me about any other way to implement this?

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-the-the-mic /

    在这里,我们可以很容易地理解在检测到一些噪音时的声音录音。

  1. How to detect the sound?
    I solved my first problem with the help of a tutorial..here is the link for this, link: http://mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/
    Here we can easily understand the recording of the sound on detection of some noise.

如何改变录制声音的音高并播放。

我在改变声音音高时面临的第二个问题。由于我们只能在 AVAudioRecorder 的帮助下录制声音,我们无法通过它来改变音调。

为此,我使用了一个外部库 DIRAC
这是Dirac图书馆的链接。

这就是一些示例项目(针对移动应用程序以及桌面应用程序)关于应用程序的dirac库的实现。

How to change the pitch of the recorded sound and play back.
Second problem I faced in changing the pitch of the sound. As we can only record the sound with the help of AVAudioRecorder we can’t alter the pitch by that.
For this purpose I used an external library DIRAC. Here is the link for the Dirac library.
This comes up with some sample project (for mobile apps as well as desktop apps)about the implementation of the dirac library to the application.

我通过实施Cocoas2D,Cocos Denshion和Dirac来解决这个问题的另一种方法。因为上述程序与我的应用程序不兼容。
这是实现此目的的链接(一个关于改变音高和游戏的示例项目录制的声音。)

Another way I found to solve this issue by implementing Cocoas2D, Cocos Denshion with Dirac. Because the above proess was not working well with my application. Here is the link for implementing this (a sample project about the changing the pitch and play the recorded sound).

我找到了其他链接与录音有关。

这篇关于录制声音并以改变的音高播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆