AudioPlaySerialflashRaw::update - code query

Status
Not open for further replies.

ObliqueStrategy

New member
Sorry if I'm missing something obvious here, but why in AudioPlaySerialflashRaw::update, why does the code zero out the second half of the freshly-read block (in red below) :
Code:
void AudioPlaySerialflashRaw::update(void)
{
	unsigned int i, n;
	audio_block_t *block;

	// only update if we're playing
	if (!playing) return;

	// allocate the audio blocks to transmit
	block = allocate();
	if (block == NULL) return;

	if (rawfile.available()) {
		// we can read more data from the file...
		n = rawfile.read(block->data, AUDIO_BLOCK_SAMPLES*2);
		file_offset += n;
		[COLOR="#FF0000"][B]for (i=n/2; i < AUDIO_BLOCK_SAMPLES; i++) {
			block->data[i] = 0;[/B][/COLOR]
		}
		transmit(block);
	} else {
		rawfile.close();
		AudioStopUsingSPI();
		playing = false;
		//Serial.println("Finished playing sample");		//TODO
	}
	release(block);
}

Any thoughts / guidance appreciated!
Thanks,
Nick.
 
n is the number of bytes actually read by rawfile.read. Normally it will be AUDIO_BLOCK_SAMPLES*2, in which case the for loop won't do anything. But if it returns less than a full block (presumably at the end of file), the for loop will zero the remainder of the buffer instead of returning random noise.

Pete
 
Ah - perfect - thank you! I'd missed that the number of bytes returned is (at best) half the number of samples (bytes -> int16)

Thanks for your help Pete! I'll sleep better tonight.

Best regards,
Nick.
 
Status
Not open for further replies.
Back
Top