在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. c语言线性表

    #include<stdio.h> #include<time.h> #include<stdlib.h> #define MAXSIZE 20 //初始长度 ty ...

  2. HttpContext.Current.Cache 过期时间

    原文:HttpContext.Current.Cache 过期时间 为了更快的读取数据,我们一般会把常用到的数据加载到Cache中 在.NET中,Cache的存在可以依赖多中方式,主要用到HttpCo ...

  3. service:jmx:rmi:///jndi/rmi

    service:jmx:rmi:///jndi/rmi://ip:9889/jmxrmi http://stackoverflow.com/questions/2768087/explain-jmx- ...

  4. ASP 代码当前记录集不支持更新问题的解决办法。

    错误类型: ADODB.Recordset (0x800A0CB3) 当前记录集不支持更新.这可能是提供程序的限制,也可能是选定锁定类型的限制. /Model/manage/Admin_Admin.a ...

  5. JavaEE Tutorials (20) - 企业应用安全入门

    20.1企业应用的基本安全任务31620.2保护企业bean安全316 20.2.1使用声明式安全保护企业bean安全318 20.2.2通过编程方式保护企业bean安全321 20.2.3传播安全身 ...

  6. STL跨DLL使用

    今天在写一个函数,需要将map作为一个引用参数传入函数体内部进行赋值,结果编译通过,执行时总是崩溃,在网上找到了一些作者写的blog,详细解释了这种情况发生的原因,特转载在这里,便于自己今后查询. 原 ...

  7. mysql入库中文乱码问题

    mysql> show variables like '%char%';+--------------------------+----------------------------+| Va ...

  8. nova availability zone

    find a bug: at first there is only one zone. create aggregate host1 in zone1 create aggregate host1 ...

  9. spoj 8222 Substrings (后缀自动机)

    spoj 8222 Substrings 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length(S)) 解题思路:我们构造S的SAM,那么对于 ...

  10. oauth2认证

    using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Con ...