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

Midi feature and example #83

Open
wants to merge 20 commits 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ Then initialize the player and use as in following example:

```c++
player.begin();
player.switchToMp3Mode();
if (player.getChipVersion() == 4) { // Only perform an update if we really are using a VS1053, not. eg. VS1003
player.loadDefaultVs1053Patches();
}
player.setVolume(VOLUME);
player.switchToMp3Mode();
player.playChunk(sampleMp3, sizeof(sampleMp3));
```

Expand Down
201 changes: 201 additions & 0 deletions examples/MidiDemo/MidiDemo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/**
A simple example to use ESP_VS1053_Library for MIDI
https://github.com/baldram/ESP_VS1053_Library

Copyright (C) 2021 M. Hund (github.com/Dr-Dawg)

Licensed under GNU GPLv3 <http://gplv3.fsf.org/>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License or later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.


Wiring:
--------------------------------
| VS1053 | ESP8266 | ESP32 |
--------------------------------
| SCK | D5 | IO18 |
| MISO | D6 | IO19 |
| MOSI | D7 | IO23 |
| XRST | RST | EN |
| CS | D1 | IO5 |
| DCS | D0 | IO16 |
| DREQ | D3 | IO4 |
| 5V | 5V | 5V |
| GND | GND | GND |
--------------------------------

Note: It's just an example, you may use a different pins definition.


To run this example define the platformio.ini as below.

[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
lib_deps =
ESP_VS1053_Library

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
ESP_VS1053_Library

*/

// This ESP_VS1053_Library
#include <VS1053.h>

#include "patches/rtmidi1003b.h"
#include "patches/rtmidi1053b.h"

// Wiring of VS1053 board (SPI connected in a standard way)
#ifdef ARDUINO_ARCH_ESP8266
#define VS1053_CS D1
#define VS1053_DCS D0
#define VS1053_DREQ D3
#endif

#ifdef ARDUINO_ARCH_ESP32
#define VS1053_CS 5
#define VS1053_DCS 16
#define VS1053_DREQ 4
#endif

#define VOLUME 100 // volume level 0-100

VS1053 player(VS1053_CS, VS1053_DCS, VS1053_DREQ);

// noteOn and noteOff based on MP3_Shield_RealtimeMIDI demo
// by Matthias Neeracher, Nathan Seidle's Sparkfun Electronics example respectively

//Send a MIDI note-on message. Like pressing a piano key
void noteOn(uint8_t channel, uint8_t note, uint8_t attack_velocity) {
player.sendMidiMessage( (0x90 | channel), note, attack_velocity);
}

//Send a MIDI note-off message. Like releasing a piano key
void noteOff(uint8_t channel, uint8_t note, uint8_t release_velocity) {
player.sendMidiMessage( (0x80 | channel), note, release_velocity);
}

void setup() {

Serial.begin(115200);

// initialize SPI
SPI.begin();

// initialize the player
player.begin();

if(player.getChipVersion() == 4)
Serial.println("Hello VS1053!");
else if(player.getChipVersion()==3)
Serial.println("Hello VS1003!");
else
Serial.println("Please check whether your device is properly connected!");


if(player.getChipVersion()==4 /*4*/) { // MIDI using a VS1053 chip
player.loadUserCode(MIDI1053, MIDI1053_SIZE);
player.writeRegister(0xA /*SCI_AIADDR*/, 0x50); // setting VS1053 Start adress for user code
Serial.println(" MIDI plugin VS1053 loaded");
}

if(player.getChipVersion()==3) { // MIDI using a VS1003 chip
player.loadUserCode(MIDI1003, MIDI1003_SIZE);
player.writeRegister(0xA /*SCI_AIADDR*/, 0x30); // setting VS1003 Start adress for user code
Serial.println(" MIDI plugin VS1003 loaded");
}

player.setVolume(VOLUME);

}

void loop() {

uint8_t channel = (uint8_t) random(16);
//channel = 9; //uncomment this, if you just want to hear percussion
uint8_t instrument = (uint8_t) random(128);
uint8_t note = (uint8_t) random(128);
uint8_t attack_velocity = (uint8_t) random(128);
uint8_t release_velocity = (uint8_t) random(128);
unsigned long duration = (unsigned long) (300 + random(2000));


/** MIDI messages, 0x80 to 0xEF Channel Messages, 0xF0 to 0xFF System Messages
* a MIDI message ranges from 1 byte to three bytes
* the first byte consists of 4 command bits and 4 channel bits, i.e. 16 channels
*
* 0x80 Note Off
* 0x90 Note On
* 0xA0 Aftertouch
* 0xB0 Continuous controller
* 0xC0 Patch change
* 0xD0 Channel Pressure
* 0xE0 Pitch bend
* 0xF0 (non-musical commands)
*/

/** 0xB0 Continuous controller commands, 0-127
* 0 Bank Select (MSB)
* 1 Modulation Wheel
* 2 Breath controller
* 3 Undefined
* 4 Foot Pedal (MSB)
* 5 Portamento Time (MSB)
* 6 Data Entry (MSB)
* 7 Volume (MSB)
* 8 Balance (MSB)
* 9 Undefined
* 10 Pan position (MSB)
* 11 Expression (MSB)
* ...
*/

// Continuous controller, set channel volume to high, i.e. 127
player.sendMidiMessage(0xB0| channel, 0x07, 127);

// Continuous controller 0, bank select: 0 gives you the default bank depending on the channel
// 0x78 (percussion) for Channel 10, i.e. channel = 9 , 0x79 (melodic) for other channels
player.sendMidiMessage(0xB0| channel, 0, 0x00); //0x00 default bank

// Patch change, select instrument
player.sendMidiMessage(0xC0| channel, instrument, 0);

// Serial output for the actual parameters
Serial.print("Channel: ");
Serial.println(channel, DEC);
Serial.print("Instrument: ");
Serial.println(instrument, DEC);
Serial.print("Note: ");
Serial.println(note, DEC);
Serial.print("Attack: ");
Serial.println(attack_velocity, DEC);
Serial.print("Release: ");
Serial.println(release_velocity, DEC);
Serial.print("Duration: ");
Serial.print(duration, DEC);
Serial.println(" milliseconds: ");
Serial.println("--------------------------------------");

//playing one (pseudo-)random note
noteOn(channel, note, attack_velocity);
delay(duration);
noteOff(channel, note, release_velocity);
delay(100);

}
89 changes: 89 additions & 0 deletions examples/MidiDemo/patches/rtmidi1003b.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#ifndef SKIP_PLUGIN_VARNAME
const unsigned short MIDI1003[] = { /* Compressed plugin */
#endif
0x0007,0x0001, /*copy 1*/
0x8030,
0x0006,0x01ee, /*copy 494*/
0x2800,0x38c0,0x0006,0x2016,0x3613,0x0024,0x0006,0x0057,
0x3e15,0x1c15,0x0020,0x1fd4,0x3580,0x3802,0xf204,0x3804,
0x0fff,0xfe44,0xa244,0x1804,0xf400,0x4094,0x2800,0x0f85,
0x3009,0x1bc2,0xf400,0x4500,0x2000,0x0000,0x36f5,0x3c15,
0x3009,0x3857,0x0030,0x0a57,0x3e14,0xf806,0x3701,0x8024,
0x0006,0x0017,0x3e04,0x9c13,0x0020,0x1fd2,0x3b81,0x8024,
0x36f4,0xbc13,0x36f4,0xd806,0x0030,0x0717,0x2100,0x0000,
0x3f05,0xdbd7,0x3613,0x0024,0x3e22,0xb815,0x3e05,0xb814,
0x3615,0x0024,0x3405,0x9014,0x36e3,0x0024,0x2000,0x0000,
0x36f2,0x9815,0x3613,0x0024,0x3e22,0xb815,0x3e05,0xb814,
0x3615,0x0024,0x3405,0x9014,0x36e3,0x0024,0x2000,0x0000,
0x36f2,0x9815,0x3613,0x0024,0x3e12,0xb817,0x3e12,0x3815,
0x3e05,0xb814,0x3615,0x0024,0x0000,0x800a,0x3e10,0x3801,
0x3e10,0xb804,0x3e01,0x7810,0x0030,0x00d0,0x2900,0x0400,
0x3001,0x0024,0x4080,0x03cc,0x3000,0x0024,0x2800,0x36c5,
0x4090,0x0024,0x0000,0x0024,0x2800,0x2485,0x0000,0x0024,
0x0000,0x0081,0x3000,0x0024,0x6012,0x0024,0x0000,0x0401,
0x2800,0x3305,0x0000,0x0024,0x6012,0x0024,0x0000,0x0024,
0x2800,0x2885,0x0000,0x0024,0x2900,0x0c80,0x0000,0x0024,
0x4088,0x008c,0x0000,0x2000,0x6400,0x0024,0x0000,0x3c00,
0x2800,0x2118,0x0000,0x0024,0x2800,0x2540,0x3801,0x0024,
0x6400,0x038c,0x0000,0x0024,0x2800,0x2558,0x0000,0x0024,
0x3013,0x0024,0x2900,0x0400,0x3801,0x0024,0x4080,0x0024,
0x0000,0x0024,0x2800,0x2495,0x0000,0x0024,0x6890,0x03cc,
0x2800,0x36c0,0x3800,0x0024,0x2900,0x0c80,0x0030,0x0110,
0x3800,0x0024,0x0000,0x3c00,0x6400,0x0024,0x003f,0xff00,
0x2800,0x2d48,0x0000,0x0024,0x0000,0x3fc0,0x6400,0x0024,
0x0000,0x3c00,0x2800,0x3605,0x6400,0x0024,0x0000,0x0024,
0x2800,0x3615,0x0000,0x0024,0xb880,0x184c,0x2900,0x0400,
0x3009,0x3800,0x4082,0x9bc0,0x6014,0x0024,0x0000,0x3c04,
0x2800,0x2b81,0x0000,0x3dc1,0x2900,0x0c80,0x0000,0x0024,
0xf400,0x4004,0x0000,0x3dc1,0x6412,0x0024,0x0030,0x0090,
0x2800,0x2cc5,0x0000,0x0000,0x0000,0x0400,0x2800,0x36c0,
0x3800,0x0024,0x0030,0x00d0,0x3001,0x4024,0xa50a,0x0024,
0x0000,0x03c0,0xb50a,0x0024,0x0000,0x0300,0x6500,0x0024,
0x0000,0x0024,0x2900,0x0408,0x0000,0x3188,0x0000,0x0380,
0x6500,0x0024,0x0000,0x0024,0x2800,0x33d5,0x0000,0x0024,
0x2900,0x0400,0x0000,0x0024,0x4080,0x03cc,0x0000,0x0080,
0x2800,0x3315,0x0000,0x0024,0x2800,0x36c0,0x3800,0x0024,
0x2900,0x0c80,0x0000,0x0024,0x408a,0x0024,0x0030,0x0110,
0x3613,0x0024,0x3e11,0x4024,0x30f0,0x0024,0x3e10,0x0024,
0x3000,0x4024,0x2926,0xa140,0x3e00,0x4024,0x36d3,0x0024,
0x0000,0x0000,0x0030,0x0090,0x3800,0x0024,0x36f1,0x5810,
0x36f0,0x9804,0x36f0,0x1801,0x3405,0x9014,0x36f3,0x0024,
0x36f2,0x1815,0x2000,0x0000,0x36f2,0x9817,0x002b,0x1105,
0x0030,0x00d2,0x0030,0x0690,0x0030,0x0291,0x0000,0x0084,
0x0006,0x6853,0x3e05,0xb814,0x3635,0x0024,0x0000,0x800a,
0xb880,0x0024,0x3800,0x0024,0x3910,0x0024,0x0000,0xc0c0,
0x3900,0x0024,0x0006,0x0051,0x0030,0x0000,0x2910,0x3740,
0x3a00,0x0024,0xb880,0x010c,0x38e0,0x184c,0x3800,0x0024,
0x3800,0x0024,0x0000,0x0f00,0x0006,0x0010,0x3009,0x2c00,
0x0003,0x1e40,0x3009,0x2410,0x0006,0x0011,0x3009,0x2410,
0x0030,0x0ad0,0x3800,0x0024,0x0000,0x09c0,0x0030,0x0690,
0x2910,0x7780,0x3800,0x0024,0x0001,0x0010,0xb882,0x0024,
0x291d,0x5800,0x0002,0xdc00,0x001f,0xff00,0x0003,0xda90,
0x2926,0x1440,0x3800,0x0024,0x2926,0x0e40,0x0030,0x0490,
0x6890,0x0024,0x3800,0x0024,0x2900,0x1740,0x0000,0x8001,
0x2919,0x8180,0x3613,0x0024,0x6012,0x0024,0x0003,0xd950,
0x2800,0x4498,0x0000,0x0024,0x2919,0x8180,0x3613,0x104c,
0x2928,0xce40,0x3c00,0x0024,0x0000,0x1000,0x2900,0x0a80,
0x34f1,0x0024,0xb882,0x0042,0x30f0,0xc024,0x4dc2,0x0024,
0x3810,0x0024,0x2800,0x4480,0x38f0,0x4024,
0x0007,0x0001, /*copy 1*/
0x5800,
0x0006, 0x8002, 0x1800, /*Rle(2)*/
0x0007,0x0001, /*copy 1*/
0x8010,
0x0006,0x0016, /*copy 22*/
0xf400,0x4095,0x0006,0x0017,0x3009,0x1c40,0x3009,0x1fc2,
0x6020,0x0024,0x0000,0x1fc2,0x2000,0x0000,0xb020,0x4542,
0x3009,0x3857,0x2800,0x1080,0x0030,0x0457,
0x0007,0x0001, /*copy 1*/
0x8025,
0x0006,0x0002, /*copy 2*/
0x2a00,0x100e,
0x0007,0x0001, /*copy 1*/
0x8022,
0x0006,0x0002, /*copy 2*/
0x2a00,0x060e,
#define MIDI1003_SIZE 546
#ifndef SKIP_PLUGIN_VARNAME
};
#endif
Loading