Skip to content

Commit

Permalink
Merge pull request #56 from DhrBaksteen/ESP8266_fixes
Browse files Browse the repository at this point in the history
ESP8266 Fixes
  • Loading branch information
DhrBaksteen authored Mar 18, 2020
2 parents fd5459b + 751df4e commit a400103
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This repository contains the OPL2 Audio Board library for Arduino, Teensy, Raspb
* Emulation with DosBox; you can use the board to output MIDI music (Teensy++ 2.0 and later)
* Use the board directly as a synthesizer by using the [OPL3BankEditor](https://github.com/Wohlstand/OPL3BankEditor) software by Wohlstand

Current library version is 1.5.0
Current library version is 1.5.1

To obtain your own OPL2 Audio Board visit the [Tindie store](https://www.tindie.com/products/DhrBaksteen/opl2-audio-board/).

Expand Down
4 changes: 2 additions & 2 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ echo "\033[1;36m / | \\ | / /_/ | | ( /_/ ) | | ( /_/ ) __ \\|
echo "\033[1;34m \\____|__ /____/\\____ | |__|\\___/ |______ /\\___(____ /__| \\____ | "
echo "\033[1;34m \\/ \\/ \\/ \\/ \\/ \033[0m"
echo "Installation script for Raspberry Pi and compatibles"
echo "Library version 1.5.0, 10th of November 2019"
echo "Copyright (c) 2016-2019 Maarten Janssen, Cheerful"
echo "Library version 1.5.1, 16th of Match 2020"
echo "Copyright (c) 2016-2020 Maarten Janssen, Cheerful"
echo ""

MYDIR="${0%/*}"
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Arduino OPL2
version=1.5.0
version=1.5.1
author=Maarten Janssen <maarten@cheerful.nl>
maintainer=Maarten Janssen <maarten@cheerful.nl>
sentence=Use this library to control the OPL2 Audio Board
Expand Down
44 changes: 22 additions & 22 deletions src/OPL2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* \____|__ /__| \____ |____/|__|___| /\____/ \_____\ \ |____| |__|
* \/ \/ \/ \/
*
* YM3812 OPL2 Audio Library for Arduino, Raspberry Pi and Orange Pi v1.5.0
* YM3812 OPL2 Audio Library for Arduino, Raspberry Pi and Orange Pi v1.5.1
* Code by Maarten Janssen (maarten@cheerful.nl) 2016-12-18
*
* Look for example code on how to use this library in the examples folder.
Expand All @@ -35,7 +35,7 @@
* IMPORTANT: Make sure you set the correct BOARD_TYPE in OPL2.h. Default is set to Arduino.
*
*
* Last updated 2019-11-10
* Last updated 2020-03-16
* Most recent version of the library can be found at my GitHub: https://github.com/DhrBaksteen/ArduinoOPL2
* Details about the YM3812 and OPL chips can be found at http://www.shikadi.net/moddingwiki/OPL_chip
*
Expand Down Expand Up @@ -160,8 +160,8 @@ byte OPL2::setRegister(byte reg, byte value) {
* Calculate register offet based on channel and operator.
*/
byte OPL2::getRegisterOffset(byte channel, byte operatorNum) {
channel = max(0, min(channel, 8));
operatorNum = max(0, min(operatorNum, 1));
channel = max(ZERO, min(channel, CHANNEL_MAX));
operatorNum = max(ZERO, min(operatorNum, ONE));
return registerOffsets[operatorNum][channel];
}

Expand All @@ -172,15 +172,15 @@ byte OPL2::getRegisterOffset(byte channel, byte operatorNum) {
*/
short OPL2::getFrequencyFNumber(byte channel, float frequency) {
float fInterval = getFrequencyStep(channel);
return (short)max(0, min(frequency / fInterval, 1023));
return max(F_NUM_MIN, min((short)(frequency / fInterval), F_NUM_MAX));
}


/**
* Get the F-Number for the given note. In this case the block is assumed to be the octave.
*/
short OPL2::getNoteFNumber(byte note) {
return noteFNumbers[max(0, min(note, 11))];
return noteFNumbers[max(ZERO, min(note, NOTE_MAX))];
}

/**
Expand Down Expand Up @@ -346,8 +346,8 @@ Instrument OPL2::getDrumInstrument(byte drumType) {
* operators.
*/
void OPL2::setInstrument(byte channel, Instrument instrument, float volume) {
channel = max(0, min(channel, 8));
volume = max(0.0, min(volume, 1.0));
channel = max(ZERO, min(channel, CHANNEL_MAX));
volume = max(VOLUME_MIN, min(volume, VOLUME_MAX));

setWaveFormSelect(true);
for (byte op = OPERATOR1; op <= OPERATOR2; op ++) {
Expand Down Expand Up @@ -385,7 +385,7 @@ void OPL2::setInstrument(byte channel, Instrument instrument, float volume) {
* proper output levels for the operator(s).
*/
void OPL2::setDrumInstrument(Instrument instrument, float volume) {
volume = max(0.0, min(volume, 1.0));
volume = max(VOLUME_MIN, min(volume, VOLUME_MAX));

setWaveFormSelect(true);
for (byte op = OPERATOR1; op <= OPERATOR2; op ++) {
Expand Down Expand Up @@ -477,7 +477,7 @@ void OPL2::setInstrument(byte channel, const unsigned char *instrument) {
byte reg;
if (i == 5) {
//Channel parameters C0..C8
reg = 0xC0 + max(0x00, min(channel, 0x08));
reg = 0xC0 + max(ZERO, min(channel, CHANNEL_MAX));
} else {
//Operator parameters 20..35, 40..55, 60..75, 80..95, E0..F5
reg = instrumentBaseRegs[i % 6] + getRegisterOffset(channel, i > 5);
Expand All @@ -502,8 +502,8 @@ void OPL2::setInstrument(byte channel, const unsigned char *instrument) {
*/
void OPL2::playNote(byte channel, byte octave, byte note) {
setKeyOn(channel, false);
setBlock(channel, max(0, min(octave, 7)));
setFNumber(channel, noteFNumbers[max(0, min(note, 11))]);
setBlock(channel, max(ZERO, min(octave, OCTAVE_MAX)));
setFNumber(channel, noteFNumbers[max(ZERO, min(note, NOTE_MAX))]);
setKeyOn(channel, true);
}

Expand Down Expand Up @@ -743,7 +743,7 @@ byte OPL2::setRelease(byte channel, byte operatorNum, byte release) {
* Get the frequenct F-number of the given channel.
*/
short OPL2::getFNumber(byte channel) {
byte offset = max(0x00, min(channel, 0x08));
byte offset = max(ZERO, min(channel, CHANNEL_MAX));
return ((oplRegisters[0xB0 + offset] & 0x03) << 8) + oplRegisters[0xA0 + offset];
}

Expand All @@ -752,7 +752,7 @@ short OPL2::getFNumber(byte channel) {
* Set frequency F-number [0, 1023] for the given channel.
*/
byte OPL2::setFNumber(byte channel, short fNumber) {
byte reg = 0xA0 + max(0x00, min(channel, 0x08));
byte reg = 0xA0 + max(ZERO, min(channel, CHANNEL_MAX));
setRegister(reg, fNumber & 0x00FF);
setRegister(reg + 0x10, (oplRegisters[reg + 0x10] & 0xFC) | ((fNumber & 0x0300) >> 8));
return reg;
Expand Down Expand Up @@ -785,7 +785,7 @@ byte OPL2::setFrequency(byte channel, float frequency) {
* Get the frequency block of the given channel.
*/
byte OPL2::getBlock(byte channel) {
byte offset = max(0x00, min(channel, 0x08));
byte offset = max(ZERO, min(channel, CHANNEL_MAX));
return (oplRegisters[0xB0 + offset] & 0x1C) >> 2;
}

Expand All @@ -802,7 +802,7 @@ byte OPL2::getBlock(byte channel) {
* 7 - 6.069 Hz, Range: 6.068 Hz -> 6208.431 Hz
*/
byte OPL2::setBlock(byte channel, byte block) {
byte reg = 0xB0 + max(0x00, min(channel, 0x08));
byte reg = 0xB0 + max(ZERO, min(channel, CHANNEL_MAX));
return setRegister(reg, (oplRegisters[reg] & 0xE3) | ((block & 0x07) << 2));
}

Expand All @@ -811,7 +811,7 @@ byte OPL2::setBlock(byte channel, byte block) {
* Is the voice of the given channel currently enabled?
*/
bool OPL2::getKeyOn(byte channel) {
byte offset = max(0x00, min(channel, 0x08));
byte offset = max(ZERO, min(channel, CHANNEL_MAX));
return oplRegisters[0xB0 + offset] & 0x20;
}

Expand All @@ -820,7 +820,7 @@ bool OPL2::getKeyOn(byte channel) {
* Enable voice on channel.
*/
byte OPL2::setKeyOn(byte channel, bool keyOn) {
byte reg = 0xB0 + max(0x00, min(channel, 0x08));
byte reg = 0xB0 + max(ZERO, min(channel, CHANNEL_MAX));
if (keyOn) {
return setRegister(reg, oplRegisters[reg] | 0x20);
} else {
Expand All @@ -833,7 +833,7 @@ byte OPL2::setKeyOn(byte channel, bool keyOn) {
* Get the feedback strength of the given channel.
*/
byte OPL2::getFeedback(byte channel) {
byte offset = max(0x00, min(channel, 0x08));
byte offset = max(ZERO, min(channel, CHANNEL_MAX));
return (oplRegisters[0xC0 + offset] & 0xE0) >> 1;
}

Expand All @@ -842,7 +842,7 @@ byte OPL2::getFeedback(byte channel) {
* Set feedback strength. 0x00 is no feedback, 0x07 is strongest.
*/
byte OPL2::setFeedback(byte channel, byte feedback) {
byte reg = 0xC0 + max(0x00, min(channel, 0x08));
byte reg = 0xC0 + max(ZERO, min(channel, CHANNEL_MAX));
return setRegister(reg, (oplRegisters[reg] & 0x01) | ((feedback & 0x07) << 1));
}

Expand All @@ -851,7 +851,7 @@ byte OPL2::setFeedback(byte channel, byte feedback) {
* Is the decay algorythm enabled for the given channel?
*/
bool OPL2::getSynthMode(byte channel) {
byte offset = max(0x00, min(channel, 0x08));
byte offset = max(ZERO, min(channel, CHANNEL_MAX));
return oplRegisters[0xC0 + offset] & 0x01;
}

Expand All @@ -861,7 +861,7 @@ bool OPL2::getSynthMode(byte channel) {
* set to true both operator 1 and operator 2 will produce sound.
*/
byte OPL2::setSynthMode(byte channel, bool isAdditive) {
byte reg = 0xC0 + max(0x00, min(channel, 0x08));
byte reg = 0xC0 + max(ZERO, min(channel, CHANNEL_MAX));
if (isAdditive) {
return setRegister(reg, oplRegisters[reg] | 0x01);
} else {
Expand Down
10 changes: 10 additions & 0 deletions src/OPL2.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@
0x20, 0x40, 0x60, 0x80, 0xE0, 0xC0
};
byte oplRegisters[256];

const byte ZERO = 0;
const byte ONE = 1;
const byte CHANNEL_MAX = 8;
const byte OCTAVE_MAX = 7;
const byte NOTE_MAX = 11;
const short F_NUM_MIN = 0;
const short F_NUM_MAX = 1023;
const float VOLUME_MIN = 0.0;
const float VOLUME_MAX = 1.0;
};
#endif

0 comments on commit a400103

Please sign in to comment.