5

私のアプリには、ユーザーが短いメッセージを録音する機能が含まれています。録音の最初と最後から無音 (または、より正確には、ボリュームが特定のしきい値を下回るオーディオ) を削除したいと思います。

AVAudioRecorder でオーディオを録音し、.aif ファイルに保存しています。オーディオ レベルがしきい値に達するまで録音の開始を待機させる方法について、他の場所で言及されているのを見たことがあります。それは私を途中まで連れて行きますが、最後から沈黙を切り取るのには役立ちません.

これを行う簡単な方法があれば、私は永遠に感謝します!

ありがとう。

4

2 に答える 2

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;
}
于 2015-03-01T06:37:26.937 に答える
0

AVAudioRecorder でオーディオを録音し、.aif ファイルに保存しています。オーディオ レベルがしきい値に達するまで録音の開始を待機させる方法について、他の場所で言及されているのを見たことがあります。それは私をそこに連れて行くだろう

適切なバッファリングがないと、開始が切り捨てられます。

簡単な方法を知りません。目的の開始点と終了点を記録して分析した後、新しいオーディオ ファイルを作成する必要があります。AIFF 形式をよく知っていて (知っている人は多くありません)、ファイルのサンプル データを読み取る簡単な方法があれば、既存のファイルを変更するのは簡単です。

分析段階は、基本的な実装では非常に簡単です。しきい値を超えるまで、サンプル データの平均パワーを評価します。最後まで逆に繰り返します。

于 2014-02-14T18:15:20.820 に答える