MIDIKeyboard.cpp
上传用户:fs3633
上传日期:2021-05-14
资源大小:909k
文件大小:4k
- // MIDIKeyboard.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MIDIDevDemo v2.h"
- #include "MIDIKeyboard.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMIDIKeyboard MIDI琴键
- // Low note end of the piano control range 低音符结束的控制范围
- const unsigned char CMIDIKeyboard::LOW_NOTE = 36;
- // High note end of the piano control range 高音符结束的控制范围
- const unsigned char CMIDIKeyboard::HIGH_NOTE = 100;
- // For determining if a key on the computer keyboard is already down
- //确定计算机上的琴键是不是已经按下去了
- const unsigned short CMIDIKeyboard::KEY_DOWN = 16384;
- // Note per octave 音符的每个八分音符
- const unsigned char CMIDIKeyboard::NOTE_PER_OCTAVE = 12;
- // Size of key map 键映射的大小
- const int CMIDIKeyboard::MAP_SIZE = 17;
- CMIDIKeyboard::CMIDIKeyboard() :
- m_KeyMap(MAP_SIZE),
- m_Oct(0)
- {
- //
- // Initialize key map for looking up keys and their note values
- //初始化琴键和音符的值,這個應該是鍵盤上面的按鍵,是電腦的鍵盤
- m_KeyMap.SetAt('Z', CPianoCtrl::C);
- m_KeyMap.SetAt('S', CPianoCtrl::C_SHARP);
- m_KeyMap.SetAt('X', CPianoCtrl::D);
- m_KeyMap.SetAt('D', CPianoCtrl::D_SHARP);
- m_KeyMap.SetAt('C', CPianoCtrl::E);
- m_KeyMap.SetAt('V', CPianoCtrl::F);
- m_KeyMap.SetAt('G', CPianoCtrl::F_SHARP);
- m_KeyMap.SetAt('B', CPianoCtrl::G);
- m_KeyMap.SetAt('H', CPianoCtrl::G_SHARP);
- m_KeyMap.SetAt('N', CPianoCtrl::A);
- m_KeyMap.SetAt('J', CPianoCtrl::A_SHARP);
- m_KeyMap.SetAt('M', CPianoCtrl::B);
- // An octave higher 八度音階的音符的高度
- m_KeyMap.SetAt(0xBC, CPianoCtrl::C + NOTE_PER_OCTAVE);
- m_KeyMap.SetAt('L', CPianoCtrl::C_SHARP + NOTE_PER_OCTAVE);
- m_KeyMap.SetAt(0xBE, CPianoCtrl::D + NOTE_PER_OCTAVE);
- m_KeyMap.SetAt(0xBA, CPianoCtrl::D_SHARP + NOTE_PER_OCTAVE);
- m_KeyMap.SetAt(0xBF, CPianoCtrl::E + NOTE_PER_OCTAVE);
- }
- CMIDIKeyboard::~CMIDIKeyboard()
- {
- }
- // Convert key to note 把鍵盤上的键转换成音符
- BOOL CMIDIKeyboard::KeyToNote(UINT Char, unsigned char &NoteId)
- {
-
- BOOL Result = m_KeyMap.Lookup(Char, NoteId);
- if(Result)
- {
- NoteId += m_Oct * NOTE_PER_OCTAVE + LOW_NOTE;
- }
- return Result;
-
-
-
-
- }
- BEGIN_MESSAGE_MAP(CMIDIKeyboard, CWnd)
- //{{AFX_MSG_MAP(CMIDIKeyboard)
- ON_WM_GETDLGCODE()
- ON_WM_KEYDOWN()
- ON_WM_KEYUP()
- ON_WM_PAINT()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_MOUSEMOVE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMIDIKeyboard message handlers 操作琴键的消息
- UINT CMIDIKeyboard::OnGetDlgCode()
- {
- return CWnd::OnGetDlgCode() | DLGC_WANTALLKEYS;
- }
- // When client presses a key 当我们按下一个琴键的时候
- void CMIDIKeyboard::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- switch(nChar)
- {
- // Keys 1-5 are for determining the octave setting
- //确定八度音階的安置
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- m_Oct = nChar - '1';
- break;
- default:
- // Make sure the key isn't already down 判断琴键是不是按下去了
- if(!(nFlags & KEY_DOWN))
- {
- unsigned char NoteId;
- // If the key is in range of the piano control, play note
- //如果这个琴键在钢琴控件范围之内按下去的,那么就播放这个音符
- if(KeyToNote(nChar, NoteId))
- {
- NoteOn(NoteId);
- }
- }
- break;
- }
-
- CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
-
-
- }
- // Client lifts a key up 当我们松开琴键的时候
- void CMIDIKeyboard::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- unsigned char NoteId;
- if(KeyToNote(nChar, NoteId))
- {
- NoteOff(NoteId);
- }
-
- CWnd::OnKeyUp(nChar, nRepCnt, nFlags);
- }
- //
- // Messages passed to the parent control
- //给控件传递消息
- void CMIDIKeyboard::OnPaint()
- {
- CPianoCtrl::OnPaint();
- }
- void CMIDIKeyboard::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CPianoCtrl::OnLButtonDown(nFlags, point);
- }
- void CMIDIKeyboard::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CPianoCtrl::OnLButtonUp(nFlags, point);
- }
- void CMIDIKeyboard::OnMouseMove(UINT nFlags, CPoint point)
- {
- CPianoCtrl::OnMouseMove(nFlags, point);
- }