“MIDI(电子琴)”的版本间的差异

来自Labplus盛思维基百科
跳转至: 导航搜索
Arduino示例
图形化示例
 
(未显示2个用户的15个中间版本)
第4行: 第4行:
  
 
== 技术参数 ==
 
== 技术参数 ==
* 工作电压:3.3V or 5V
+
* 工作电压:VCC 3.3-5V
 
* 通讯方式:UART
 
* 通讯方式:UART
 
* 集成功放、双声道喇叭
 
* 集成功放、双声道喇叭
第22行: 第22行:
  
 
== 使用教程 ==
 
== 使用教程 ==
MIDI音符音色代码资料:
+
{|style="background-color:#FCF8E3;color:#8A6D3B;"
 +
|style="padding: 2px;"|
 +
1.RXD连接至microbit串口的TX,在定义串口引脚时需要注意下!<br/>
 +
2.串口波特率:31250bps <br/>
 +
3.程序烧下载完成后须断电重启  <br/>
 +
4.MIDI音符音色代码资料:
 
* [http://wiki.labplus.cn/images/1/16/MIDI%E9%9F%B3%E7%AC%A6%E4%BB%A3%E7%A0%81%E8%A1%A8.zip MIDI音符代码表]
 
* [http://wiki.labplus.cn/images/1/16/MIDI%E9%9F%B3%E7%AC%A6%E4%BB%A3%E7%A0%81%E8%A1%A8.zip MIDI音符代码表]
 
* [http://wiki.labplus.cn/images/4/40/MIDI_%E6%A0%87%E5%87%86%E9%9F%B3%E4%B9%90%E7%A0%81.zip MIDI标准音乐码]
 
* [http://wiki.labplus.cn/images/4/40/MIDI_%E6%A0%87%E5%87%86%E9%9F%B3%E4%B9%90%E7%A0%81.zip MIDI标准音乐码]
 +
|}
 
=== <small>Arduino示例</small> ===
 
=== <small>Arduino示例</small> ===
 
* [http://wiki.labplus.cn/images/c/cf/MidiPlayPrj.zip Arduino midi库下载]
 
* [http://wiki.labplus.cn/images/c/cf/MidiPlayPrj.zip Arduino midi库下载]
第84行: 第90行:
 
}
 
}
 
</pre>
 
</pre>
 
+
=== <small>python示例</small> ===
=== <small>MicroPython示例</small> ===
+
:::
使用Bluebit主板控制MIDI模块中间需要加I2C转串口模块
+
{|
 +
|-
 +
|[[文件:MIDI 连接图.png|600px|无框]]
 +
|-
 +
|style="text-align:center"|'''连接示意图'''
 +
|}
 
<pre style="color:blue">
 
<pre style="color:blue">
 
from microbit import *
 
from microbit import *
  
def midiInit(volume=0xb4):
+
def midi_init(pin):
     i2c.write(0x0c, bytearray([0x01, 0x12, 0x7a]))         # set baudrate 31250
+
    uart.init(baudrate=31250,tx=pin)
     i2c.write(0x0c, bytearray([0x02, 0xb0, 0x79, 0x00]))   # reset
+
    sleep(30)
     i2c.write(0x0c, bytearray([0x02, 0xb0, 0x79, 0x7b]))   # close all
+
     uart.write(bytearray([0xb0,0x78,0x00]))
     i2c.write(0x0c, bytearray([0x02, 0xb0, 0x07, volume])) # set volume
+
     sleep(5)
 +
    uart.write(bytearray([0xb0,0x79,0x7f]))
 +
     sleep(15)
 +
   
 +
def midi_set_volume(vol):
 +
    uart.write(bytearray([0xb0,0x07,vol]))
 +
    sleep(10)
 +
   
 +
def midi_set_instrument(ins):
 +
    uart.write(bytearray([0xc0,ins]))
 +
    sleep(10)
 +
   
 +
def midi_note(note, on_off):
 +
    if on_off == 1:
 +
        uart.write(bytearray([0x90,note,0x7f]))
 +
     elif on_off == 0:
 +
        uart.write(bytearray([0x80,note,0x00]))
 +
    sleep(5)
 +
   
 +
#test code
 +
music=bytearray([60,62,64,60,60,62,64,60,64,65,67,64,65,67])    #两只老虎
 +
duration=bytearray([1,1,1,1,1,1,1,1,1,1,2,1,1,2])               #节拍
 +
music_len=len(music)
 +
midi_init(pin1)                            #设置串口tx引脚
 +
midi_set_instrument(58)                    #设置音色
 +
midi_set_volume(100)                       #设置音量,范围 0-127
  
def midiChangeProgram(channel, ins): # select instrument
+
while True:
    i2c.write(0x0c, bytearray([0x02, (0xC0|(channel & 0x0F)), ins]))
+
    for i in range(music_len):
 +
        midi_note(music[i],1)
 +
        sleep(200*duration[i])
 +
        midi_note(music[i],0)
 +
        sleep(200)
 +
</pre>
  
def midiNoteOn(data1, data2=50, cmd=0x90):
+
=== <small>图形化示例</small> ===
    i2c.write(0x0c, bytearray([0x02, cmd, data1, data2]))
+
{|-
 +
|程序功能:播放钢琴的部分音符声音
 +
|-
 +
|
 +
[[文件:电子琴.png|900px|无框|左]]
 +
|-}
  
def midiNoteOff(data1, data2=50, cmd=0x80):
+
== 版本历史记录 ==
    i2c.write(0x0c, bytearray([0x02, cmd, data1, data2]))
 
  
# test code
+
{|  border="1" cellspacing="0" align="left" cellpadding="0" width="60%" style="text-align:center;"
midiInit()
+
|- style="text-align:center;background-color:#6fa8dc;color:#fffff;"
midiChangeProgram(0,41)
+
!width="10%"|Version !!width="15%"| Date !! Note  <small>[+]新增[-]删除[^]修复</small>
while True:
+
|-
    midiNoteOn(69)
+
| V2.0 || || style="text-align:left"|
    sleep(500)
+
|}
    midiNoteOff(69)
 
    sleep(500)
 
</pre>
 
=== <small>图形化示例</small> ===
 

2018年5月4日 (五) 10:44的最新版本

黑色传感器最终版12.20-05.png

概述

用于播放乐符,可模拟电子琴等各种乐器演奏效果

技术参数

  • 工作电压:VCC 3.3-5V
  • 通讯方式:UART
  • 集成功放、双声道喇叭
  • 模块尺寸:24x46x7.5mm

引脚定义

VCC 电源
RXD 串口接收
TXD 串口发送
GND

使用教程

1.RXD连接至microbit串口的TX,在定义串口引脚时需要注意下!
2.串口波特率:31250bps
3.程序烧下载完成后须断电重启
4.MIDI音符音色代码资料:

Arduino示例

#include <SoftwareSerial.h>
#include "vs1103b.h"


#define NOTE_C3 60
#define NOTE_D3 62
#define NOTE_E3 64
#define NOTE_F3 65
#define NOTE_G3 67
#define NOTE_A3 69
#define NOTE_B3 71
#define NOTE_C4 72
#define NOTE_D4 74
#define NOTE_E4 76
#define NOTE_F4 77
#define NOTE_G4 79
#define NOTE_A4 81
#define NOTE_B4 83

#define VELOCITY1   0x7F
#define VELOCITY2   0x00

uint8_t noteArr[14] = { 60, 62, 64, 65, 67, 69, 71, 72, 74, 76, 77, 79, 81, 83 };


Vs1103bClass vs1103b(A5, A4); //RX TX
;
void midiVolume(uint8_t vol);

void setup()
{
	vs1103b.begin();
	vs1103b.usartSendChannelMsg(CHANNEL0, MSG_CONTROL_CHANGE, DAT1_RESET, 0); //复位
	vs1103b.usartSendChannelMsg(CHANNEL0, MSG_CONTROL_CHANGE, DAT1_NOTE_OFF, 0); //关闭所有音
	midiVolume(180);  //音量设置
}

void loop()
{
	/*音调测试*/
	for (int i = 0; i < 14; i++)
	{
		vs1103b.usartSendChannelMsg(CHANNEL0, MSG_NOTE_OPEN, noteArr[i], 0x7F);
		delay(500);
		vs1103b.usartSendChannelMsg(CHANNEL0, MSG_NOTE_OPEN, noteArr[i], 0x00);
		delay(500);
	}

}

void midiVolume(uint8_t vol)
{
	vs1103b.usartSendChannelMsg(CHANNEL0, MSG_CONTROL_CHANGE, 0x07, vol);
	delay(2);
}

python示例

MIDI 连接图.png
连接示意图
from microbit import *

def midi_init(pin):
    uart.init(baudrate=31250,tx=pin)
    sleep(30)
    uart.write(bytearray([0xb0,0x78,0x00]))
    sleep(5)
    uart.write(bytearray([0xb0,0x79,0x7f]))
    sleep(15)
    
def midi_set_volume(vol):
    uart.write(bytearray([0xb0,0x07,vol]))
    sleep(10)
    
def midi_set_instrument(ins):
    uart.write(bytearray([0xc0,ins]))
    sleep(10)
    
def midi_note(note, on_off):
    if on_off == 1:
        uart.write(bytearray([0x90,note,0x7f]))
    elif on_off == 0:
        uart.write(bytearray([0x80,note,0x00]))
    sleep(5)
    
#test code 
music=bytearray([60,62,64,60,60,62,64,60,64,65,67,64,65,67])     #两只老虎
duration=bytearray([1,1,1,1,1,1,1,1,1,1,2,1,1,2])                #节拍
music_len=len(music)
midi_init(pin1)                             #设置串口tx引脚
midi_set_instrument(58)                     #设置音色
midi_set_volume(100)                        #设置音量,范围 0-127

while True:
    for i in range(music_len):
        midi_note(music[i],1)
        sleep(200*duration[i])
        midi_note(music[i],0)
        sleep(200)

图形化示例

版本历史记录

程序功能:播放钢琴的部分音符声音
电子琴.png
Version Date Note [+]新增[-]删除[^]修复
V2.0