Quick question: Should the condition in the if just before it:

Code:
 		/* add up the samples */
 		if (channelsamples > 0)


be changed to:

Code:
 		/* add up the samples */
 		if (chunksamples > 0)


Since channelsamples was changed to chunksamples in your diff on the line beneath it?