Roland钢琴开发中音符值、度、与音名之间的转换算法
在Roland钢琴伴侣的开发中,首先将mid文件解析出来取到每一个音符的起始时间,每一个音符的时值,音符值(比如中央C的值是60),在绘五线谱的时候需要将每一个音符值与它对应的度(octave)和音名之间相互转换。
WhiteNote.h
/**@class WhiteNote
* The WhiteNote class represents a white keynote, a non-sharp,non-flat note. Todisplay midi notes as sheet music, the notes
must be converted to white notes andaccidentals.
*
* White notes consist of a letter (A thru G)and an octave (0 thru 10).
* The octave changes from G to A. After G2 comes A3. Middle-C is C4.
*
* The main operations are calculatingdistances between notes, and comparing notes.
*/
/*The table below is very important,you must be understand it.
@class WhiteNote includes two attributes(one is ‘letter’,another is‘Octave’)
int letter; /* The letter of the note, A thru G */
int octave; /* The octave, 0 thru 10. */
*/
这个类里面有一个很重要的枚举类型:
/** Enumeration of the notes in a scale (A, A#, ... G#) */
enum {
NoteScale_A = ,
NoteScale_Asharp = ,
NoteScale_Bflat = ,
NoteScale_B = ,
NoteScale_C = ,
NoteScale_Csharp = ,
NoteScale_Dflat = ,
NoteScale_D = ,
NoteScale_Dsharp = ,
NoteScale_Eflat = ,
NoteScale_E = ,
NoteScale_F = ,
NoteScale_Fsharp = ,
NoteScale_Gflat = ,
NoteScale_G = ,
NoteScale_Gsharp = ,
NoteScale_Aflat =
};
这个枚举里面的值和下表里面的A A# B C C# D D# E F F# G G# 这些值一一对应。
/**Convert a note (A, A#, B, etc) and octave into a Midi Note number.
*/
int notescale_to_number(int notescale, int octave) {
+ notescale +octave * ;
}
notescale对应上面枚举里面的值,octave对应下表的组数,举一个例,我们找到第四组的的C,在上面枚举中查找C的值和NoteScale_C的值相等(等于3),
所以notescale_to_number(NoteScale_C, ) = 60,刚好和第四组的C的音符值对应。这是一个通过scale和octave转换成音符的核心算法。
第-1组
0 1 2 3 4 5 6 7 8
C C# D D# E F F# G G#
第0组
9 10 11 12 13 14 15 16 17 18
A A# B C C# D D# E F F#
19 20
G G#
第1组
21 22 23 24 25 26 27 28 29 30
A A# B C C# D D# E F F#
31 32
G G#
第2组
33 34 35 36 37 38 39 40 41 42
A A# B C C# D D# E F F#
43 44
G G#
第3组
45 46 47 48 49 50 51 52 53 54
A A# B C C# D D# E F F#
55 56
G G#
第4组
57 58 59 60 61 62 63 64 65 66
A A# B C C# D D# E F F#
67 68
G G#
第5组
69 70 71 72 73 74 75 76 77 78
A A# B C C# D D# E F F#
79 80
G G#
第6组
81 82 83 84 85 86 87 88 89 90
A A# B C C# D D# E F F#
91 92
G G#
第7组
93 94 95 96 97 98 99 100 101 102
A A# B C C# D D# E F F#
103 104
G G#
第8组
105 106 107 108 109 110 111 112 113 114
G G#
第9组
117 118 119 120 121 122 123 124 125 126
A A# B C C# D D# E F F#
127
接下来介绍另外一个核心方法,这个方法是将音符值转换成NoteScale枚举值,例如60,转换之后notescale_from_number()就等于NoteScale_C( 3 ).
int notescale_from_number(int number) {
) % ;
}
/** Return true if this notescale number is a black key */
BOOL notescale_is_black_key(int notescale) {
if (notescale == NoteScale_Asharp ||
notescale == NoteScale_Csharp ||
notescale == NoteScale_Dsharp ||
notescale == NoteScale_Fsharp ||
notescale == NoteScale_Gsharp) {
return YES;
}
else {
return NO;
}
}
上面这个方法是判断一个音符是否为black key.
Roland钢琴开发中音符值、度、与音名之间的转换算法的更多相关文章
- java中数组、集合、字符串之间的转换,以及用加强for循环遍历
java中数组.集合.字符串之间的转换,以及用加强for循环遍历: @Test public void testDemo5() { ArrayList<String> list = new ...
- java中 列表,集合,数组之间的转换
java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 List和Set都是接口,它们继承Collection(集合),集合里面任何数据 ...
- IOS开发中返回值为null时的处理
在IOS开发中,如果得到了null返回值很容易造成程序崩溃,null和nil的判断方法不同. nil的判断方法: if(data==nil) { NSLog(@"data is n ...
- HTML中字体单位px pt em之间的转换
在实现打印功能时,遇到一个问题,使用px作为单位在不同的机器或者打印机上打印出的字体大小不一样,所以经过查询,发现使用pt为单位能够进行物流适配,下面是各单位之间的转换: 定义字体大小有常见三种单位, ...
- OpenCV中Mat与二维数组之间的转换
---恢复内容开始--- 在OpenCV中将Mat(二维)与二维数组相对应,即将Mat中的每个像素值赋给一个二维数组. 全部代码如下: #include <iostream> #inclu ...
- Java基础知识强化106:Java中 int 的各进制之间的转换
1.二.八.十.十六进制之间的转换 下面是示例代码,我们直接通过JDK工具库中的方法实现的,如下: public static Integer valueOf(String s, int radix ...
- org.Hs.eg.db包简介(转换NCBI、ensemble等数据库中基因ID,symbol等之间的转换)
1)安装载入 ------------------------------------------- if("org.Hs.eg.db" %in% rownames(install ...
- java中byte,byte[]和int之间的转换
1>byte类型转换为,直接隐式转换,适用于要求保持数值不变,例如要求进行数值计算 如 byte b=0x01; int i=b; 2>另一种是要求保持最低字节中各个位不变,3个高字节全部 ...
- iOS开发中的错误整理,线程之间通信练习,加载图片的练习中出现的错误 -- Http请求错误
控制台打印:Application Transport Security has blocked a cleartext HTTP (http://) resource load since it i ...
随机推荐
- MFC中SQLite数据库的使用
1打开数据库 BOOL playDlg::openData() { WCHAR a[100]; CString path; path = m_exePath+L"sentence_makin ...
- QT函数
1 move 移动 2 resize 改变窗口大小 3 setNum 设置数字 4 setText 设置文本 5 setWindowTitle 设置窗口文本 6 show 弹出窗口 7 text 获取 ...
- linux 命令之 uptime
uptime 命令是用来查询linux系统负载的. 命令格式 uptime [OPTION] -V 显示版本号 不带參数的 uptime 直接输出系统负载. 何为系统负载呢? 系统平均负载被定义为在特 ...
- Android上下左右滑动,显示底层布局
转载博客地址:http://www.cnblogs.com/flyme2012/p/4106308.html 闲着没事做了一个小东西.Android的上下左右滑动,显示底层布局.可以做类似于QQ消息列 ...
- [转载]解析WINDOWS中的DLL文件---经典DLL解读
[转载]解析WINDOWS中的DLL文件---经典DLL解读 在Windows世界中,有无数块活动的大陆,它们都有一个共同的名字——动态链接库.现在就走进这些神奇的活动大陆,找出它们隐藏已久的秘密吧! ...
- jquery设置文本框值 与获取文本框的值
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- JS禁止/启用滚动条
//禁止滚动条 $(document.body).css({ "overflow-x": "hidden", "overflow-y": & ...
- 关于在storyboard拖按钮控件,手动设置代码不成功的问题
首先,在 storyboard 中拖拽一个按钮控件.设置好约束条件
- 我本人一直以来犯的错误,在看了《Think In Java》后才抓了出来(转)
也许你是只老鸟,也许你的程序编的很精,但是,在你的程序生活,你也许没有注意到一些“常识性”的问题,因为有些时候我们不需要去注意,我们的程序 照样能够运行得飞快,但是如果那天有一个无聊的人问你一个像这样 ...
- Editplus配置java运行环境
Editplus配置java运行环境 下载及安装: editplus官网下载地址:https://www.editplus.com/ 安装方法和安装普通exe应用程序一样,选在安装路径,下一步下一步, ...