2010-11-11 87 views
1

我正在使用音频单元进行一些自定义音频后期处理。我有两个文件合并在一起(下面的链接),但是在输出中会出现一些奇怪的噪音。我究竟做错了什么?使用扩展音频文件服务将两个文件混合在一起

我已经验证,在此步骤之前,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

回答

1

原来有在这里两个问题。

缓冲处理代码

int32_t mixed = s1 + s2;是造成剪裁。更好的方法是除以混合的通道数:int32_t mixed = (s1 + s2)/2;然后在另一个通道中稍后进行标准化。

帧!=字节 当归零第二轨道的缓冲区时,声音跑了出去,我被错误地设定偏移量和持续时间不帧字节。这会在缓冲区中产生垃圾,并产生周期性听到的噪音。很容易解决:

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

现在的样本是伟大的:http://cl.ly/1E2q1L441s2b3e2X2z0J

+0

Iam试图做同样的bt不会能够做到这一点,你可以plz帮助我,, – Aadil 2011-11-21 11:53:33

0

您发布的答案看起来不错;我只能看到一个小问题。您的裁剪解决方案,除以两将有所帮助,但它也相当于应用50%的增益减少。那就是不是正常化一样; normalization是查看整个音频文件,查找最高峰值,并应用给定增益降低以使该峰值达到一定水平(通常为0.0dB)的过程。结果是在正常情况下(即非削波)情况下,输出信号会非常低,需要再次提升。

在你的混音过程中,你无疑会遇到一个导致失真的溢出,因为这个值会绕过并导致信号跳跃。你想要做的就是应用一种称为“brick-wall limiter”的技术,该技术基本上将硬天花板应用于剪裁的样本。要做到这一点最简单的方法是:

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

这种技术的结果是,你会听到有点失真的周围被剪裁的样品,但声音不会完全错位的将是整数的情况下,溢出。失真虽然存在,但不会破坏听觉体验。

+2

我明白,我在做什么不是规范化,我在另一个传递中应用规范化,通过按比例扩大每个样本最大峰值达到-0.5db。重读前面的答案,这是不明确的。编辑。 – coneybeare 2010-11-12 15:15:19

+0

根据实施情况,砖墙限制器与裁剪相同。这取决于如何处理溢出。无论如何,这听起来很糟糕。在添加样品之前,我会随着50%的增益减少,然后正常化。 – lucius 2010-11-22 05:13:48

相关问题