Skip to content

Commit 0593fb8

Browse files
committed
vs1053 streaming midi
1 parent 7818061 commit 0593fb8

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed

examples/examples-vs1053/streams-memory_midi-vs1053/streams-memory_midi-vs1053.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file streams-memory_midi-vs1953.ino
33
* @author Phil Schatzmann
4-
* @brief The module can play Midi Files: Compile with Partition Scheme Hughe APP.
4+
* @brief The module can play Midi Files: Compile with Partition Scheme Hughe APP. The midi file was converted with xxd to a header file.
55
* It seems that only midi files type 0 are supported.
66
* @version 0.1
77
* @date 2021-01-24
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @file streams-midi-vs1053.ino
3+
* @author Phil Schatzmann
4+
* @brief Demo that shows how to send midi commands to the vs1053
5+
* @version 0.1
6+
* @date 2022-08-23
7+
*
8+
* @copyright Copyright (c) 2022
9+
*
10+
*/
11+
#include "AudioTools.h"
12+
#include "AudioLibs/VS1053Stream.h"
13+
14+
15+
VS1053Stream vs1053; // final output
16+
MidiStreamOut out(vs1053);
17+
uint16_t note = 65; // 0 to 128
18+
uint16_t amplitude = 128; // 0 to 128
19+
int channel = 0;
20+
21+
//Send a MIDI note-on message. Like pressing a piano key
22+
void noteOn(uint8_t channel, uint8_t note, uint8_t attack_velocity=100) {
23+
vs1053.sendMidiMessage( (0x90 | channel), note, attack_velocity);
24+
}
25+
26+
//Send a MIDI note-off message. Like releasing a piano key
27+
void noteOff(uint8_t channel, uint8_t note, uint8_t release_velocity=100) {
28+
vs1053.sendMidiMessage( (0x80 | channel), note, release_velocity);
29+
}
30+
31+
32+
void setup() {
33+
Serial.begin(115200);
34+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
35+
36+
// setup vs1053
37+
auto cfg = vs1053.defaultConfig();
38+
cfg.is_midi = true; // vs1053 is accepting streaming midi data
39+
// Use your custom pins or define in AudioCodnfig.h
40+
//cfg.cs_pin = VS1053_CS;
41+
//cfg.dcs_pin = VS1053_DCS;
42+
//cfg.dreq_pin = VS1053_DREQ;
43+
//cfg.reset_pin = VS1053_RESET;
44+
vs1053.begin(cfg);
45+
46+
}
47+
48+
void loop() {
49+
Serial.println();
50+
Serial.print("playing ");
51+
Serial.println(++note);
52+
53+
out.noteOn(channel, note, amplitude );
54+
delay(900);
55+
out.noteOff(channel, note, 20 );
56+
delay(200);
57+
if (note>=90) {
58+
note = 64;
59+
}
60+
}

src/AudioLibs/VS1053Stream.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class VS1053BaseStream : public AudioStreamX {
4444
digitalWrite(_reset_pin, HIGH);
4545
delay(200);
4646
}
47+
4748

4849
p_vs1053->begin();
4950
p_vs1053->startSong();
@@ -200,7 +201,7 @@ class VS1053Config : public AudioBaseInfo {
200201
uint8_t cs_sd_pin = VS1053_CS_SD;
201202
RxTxMode mode;
202203
bool is_encoded_data = false;
203-
bool is_midi_mode = false;
204+
bool is_midi = false;
204205
bool is_start_spi = true;
205206
};
206207

@@ -237,12 +238,13 @@ class VS1053Stream : public AudioStreamX {
237238
LOGI(LOG_METHOD);
238239
bool result = true;
239240
// enfornce encoded data for midi mode
240-
if (cfg.is_midi_mode){
241+
if (cfg.is_midi){
241242
cfg.is_encoded_data = true;
242243
}
243244
this->cfg = cfg;
244245
setAudioInfo(cfg);
245246
LOGI("is_encoded_data: %s", cfg.is_encoded_data?"true":"false");
247+
LOGI("is_midi: %s", cfg.is_midi?"true":"false");
246248
LOGI("cs_pin: %d", cfg.cs_pin);
247249
LOGI("dcs_pin: %d", cfg.dcs_pin);
248250
LOGI("dreq_pin: %d", cfg.dreq_pin);
@@ -260,11 +262,11 @@ class VS1053Stream : public AudioStreamX {
260262
// hack to treat midi as separate mode
261263
const int MIDI_MODE = 100;
262264
int mode = cfg.mode;
263-
if (cfg.is_midi_mode){
265+
if (cfg.is_midi){
264266
mode = MIDI_MODE;
265267
}
266268

267-
switch(cfg.mode){
269+
switch(mode){
268270

269271
case TX_MODE:
270272
p_out->begin(cfg);
@@ -273,6 +275,8 @@ class VS1053Stream : public AudioStreamX {
273275
break;
274276
#if VS1053_EXT
275277
case MIDI_MODE:
278+
LOGI("Using MIDI")
279+
p_out->begin(cfg);
276280
getVS1053().beginMIDI();
277281
delay(100);
278282
setVolume(VS1053_DEFAULT_VOLUME);
@@ -310,7 +314,7 @@ class VS1053Stream : public AudioStreamX {
310314

311315
/// Sets the volume: value from 0 to 1.0
312316
void setVolume(float volume){
313-
LOGI(LOG_METHOD);
317+
LOGI("setVolume: %f", volume);
314318
if (p_driver==nullptr) {
315319
logError(__FUNCTION__);
316320
return;
@@ -438,6 +442,21 @@ class VS1053Stream : public AudioStreamX {
438442
}
439443
p_driver->setBassFrequencyLimit(value);
440444
}
445+
446+
/// Sends a midi message to the VS1053
447+
void sendMidiMessage(uint8_t cmd, uint8_t data1, uint8_t data2) {
448+
LOGI(LOG_METHOD);
449+
if (!cfg.is_midi){
450+
LOGE("start with is_midi=true");
451+
return;
452+
}
453+
if (p_driver==nullptr) {
454+
logError(__FUNCTION__);
455+
return;
456+
}
457+
getVS1053().sendMidiMessage(cmd, data1, data2);
458+
}
459+
441460
#endif
442461

443462
protected:

0 commit comments

Comments
 (0)