Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: AudioPlaySerialflashRaw::update - code query

  1. #1
    Junior Member
    Join Date
    Aug 2015
    Location
    Australia
    Posts
    3

    AudioPlaySerialflashRaw::update - code query

    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;
    		for (i=n/2; i < AUDIO_BLOCK_SAMPLES; i++) {
    			block->data[i] = 0;
    		}
    		transmit(block);
    	} else {
    		rawfile.close();
    		AudioStopUsingSPI();
    		playing = false;
    		//Serial.println("Finished playing sample");		//TODO
    	}
    	release(block);
    }
    Any thoughts / guidance appreciated!
    Thanks,
    Nick.

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,377
    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

  3. #3
    Junior Member
    Join Date
    Aug 2015
    Location
    Australia
    Posts
    3
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •