Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OPL3_WriteRegDelayed() to buffer with a custom delay #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions opl3.c
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,35 @@ void OPL3_WriteReg(opl3_chip *chip, uint16_t reg, uint8_t v)
}
}

void OPL3_WriteRegDelayed(opl3_chip *chip, uint16_t reg, uint8_t v, uint64_t delay)
{
uint64_t currentTime;
opl3_writebuf *writebuf = &chip->writebuf[chip->writebuf_last];

currentTime = chip->writebuf_lasttime;

/* If we've overflowed the ringbuffer, process the next event early */
if (writebuf->reg & 0x200)
{
OPL3_WriteReg(chip, writebuf->reg & 0x1ff, writebuf->data);
}

/* If the previous write was in the past... */
if (currentTime < chip->writebuf_samplecnt)
{
currentTime = chip->writebuf_samplecnt;
}

/* Schedule the write. */
writebuf->time = currentTime + delay;
writebuf->reg = reg | 0x200;
writebuf->data = v;

/* Advance the writebuf ringbuffer. */
chip->writebuf_last = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE;
chip->writebuf_lasttime = writebuf->time;
}

void OPL3_WriteRegBuffered(opl3_chip *chip, uint16_t reg, uint8_t v)
{
uint64_t time1, time2;
Expand Down
1 change: 1 addition & 0 deletions opl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ void OPL3_GenerateResampled(opl3_chip *chip, int16_t *buf);
void OPL3_Reset(opl3_chip *chip, uint32_t samplerate);
void OPL3_WriteReg(opl3_chip *chip, uint16_t reg, uint8_t v);
void OPL3_WriteRegBuffered(opl3_chip *chip, uint16_t reg, uint8_t v);
void OPL3_WriteRegDelayed(opl3_chip *chip, uint16_t reg, uint8_t v, uint64_t delay);
void OPL3_GenerateStream(opl3_chip *chip, int16_t *sndptr, uint32_t numsamples);

void OPL3_Generate4Ch(opl3_chip *chip, int16_t *buf4);
Expand Down