Arduino送Midi訊號給電腦發音,Part 2: 同時發出數個音符

Просмотров: 31   |   Загружено: 1 год.
icon
Y. Eugene (SML)
icon
0
icon
Скачать
iconПодробнее о видео
前情提要:


/*
* MIDIUSB_test.ino
*
* Created: 4/6/2015 10:47:08 AM
* Author: gurbrinder grewal
* Modified by Arduino LLC (2015)
*/

#include "MIDIUSB.h"

// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}

void setup() {
Serial.begin(115200);
}

// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}

void loop() {
Serial.println("Sending note on");
noteOn(0, 48, 64); // Channel 0, middle C, normal velocity
MidiUSB.flush();
delay(500);

Serial.println("Sending note off");
noteOff(0, 48, 64); // Channel 0, middle C, normal velocity
MidiUSB.flush();
delay(500);

Serial.println("Sending note on");
noteOn(0, 50, 64); // Channel 0, middle D, normal velocity
MidiUSB.flush();
delay(500);

Serial.println("Sending note off");
noteOff(0, 50, 64); // Channel 0, middle D, normal velocity
MidiUSB.flush();
delay(500);

Serial.println("Sending note on");
noteOn(0, 52, 64); // Channel 0, middle E, normal velocity
MidiUSB.flush();
delay(500);

Serial.println("Sending note off");
noteOff(0, 52, 64); // Channel 0, middle E, normal velocity
MidiUSB.flush();
delay(500);

Serial.println("Sending note on");
noteOn(0, 53, 64); // Channel 0, middle F, normal velocity
MidiUSB.flush();
delay(500);

Serial.println("Sending note off");
noteOff(0, 53, 64); // Channel 0, middle F, normal velocity
MidiUSB.flush();
delay(500);

Serial.println("Sending note on");
noteOn(0, 55, 64); // Channel 0, middle G, normal velocity
MidiUSB.flush();
delay(500);

Serial.println("Sending note off");
noteOff(0, 55, 64); // Channel 0, middle G, normal velocity
MidiUSB.flush();
delay(500);

// 以下是同時發音時的寫法。注意!noteOn的寫法,在同時發音的音符之間要有個小小的dly
// 以便讓音符串流能夠順利地寫出,能有百萬分之100秒(0.0001秒)即可
int dly = 100;

Serial.println("Sending note on");
noteOn(0, 48, 124); // Channel 0, middle C, normal velocity
// MidiUSB.flush();
delayMicroseconds(dly);
noteOn(0, 52, 124); // Channel 0, middle E, normal velocity
// MidiUSB.flush();
delayMicroseconds(dly);
noteOn(0, 55, 124); // Channel 0, middle G, normal velocity
MidiUSB.flush();
delay(1000);

Serial.println("Sending note off");
noteOff(0, 48, 124); // Channel 0, middle C, normal velocity
// MidiUSB.flush();
delayMicroseconds(dly);
noteOff(0, 52, 124); // Channel 0, middle E, normal velocity
// MidiUSB.flush();
delayMicroseconds(dly);
noteOff(0, 55, 124); // Channel 0, middle G, normal velocity
MidiUSB.flush();
delay(500);


delay(500);

// controlChange(0, 10, 65); // Set the value of controller 10 on channel 0 to 65
}

Похожие видео

Добавлено: 55 год.
Добавил:
  © 2019-2021
  Arduino送Midi訊號給電腦發音,Part 2: 同時發出數個音符 - RusLar.Me