Reply To: AirPlay problem, sample rate?

Home Forums OpenEars AirPlay problem, sample rate? Reply To: AirPlay problem, sample rate?

#1017014
ransomweaver
Participant

Hi,

Thought as much. Actually I am doing an amplification of the file (using a method from a stack overflow thread you directed me to some time ago. In that method (which uses ExtAudioFile api) it reads a wav file, including the sample rate, then sets a AudioStreamBasicDescription object with the sample rate to indicate the format for the returned samples.  If I set that to 44100, the audio will actually sound fine on Airplay BUT the file is not complete; there is less than 1/2 the audio.

i wonder if I could add in an interpolation into this method to raise the samples up to the number that should be in a 44.1khz file.

Here’s the full method. Probably it involves doing this in the loop over the buffers. Any thoughts?

<code>

void ScaleAudioFileAmplitude(NSURL *theURL, float ampScale) {

OSStatus err = noErr;

ExtAudioFileRef audiofile;

ExtAudioFileOpenURL((CFURLRef)theURL, &audiofile);

assert(audiofile);

// get some info about the file’s format.

AudioStreamBasicDescription fileFormat;

UInt32 size = sizeof(fileFormat);

err = ExtAudioFileGetProperty(audiofile, kExtAudioFileProperty_FileDataFormat, &size, &fileFormat);

// we’ll need to know what type of file it is later when we write

AudioFileID aFile;

size = sizeof(aFile);

err = ExtAudioFileGetProperty(audiofile, kExtAudioFileProperty_AudioFile, &size, &aFile);

AudioFileTypeID fileType;

size = sizeof(fileType);

err = AudioFileGetProperty(aFile, kAudioFilePropertyFileFormat, &size, &fileType);

// tell the ExtAudioFile API what format we want samples back in

AudioStreamBasicDescription clientFormat;

bzero(&clientFormat, sizeof(clientFormat));

clientFormat.mChannelsPerFrame = fileFormat.mChannelsPerFrame;

clientFormat.mBytesPerFrame = 4;

clientFormat.mBytesPerPacket = clientFormat.mBytesPerFrame;

clientFormat.mFramesPerPacket = 1;

clientFormat.mBitsPerChannel = 32;

clientFormat.mFormatID = kAudioFormatLinearPCM;

clientFormat.mSampleRate = fileFormat.mSampleRate;

NSLog(@”Sample Rate is %1.2f”,clientFormat.mSampleRate);

clientFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat | kAudioFormatFlagIsNonInterleaved;

err = ExtAudioFileSetProperty(audiofile, kExtAudioFileProperty_ClientDataFormat, sizeof(clientFormat), &clientFormat);

// find out how many frames we need to read

SInt64 numFrames = 0;

size = sizeof(numFrames);

err = ExtAudioFileGetProperty(audiofile, kExtAudioFileProperty_FileLengthFrames, &size, &numFrames);

// create the buffers for reading in data

AudioBufferList *bufferList = malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer) * (clientFormat.mChannelsPerFrame – 1));

bufferList->mNumberBuffers = clientFormat.mChannelsPerFrame;

for (int ii=0; ii < bufferList->mNumberBuffers; ++ii) {

bufferList->mBuffers[ii].mDataByteSize = sizeof(float) * numFrames;

bufferList->mBuffers[ii].mNumberChannels = 1;

bufferList->mBuffers[ii].mData = malloc(bufferList->mBuffers[ii].mDataByteSize);

}

// read in the data

UInt32 rFrames = (UInt32)numFrames;

err = ExtAudioFileRead(audiofile, &rFrames, bufferList);

// close the file

err = ExtAudioFileDispose(audiofile);

// process the audio

for (int ii=0; ii < bufferList->mNumberBuffers; ++ii) {

float *fBuf = (float *)bufferList->mBuffers[ii].mData;

for (int jj=0; jj < rFrames; ++jj) {

*fBuf = *fBuf * ampScale;

fBuf++;

}

}

// open the file for writing

err = ExtAudioFileCreateWithURL((CFURLRef)theURL, fileType, &fileFormat, NULL, kAudioFileFlags_EraseFile, &audiofile);

// tell the ExtAudioFile API what format we’ll be sending samples in

err = ExtAudioFileSetProperty(audiofile, kExtAudioFileProperty_ClientDataFormat, sizeof(clientFormat), &clientFormat);

// write the data

err = ExtAudioFileWrite(audiofile, rFrames, bufferList);

// close the file

ExtAudioFileDispose(audiofile);

// destroy the buffers

for (int ii=0; ii < bufferList->mNumberBuffers; ++ii) {

free(bufferList->mBuffers[ii].mData);

}

free(bufferList);

bufferList = NULL;

}

</code>