• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
read segments from multiple audio files to one buffer with extaudiofile
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

read segments from multiple audio files to one buffer with extaudiofile


  • Subject: read segments from multiple audio files to one buffer with extaudiofile
  • From: Robert Carroll <email@hidden>
  • Date: Thu, 18 Sep 2014 12:03:34 -0400

I need to be able to assemble audio from several files into a single buffer (stereo). My code is working as expected if I load each file into its own buffer. Looping through several files and losing into one larger buffer only plays back the segment from the last file.

Its possible that the header info is getting copied each time, or that the same area of the buffer is just being over-written with each new file.

Any suggestions would be appreciated.

Some code is listed below. I'm reading from encrypted files, so I'm using NSData and AudioFileOpenWithCallbacks.

  // Assign the frame count to the soundStructArray instance variable
UInt64 totalFrames = [[inputNotes.stopTimes lastObject] intValue];
self.soundStructArray[0]->frameCount = (UInt32)totalFrames;
self.soundStructArray[0]->audioDataLeft =
(AudioUnitSampleType *) calloc (totalFrames, sizeof (AudioUnitSampleType));



AudioStreamBasicDescription importFormat = {0};
self.soundStructArray[0]->isStereo = YES;

    
self.soundStructArray[0]->audioDataRight = (AudioUnitSampleType *) calloc (totalFrames, sizeof (AudioUnitSampleType));


    
// Allocate memory for the buffer list struct according to the number of

    
//    channels it represents.
AudioBufferList *bufferList;
UInt32 channelCount = 2;


bufferList = (AudioBufferList *) malloc (

                                         
sizeof (AudioBufferList) + sizeof (AudioBuffer) * (channelCount - 1)

                                         
);



if (NULL == bufferList) {DLog (@"*** malloc failure for allocating bufferList memory"); return;}


    
// initialize the mNumberBuffers member

bufferList->mNumberBuffers = channelCount;


    
// initialize the mBuffers member to 0
AudioBuffer emptyBuffer = {0};
size_t arrayIndex;
for (arrayIndex = 0; arrayIndex < channelCount; arrayIndex++) {

    bufferList->mBuffers[arrayIndex] = emptyBuffer;
}


    
// set up the AudioBuffer structs in the buffer list

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

bufferList
->mBuffers[0].mDataByteSize    = (UInt32)totalFrames * sizeof (AudioUnitSampleType);

bufferList->mBuffers[0].mData            = self.soundStructArray[0]->audioDataLeft;



if (2 == channelCount) {

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

    bufferList->mBuffers[1].mDataByteSize    = (UInt32)totalFrames * sizeof (AudioUnitSampleType);

    bufferList->mBuffers[1].mData            = self.soundStructArray[0]->audioDataRight;
}




NSString *fileType = @"m4a";



for (int audioFile = 0; audioFile < inputVoicesCount; ++audioFile)  {

    
@autoreleasepool {

        
NSData *encData;

        
NSData *audioData;

        
AudioFileID         refAudioFileID;


        
DLog (@"readAudioFilesIntoMemory - file %i", audioFile);


        
NSString *source = [[NSBundle mainBundle] pathForResource:[inputNotes.notes objectAtIndex:audioFile] ofType:fileType];


            
//            NSURL *url = "" encryptedFileURLWithPath:source];

        
if ([[NSFileManager defaultManager] fileExistsAtPath:source])

            
{

                
//File exists

            encData  = [[NSData alloc] initWithContentsOfFile:source];

            
if (encData)

                
{

                
NSError *error;

                audioData = [RNDecryptor decryptData:encData
                                        withPassword
:
key
                                               error
:&error];
                    
                
}

            
}

        
else

            
{

            
DLog(@"File does not exist");

            
}


        
OSStatus result = AudioFileOpenWithCallbacks((__bridge void *)(audioData), readProc, 0, getSizeProc, NULL, kAudioFileMPEG4Type, &refAudioFileID);

        
if(result != noErr){

            
DLog(@"problem in theAudioFileReaderWithData function: result code %i \n", result);

        
}


            
// Instantiate an extended audio file object.

        
ExtAudioFileRef audioFileObject = 0;


        result = ExtAudioFileWrapAudioFileID(refAudioFileID, NO, &audioFileObject);

        
if (result != noErr){DLog(@"problem in theAudioFileReaderWithData function Wraping the audio FileID: result code %i \n", result);

        
}



            
// Get the audio file's number of channels.

        
AudioStreamBasicDescription fileAudioFormat = {0};

        
UInt32 formatPropertySize = sizeof (fileAudioFormat);


        result =    ExtAudioFileGetProperty (

                                             audioFileObject
,

                                             kExtAudioFileProperty_FileDataFormat
,

                                             
&formatPropertySize,

                                             
&
fileAudioFormat
                                             
);


        
if (noErr != result) {[self printErrorMessage: @"ExtAudioFileGetProperty (file audio format)" withStatus: result]; return;}



        importFormat = stereoStreamFormat;


        result =    ExtAudioFileSetProperty (

                                             audioFileObject
,

                                             kExtAudioFileProperty_ClientDataFormat
,

                                             
sizeof (importFormat),

                                             
&
importFormat
                                             
);
        
if (noErr != result) {[self printErrorMessage: @"ExtAudioFileSetProperty (client data format)" withStatus: result]; return;}
           
// Assign the frame count to the soundStructArray instance variable

        
UInt64 desiredFrames = (UInt64) ([[inputNotes.stopTimes objectAtIndex:audioFile] intValue] - [[inputNotes.startTimes objectAtIndex:audioFile] intValue]);
            
// Perform a synchronous, sequential read of the audio data out of the file and into the soundStructArray[audioFile].audioDataLeft and (if stereo) .audioDataRight members.

        
UInt32 numberOfPacketsToRead = (UInt32) desiredFrames;


        result = ExtAudioFileRead (

                                   audioFileObject
,

                                   
&numberOfPacketsToRead,

                                   bufferList
                                   
);

        
if (noErr != result) {[self printErrorMessage: @"ExtAudioFileRead failure - " withStatus: result];


            free (self.soundStructArray[0]->audioDataLeft);
self.soundStructArray[0]->audioDataLeft = 0;
                    
            free (self.soundStructArray[0]->audioDataRight);
self.soundStructArray[0]->audioDataRight = 0;
            
ExtAudioFileDispose (audioFileObject);

            
return;

        
}

        
ExtAudioFileDispose (audioFileObject);

        
AudioFileClose(refAudioFileID);
    
}
}//end of @autoreleasepool

        free (bufferList);

            
// Set the sample index to zero, so that playback starts at the beginning of the sound.
        
self.soundStructArray[0]->sampleNumber = 0;
DLog (@"Finished reading all files into memory");

readingFiles 
= NO;




thanks,

Robert Carroll
RSM Records
Toronto
http://www.rsmrecords.com



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Connecting two kAudioUnitType_Effect nodes in augraph
  • Next by Date: Trouble loading up audiounit (Xcode 6.0 / OS X 10.9)
  • Previous by thread: Connecting two kAudioUnitType_Effect nodes in augraph
  • Next by thread: Trouble loading up audiounit (Xcode 6.0 / OS X 10.9)
  • Index(es):
    • Date
    • Thread