在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

A       A#     B     C     C#      D    D#     E        F    F#
115  116 

G       G#

 

 

第9组

117  118  119  120  121  122  123  124  125  126

A       A#      B     C    C#      D     D#    E       F    F#

127

 G

接下来介绍另外一个核心方法,这个方法是将音符值转换成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钢琴开发中音符值、度、与音名之间的转换算法的更多相关文章

  1. java中数组、集合、字符串之间的转换,以及用加强for循环遍历

    java中数组.集合.字符串之间的转换,以及用加强for循环遍历: @Test public void testDemo5() { ArrayList<String> list = new ...

  2. java中 列表,集合,数组之间的转换

    java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 List和Set都是接口,它们继承Collection(集合),集合里面任何数据 ...

  3. IOS开发中返回值为null时的处理

    在IOS开发中,如果得到了null返回值很容易造成程序崩溃,null和nil的判断方法不同. nil的判断方法: if(data==nil) {      NSLog(@"data is n ...

  4. HTML中字体单位px pt em之间的转换

    在实现打印功能时,遇到一个问题,使用px作为单位在不同的机器或者打印机上打印出的字体大小不一样,所以经过查询,发现使用pt为单位能够进行物流适配,下面是各单位之间的转换: 定义字体大小有常见三种单位, ...

  5. OpenCV中Mat与二维数组之间的转换

    ---恢复内容开始--- 在OpenCV中将Mat(二维)与二维数组相对应,即将Mat中的每个像素值赋给一个二维数组. 全部代码如下: #include <iostream> #inclu ...

  6. Java基础知识强化106:Java中 int 的各进制之间的转换

    1.二.八.十.十六进制之间的转换  下面是示例代码,我们直接通过JDK工具库中的方法实现的,如下: public static Integer valueOf(String s, int radix ...

  7. org.Hs.eg.db包简介(转换NCBI、ensemble等数据库中基因ID,symbol等之间的转换)

    1)安装载入 ------------------------------------------- if("org.Hs.eg.db" %in% rownames(install ...

  8. java中byte,byte[]和int之间的转换

    1>byte类型转换为,直接隐式转换,适用于要求保持数值不变,例如要求进行数值计算 如 byte b=0x01; int i=b; 2>另一种是要求保持最低字节中各个位不变,3个高字节全部 ...

  9. iOS开发中的错误整理,线程之间通信练习,加载图片的练习中出现的错误 -- Http请求错误

    控制台打印:Application Transport Security has blocked a cleartext HTTP (http://) resource load since it i ...

随机推荐

  1. TCP粘包拆包问题

    阿π 专注于网络协议,系统底层,服务器软件 C++博客 | 首页 | 发新随笔 | 发新文章 | | | 管理 Socket粘包问题 这两天看csdn有一些关于socket粘包,socket缓冲区设置 ...

  2. fragment中listview触发事件setOnItemClickListener不好使

    <listView/>中// listview点击 ,高度wrap_content改成fill_prarent

  3. Ribbon Gymnastics

    Robert is a gymnastics coach. Unfortunately, he got four gymnastics beginners to attend the coming c ...

  4. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  5. (Pre sell) ZOPO ZP998 (C2 II) 5.5 inch smart phone True Octa Core MTK6592 1920X1080 FHD screen 401 ppi 2GB/32GB 14.0Mp camera-in Mobile Phones from Electronics on Aliexpress.com

    (Pre sell) ZOPO ZP998 (C2 II) 5.5 inch smart phone True Octa Core MTK6592 1920X1080 FHD screen 401 p ...

  6. AndroidUI 引导页面的使用

    一个应用程序都少不了欢迎页面和引导页面,本文主要讲如何制作一个引页面: 首页所有的目录结构: 新建Welcome引导页面和Activity: <RelativeLayout xmlns:andr ...

  7. 大数据Lambda架构

    1 Lambda架构介绍 Lambda架构划分为三层.各自是批处理层,服务层,和加速层.终于实现的效果,能够使用以下的表达式来说明. query = function(alldata) 1.1 批处理 ...

  8. 利用分布类防止EF更新模型丢失验证信息

    数据库表TT,EF生成的model是这样的.在这里添加代码,从数据库更新模型是会冲掉. //------------------------------------------------------ ...

  9. Emoji字符检查与替换

    当文本包含Emoji字符的时候,存储到数据库或读取的时候需要进行编码和解码(如UTF_8),否则MySQL的存储可能有异常. 当有的文本不允许输入Emoji字符,或者显示时需要将Emoji替换为指定字 ...

  10. ORA-600[kcratr_scan_lastbwr]逻辑坏块解决

    数据库版本: 11.2.0.3 问题现象: 今天在启动一台测试数据库的时候,发现db不能open,报错如下: ERROR at line 1: ORA-00600: internal error co ...