funkcni vystup a zmena nastroje

This commit is contained in:
severak 2019-10-12 16:11:44 +02:00
parent ab61870686
commit fb981db3c5
3 changed files with 149 additions and 10 deletions

View File

@ -6,6 +6,5 @@ Windows only for now. Sorry.
## resources
- https://github.com/SiENcE/lovemidi
- https://github.com/wilkie/djehuty/blob/master/binding/win32/mmsystem.d
- http://www.giordanobenicchi.it/midi-tech/lowmidi.htm
- http://www.ccarh.org/courses/253/lab/midiprog/
- https://jimmenard.com/midi_ref.html

View File

@ -52,17 +52,129 @@ static int midiwire_open_out (lua_State *L) {
luaL_error(L, "There was an error opening MIDI Mapper!");
return 0;
}
printf("outHandle: %p", outHandle);
//luaL_error(L, "Foo");
lua_pushnil (L);
lua_pushlightuserdata (L, outHandle);
return 1;
}
static int midiwire_reset (lua_State *L) {
unsigned long result;
HMIDIOUT outHandle;
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
outHandle = lua_touserdata (L, 1);
midiOutReset(outHandle);
return 0;
}
static int midiwire_drum (lua_State *L) {
unsigned long result;
HMIDIOUT outHandle;
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
int drum = luaL_checkinteger(L, 2);
outHandle = lua_touserdata (L, 1);
union { unsigned long word; unsigned char data[4]; } message;
// message.data[0] = command byte of the MIDI message, for example: 0x90
// message.data[1] = first data byte of the MIDI message, for example: 60
// message.data[2] = second data byte of the MIDI message, for example 100
// message.data[3] = not used for any MIDI messages, so set to 0
message.data[0] = 0x99; // MIDI note-on message (requires to data bytes)
message.data[1] = drum; // MIDI note-on message: Key number (60 = middle C)
message.data[2] = 127; // MIDI note-on message: Key velocity (100 = loud)
message.data[3] = 0; // Unused parameter
midiOutShortMsg(outHandle, message.word);
return 0;
}
static int midiwire_note_on (lua_State *L) {
unsigned long result;
HMIDIOUT outHandle;
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
int note = luaL_checkinteger(L, 2);
int velocity = luaL_checkinteger(L, 3);
int channel = 0;
if (lua_gettop (L)==4) {
channel = luaL_checkinteger(L, 4) - 1;
}
outHandle = lua_touserdata (L, 1);
union { unsigned long word; unsigned char data[4]; } message;
// message.data[0] = command byte of the MIDI message, for example: 0x90
// message.data[1] = first data byte of the MIDI message, for example: 60
// message.data[2] = second data byte of the MIDI message, for example 100
// message.data[3] = not used for any MIDI messages, so set to 0
message.data[0] = 0x90 + channel; // MIDI note-on message (requires to data bytes)
message.data[1] = note; // MIDI note-on message: Key number (60 = middle C)
message.data[2] = velocity; // MIDI note-on message: Key velocity (100 = loud)
message.data[3] = 0; // Unused parameter
midiOutShortMsg(outHandle, message.word);
return 0;
}
static int midiwire_note_off (lua_State *L) {
unsigned long result;
HMIDIOUT outHandle;
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
int note = luaL_checkinteger(L, 2);
int channel = 0;
if (lua_gettop (L)==3) {
channel = luaL_checkinteger(L, 3) - 1;
}
outHandle = lua_touserdata (L, 1);
union { unsigned long word; unsigned char data[4]; } message;
message.data[0] = 0x80 + channel; // MIDI note-on message (requires to data bytes)
message.data[1] = note; // MIDI note-on message: Key number (60 = middle C)
message.data[2] = 0; // MIDI note-on message: Key velocity (100 = loud)
message.data[3] = 0; // Unused parameter
midiOutShortMsg(outHandle, message.word);
return 0;
}
static int midiwire_program_change (lua_State *L) {
unsigned long result;
HMIDIOUT outHandle;
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
int program = luaL_checkinteger(L, 2) - 1;
int channel = 0;
if (lua_gettop (L)==3) {
channel = luaL_checkinteger(L, 3) - 1;
}
outHandle = lua_touserdata (L, 1);
union { unsigned long word; unsigned char data[4]; } message;
message.data[0] = 0xC0 + channel; // MIDI note-on message (requires to data bytes)
message.data[1] = program; // MIDI note-on message: Key number (60 = middle C)
message.data[2] = 0; // MIDI note-on message: Key velocity (100 = loud)
message.data[3] = 0; // Unused parameter
midiOutShortMsg(outHandle, message.word);
return 0;
}
static const struct luaL_Reg midiwire[] = {
{"sleep", midiwire_sleep},
{"device_count", midiwire_device_count},
{"name", midiwire_name},
{"open_out", midiwire_open_out},
{"note_on", midiwire_note_on},
{"note_off", midiwire_note_off},
{"drum", midiwire_drum},
{"program_change", midiwire_program_change},
{"reset", midiwire_reset},
{NULL, NULL},
};

View File

@ -1,8 +1,5 @@
local mw = require "midiwire"
print "sleeping..."
-- mw.sleep(1000)
print "# of devices:"
print(mw.device_count())
@ -13,6 +10,37 @@ end
print "open port 0:"
port = mw.open_out(0)
print(type(port))
print(type(port), port)
print "notes: (incl sleeping)"
mw.note_on(port, 60, 127)
mw.sleep(1000)
mw.note_on(port, 62, 127)
mw.sleep(2000)
print "drums:"
mw.drum(port, 56)
mw.sleep(500)
mw.drum(port, 40)
mw.sleep(500)
mw.drum(port, 39)
mw.sleep(500)
mw.drum(port, 39)
mw.sleep(500)
print "program change:"
mw.program_change(port, 19,2)
print "channel 2:"
mw.note_on(port, 60, 127, 2)
mw.sleep(1000)
print "off:"
mw.note_off(port, 60, 2)
mw.sleep(2000)
print "reset"
mw.reset(port)
print "Alles gute!"