Only fill FIFO up to the amount of samples needed.

This commit is contained in:
Zachary Hall 2024-10-15 13:34:42 -07:00
parent a4b0fbdcf4
commit 9268b4ef42
3 changed files with 19 additions and 9 deletions

View file

@ -180,3 +180,12 @@ pcm_render(int16_t *buf, unsigned num_samples)
*(buf++) = (int16_t)((int32_t)cur_r * volume_lut[ctrl & 0xF] / 64); *(buf++) = (int16_t)((int32_t)cur_r * volume_lut[ctrl & 0xF] / 64);
} }
} }
uint32_t pcm_fifo_avail(void) {
switch ((ctrl >> 4) & 3) {
case 0: return fifo_cnt;
case 1: return fifo_cnt / 2;
case 2: return fifo_cnt / 2;
case 3: return fifo_cnt / 4;
}
return 0;
}

View file

@ -14,4 +14,5 @@ void pcm_write_rate(uint8_t val);
uint8_t pcm_read_rate(void); uint8_t pcm_read_rate(void);
void pcm_write_fifo(uint8_t val); void pcm_write_fifo(uint8_t val);
void pcm_render(int16_t *buf, unsigned num_samples); void pcm_render(int16_t *buf, unsigned num_samples);
uint32_t pcm_fifo_avail(void);
bool pcm_is_fifo_almost_empty(void); bool pcm_is_fifo_almost_empty(void);

View file

@ -84,7 +84,7 @@ class ZsmBackend : public PlaybackBackend {
return (int16_t)((((int32_t)a) + ((int32_t)b)) >> 1); return (int16_t)((((int32_t)a) + ((int32_t)b)) >> 1);
} }
void audio_step(size_t samples) { void audio_step(size_t samples) {
while (((pcm_read_ctrl() & 0x80) == 0) && remain > 0) { while (((pcm_read_ctrl() & 0x80) == 0 || pcm_fifo_avail() < samples) && remain > 0) {
remain--; remain--;
size_t oldpos = file->get_pos(); size_t oldpos = file->get_pos();
file->seek((cur++) + pcm_data_offs, SeekType::SET); file->seek((cur++) + pcm_data_offs, SeekType::SET);