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

来自Labplus盛思维基百科
跳转至: 导航搜索
(Tangliufeng移动页面电子琴模块电子琴覆盖重定向)
MicroPython示例
第85行: 第85行:
 
</pre>
 
</pre>
  
=== <small>MicroPython示例</small> ===
 
使用Bluebit主板控制MIDI模块中间需要加I2C转串口模块
 
<pre style="color:blue">
 
 
from microbit import *
 
from microbit import *
  
def midiInit(volume=0xb4):
+
#初始化
 +
def midiInit(volume=127):
 
     i2c.write(0x0c, bytearray([0x01, 0x12, 0x7a]))        # set baudrate 31250
 
     i2c.write(0x0c, bytearray([0x01, 0x12, 0x7a]))        # set baudrate 31250
     i2c.write(0x0c, bytearray([0x02, 0xb0, 0x79, 0x00]))  # reset
+
    sleep(2)
     i2c.write(0x0c, bytearray([0x02, 0xb0, 0x79, 0x7b]))  # close all
+
     i2c.write(0x0c, bytearray([0x02, 0xb0, 0x78, 0x00]))  # all sound off
 +
    sleep(2)
 +
     i2c.write(0x0c, bytearray([0x02, 0xb0, 0x79, 0x7f]))  # reset all controllers
 +
    sleep(2)
 
     i2c.write(0x0c, bytearray([0x02, 0xb0, 0x07, volume])) # set volume
 
     i2c.write(0x0c, bytearray([0x02, 0xb0, 0x07, volume])) # set volume
 +
    sleep(10)
  
 +
#设置音量(0-127)
 +
def midiSetVolume(vol):
 +
    i2c.write(0x0c, bytearray([0x02, 0xb0, 0x07, vol])) # set volume
 +
    sleep(2)
 +
   
 +
# 选择乐器
 
def midiChangeProgram(channel, ins): # select instrument
 
def midiChangeProgram(channel, ins): # select instrument
 
     i2c.write(0x0c, bytearray([0x02, (0xC0|(channel & 0x0F)), ins]))
 
     i2c.write(0x0c, bytearray([0x02, (0xC0|(channel & 0x0F)), ins]))
 +
    sleep(2)
  
def midiNoteOn(data1, data2=50, cmd=0x90):
+
# 播放音调
 +
def midiNoteOn(data1, data2=127, cmd=0x90):  
 
     i2c.write(0x0c, bytearray([0x02, cmd, data1, data2]))
 
     i2c.write(0x0c, bytearray([0x02, cmd, data1, data2]))
  
def midiNoteOff(data1, data2=50, cmd=0x80):
+
# 停止音调播放
 +
def midiNoteOff(data1, data2=0, cmd=0x80):
 
     i2c.write(0x0c, bytearray([0x02, cmd, data1, data2]))
 
     i2c.write(0x0c, bytearray([0x02, cmd, data1, data2]))
  
 
# test code
 
# test code
 
midiInit()
 
midiInit()
midiChangeProgram(0,41)
+
midiChangeProgram(0,40)
 +
midiSetVolume(40)
 
while True:
 
while True:
     midiNoteOn(69)
+
    '''
     sleep(500)
+
    #播放单个音调
     midiNoteOff(69)
+
    midiNoteOn(i)
     sleep(500)
+
    sleep(200)
</pre>
+
    midiNoteOff(i)
 +
   
 +
    sleep(2000)
 +
    '''
 +
   
 +
    #播放组合音调
 +
    midiNoteOn(61)
 +
     midiNoteOn(65)
 +
    midiNoteOn(75)
 +
     sleep(200)
 +
     midiNoteOff(61)
 +
    midiNoteOff(65)
 +
    midiNoteOff(75)
 +
   
 +
     sleep(2000)
 +
 
 
=== <small>图形化示例</small> ===
 
=== <small>图形化示例</small> ===

2017年12月27日 (三) 12:09的版本

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

概述

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

技术参数

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

引脚定义

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

使用教程

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);
}

from microbit import *

  1. 初始化

def midiInit(volume=127):

   i2c.write(0x0c, bytearray([0x01, 0x12, 0x7a]))         # set baudrate 31250
   sleep(2)
   i2c.write(0x0c, bytearray([0x02, 0xb0, 0x78, 0x00]))   # all sound off
   sleep(2)
   i2c.write(0x0c, bytearray([0x02, 0xb0, 0x79, 0x7f]))   # reset all controllers
   sleep(2)
   i2c.write(0x0c, bytearray([0x02, 0xb0, 0x07, volume])) # set volume
   sleep(10)
  1. 设置音量(0-127)

def midiSetVolume(vol):

   i2c.write(0x0c, bytearray([0x02, 0xb0, 0x07, vol])) # set volume
   sleep(2)
   
  1. 选择乐器

def midiChangeProgram(channel, ins): # select instrument

   i2c.write(0x0c, bytearray([0x02, (0xC0|(channel & 0x0F)), ins]))
   sleep(2)
  1. 播放音调

def midiNoteOn(data1, data2=127, cmd=0x90):

   i2c.write(0x0c, bytearray([0x02, cmd, data1, data2]))
  1. 停止音调播放

def midiNoteOff(data1, data2=0, cmd=0x80):

   i2c.write(0x0c, bytearray([0x02, cmd, data1, data2]))
  1. test code

midiInit() midiChangeProgram(0,40) midiSetVolume(40) while True:

    
   #播放单个音调
   midiNoteOn(i)
   sleep(200)
   midiNoteOff(i)
   
   sleep(2000)
   
   
   #播放组合音调
   midiNoteOn(61)
   midiNoteOn(65)
   midiNoteOn(75)
   sleep(200)
   midiNoteOff(61)
   midiNoteOff(65)
   midiNoteOff(75)
   
   sleep(2000)

图形化示例