我的應用程序包含用戶錄製短信的功能;我想從記錄的開頭和結尾刪除任何沉默(或者,更確切地說,音量低於給定閾值的任何音頻)。iOS:如何修剪.aif錄音開始和結束時的靜音?
我用AVAudioRecorder錄製音頻,並將其保存到.aif文件。我已經在其他地方看到了一些方法,我可以用它來等待開始錄製,直到音頻電平達到閾值;那會讓我在中途停下來,但無助於修剪沉默。
如果有一個簡單的方法來做到這一點,我會永遠感激!
謝謝。
我的應用程序包含用戶錄製短信的功能;我想從記錄的開頭和結尾刪除任何沉默(或者,更確切地說,音量低於給定閾值的任何音頻)。iOS:如何修剪.aif錄音開始和結束時的靜音?
我用AVAudioRecorder錄製音頻,並將其保存到.aif文件。我已經在其他地方看到了一些方法,我可以用它來等待開始錄製,直到音頻電平達到閾值;那會讓我在中途停下來,但無助於修剪沉默。
如果有一個簡單的方法來做到這一點,我會永遠感激!
謝謝。
該項目從麥克風中獲取音頻,在安靜時觸發巨大的噪音和非觸發器。它也會在末端修剪和淡入/淡出。
https://github.com/fulldecent/FDSoundActivatedRecorder
相關的代碼你正在尋找:
- (NSString *)recordedFilePath
{
// Prepare output
NSString *trimmedAudioFileBaseName = [NSString stringWithFormat:@"recordingConverted%x.caf", arc4random()];
NSString *trimmedAudioFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:trimmedAudioFileBaseName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:trimmedAudioFilePath]) {
NSError *error;
if ([fileManager removeItemAtPath:trimmedAudioFilePath error:&error] == NO) {
NSLog(@"removeItemAtPath %@ error:%@", trimmedAudioFilePath, error);
}
}
NSLog(@"Saving to %@", trimmedAudioFilePath);
AVAsset *avAsset = [AVAsset assetWithURL:self.audioRecorder.url];
NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio];
AVAssetTrack *track = [tracks objectAtIndex:0];
AVAssetExportSession *exportSession = [AVAssetExportSession
exportSessionWithAsset:avAsset
presetName:AVAssetExportPresetAppleM4A];
// create trim time range
CMTime startTime = CMTimeMake(self.recordingBeginTime*SAVING_SAMPLES_PER_SECOND, SAVING_SAMPLES_PER_SECOND);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, kCMTimePositiveInfinity);
// create fade in time range
CMTime startFadeInTime = startTime;
CMTime endFadeInTime = CMTimeMake(self.recordingBeginTime*SAVING_SAMPLES_PER_SECOND + RISE_TRIGGER_INTERVALS*INTERVAL_SECONDS*SAVING_SAMPLES_PER_SECOND, SAVING_SAMPLES_PER_SECOND);
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:trimmedAudioFilePath];
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.timeRange = exportTimeRange;
exportSession.audioMix = exportAudioMix;
// 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);
if (AVAssetExportSessionStatusCompleted == exportSession.status) {
NSLog(@"AVAssetExportSessionStatusCompleted");
return trimmedAudioFilePath;
} 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);
}
return nil;
}
我用AVAudioRecorder錄製音頻,並將其保存到.aif文件。我已經在其他地方看到了一些方法,我可以用它來等待開始錄製,直到音頻電平達到閾值;那會讓我一半在那裏
沒有足夠的緩衝,這將截斷開始。
我不知道一個簡單的方法。錄製和分析所需的開始和結束點後,您將不得不編寫新的音頻文件。如果您知道AIFF格式(不是很多人),並且可以輕鬆讀取文件的示例數據,那麼修改現有文件將很簡單。
分析階段對於基本實現非常簡單 - 評估樣本數據的平均功率,直到超過閾值。結束重複。
提供的GitHub示例給出錯誤:由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'*** - [NSURL initFileURLWithPath:]:nil string parameter' –