将MIDI编号输出回用户

时间:2015-01-31 17:59:13

标签: c++ midi

我是一个漫长项目的最后一道障碍。最后一步是将一串noteNames转换为MIDI数字,然后将这些数字输出回用户。我已经编写了一个合适的代码来将字符(C-B)转换为MIDI数字,但我现在需要将这些数字输出回用户。我不确定在哪里存储它们以便能够回忆起它们......任何指针都会很棒!我是编码的绝对业余爱好者请原谅任何无知。此外,由于某些原因,当我从应用程序复制并粘贴代码时,它会破坏布局,因此请提前道歉。

‪#‎include‬ <iostream> 
#include <string> 
#include <vector> 

using namespace std;
 int notenumber;
 struct noteStorage {
  string noteName;
  string bassnoteName;
  int midiNumber;
  int noteLength;
  int bassnoteLength;
}
;
///////////////////////////////////////////////////////////////////////////////////////////////
//VALIDATION FOR NOTE NAME
bool ValidateNote(string note) {
  // Step 1: If note name length is less than 2 OR more than 3, return false
  if (note.length() <2 || note.length() >3) {
    cout<<"Note length must be 2 or 3 characters\n";
return false;
  }
  //Step 2: If true, the note must be/(or be) between A and G
  else if(tolower(note[0])<'a' || tolower(note[0]) >'g') {
    cout<<"Note must be A-G\n";
return false;
  }
  //Step 3: If true, the last character must be a digit
  else if(isdigit(note[note.length()-1])==false) {
    cout<<"Last character must be a digit\n";
return false;
  }
  //Step 4: If note length is 3 note[1] (character 2) must be '#'.
  else if(note.length()==3 && note[1] !='#') {
    "Invalid sharp note\n";
return false;
  }
  return true;
}
/////////////////////////////////////////////////////////////////////////////////////////
//VALIDATION FOR NOTE LENGTH
bool ValidateNoteLength (int length)//Step 1 - If notelength is not a digit, return FALSE
 {
  if (length==false) {
    cout<<"Note length must be a digit/number, please re-enter";
    return false;
  }
  //Step 2 - If notelength is less than or equal to 0 or more than 16, return FALSE
  if (length <=0 || length > 16) {
    cout<<"Note length value cannot be less than 1 or more than 16, please re-enter";
    return false;
  }
  return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
int CalculateNoteNumber(string tempName) {
  int Octave;
  int notenumber;
  tempName[0]=toupper(tempName[0]);
  Octave=((tempName[tempName.length()-1]) -48) * 12;
  if (tempName.length()==2) {
    if(tempName[0]=='C') {
      return notenumber=0;
    }
    else if(tempName[0]=='D') {
      return notenumber=2;
    }
    else if(tempName[0]=='E') {
      return notenumber=4;
    }
    else if(tempName[0]=='F') {
      return notenumber=5;
    }
    else if(tempName[0]=='G') {
      return notenumber=7;
    }
    else if(tempName[0]=='A') {
      return notenumber=9;
    }
    else {
      return notenumber=11;
    }
  }
  else if (tempName.length()==3) {
    if(tempName[0]=='C') {
      return notenumber=1;
    }
    else if(tempName[0]=='D') {
      return notenumber=3;
    }
    else if(tempName[0]=='F') {
      return notenumber=6;
    }
    else if(tempName[0]=='G') {
      return notenumber=8;
    }
    else {
      return notenumber=10;
    }
  }
}
int main() {
  noteStorage noteData[8];
  //string note;
  for (int i=0;
  i < 8;
  i++) {
    cout<<"Please enter melody note: " << i <<": ";
    while (1) {
      string tempName;
      cin>>tempName;
      int noteNumber=CalculateNoteNumber(tempName);
      if (ValidateNote(tempName)==true) {
        noteData[i].noteName=tempName;
        break;
      }
      else {
        cout <<"Please enter correctly: ";
      }
    }
    //end first while
    cout<<"Please enter note length: ";
    while (1) {
      int tempLength;
      cin>>tempLength;
      if (ValidateNoteLength(tempLength)==true) {
        noteData[i].noteLength=tempLength;
        break;
      }
      else {
        cout <<"Please enter correctly: ";
      }
    }
    //end while 2
    cout<<"Thank you\n";
////////////////////////////////////////////////////////////////////////

  }
  // Bass Melody Input
  {
    noteStorage noteData[8];
    //string note;
    for (int i=0;
    i < 8;
    i++) {
      cout<<"Please enter bass melody note: " << i <<": ";
      while (1) {
        string basstempName;
        cin>>basstempName;
        int noteNumber=CalculateNoteNumber(basstempName);
        if (ValidateNote(basstempName)==true) {
          noteData[i].bassnoteName=basstempName;
          break;
        }
        else {
          cout <<"Please enter correctly: ";
        }
      }
      //end first while
      cout<<"Please enter note length: ";
      while (1) {
        int basstempLength;
        cin>>basstempLength;
        if (ValidateNoteLength(basstempLength)==true) {
          noteData[i].bassnoteLength=basstempLength;
          break;
        }
        else {
          cout <<"Please enter correctly: ";
        }
      }
      //end while 2
      cout<<"Thank you\n";

    }
    ////////////////////////////////////////////////////////////////////////

  }
  //end for
  cout<<"Your melody notes and note lengths are: "<<endl;
  for (int i=0;
  i < 8;
  i++) {
    cout<<noteData[i].noteName<<"Length: ";
    cout<<noteData[i].noteLength<<endl;
  }
  cout<<"Your bass notes and note lengths are: "<<endl;
  for (int i=0;
  i < 8;
  i++) {
    cout<<noteData[i].bassnoteName<<"Length: ";
    cout<<noteData[i].bassnoteLength<<endl;
  }
  /*system("pause");
return 0;
*/
}

1 个答案:

答案 0 :(得分:0)

你就在那里! int noteNumber=CalculateNoteNumber(tempName);已在noteNumber中存储您需要的信息。 cout支持不同类型,包括int,因此您可以执行此操作,例如:cout << endl << "Current note MIDI number: " << noteNumber使用注释编号将新行写入stdout。

这是否解决了您正在寻找的问题?