iOS:如何修剪.aif录音的开始和结束的沉默?

我的应用程序包括用户录制短信的function; 我想从logging的开始和结束中删除任何沉默(或者,更确切地说,音量低于给定阈值的任何audio)。

我正在使用AVAudioRecorder录制audio,并将其保存为.aif文件。 我在其他地方已经看到了一些方法,我可以等待开始logging,直到audio电平达到阈值; 那会让我在一半的地方,但不会有助于修剪沉默。

如果有一个简单的方法来做到这一点,我将永远感激!

谢谢。

这个项目从麦克风获取audio,在安静的时候触发巨大的噪音和非触发。 它也会在末端修剪和淡入淡出。

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录制audio,并将其保存为.aif文件。 我在其他地方已经看到了一些方法,我可以等待开始logging,直到audio电平达到阈值; 那会让我走到一半

没有足够的缓冲,这将截断开始。

我不知道一个简单的方法。 录制和分析所需的起点和终点后,您将不得不编写一个新的audio文件。 如果您知道AIFF格式(不是很多人),并且有一个简单的方法来读取文件的样本数据,那么修改现有的文件将会很简单。

分析阶段对于基本实现来说非常简单 – 评估样本数据的平均功率,直到超过阈值。 重复相反的结束。