LoginSignup
12
10

More than 5 years have passed since last update.

Arduino UNOで作る一番簡単なMIDI音源

Last updated at Posted at 2015-11-11

自分で作るMIDI音源 Hello World

2015.11.12 某イベント向けチュートリアル

MIDI音源という、ちょっと古めかしい言い方にピーンと来たあなた!
Arduinoを使えば簡単に作れちゃいますよ!
というわけで、作ってみましょう。

こんなのが簡単に作れます。

ヘタクソな演奏例w

用意するもの

  • Arduino UNO Rev.3 (mocoLUFAを焼き込んだもの)
  • しょぼいスピーカー
  • USBケーブル (ArduinoとPC/Macを繋ぐやつ)
  • PC/Mac
  • ISP端子をショートするためのジャンパーピン

mocoLUFAの焼き方は かわいさんの手順書あたりを参考にしてください!

作り方

ハード!

Arduinoの11番ピンにスピーカーのプラス側を、ArduinoのGNDピンにスピーカーのマイナス側を繋いでください。

以上!!

スケッチ!

下記スケッチをArduinoに書き込んでください

// MIDI to Tone Sounder
// 2015.11.11 D.F.Mac.

#include <avr/pgmspace.h> 
#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

// Note Number -> Frequency
PROGMEM const int freqs[] = {
  8,8,9,9,10,11,11,12,13,14,15,15,16,17,18,19,
  21,22,23,24,26,28,29,31,33,35,37,39,41,44,46,49,
  52,55,58,62,65,69,73,78,82,87,93,98,104,110,117,123,
  131,139,147,156,165,175,185,196,208,220,233,247,262,277,294,311,
  330,349,370,392,415,440,466,494,523,554,587,623,659,698,740,784,
  831,880,932,988,1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976,
  2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,4186,4435,4699,4978,
  5274,5588,5920,6272,6645,7040,7459,7902,8372,8870,9397,9956,10548,11175,11840,12544
};

const int SPEAKER = 11;      // Speaker PIN 
const int LED = 13;
int isplay = 0;

void setup() {
  pinMode(SPEAKER,OUTPUT);  // Speaker
  pinMode(LED,OUTPUT);      // LED
  MIDI.begin();             // Start MIDI
  MIDI.turnThruOff();       // MIDI Thru Off
}

void loop() {
  if(MIDI.read()) {
    int kind = MIDI.getType();
    if(kind==midi::NoteOn){
      digitalWrite(LED,HIGH);
      isplay ++;
      int playnote = MIDI.getData1();
      int freq = pgm_read_word(freqs + playnote);
      tone(SPEAKER,freq);
    }else if(kind==midi::NoteOff){
      digitalWrite(LED,LOW);
      isplay --;
      if(isplay == 0){
        noTone(SPEAKER);
      }
    }
  }
}

鳴らすには!?

MIDIキーボードを繋いで、MIDIデバイスから「MIDI/MOCO for LUFA」を選んで弾くだけ!

しょぼいビープ音(単音)が鳴ったら成功!

MIDIキーボード持ってきてねー!

https://ryoyakawai.github.io/x-webmidi/src/

次は!?

こちらも是非!

[WIP] Arduinoで音を出したい(メモ)

12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10