1

オーディオ ユニットを使用してカスタム オーディオ後処理を行っています。2 つのファイルをマージしていますが (以下のリンク)、出力に奇妙なノイズが発生しています。私は何を間違っていますか?

このステップの前に、2 つのファイル (workTrack1workTrack2) が適切な状態にあり、良好なサウンドであることを確認しました。プロセスでもエラーは発生しません。

バッファ処理コード:

- (BOOL)mixBuffersWithBuffer1:(const int16_t *)buffer1 buffer2:(const int16_t *)buffer2 outBuffer:(int16_t *)mixbuffer outBufferNumSamples:(int)mixbufferNumSamples {
    BOOL clipping = NO;

    for (int i = 0 ; i < mixbufferNumSamples; i++) {
        int32_t s1 = buffer1[i];
        int32_t s2 = buffer2[i];
        int32_t mixed = s1 + s2;

        if ((mixed < -32768) || (mixed > 32767)) {
            clipping = YES; // don't break here because we dont want to lose data, only to warn the user
        }

        mixbuffer[i] = (int16_t) mixed;
    }
    return clipping;
}

ミックスダウン コード:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////      PHASE 4      ////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// In phase 4, open workTrack1 and workTrack2 for reading,
// mix together, and write out to outfile.

// open the outfile for writing -- this will erase the infile if they are the same, but its ok cause we are done with it
err = [self openExtAudioFileForWriting:outPath audioFileRefPtr:&outputAudioFileRef numChannels:numChannels];
if (err) { [self cleanupInBuffer1:inBuffer1 inBuffer2:inBuffer2 outBuffer:outBuffer err:err]; return NO; }

// setup vars
framesRead = 0;
totalFrames = [self totalFrames:mixAudioFile1Ref]; // the long one.
NSLog(@"Mix-down phase, %d frames (%0.2f secs)", totalFrames, totalFrames / RECORD_SAMPLES_PER_SECOND);

moreToProcess = YES;
while (moreToProcess) {

    conversionBuffer1.mBuffers[0].mDataByteSize = LOOPER_BUFFER_SIZE;
    conversionBuffer2.mBuffers[0].mDataByteSize = LOOPER_BUFFER_SIZE;

    UInt32 frameCount1 = framesInBuffer;
    UInt32 frameCount2 = framesInBuffer;

    // Read a buffer of input samples up to AND INCLUDING totalFrames
    int numFramesRemaining = totalFrames - framesRead; // Todo see if we are off by 1 here.  Might have to add 1
    if (numFramesRemaining == 0) {
        moreToProcess = NO; // If no frames are to be read, then this phase is finished

    } else {
        if (numFramesRemaining < frameCount1) { // see if we are near the end
            frameCount1 = numFramesRemaining;
            frameCount2 = numFramesRemaining;
            conversionBuffer1.mBuffers[0].mDataByteSize = (frameCount1 * bytesPerFrame);
            conversionBuffer2.mBuffers[0].mDataByteSize = (frameCount2 * bytesPerFrame);
        }

        NSbugLog(@"Attempting to read %d frames from mixAudioFile1Ref", (int)frameCount1);
        err = ExtAudioFileRead(mixAudioFile1Ref, &frameCount1, &conversionBuffer1);
        if (err) { [self cleanupInBuffer1:inBuffer1 inBuffer2:inBuffer2 outBuffer:outBuffer err:err]; return NO; }

        NSLog(@"Attempting to read %d frames from mixAudioFile2Ref", (int)frameCount2);
        err = ExtAudioFileRead(mixAudioFile2Ref, &frameCount2, &conversionBuffer2);
        if (err) { [self cleanupInBuffer1:inBuffer1 inBuffer2:inBuffer2 outBuffer:outBuffer err:err]; return NO; }

        NSLog(@"Read %d frames from mixAudioFile1Ref in mix-down phase", (int)frameCount1);
        NSLog(@"Read %d frames from mixAudioFile2Ref in mix-down phase", (int)frameCount2);

        // If no frames were returned, phase is finished
        if (frameCount1 == 0) {
            moreToProcess = NO;

        } else { // Process pcm data

            // if buffer2 was not filled, fill with zeros
            if (frameCount2 < frameCount1) {
                bzero(inBuffer2 + frameCount2, (frameCount1 - frameCount2));
                frameCount2 = frameCount1;
            }

            const int numSamples = (frameCount1 * bytesPerFrame) / sizeof(int16_t);

            if ([self mixBuffersWithBuffer1:(const int16_t *)inBuffer1
                                    buffer2:(const int16_t *)inBuffer2
                                  outBuffer:(int16_t *)outBuffer
                        outBufferNumSamples:numSamples]) {
                NSLog(@"Clipping");
            }
            // Write pcm data to the main output file
            conversionOutBuffer.mBuffers[0].mDataByteSize = (frameCount1 * bytesPerFrame);
            err = ExtAudioFileWrite(outputAudioFileRef, frameCount1, &conversionOutBuffer);

            framesRead += frameCount1;
        } // frame count
    } // else

    if (err) {
        moreToProcess = NO;
    }
} // while moreToProcess

// Check for errors
TTDASSERT(framesRead == totalFrames);
if (err) {
    if (error) *error = [NSError errorWithDomain:kUAAudioSelfCrossFaderErrorDomain
                                            code:UAAudioSelfCrossFaderErrorTypeMixDown
                                        userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:err],@"Underlying Error Code",[self commonExtAudioResultCode:err],@"Underlying Error Name",nil]];
    [self cleanupInBuffer1:inBuffer1 inBuffer2:inBuffer2 outBuffer:outBuffer err:err];
    return NO;
}
NSLog(@"Done with mix-down phase");


仮定

  • mixAudioFile1Ref常により長いmixAudioFile2Ref
  • mixAudioFile2Refがバイトを使い果たした後、は次のようにoutputAudioFileRef聞こえるはずです。mixAudioFile2Ref

予想されるサウンドは、トラックがループされたときに自己クロスフェードを生成するために、最初にフェードアウトの上にフェードインをミックスすることになっています。出力を聞いて、コードを見て、どこが間違っているか教えてください。

元のトーン音: http://cl.ly/2g2F2A3k1r3S36210V23
結果のトーン音: http://cl.ly/3q2w3S3Y0x0M3i2a1W3v

4

2 に答える 2

1

ここには 2 つの問題があったことがわかりました。

バッファ処理コード

int32_t mixed = s1 + s2;クリッピングを起こしていました。より良い方法は、混合されたチャネルの数で割り、int32_t mixed = (s1 + s2)/2;後で別のパスで正規化することです。

フレーム != バイト サウンドがなくなったときに 2 番目のトラックのバッファーをゼロにするときに、オフセットとデュレーションをバイトではなくフレームとして誤って設定していました。これにより、バッファにガベージが生成され、定期的に聞こえるノイズが発生しました。修正が簡単:

if (frameCount2 < frameCount1) {
    bzero(inBuffer2 + (frameCount2 * bytesPerFrame), (frameCount1 - frameCount2) * bytesPerFrame);
    frameCount2 = frameCount1;
}

サンプルは素晴らしいです: http://cl.ly/1E2q1L441s2b3e2X2z0J

于 2010-11-11T23:59:07.880 に答える
0

投稿された回答は良さそうです。小さな問題が 1 つだけ見られます。クリッピングのソリューションである 2 で割ることは役に立ちますが、50% のゲイン リダクションを適用するのと同じです。これは正規化と同じではありません。ノーマライゼーションとは、オーディオ ファイル全体を調べて最高のピークを見つけ、特定のゲイン リダクションを適用して、このピークが特定のレベル (通常は 0.0dB) になるようにするプロセスです。その結果、通常の (つまり、クリッピングのない) 状況では、出力信号が非常に低くなり、再度ブーストする必要があります。

ミックスダウン中に、オーバーフローが発生して歪みが発生したことは間違いありません。値がラップアラウンドして信号にジャンプが発生するからです。代わりにやりたいことは、「レンガ壁リミッター」と呼ばれるテクニックを適用することです。これは、基本的に、クリッピングしているサンプルにハード シーリングを適用します。これを行う最も簡単な方法は次のとおりです。

int32_t mixed = s1 + s2;
if(mixed >= 32767) {
  mixed = 32767;
}
else if(mixed <= -32767) {
  mixed = -32767;
}

この手法の結果、クリッピングしているサンプルの周りで少し歪みが聞こえますが、整数オーバーフローの場合のようにサウンドが完全に壊れることはありません。歪みは存在しますが、リスニング体験を損なうことはありません。

于 2010-11-12T08:44:18.040 に答える