お久しぶりです。TKOZです。
環境は、WindowsXP
BCCDeveloper+BorlandC++Compiler
です。
うまく鳴ってくれないのですが、どこが
おかしいでしょうか?お願いします。
ソースを張ります。
#include<stdio.h>
#include<iostream>
#include<windows.h>
//グローバル変数
HMIDIOUT *hMIDI; //MIDIポートハンドル
using namespace std;
wchar_t *strmncpy(wchar_t *str1,wchar_t *str2,int m,int n)
{
//文字列str2のm番目からn文字数だけ文字列str1にコピーする関数
int i;
for(i=0;i<n;i++)
{
str1[i] = str2[i+m];
}
str1[n]=L'\0';
return str1;
}
class MakeMid
{
public:
int counter;
wchar_t Kashi[500][100];//歌詞が入る変数
int Load()
{
FILE *fp;
int i=0;
fp=fopen("kashi.txt","r");
while(fscanf(fp,"%s",Kashi[i])!=EOF)
{
i++;
}
fclose(fp);
return i;
}
int PlayMidi(DWORD Value)
{
DWORD _Time;
//出力リセット
midiOutReset((HMIDIOUT)hMIDI);
//音を発音
midiOutShortMsg((HMIDIOUT)hMIDI, 0x7F3C90 + (Value * 256));
//音を伸ばす(本当はマウスイベントなどでやる方が良いが面倒なのでこれにする)
_Time= GetTickCount();
while (300>=GetTickCount()-_Time);
midiOutShortMsg((HMIDIOUT)hMIDI, 0x3C90 + (Value*256));
}
int MakeAI(int counter)
{
static int j=0;
if(j<wcslen(Kashi[counter]) && Kashi[counter]!=0)
{
Sleep(500);
j++;
}
wchar_t tmp[100];
//wcsncpy(tmp,Kashi[counter],j);
strmncpy(tmp,Kashi[counter],j-1,1);
if(Kashi[counter]==0)
{
j=0;
return 1;
}
printf("tmp=%s",tmp);
if(wcscmp(tmp,L"あ")==0 || wcscmp(tmp,L"い")==0 ||
wcscmp(tmp,L"う")==0 || wcscmp(tmp,L"え")==0 ||
wcscmp(tmp,L"お")==0)
{
printf("ド\n");
PlayMidi(0);
}
else if(wcscmp(tmp,L"か")==0 || wcscmp(tmp,L"き")==0 ||
wcscmp(tmp,L"く")==0 || wcscmp(tmp,L"け")==0 ||
wcscmp(tmp,L"こ")==0)
{
printf("レ\n");
PlayMidi(2);
}
else if(wcscmp(tmp,L"さ")==0 || wcscmp(tmp,L"し")==0 ||
wcscmp(tmp,L"す")==0 || wcscmp(tmp,L"せ")==0 ||
wcscmp(tmp,L"そ")==0)
{
printf("ミ\n");
PlayMidi(4);
}
else if(wcscmp(tmp,L"た")==0 || wcscmp(tmp,L"ち")==0 ||
wcscmp(tmp,L"つ")==0 || wcscmp(tmp,L"て")==0 ||
wcscmp(tmp,L"と")==0)
{
printf("ファ\n");
PlayMidi(5);
}
else if(wcscmp(tmp,L"な")==0 || wcscmp(tmp,L"に")==0 ||
wcscmp(tmp,L"ぬ")==0 || wcscmp(tmp,L"ね")==0 ||
wcscmp(tmp,L"の")==0)
{
printf("ソ\n");
PlayMidi(7);
}
else if(wcscmp(tmp,L"は")==0 || wcscmp(tmp,L"ひ")==0 ||
wcscmp(tmp,L"ふ")==0 || wcscmp(tmp,L"へ")==0 ||
wcscmp(tmp,L"ほ")==0)
{
printf("ラ\n");
PlayMidi(9);
}
else if(wcscmp(tmp,L"ま")==0 || wcscmp(tmp,L"み")==0 ||
wcscmp(tmp,L"む")==0 || wcscmp(tmp,L"め")==0 ||
wcscmp(tmp,L"も")==0)
{
printf("シ\n");
PlayMidi(11);
}
else if(wcscmp(tmp,L"や")==0 || wcscmp(tmp,L"ゆ")==0 ||
wcscmp(tmp,L"よ")==0)
{
printf("ド2\n");
PlayMidi(12);
}
else if(wcscmp(tmp,L"ら")==0 || wcscmp(tmp,L"り")==0 ||
wcscmp(tmp,L"る")==0 || wcscmp(tmp,L"れ")==0 ||
wcscmp(tmp,L"ろ")==0)
{
printf("レ2\n");
PlayMidi(14);
}
else if(wcscmp(tmp,L"わ")==0 || wcscmp(tmp,L"を")==0 ||
wcscmp(tmp,L"ん")==0)
{
printf("ミ2\n");
PlayMidi(16);
}
return 0;
}
};
int main()
{
int Limit=0;
//MIDIポートを開く
if (midiOutGetNumDevs<=0)
printf("あなたの環境ではMIDI音源が使用できません。\n");
else
midiOutOpen((LPHMIDIOUT)&hMIDI,0, 0, 0, CALLBACK_NULL);
MakeMid Midi;
Limit=Midi.Load();
while(1)
{
static int counter=0;
if(Midi.MakeAI(counter)==1)
{
counter++;
}
Sleep(500);
if(counter>=Limit)
break;
}
printf("演奏を終了します。\n");
midiOutReset((HMIDIOUT)hMIDI); //MIDI出力リセット
midiOutClose((HMIDIOUT)hMIDI); //MIDIポートを閉じる
return 0;
}