Skip to content

Commit

Permalink
ESFM update
Browse files Browse the repository at this point in the history
  • Loading branch information
Cacodemon345 committed Mar 1, 2024
1 parent bf7d5f0 commit a8622e3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/sound/esfmu/esfm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2117,7 +2117,7 @@ ESFM_update_timers(esfm_chip *chip)
{
if (chip->timer_enable[i])
{
chip->timer_accumulator[i] += i == 0 ? TIMER1_CONST : TIMER2_CONST;
chip->timer_accumulator[i] += (i == 0) ? TIMER1_CONST : TIMER2_CONST;
if (chip->timer_accumulator[i] > 1.0)
{
chip->timer_accumulator[i] -= 1.0;
Expand All @@ -2126,6 +2126,7 @@ ESFM_update_timers(esfm_chip *chip)
{
if (chip->timer_mask[i] == 0)
{
chip->irq_bit = true;
chip->timer_overflow[i] = true;
}
chip->timer_counter[i] = chip->timer_reload[i];
Expand Down
15 changes: 11 additions & 4 deletions src/sound/esfmu/esfm_registers.c
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,11 @@ ESFM_write_reg_emu (esfm_chip *chip, uint16_t address, uint8_t data)
break;
case 0x02:
chip->timer_reload[0] = data;
chip->timer_counter[0] = data;
break;
case 0x03:
chip->timer_reload[1] = data;
chip->timer_counter[1] = data;
break;
case 0x04:
for (i = 0; i < 3; i++)
Expand Down Expand Up @@ -685,19 +687,24 @@ ESFM_write_reg_emu (esfm_chip *chip, uint16_t address, uint8_t data)
break;
case 0x02:
chip->timer_reload[0] = data;
chip->timer_counter[0] = data;
break;
case 0x03:
chip->timer_reload[1] = data;
chip->timer_counter[1] = data;
break;
case 0x04:
chip->timer_enable[0] = data & 0x01;
chip->timer_enable[1] = (data & 0x02) != 0;
chip->timer_mask[0] = (data & 0x20) != 0;
chip->timer_mask[1] = (data & 0x40) != 0;
if (data & 0x80)
{
chip->irq_bit = 0;
chip->timer_overflow[0] = 0;
chip->timer_overflow[1] = 0;
break;
}
chip->timer_enable[0] = data & 0x01;
chip->timer_enable[1] = (data & 0x02) != 0;
chip->timer_mask[1] = (data & 0x20) != 0;
chip->timer_mask[0] = (data & 0x40) != 0;
break;
case 0x08:
chip->keyscale_mode = (data & 0x40) != 0;
Expand Down
32 changes: 6 additions & 26 deletions src/sound/snd_opl_esfm.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,44 +54,25 @@ typedef struct {
uint16_t timer_count[2];
uint16_t timer_cur_count[2];

// OPL3L
int32_t rateratio;
int32_t samplecnt;
int32_t oldsamples[2];
int32_t samples[2];

int pos;
int32_t buffer[SOUNDBUFLEN * 2];
} esfm_drv_t;

void
esfm_drv_generate_resampled(esfm_drv_t *dev, int32_t *bufp)
esfm_generate_raw(esfm_drv_t *dev, int32_t *bufp)
{
while (dev->samplecnt >= dev->rateratio) {
int16_t samples[2] = { 0, 0 };
dev->oldsamples[0] = dev->samples[0];
dev->oldsamples[1] = dev->samples[1];
ESFM_generate(&dev->opl, samples);
dev->samples[0] = samples[0];
dev->samples[1] = samples[1];
dev->samplecnt -= dev->rateratio;
}

bufp[0] = (int32_t) ((dev->oldsamples[0] * (dev->rateratio - dev->samplecnt)
+ dev->samples[0] * dev->samplecnt)
/ dev->rateratio);
bufp[1] = (int32_t) ((dev->oldsamples[1] * (dev->rateratio - dev->samplecnt)
+ dev->samples[1] * dev->samplecnt)
/ dev->rateratio);
int16_t samples[2] = { 0, 0 };
ESFM_generate(&dev->opl, samples);

dev->samplecnt += 1 << RSM_FRAC;
bufp[0] = (int32_t) samples[0];
bufp[1] = (int32_t) samples[1];
}

void
esfm_drv_generate_stream(esfm_drv_t *dev, int32_t *sndptr, uint32_t num)
{
for (uint32_t i = 0; i < num; i++) {
esfm_drv_generate_resampled(dev, sndptr);
esfm_generate_raw(dev, sndptr);
sndptr += 2;
}
}
Expand All @@ -115,7 +96,6 @@ esfm_drv_init(const device_t *info)

/* Initialize the ESFMu object. */
ESFM_init(&dev->opl);
dev->rateratio = (SOUND_FREQ << RSM_FRAC) / 49716;

return dev;
}
Expand Down

0 comments on commit a8622e3

Please sign in to comment.