MIDI

はじめに

シリアルポートにMIDIデータを送信します。

ArduinoとPCとをUSBケーブルで接続します。

プログラム

定義等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
  MIDI note player

  This sketch shows how to use the serial transmit pin (pin 1) to send MIDI note data.
  If this circuit is connected to a MIDI synth, it will play the notes
  F#-0 (0x1E) to F#-5 (0x5A) in sequence.

  The circuit:
  - digital in 1 connected to MIDI jack pin 5
  - MIDI jack pin 2 connected to ground
  - MIDI jack pin 4 connected to +5V through 220 ohm resistor
  - Attach a MIDI cable to the jack, then to a MIDI synth, and play music.

  created 13 Jun 2006
  modified 13 Aug 2012
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Midi
*/
  

このプログラムでは特に何もしていません。

setup()

23
24
25
26
27
void setup() {
  // Set MIDI baud rate:
  Serial.begin(31250);
}
 

Serial.begin()を使って、シリアルポートを初期化します。MIDIなので、31250bpsに設定しています。

loop()

28
29
30
31
32
33
34
35
36
37
38
39
void loop() {
  // play notes from F#-0 (0x1E) to F#-5 (0x5A):
  for (int note = 0x1E; note < 0x5A; note ++) {
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
    delay(100);
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00);
    delay(100);
  }
}
 

for文を使って、変数noteを0x1Eから0x50まで順に変化させます。

noteOn()という関数を、1回目は引数0x90、note、0x45で、2回目は引数0x90、note、0x00で呼び出します。noteOn()を呼ぶ間隔をdelay()で調整しています。

noteOn()

40
41
42
43
44
45
46
// plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that
// data values are less than 127:
void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

noteOn()という関数を作成しています。この関数の中では、Serial.write()を使って、与えられた引数を順に送信します。ここで送信しているのは、MIDIのノートオンというコマンドです。音源に対して、指定した音階(pitch)を指定した強さ(velocity)で演奏するよう指示します。

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system