{"issue":{"state":"closed","created_at":"2011-12-06T13:18:37Z","updated_at":"2011-12-08T16:17:00Z","comments":1,"user":{"gravatar_id":"8ccbccb087aaf1472021b0a46a47bf80","id":1244507,"url":"https://api.github.com/users/bartekhugo","avatar_url":"https://secure.gravatar.com/avatar/8ccbccb087aaf1472021b0a46a47bf80?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"bartekhugo"},"body":"well i dont know too much about all this stuff but i was getting an infinite loop while loading some audio files (most of them was working ok). so traced it and find out that when ExtAudioFileRead(fileHandle, &numFramesRead, &bufferList) returns in numFramesRead 0 it indicates that end-of-file was reached. so ive added a simple break for it and it seems to be working for me, i just wanted to share it and get an opinion about it\r\nhere's the whole method\r\n\r\n\r\n- (void*) audioDataWithStartFrame:(SInt64) startFrame\r\n\t\t\t\t\t\tnumFrames:(SInt64) numFrames\r\n\t\t\t\t\t   bufferSize:(UInt32*) bufferSize\r\n{\r\n\t@synchronized(self)\r\n\t{\r\n\t\tif(nil == fileHandle)\r\n\t\t{\r\n\t\t\tOAL_LOG_ERROR(@\"Attempted to read from closed file. Returning nil (url = %@)\", url);\r\n\t\t\treturn nil;\r\n\t\t}\r\n\t\t\r\n\t\tOSStatus error;\r\n\t\tUInt32 numFramesRead;\r\n        AudioBufferList bufferList;\r\n        UInt32 bufferOffset = 0;\r\n\r\n\t\t\r\n\t\t// < 0 means read to the end of the file.\r\n\t\tif(numFrames < 0)\r\n\t\t{\r\n\t\t\tnumFrames = totalFrames - startFrame;\r\n\t\t}\r\n\t\t\r\n\t\t// Allocate some memory to hold the data\r\n\t\tUInt32 streamSizeInBytes = (UInt32)(streamDescription.mBytesPerFrame * numFrames);\r\n\t\tvoid* streamData = malloc(streamSizeInBytes);\r\n\t\tif(nil == streamData)\r\n\t\t{\r\n\t\t\tOAL_LOG_ERROR(@\"Could not allocate %d bytes for audio buffer from file (url = %@)\",\r\n\t\t\t\t\t\t  streamSizeInBytes,\r\n\t\t\t\t\t\t  url);\r\n\t\t\tgoto onFail;\r\n\t\t}\r\n\t\t\r\n\t\tif(noErr != (error = ExtAudioFileSeek(fileHandle, startFrame)))\r\n\t\t{\r\n\t\t\tREPORT_EXTAUDIO_CALL(error, @\"Could not seek to %ll in file (url = %@)\",\r\n\t\t\t\t\t\t\t\t startFrame,\r\n\t\t\t\t\t\t\t\t url);\r\n\t\t\tgoto onFail;\r\n\t\t}\r\n\t\t\r\n        \r\n        bufferList.mNumberBuffers = 1;\r\n        bufferList.mBuffers[0].mNumberChannels = streamDescription.mChannelsPerFrame;\r\n        for(UInt32 framesToRead = (UInt32) numFrames; framesToRead > 0; framesToRead -= numFramesRead)\r\n        {\r\n            bufferList.mBuffers[0].mDataByteSize = streamDescription.mBytesPerFrame * framesToRead;\r\n            bufferList.mBuffers[0].mData = streamData + bufferOffset;\r\n            \r\n            numFramesRead = framesToRead;\r\n            if(noErr != (error = ExtAudioFileRead(fileHandle, &numFramesRead, &bufferList)))\r\n            {\r\n                REPORT_EXTAUDIO_CALL(error, @\"Could not read audio data in file (url = %@)\",\r\n                                     url);\r\n                goto onFail;\r\n            }\r\n            if (numFramesRead == 0)\r\n                break;\r\n\r\n            bufferOffset += streamDescription.mBytesPerFrame * numFramesRead;\r\n        }\r\n        \r\n\t\tif(nil != bufferSize)\r\n\t\t{\r\n\t\t\t*bufferSize = streamSizeInBytes;\r\n\t\t}\r\n\t\t\r\n\t\treturn streamData;\r\n\t\t\r\n\tonFail:\r\n\t\tif(nil != streamData)\r\n\t\t{\r\n\t\t\tfree(streamData);\r\n\t\t}\r\n\t\treturn nil;\r\n\t}\r\n}\r\n","assignee":null,"id":2461950,"number":20,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"labels":[],"title":"infinite loop while loading file","html_url":"https://github.com/kstenerud/ObjectAL-for-iPhone/issues/20","closed_at":"2011-12-08T16:17:00Z","url":"https://api.github.com/repos/kstenerud/ObjectAL-for-iPhone/issues/20","milestone":null},"action":"closed"}