简体   繁体   English

iOS:用于记录网络传输的语音编码的音频格式

[英]iOS: Audio format for recording voice encoding for network transfer

I am looking for a good audio format to save voice recordings locally and for transport over the network. 我正在寻找一种良好的音频格式,以便在本地保存录音并通过网络传输。 Requirements are: 要求是:

  • Decent quality. 体面的品质。 These clips, when received will be listened to many times 接收到的这些片段将被多次收听
  • Workflow should support trimming and fading before transport 工作流程应在运输前支持修剪和褪色
  • Decent file size 体面的文件大小

This is my current approach for recording: 这是我目前的录音方法:

// SEE IMA4 vs M4A http://stackoverflow.com/questions/3509921/recorder-works-on-iphone-3gs-but-not-on-iphone-3g
NSDictionary *recordSettings =
[[NSDictionary alloc] initWithObjectsAndKeys:
 [NSNumber numberWithFloat: 11025],               AVSampleRateKey,
 [NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey,
 [NSNumber numberWithInt: 1],                     AVNumberOfChannelsKey,
 [NSNumber numberWithBool:NO],                    AVLinearPCMIsFloatKey,
 [NSNumber numberWithInt: AVAudioQualityMax],     AVEncoderAudioQualityKey,
 nil];

NSError *error = nil;
self.audioRecorder = [[ AVAudioRecorder alloc] initWithURL:self.recordingFile settings:recordSettings error:&error];

And approach for encoding: 和编码方法:

NSString *file = [NSString stringWithFormat:@"recordingConverted%x.caf", arc4random()];
self.filePath = [NSTemporaryDirectory() stringByAppendingPathComponent: file];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:self.filePath]) {
    NSError *error;
    if ([fileManager removeItemAtPath:self.filePath error:&error] == NO) {
        NSLog(@"removeItemAtPath %@ error:%@", self.filePath, error);
    }
}
NSLog(@"IN: %@", self.recordingFile);
NSLog(@"OUT: %@", self.filePath);

AVAsset *avAsset = [AVAsset assetWithURL:self.recordingFile];

// get the first audio track
NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio];
if ([tracks count] == 0) return nil;
AVAssetTrack *track = [tracks objectAtIndex:0];

// create the export session
// no need for a retain here, the session will be retained by the
// completion handler since it is referenced there
AVAssetExportSession *exportSession = [AVAssetExportSession
                                       exportSessionWithAsset:avAsset
                                       presetName:AVAssetExportPresetAppleM4A];
if (nil == exportSession) return nil;

// create trim time range
CMTime startTime = CMTimeMake(self.speakingBeginTime*44100, 44100);
CMTime stopTime = CMTimeMake((self.speakingBeginTime+[self.duration doubleValue])*44100, 44100);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

// create fade in time range
CMTime startFadeInTime = startTime;
CMTime endFadeInTime = CMTimeMake((self.speakingBeginTime+RECORDING_INTERVAL)*1.5*44100, 44100);
CMTimeRange fadeInTimeRange = CMTimeRangeFromTimeToTime(startFadeInTime,
                                                        endFadeInTime);

// setup audio mix
AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];
AVMutableAudioMixInputParameters *exportAudioMixInputParameters =
[AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];

[exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0
                                                  timeRange:fadeInTimeRange]; 
exportAudioMix.inputParameters = [NSArray
                                  arrayWithObject:exportAudioMixInputParameters]; 

// configure export session  output with all our parameters
exportSession.outputURL = [NSURL fileURLWithPath:self.filePath]; // output path
exportSession.outputFileType = AVFileTypeAppleM4A; // output file type
exportSession.timeRange = exportTimeRange; // trim time range
exportSession.audioMix = exportAudioMix; // fade in audio mix

// MAKE THE EXPORT SYNCHRONOUS
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);

if (AVAssetExportSessionStatusCompleted == exportSession.status) {
    return self.filePath;
    //NSLog(@"AVAssetExportSessionStatusCompleted");
} else if (AVAssetExportSessionStatusFailed == exportSession.status) {
    // a failure may happen because of an event out of your control
    // for example, an interruption like a phone call comming in
    // make sure and handle this case appropriately
    NSLog(@"AVAssetExportSessionStatusFailed %@", exportSession.error.localizedDescription);
} else {
    NSLog(@"Export Session Status: %d", exportSession.status);
}

Currently, performance on a 3 second audio clip is: 62,228 bytes for PCM and 36,654 bytes for encoded. 目前,3秒音频片段的性能为:PCM为62,228字节,编码为36,654字节。 Seems like I could do better. 好像我可以做得更好。

I have found the guide here: 我在这里找到了指南:

http://gamua.com/blog/2010/06/sound-on-ios-best-practices/ http://gamua.com/blog/2010/06/sound-on-ios-best-practices/

to be helpful in picking sound formats (especially the comments) 有助于选择声音格式(尤其是评论)

There are also some good examples here: 这里也有一些很好的例子:

How do I record audio on iPhone with AVAudioRecorder? 如何使用AVAudioRecorder在iPhone上录制音频?

especially the example with various formats for export, and this answer: 特别是有各种格式的导出示例,这个答案:

https://stackoverflow.com/a/3870385/2214106 https://stackoverflow.com/a/3870385/2214106

which reduced file size significantly. 这大大减少了文件大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM