Skip to content

Commit

Permalink
Change setInstrument() to support SRAM on Arduino
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuaCode committed Sep 30, 2019
1 parent 499428a commit 6664009
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/OPL2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,14 @@ byte OPL2::getFrequencyBlock(float frequency) {
* the channel will depend on the type of drum and the channel parameter will be ignored.
* See instruments.h for instrument definition format.
*/
void OPL2::setInstrument(byte channel, const unsigned char *instrument) {
#if BOARD_TYPE == OPL2_BOARD_TYPE_ARDUINO
unsigned char percussionChannel = pgm_read_byte_near(instrument);
#else
#if BOARD_TYPE == OPL2_BOARD_TYPE_ARDUINO
void OPL2::setInstrument(byte channel, const unsigned char *instrument, const bool progmem = true) {
unsigned char percussionChannel = progmem ? pgm_read_byte_near(instrument) : instrument[0];
#else
void OPL2::setInstrument(byte channel, const unsigned char *instrument) {
unsigned char percussionChannel = instrument[0];
#endif
#endif


setWaveFormSelect(true);
switch (percussionChannel) {
Expand All @@ -222,15 +224,15 @@ void OPL2::setInstrument(byte channel, const unsigned char *instrument) {
setRegister(
instrumentBaseRegs[i] + drumOffsets[0],
#if BOARD_TYPE == OPL2_BOARD_TYPE_ARDUINO
pgm_read_byte_near(instrument + i + 1)
progmem ? pgm_read_byte_near(instrument + i + 1) : instrument[i + 1]
#else
instrument[i + 1]
#endif
);
setRegister(
instrumentBaseRegs[i] + drumOffsets[1],
#if BOARD_TYPE == OPL2_BOARD_TYPE_ARDUINO
pgm_read_byte_near(instrument + i + 1)
progmem ? pgm_read_byte_near(instrument + i + 1) : instrument[i + 1]
#else
instrument[i + 1]
#endif
Expand All @@ -246,7 +248,7 @@ void OPL2::setInstrument(byte channel, const unsigned char *instrument) {
setRegister(
instrumentBaseRegs[i] + drumOffsets[percussionChannel - 5],
#if BOARD_TYPE == OPL2_BOARD_TYPE_ARDUINO
pgm_read_byte_near(instrument + i + 1)
progmem ? pgm_read_byte_near(instrument + i + 1) : instrument[i + 1]
#else
instrument[i + 1]
#endif
Expand All @@ -265,10 +267,14 @@ void OPL2::setInstrument(byte channel, const unsigned char *instrument) {
reg = instrumentBaseRegs[i % 6] + getRegisterOffset(channel, i > 5);
}

#if BOARD_TYPE == OPL2_BOARD_TYPE_ARDUINO
unsigned char val = progmem ? pgm_read_byte_near(instrument + i + 1) : instrument[i + 1];
#endif

setRegister(
reg,
#if BOARD_TYPE == OPL2_BOARD_TYPE_ARDUINO
pgm_read_byte_near(instrument + i + 1)
(i == 1 || i == 7) ? (val & 0xC0) | (getRegister(reg) & 0x3F) : val
#else
instrument[i + 1]
#endif
Expand Down
6 changes: 5 additions & 1 deletion src/OPL2.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@
byte getDrums();
byte getWaveForm(byte channel, byte operatorNum);

void setInstrument(byte channel, const unsigned char *instrument);
#if BOARD_TYPE == OPL2_BOARD_TYPE_ARDUINO
void setInstrument(byte channel, const unsigned char *instrument, bool progmem = true);
#else
void setInstrument(byte channel, const unsigned char *instrument);
#endif
void playNote(byte channel, byte octave, byte note);
void playDrum(byte drum, byte octave, byte note);
byte setRegister(byte reg, byte value);
Expand Down

0 comments on commit 6664009

Please sign in to comment.