Question

I'm looking to reduce the amplitude of ExtAudioFileRead's output before resaving it to file. Here's my original code:

    AudioBufferList convertedData;
    convertedData.mNumberBuffers=1;
    convertedData.mBuffers[0].mNumberChannels=mySettings->outputFormat.mChannelsPerFrame;
    convertedData.mBuffers[0].mDataByteSize=outputBufferSize;
    convertedData.mBuffers[0].mData=outputBuffer;
    frameCount=packetsPerBuffer;
    CheckError(ExtAudioFileRead(mySettings->inputFile,
                                &frameCount,
                                &convertedData)

I'd thought to loop through convertedData.mBuffers[0].mData, multiplying each sample by a fraction, but it's of type "void." Any ideas would be terrific, thanks for reading!

EDIT:

Here's the path I'm going down now:

UInt8 *littleBuffer=(UInt8*)malloc(sizeof(UInt8)*outputBufferSize);
Float32 *signalData=(Float32*)convertedData.mBuffers[0].mData;
for (int i=0;i<outputBufferSize;i++){
  Float32 frameData=signalData[i];
  littleBuffer[i]=0.5*(UInt8)frameData;
}

The output file is definitely not a scaled version of the input, but I think this may be due to incorrect variable choices above.

Was it helpful?

Solution

Got it. By replacing littleBuffer[i]=0.5*(UInt8)frameData; with the code below, the audio scales as expected.

float frameData=signalData[i];
if (frameData>127){
    frameData=255-frameData;
    frameData*=scaleFactor;
    frameData=255-frameData;
}else{
    frameData*=scaleFactor;
}
littleBuffer[i]=(int)frameData;

Admittedly, there's some noise after the scaling—my best guess is that it's quantization noise related to the low bit-rate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top