“MIDI(电子琴)”的版本间的差异
来自Labplus盛思维基百科
Tangliufeng(讨论 | 贡献) |
Labplus课程组(讨论 | 贡献) (→图形化示例) |
||
(未显示1个用户的3个中间版本) | |||
第26行: | 第26行: | ||
1.RXD连接至microbit串口的TX,在定义串口引脚时需要注意下!<br/> | 1.RXD连接至microbit串口的TX,在定义串口引脚时需要注意下!<br/> | ||
2.串口波特率:31250bps <br/> | 2.串口波特率:31250bps <br/> | ||
− | 3.MIDI音符音色代码资料: | + | 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标准音乐码] | ||
第129行: | 第130行: | ||
midi_init(pin1) #设置串口tx引脚 | midi_init(pin1) #设置串口tx引脚 | ||
midi_set_instrument(58) #设置音色 | midi_set_instrument(58) #设置音色 | ||
− | midi_set_volume(100) #设置音量 | + | midi_set_volume(100) #设置音量,范围 0-127 |
while True: | while True: | ||
第140行: | 第141行: | ||
=== <small>图形化示例</small> === | === <small>图形化示例</small> === | ||
+ | {|- | ||
+ | |程序功能:播放钢琴的部分音符声音 | ||
+ | |- | ||
+ | | | ||
+ | [[文件:电子琴.png|900px|无框|左]] | ||
+ | |-} | ||
+ | |||
+ | == 版本历史记录 == | ||
+ | |||
+ | {| border="1" cellspacing="0" align="left" cellpadding="0" width="60%" style="text-align:center;" | ||
+ | |- style="text-align:center;background-color:#6fa8dc;color:#fffff;" | ||
+ | !width="10%"|Version !!width="15%"| Date !! Note <small>[+]新增[-]删除[^]修复</small> | ||
+ | |- | ||
+ | | V2.0 || || style="text-align:left"| | ||
+ | |} |
2018年5月4日 (五) 10:44的最新版本
概述
用于播放乐符,可模拟电子琴等各种乐器演奏效果
技术参数
- 工作电压:VCC 3.3-5V
- 通讯方式:UART
- 集成功放、双声道喇叭
- 模块尺寸:24x46x7.5mm
引脚定义
VCC | 电源 |
RXD | 串口接收 |
TXD | 串口发送 |
GND | 地 |
使用教程
1.RXD连接至microbit串口的TX,在定义串口引脚时需要注意下! |
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示例
连接示意图 |
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)
图形化示例
程序功能:播放钢琴的部分音符声音 | ||
Version | Date | Note [+]新增[-]删除[^]修复 |
---|---|---|
V2.0 |