2014-02-14 44 views
2

我的應用程序包含用戶錄製短信的功能;我想從記錄的開頭和結尾刪除任何沉默(或者,更確切地說,音量低於給定閾值的任何音頻)。iOS:如何修剪.aif錄音開始和結束時的靜音?

我用AVAudioRecorder錄製音頻,並將其保存到.aif文件。我已經在其他地方看到了一些方法,我可以用它來等待開始錄製,直到音頻電平達到閾值;那會讓我在中途停下來,但無助於修剪沉默。

如果有一個簡單的方法來做到這一點,我會永遠感激!

謝謝。

回答

3

該項目從麥克風中獲取音頻,在安靜時觸發巨大的噪音和非觸發器。它也會在末端修剪和淡入/淡出。

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; 
} 
+0

提供的GitHub示例給出錯誤:由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'*** - [NSURL initFileURLWithPath:]:nil string parameter' –

0

我用AVAudioRecorder錄製音頻,並將其保存到.aif文件。我已經在其他地方看到了一些方法,我可以用它來等待開始錄製,直到音頻電平達到閾值;那會讓我一半在那裏

沒有足夠的緩衝,這將截斷開始。

我不知道一個簡單的方法。錄製和分析所需的開始和結束點後,您將不得不編寫新的音頻文件。如果您知道AIFF格式(不是很多人),並且可以輕鬆讀取文件的示例數據,那麼修改現有文件將很簡單。

分析階段對於基本實現非常簡單 - 評估樣本數據的平均功率,直到超過閾值。結束重複。

+0

感謝您的答覆......我知道音頻處理微乎其微,編程說話。我知道我需要做的是設置一個閾值,然後修剪所有不符合文件正面和背面的音頻。我試圖找出的是如何使用ios sdk和/或任何開源工具實際做到這一點。 – DanM

+0

@DanM您可以使用ExtAudioFile API讀取和寫入音頻文件並將樣本數據轉換爲您所需的格式(用於分析目的)。這不是一個簡單的方法 - 你需要編寫幾百行代碼來完成這個任務。 – justin

相關問題