代码之家  ›  专栏  ›  技术社区  ›  Babul

当我们录制音频时,如何将音频录制设置更改为16Khz和16位?

  •  2
  • Babul  · 技术社区  · 13 年前

    我的设置如下所示。

    我想在录制音频时将音频录制设置更改为16Khz和16位。

    NSArray *dirPaths;
    NSString *docsDir;
    
    dirPaths = NSSearchPathForDirectoriesInDomains(
                                                   NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *soundFilePath = [docsDir
                               stringByAppendingPathComponent:@"sound.wav"];
    
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
    
    NSDictionary *recordSettings = [NSDictionary
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16],
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt: 2],
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:44100.0],
                                    AVSampleRateKey,
                                    nil];
    
    NSError *error = nil;
    
    audioRecorder = [[AVAudioRecorder alloc]
                     initWithURL:soundFileURL
                     settings:recordSettings
                     error:&error];
    
    if (error)
    {
    
    } else
    {
        [audioRecorder prepareToRecord];
    }
    

    如何设置这些设置?

    编辑问题 :

    感谢您的回复,我尝试了这些方法,但对我来说不起作用,因为我的客户正在将录制的语音(我以字节格式发送的录制语音)发送到ASR引擎(自动语音识别)。我没有收到我发送的相同的回复(我收到的回复音频上写着“引号”)。客户说你没有以16KHz和16位采样率录制语音,这就是为什么你会得到这样的回应。但我问他我发送给他的服务器的字节数,他给出了.wav文件,它播放得很好。但是,如果他发送给ASR引擎的是同一个,ASR引擎不会接受我发送的录音(他说ASR不会接受,因为你没有以16KHz和16位采样率录制音频)。客户给出了以下回应。(但是,我尝试了你建议的所有方法)

    Filename:   sv_SE_356985580762248932.wav
    Folder: E:\developApp\TestappName\Mortionsn_dev\2nd-iteration\test_wfiles
    File Type:  44100Hz, 16-bit, Stereo
    Uncompressed Size:  1.63 MB (1,713,696 bytes)
    File Format:    Windows PCM
    Windows PCM
    Size on Disk:   1.63 MB (1,717,892 bytes)
    Last Written (local):   3/11/2013  00:21:00.000
    Length: 0:09.714
    428,424 samples
    

    使用以下答案第二次编辑问题 :

    后来,通过给出建议,我将设置代码更改为:

    NSMutableDictionary *recordSettings = [NSMutableDictionary dictionary];
    
    
    [recordSettings setValue: [NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    
    [recordSettings setValue: [NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];//8000.0
    
    [recordSettings setValue: [NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
    
    [recordSettings setValue: [NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    
    [recordSettings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    
    [recordSettings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
    
    [recordSettings setValue: [NSNumber numberWithInt: AVAudioQualityMax] forKey:AVEncoderAudioQualityKey];
    
    2 回复  |  直到 13 年前
        1
  •  3
  •   Vedchi    13 年前

    试试这个, 常规音频设置为,

    AVFormatIDKey,
    AVSampleRateKey,
    AVNumberOfChannelsKey.
    

    对于录音机

    AVEncoderAudioQualityKey;
    AVEncoderBitRateKey;
    AVEncoderBitRatePerChannelKey;
    AVEncoderBitDepthHintKey;
    

    请确保已包含常规设置和记录器设置。

    并更改您的 AVSampleRateKey 16000.0 ,

    NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:                 
                                    [NSNumber numberWithInt:kAudioFormatLinearPCM],
                                    AVFormatIDKey
                                    [NSNumber numberWithInt: 2],
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:16000.0],
                                    AVSampleRateKey,
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16],
                                    AVEncoderBitRateKey,
                                    nil];
    
        2
  •  2
  •   Robert Mars_win    13 年前

    您现有的设置是44.1kHz和16位,因此(假设上面的设置已经有效)您需要更改的唯一线路是:

    [NSNumber numberWithFloat:44100.0] 
    

    收件人:

    [NSNumber numberWithFloat:16000.0]