用 C midi 编程

Posted

技术标签:

【中文标题】用 C midi 编程【英文标题】:Programming in C midi 【发布时间】:2014-01-14 07:11:40 【问题描述】:

我的代码如下:

    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>

    int natural_minor_scale [8] = 0, 2, 3, 5, 7, 8, 10, 12;  /* The Natural Minor scale we will be using in the code*/
    int harmonic_minor_scale [8] = 0, 2, 3, 5, 7, 8, 11, 12; /* The Harmonic Minor scale we will be using in the code*/
    int major_scale [8] = 0, 2, 4, 5, 7, 9, 11, 12;          /* The Major scale we will be using in the code*/
    int scale(void);
    int key(void);
    void welcome_screen(void);

    struct musical_choice                                      /* Building of a structure taken from lecture notes 6.Structures */
                                                          /* from http://www.elec.york.ac.uk/internal_web//meng/yr1/modules/Maths_and_Programming/CProgramming/c-lecture6.pdf */
    int decision;
    int scale;
    int key;

    ;
    struct musical_choice choice;            /*We will use a structure to get information easier here */

    int main(void)
    
    welcome_screen(); /* Open the Console and display the first message, so as to introduce the program to the user and to wish the user a nice day*/
    scanf("%i", &choice.decision);
     while(choice.decision!=1 && choice.decision!=2) /* To choose between just 1 and 2 */

         printf("\nPlease, select one of the options offered above.\n");
         scanf("%i", &choice.decision);
    
    if (choice.decision==1)
    scale();
    key();
    printf("I will now play what you wish");
    playing(choice.scale, choice.key);
    
    else if (choice.decision==2)
    choice.scale=random_number(1, 3);
    choice.key=random_number(1, 3);
    
    return 0;
    


    void (welcome_screen())  /* This is the function for the welcoming screen */
      
      printf("Welcome to this new piece of software. \nIt will help you decide what you want me, your Software Guitarist, to play. \nHave a nice day!\nFor me to play based on your desires, press 1.\nIf you would like me to prove my talent and improvise, press 2\n");
      



    int scale(void)          /* Receive input for which scale you want to have played */
    
    printf("\nPress a number to choose whether you want me to play in:\nThe Major Scale, which requires you to press 1;\nThe Natural Minor scale, which requires you to press 2;\nThe Harmonic Minor scale, which requires you to press 3;\n");
    scanf("%i", &choice.scale);
    while(choice.scale != 1 && choice.scale != 2 && choice.scale != 3)          /* This command makes it so that you can't choose a letter or a number higher than 3 or lower than 1 */
        printf("\nPlease, pick a number between 1 and 3.\n");
        scanf("%i", &choice.scale);

        
         return choice.scale;
    
    int key(void)          /* Receive input for which key you want to have played */
    
        printf("\nNow choose between the music being played in\nThe key of A for which you press 1 \nThe key of C for which you press 2 \nThe key of E for which you press 3.\n");
    scanf("%i", &choice.key);
    while(choice.key != 1 && choice.key !=2 && choice.key !=3)
        printf("\nPlease, pick a number between 1 and 3.\n");
        scanf("%i", &choice.key);

        
        return choice.key;


    int playing (scale, key)
    int key_note;
    int scale_chosen;

    int velocity = random_number (60-110);
    if (choice.scale=1)
    scale_chosen=major_scale;
    else if (choice.scale=2)
    scale_chosen=natural_minor_scale;
    else if (choice.scale=3)
    scale_chosen=harmonic_minor_scale;
    if (choice.key=1)
    key_note=57;
    else if (choice.key=2)
    key_note=60;
    else if (choice.key=3)
    key_note=64;

    do
    midi_note(key_note+scale_chosen, 26, velocity); 

    while (kbhit()==0);
    return 0;

关于我设置的选项的选择似乎一切正常,但是没有播放 MIDI 音符。

我可能在哪里犯了错误?如果这真的很愚蠢,我真诚地道歉,因为我还没睡,现在这对我来说真的很累。

感谢大家的宝贵时间!

【问题讨论】:

【参考方案1】:

playing (scale, key) 函数中的 if 条件错误,拼写错误 == 例如:

if (choice.scale=1)
//              ^  ??  it should be ==

应该是:

if (choice.scale == 1)

另外,学习Indenting C Programs。

【讨论】:

天哪,非常感谢您的留言!我完全错过了我的那部分代码!但是音符仍然没有播放,这很奇怪......我错过了什么吗? key_note=64 也应该是 key_note==64 吗?我太累了,现在想不起来了 @user3186187 no === 是不同的运算符!因为key_note=64 不是条件而是赋值,所以它是正确的,仅在 if 中更正比较条件。 我已经修复了,但仍然缺少一些东西......跨度> @user3186187 我找不到任何其他错误,但更正所有 if 条件......并正确缩进您的代码。 @user3186187 去睡觉,睡觉,恢复健康后,你可能会在 5 或 10 分钟内找到 int。

以上是关于用 C midi 编程的主要内容,如果未能解决你的问题,请参考以下文章

无法继续进行 switch case C 编程

以编程方式配置 Mac OS X MIDI

是否可以通过 Chrome 以编程方式创建虚拟 MIDI 设备?

Win 10 应用开发MIDI 音乐合成——音符消息篇

使用 PIC 微处理器上的 C 进行 MIDI 输出的二进制输出

C++ 中的 MIDI 音符消息