Add MIDI Input support (Windows only).

This commit is contained in:
Henrik Rydgard 2012-04-12 12:52:55 +02:00
parent 97ea3356a9
commit 9684be3257
9 changed files with 132 additions and 3 deletions

View File

@ -13,6 +13,7 @@ Features
* basic logging
* Really simple audio mixer with OGG sample support
* RIFF file read/write
* MIDI Input (only on Windows)
Notes
-----

View File

@ -121,7 +121,7 @@ int main(int argc, char *argv[]) {
g_xres = 800;
g_yres = 480;
} else {
g_xres = 480;
g_xres = 1480;
g_yres = 800;
}

12
midi/CMakeLists.txt Normal file
View File

@ -0,0 +1,12 @@
set(SRCS
midi_input.cpp
)
set(SRCS ${SRCS})
add_library(midi STATIC ${SRCS})
if(UNIX)
add_definitions(-fPIC)
endif(UNIX)

77
midi/midi_input.cpp Normal file
View File

@ -0,0 +1,77 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <MMSystem.h>
#include "base/basictypes.h"
#include "base/logging.h"
#include "midi/midi_input.h"
std::vector<std::string> MidiInGetDevices() {
int numDevs = midiInGetNumDevs();
std::vector<std::string> devices;
for (int i = 0; i < numDevs; i++) {
MIDIINCAPS caps;
midiInGetDevCaps(i, &caps, sizeof(caps));
devices.push_back(caps.szPname);
}
return devices;
}
static void CALLBACK MidiCallback(HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance,
DWORD_PTR dwParam1, DWORD_PTR dwParam2) {
MidiListener *listener = (MidiListener*)dwInstance;
uint8_t cmd[3] = {0};
switch (wMsg) {
case MM_MIM_OPEN:
ILOG("Got MIDI Open message");
break;
case MM_MIM_CLOSE:
ILOG("Got MIDI Close message");
break;
case MM_MIM_DATA:
cmd[0] = dwParam2 & 0xFF;
cmd[1] = (dwParam2 >> 8) & 0xFF;
cmd[2] = (dwParam2 >> 16) & 0xFF;
ILOG("Got MIDI Data: %02x %02x %02x", cmd[0], cmd[1], cmd[2]);
listener->midiEvent(cmd);
break;
default:
WLOG("Got unexpected MIDI message: %08x", (uint32_t)wMsg);
break;
}
}
MidiDevice MidiInStart(int deviceID, MidiListener *listener) {
HMIDIIN hMidiIn;
MMRESULT result = midiInOpen(&hMidiIn, deviceID, (DWORD_PTR)(&MidiCallback), (DWORD_PTR)listener, CALLBACK_FUNCTION);
midiInStart(hMidiIn);
return (MidiDevice)hMidiIn;
}
void MidiInStop(MidiDevice device) {
HMIDIIN hMidiIn = (HMIDIIN)device;
midiInStop(hMidiIn);
midiInClose(hMidiIn);
}
#else
// Stubs for other platforms.
std::vector<std::string> MidiInGetDevices() {
return std::vector<std::string>();
}
MidiDevice MidiInStart(int deviceID, MidiListener *listener) {
FLOG("Invalid MIDI device");
}
void MidiInStop(MidiDevice device) {
FLOG("Invalid MIDI device");
}
#endif

23
midi/midi_input.h Normal file
View File

@ -0,0 +1,23 @@
// MIDI input utilities.
//
// Currently, this is just a platform-clean wrapper around the Win32 MIDI input facilities.
// Thus, it only supports Windows. Other platforms will get an empty list of midi in devices.
#include <vector>
#include <string>
typedef void *MidiDevice;
// Listeners inherit from this. Pass them into MidiInStart.
class MidiListener
{
public:
virtual void midiEvent(const uint8_t *cmd);
};
// Gets the names of the devices in a vector. The device identifier is the index in the vector.
std::vector<std::string> MidiInGetDevices();
MidiDevice MidiInStart(int deviceID, MidiListener *listener);
void MidiInStop(MidiDevice device);

View File

@ -132,6 +132,7 @@
<ClInclude Include="math\lin\quat.h" />
<ClInclude Include="math\lin\vec3.h" />
<ClInclude Include="math\math_util.h" />
<ClInclude Include="midi\midi_input.h" />
<ClInclude Include="profiler\profiler.h" />
<ClInclude Include="ui\ui.h" />
</ItemGroup>
@ -175,6 +176,7 @@
<ClCompile Include="math\lin\quat.cpp" />
<ClCompile Include="math\lin\vec3.cpp" />
<ClCompile Include="math\math_util.cc" />
<ClCompile Include="midi\midi_input.cpp" />
<ClCompile Include="profiler\profiler.cpp" />
<ClCompile Include="ui\ui.cpp" />
</ItemGroup>

View File

@ -147,6 +147,9 @@
<ClInclude Include="ui\ui.h">
<Filter>ui</Filter>
</ClInclude>
<ClInclude Include="midi\midi_input.h">
<Filter>midi</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="gfx\gl_debug_log.cpp">
@ -263,6 +266,9 @@
<ClCompile Include="ui\ui.cpp">
<Filter>ui</Filter>
</ClCompile>
<ClCompile Include="midi\midi_input.cpp">
<Filter>midi</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="gfx">
@ -301,5 +307,8 @@
<Filter Include="ui">
<UniqueIdentifier>{d738c2d1-749d-4b60-b98f-f3da0bbbf40c}</UniqueIdentifier>
</Filter>
<Filter Include="midi">
<UniqueIdentifier>{4710a9a2-d1fa-4920-ba1b-a7527902be53}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>

View File

@ -70,8 +70,12 @@ void UIEnd() {
}
void UIText(int x, int y, const char *text, uint32_t color, float scale, int align) {
UIText(themeUIFont, x, y, text, color, scale, align);
}
void UIText(int font, int x, int y, const char *text, uint32_t color, float scale, int align) {
ui_draw2d.SetFontScale(scale, scale);
ui_draw2d.DrawTextShadow(themeUIFont, text, x, y, color, align);
ui_draw2d.DrawTextShadow(font, text, x, y, color, align);
ui_draw2d.SetFontScale(1.0f, 1.0f);
}

View File

@ -19,7 +19,7 @@
#include "gfx_es2/draw_buffer.h"
#include <string >
#include <string>
#include <vector>
// Mouse out of habit, applies just as well to touch events.
@ -127,6 +127,7 @@ int UIVSlider(int id, int x, int y, int h, int max, int *value);
int UIHSlider(int id, int x, int y, int w, int max, int *value);
// Draws static text, that does not participate in any focusing scheme etc, it just is.
void UIText(int font, int x, int y, const char *text, uint32_t color, float scale = 1.0f, int align = ALIGN_TOPLEFT);
void UIText(int x, int y, const char *text, uint32_t color, float scale = 1.0f, int align = ALIGN_TOPLEFT);
// Slide choice, like the Angry Birds level selector. Not yet working.