Skip to content

Commit

Permalink
Merge pull request #58 from DhrBaksteen/Drums_Update
Browse files Browse the repository at this point in the history
Drums Update
  • Loading branch information
DhrBaksteen authored Apr 11, 2020
2 parents 4112945 + 9b280b1 commit 4910ca9
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 17 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.2
Current library version is 1.5.3

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

Expand Down
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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.2, 10th of April 2020"
echo "Library version 1.5.3, 10th of April 2020"
echo "Copyright (c) 2016-2020 Maarten Janssen, Cheerful"
echo ""

Expand Down
3 changes: 2 additions & 1 deletion examples/PianoBoard/PianoBoard.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* This is a simple demo sketch for the OPL2 library. It makes use of the Piano Board and assumes then you also have the
* This is a simple demo sketch for the OPL2 library. It makes use of the Cheerful Electronic Piano Board
* (https://www.tindie.com/products/cheerful/arduino-piano-board-2/) and assumes then you also have the
* ArduinoPianoBoard library installed (see https://github.com/DhrBaksteen/ArduinoPianoBoard).
*
* This sketch makes a simple piano out of the OPL2 Board and the Piano Board. Use the piano keys to play a tune. User
Expand Down
68 changes: 68 additions & 0 deletions examples/PianoBoardDrummer/PianoBoardDrummer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* This is a simple demo sketch for the OPL2 library. It makes use of the Cheerful Electronic Piano Board
* (https://www.tindie.com/products/cheerful/arduino-piano-board-2/) and assumes then you also have the
* ArduinoPianoBoard library installed (see https://github.com/DhrBaksteen/ArduinoPianoBoard).
*
* This sketch makes a drum machine out of the Piano Board. The first 5 white keys are used to play various drum sounds.
*
* OPL2 Board is connected as follows:
* Pin 8 - Reset
* Pin 9 - A0
* Pin 10 - Latch
* Pin 11 - Data
* Pin 13 - Shift
*
* Piano Board is connected as follows:
* Pin 7 - /SS
* Pin 12 - MISO
* Pin 13 - SCK
*
* Code by Maarten Janssen (maarten@cheerful.nl) 2020-04-11
* Most recent version of the library can be found at my GitHub: https://github.com/DhrBaksteen/ArduinoOPL2
*/

#include <SPI.h>
#include <OPL2.h>
#include <PianoKeys.h>
#include <instruments.h>

#define NO_DRUM 255

OPL2 opl2;
PianoKeys piano(7);


byte drums[8] = {DRUM_BASS, NO_DRUM, DRUM_SNARE, NO_DRUM, DRUM_TOM, DRUM_HI_HAT, NO_DRUM, DRUM_CYMBAL};


void setup() {
opl2.init();

// Load drum instruments and set percusive mode.
Instrument bass = opl2.loadInstrument(INSTRUMENT_BDRUM1);
Instrument snare = opl2.loadInstrument(INSTRUMENT_RKSNARE1);
Instrument tom = opl2.loadInstrument(INSTRUMENT_TOM2);
Instrument cymbal = opl2.loadInstrument(INSTRUMENT_CYMBAL1);
Instrument hihat = opl2.loadInstrument(INSTRUMENT_HIHAT2);

opl2.setPercussion(true);
opl2.setDrumInstrument(bass);
opl2.setDrumInstrument(snare);
opl2.setDrumInstrument(tom);
opl2.setDrumInstrument(cymbal);
opl2.setDrumInstrument(hihat);
}


void loop() {
piano.updateKeys();

// Handle keys that are being pressed.
for (int i = KEY_C; i <= KEY_G; i ++) {
if (piano.wasKeyPressed(i) && drums[i] != NO_DRUM) {
opl2.playDrum(drums[i], 4, NOTE_C);
}
}

delay(20);
}
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.2
version=1.5.3
author=Maarten Janssen <maarten@cheerful.nl>
maintainer=Maarten Janssen <maarten@cheerful.nl>
sentence=Use this library to control the OPL2 Audio Board
Expand Down
40 changes: 33 additions & 7 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.1
* YM3812 OPL2 Audio Library for Arduino, Raspberry Pi and Orange Pi v1.5.3
* 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 2020-03-16
* Last updated 2020-04-11
* 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 @@ -508,6 +508,24 @@ void OPL2::playNote(byte channel, byte octave, byte note) {
}


/**
* Play a drum sound at a given note and frequency.
* The OPL2 must be put into percusive mode first and the parameters of the drum sound must be set in the required
* operator(s). Note that changing octave and note frequenct will influence both drum sounds if they occupy only a
* single operator (Snare + Hi-hat and Tom + Cymbal).
*/
void OPL2::playDrum(byte drum, byte octave, byte note) {
drum = drum % DRUM_SOUND_MAX;
byte drumState = getDrums();

setDrums(drumState & ~drumBits[drum]);
byte drumChannel = drumChannels[drum % DRUM_SOUND_MAX];
setBlock(drumChannel, max(ZERO, min(octave, OCTAVE_MAX)));
setFNumber(drumChannel, noteFNumbers[max(ZERO, min(note, NOTE_MAX))]);
setDrums(drumState | drumBits[drum]);
}


/**
* Is wave form selection currently enabled.
*/
Expand Down Expand Up @@ -939,17 +957,25 @@ byte OPL2::getDrums() {
}


/**
* Set the OPL2 drum registers all at once.
*/
byte OPL2::setDrums(byte drums) {
return setRegister(0xBD, (oplRegisters[0xBD] & 0xE0) | (drums & 0x1F));
}


/**
* Enable or disable various drum sounds.
* Note that keyOn for channels 6, 7 and 8 must be false in order to use rhythms.
*/
byte OPL2::setDrums(bool bass, bool snare, bool tom, bool cymbal, bool hihat) {
byte drums = 0;
drums += bass ? DRUM_BASS : 0x00;
drums += snare ? DRUM_SNARE : 0x00;
drums += tom ? DRUM_TOM : 0x00;
drums += cymbal ? DRUM_CYMBAL : 0x00;
drums += hihat ? DRUM_HI_HAT : 0x00;
drums += bass ? DRUM_BITS_BASS : 0x00;
drums += snare ? DRUM_BITS_SNARE : 0x00;
drums += tom ? DRUM_BITS_TOM : 0x00;
drums += cymbal ? DRUM_BITS_CYMBAL : 0x00;
drums += hihat ? DRUM_BITS_HI_HAT : 0x00;
setRegister(0xBD, oplRegisters[0xBD] & ~drums);
return setRegister(0xBD, oplRegisters[0xBD] | drums);
}
Expand Down
21 changes: 15 additions & 6 deletions src/OPL2.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,18 @@
#define ADDITIVE_SYNTH true

// Drum sounds.
#define DRUM_BASS 0x10
#define DRUM_SNARE 0x08
#define DRUM_TOM 0x04
#define DRUM_CYMBAL 0x02
#define DRUM_HI_HAT 0x01
#define DRUM_BASS 0
#define DRUM_SNARE 1
#define DRUM_TOM 2
#define DRUM_CYMBAL 3
#define DRUM_HI_HAT 4

// Drum sound bit masks.
#define DRUM_BITS_BASS 0x10
#define DRUM_BITS_SNARE 0x08
#define DRUM_BITS_TOM 0x04
#define DRUM_BITS_CYMBAL 0x02
#define DRUM_BITS_HI_HAT 0x01

// Note to frequency mapping.
#define NOTE_C 0
Expand Down Expand Up @@ -195,6 +202,7 @@
byte setDeepTremolo(bool enable);
byte setDeepVibrato(bool enable);
byte setPercussion(bool enable);
byte setDrums(byte drums);
byte setDrums(bool bass, bool snare, bool tom, bool cymbal, bool hihat);
byte setWaveForm(byte channel, byte operatorNum, byte waveForm);

Expand Down Expand Up @@ -229,7 +237,7 @@
6, 7, 8, 8, 7
};
const byte drumBits[5] = {
0x10, 0x08, 0x04, 0x02, 0x01
DRUM_BITS_BASS, DRUM_BITS_SNARE, DRUM_BITS_TOM, DRUM_BITS_CYMBAL, DRUM_BITS_HI_HAT
};
const byte instrumentBaseRegs[6] = {
0x20, 0x40, 0x60, 0x80, 0xE0, 0xC0
Expand All @@ -241,6 +249,7 @@
const byte CHANNEL_MAX = 8;
const byte OCTAVE_MAX = 7;
const byte NOTE_MAX = 11;
const byte DRUM_SOUND_MAX = 5;
const short F_NUM_MIN = 0;
const short F_NUM_MAX = 1023;
const float VOLUME_MIN = 0.0;
Expand Down

0 comments on commit 4910ca9

Please sign in to comment.