« Return to Thread: Get part of a source / buffer

Re: Get part of a source / buffer

by Niall09 :: Rate this Message:

Reply to Author | View in Thread

Chris Robinson-5 wrote:
How are you setting the frames/channels/freq/data variables?
For the channels: channels = AL_FORMAT_STEREO16;

For the frames, freq and data: data = getOpenALAudioData(fileURL, &size3, &format3, &freq,&frames);

void* getOpenALAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDataFormat, ALuint* outSampleRate,ALuint * frames)
{
        OSStatus err = noErr;
        SInt64 theFileLengthInFrames = 0;
        AudioStreamBasicDescription theFileFormat;
        UInt32 thePropertySize = sizeof(theFileFormat);
        printf("\nT: %i : %d \n",sizeof(theFileFormat),sizeof(theFileFormat));
        ExtAudioFileRef extRef = NULL;
        void* theData = NULL;
        AudioStreamBasicDescription theOutputFormat;
       
        // Open a file with ExtAudioFileOpen()
        err = ExtAudioFileOpenURL(inFileURL, &extRef);
        if(err) { printf("MyGetOpenALAudioData: ExtAudioFileOpenURL FAILED, Error = %ld\n", err); goto Exit; }
       
        // Get the audio data format
        err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileDataFormat, &thePropertySize, &theFileFormat);
        if(err) { printf("MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld\n", err); goto Exit; }
        if (theFileFormat.mChannelsPerFrame > 2)  { printf("MyGetOpenALAudioData - Unsupported Format, channel count is greater than stereo\n"); goto Exit;}
       
        // Set the client format to 16 bit signed integer (native-endian) data
        // Maintain the channel count and sample rate of the original source format
        theOutputFormat.mSampleRate = theFileFormat.mSampleRate;
        theOutputFormat.mChannelsPerFrame = theFileFormat.mChannelsPerFrame;
       
        theOutputFormat.mFormatID = kAudioFormatLinearPCM;
        theOutputFormat.mBytesPerPacket = 2 * theOutputFormat.mChannelsPerFrame;
        theOutputFormat.mFramesPerPacket = 1;
        theOutputFormat.mBytesPerFrame = 2 * theOutputFormat.mChannelsPerFrame;
        theOutputFormat.mBitsPerChannel = 16;
        theOutputFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
       
        // Set the desired client (output) data format
        err = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(theOutputFormat), &theOutputFormat);
        if(err) { printf("MyGetOpenALAudioData: ExtAudioFileSetProperty(kExtAudioFileProperty_ClientDataFormat) FAILED, Error = %ld\n", err); goto Exit; }
       
        // Get the total frame count
        thePropertySize = sizeof(theFileLengthInFrames);
        //AL_SAMPLE
        err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileLengthFrames, &thePropertySize, &theFileLengthInFrames);
        if(err) { printf("MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld\n", err); goto Exit; }
       
        // Read all the data into memory
        UInt32 dataSize = theFileLengthInFrames * theOutputFormat.mBytesPerFrame;
        theData = malloc(dataSize);
        *frames = theFileLengthInFrames;
        if (theData)
        {
                AudioBufferList theDataBuffer;
                theDataBuffer.mNumberBuffers = 1;
                theDataBuffer.mBuffers[0].mDataByteSize = dataSize;
                theDataBuffer.mBuffers[0].mNumberChannels = theOutputFormat.mChannelsPerFrame;
                theDataBuffer.mBuffers[0].mData = theData;
               
                // Read the data into an AudioBufferList
                err = ExtAudioFileRead(extRef, (UInt32*)&theFileLengthInFrames, &theDataBuffer);
                if(err == noErr)
                {
                        // success
                        *outDataSize = (ALsizei)dataSize;
                        *outDataFormat = (theOutputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
                        *outSampleRate = (ALsizei)theOutputFormat.mSampleRate;
                }
                else
                {
                        // failure
                        free (theData);
                        theData = NULL; // make sure to return NULL
                        printf("MyGetOpenALAudioData: ExtAudioFileRead FAILED, Error = %ld\n", err); goto Exit;
                }
        }
       
Exit:
        // Dispose the ExtAudioFileRef, it is no longer needed
        if (extRef) ExtAudioFileDispose(extRef);
        return theData;
}

 « Return to Thread: Get part of a source / buffer