www.pudn.com > EndpointDetection_Pitch.zip > Prompt.cpp
// Prompt.cpp : implementation file
//
#include "stdafx.h"
#include "Pitch.h"
#include "Prompt.h"
#include "PitchDlg.h"
#include "PerfDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern int PerfectX, PerfectY;
extern int PerfectPlayedNote;
extern int PerfectGuessedNote;
extern BYTE CurrentChannel;
extern BYTE bStatus; // Midi status byte
extern BYTE bChannel; // Midi channel byte
extern BYTE bData1; // First midi data byte
extern BYTE bData2; // Second midi data byte// Play note and wait until it finishes
extern NoteWaitTimeLength;
extern BYTE InstrumentNumber;
extern char NoteDescription[];
extern char NoteTable2[];
extern int LowPianoNote;
extern int HighPianoNote;
MSG PromptMessage;
int PromptTimerKey;
int PromptWaitLength = 5; // 250 milliseconds
int PromptTimerWaitSwitch = 0;
/////////////////////////////////////////////////////////////////////////////
// CPrompt dialog
CPrompt::CPrompt(CWnd* pParent /*=NULL*/)
: CDialog(CPrompt::IDD, pParent)
{
//{{AFX_DATA_INIT(CPrompt)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CPrompt::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPrompt)
DDX_Control(pDX, IDC_GetOutOfPrompt, m_GetOutOfPrompt);
DDX_Control(pDX, IDC_Incorrect, m_Incorrect);
DDX_Control(pDX, IDC_Correct, m_Correct);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPrompt, CDialog)
//{{AFX_MSG_MAP(CPrompt)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_GetOutOfPrompt, OnGetOutOfPrompt)
ON_BN_CLICKED(IDC_Correct, OnCorrect)
ON_BN_CLICKED(IDC_Incorrect, OnIncorrect)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPrompt message handlers
BEGIN_EVENTSINK_MAP(CPrompt, CDialog)
//{{AFX_EVENTSINK_MAP(CPrompt)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
BOOL CPrompt::OnInitDialog()
{
CDialog::OnInitDialog();
char TempDescription[3] = " ";
int BaseNotePlayed, BaseNoteGuessed;
// Popup dialog below notes on relative dialog
m_GetOutOfPrompt.SetShade(SHS_HARDBUMP,6,30);
m_GetOutOfPrompt.SetTextColor(RedColour);
m_GetOutOfPrompt.SetFont("Signature",24,20);
MoveWindow(PerfectX+200,PerfectY+5,350,180,TRUE);
BaseNotePlayed = ((PerfectPlayedNote+3)%12) + 1;
BaseNoteGuessed = PerfectGuessedNote;
strncpy(TempDescription,NoteTable2 + (BaseNotePlayed-1)*2, 2);
m_Correct.SetWindowText(TempDescription);
strncpy(TempDescription,NoteTable2 + (BaseNoteGuessed-1)*2 ,2);
m_Incorrect.SetWindowText(TempDescription);
m_Correct.SetTextColor(GreenColour);
m_Incorrect.SetTextColor(RedColour);
BaseNoteGuessed = (BaseNoteGuessed - BaseNotePlayed);
// Rework PerfectGuessedNote for play routine
PerfectGuessedNote = PerfectPlayedNote + BaseNoteGuessed;
if (PerfectGuessedNote > HighPianoNote)
{PerfectGuessedNote = PerfectGuessedNote - 12;}
if (PerfectGuessedNote < LowPianoNote)
{PerfectGuessedNote = PerfectGuessedNote - 12;}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
long CPrompt::PlayPromptNote(int NoteNumber)
{
CPitchDlg abc;
long NoteError = 0;
// Send instrument
bStatus = MIDI_PROGRAM_CHANGE;
bChannel = CurrentChannel;
bData1 = InstrumentNumber;
bData2 = '\x00'; // Not used
abc.SendShortMessage();
bStatus = MIDI_NOTE_ON;
bChannel = CurrentChannel;
bData1 = NoteNumber;
bData2 = '\x7f'; // Volume
abc.SendShortMessage();
DoPromptWait(NoteWaitTimeLength);
bStatus = MIDI_NOTE_OFF;
bChannel = CurrentChannel;
bData1 = NoteNumber;
bData2 = '\x00'; // Volume
abc.SendShortMessage();
return (NoteError);
}
void CPrompt::OnTimer(UINT nIDEvent)
{
PromptTimerWaitSwitch = 1;
CDialog::OnTimer(nIDEvent);
}
void CPrompt::DoPromptWait(int PromptWaitTimeInMilliseconds)
{
PromptTimerWaitSwitch = 0;
// default is a value of 5 which would be made into 25 milliseconds
PromptTimerKey = SetTimer(1, PromptWaitTimeInMilliseconds, NULL);
ASSERT(PromptTimerKey !=0);
// Wait until the timer changes RelativeTimerWaitSwitch to non zero.
// The PeekMessage will allow the message through
while (PromptTimerWaitSwitch == 0)
{if(::PeekMessage(&PromptMessage, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&PromptMessage);
::DispatchMessage(&PromptMessage);
}
};
KillTimer(PromptTimerKey);
}
void CPrompt::OnCorrect()
{
PlayPromptNote(PerfectPlayedNote);
}
void CPrompt::OnIncorrect()
{
PlayPromptNote(PerfectGuessedNote);
}
void CPrompt::OnGetOutOfPrompt()
{
CDialog::OnOK();
}