diff options
-rw-r--r-- | applause.c | 29 | ||||
-rw-r--r-- | applause.lua | 16 |
2 files changed, 28 insertions, 17 deletions
@@ -51,7 +51,7 @@ static jack_port_t *midi_port; * The values are the NOTE ON velocities. * Access must be syncronized with `midi_mutex`. */ -static uint8_t midi_notes[16][127]; +static uint8_t midi_notes[16][128]; /** The MIDI note triggered last on a channel */ static int midi_notes_last[16]; @@ -62,7 +62,7 @@ static int midi_notes_last[16]; * Perhaps this should use atomic operations instead * (wasting a few kilobytes). */ -static uint8_t midi_controls[16][127]; +static uint8_t midi_controls[16][128]; /** * Mutex for synchronizing access to `midi_controls`. @@ -195,7 +195,7 @@ jack_process(jack_nframes_t nframes, void *arg) channel = event.buffer[0] & 0x0F; switch (event.buffer[0] & 0xF0) { - case 0x80: + case 0x80: /* NOTE OFF */ pthread_mutex_lock(&midi_mutex); /* NOTE: The NOTE OFF velocity is currently ignored */ midi_notes[channel] @@ -204,15 +204,16 @@ jack_process(jack_nframes_t nframes, void *arg) pthread_mutex_unlock(&midi_mutex); break; - case 0x90: + case 0x90: /* NOTE ON */ pthread_mutex_lock(&midi_mutex); + /* NOTE: Velocity of 0 has the same effect as NOTE OFF */ midi_notes[channel] [event.buffer[1]] = event.buffer[2]; midi_notes_last[channel] = event.buffer[1]; pthread_mutex_unlock(&midi_mutex); break; - case 0xB0: + case 0xB0: /* Control Change */ pthread_mutex_lock(&midi_mutex); midi_controls[channel] [event.buffer[1]] = event.buffer[2]; @@ -386,8 +387,10 @@ l_MIDIVelocityStream_getValue(lua_State *L) luaL_argcheck(L, 0 <= note && note <= 127, note, "Invalid note number range (0 <= x <= 127)"); channel = lua_tointeger(L, 2); - luaL_argcheck(L, 0 <= channel && channel <= 15, channel, - "Invalid channel range (0 <= x <= 15)"); + luaL_argcheck(L, 1 <= channel && channel <= 16, channel, + "Invalid channel range (1 <= x <= 16)"); + /* The NOTE arrays are 0-based */ + channel--; pthread_mutex_lock(&midi_mutex); /* @@ -415,8 +418,10 @@ l_MIDINoteStream_getValue(lua_State *L) luaL_checktype(L, 1, LUA_TNUMBER); channel = lua_tointeger(L, 1); - luaL_argcheck(L, 0 <= channel && channel <= 15, channel, - "Invalid channel range (0 <= x <= 15)"); + luaL_argcheck(L, 1 <= channel && channel <= 16, channel, + "Invalid channel range (1 <= x <= 16)"); + /* The NOTE arrays are 0-based */ + channel--; pthread_mutex_lock(&midi_mutex); /* @@ -449,8 +454,10 @@ l_MIDICCStream_getValue(lua_State *L) luaL_argcheck(L, 0 <= control && control <= 127, control, "Invalid control number range (0 <= x <= 127)"); channel = lua_tointeger(L, 2); - luaL_argcheck(L, 0 <= channel && channel <= 15, channel, - "Invalid channel range (0 <= x <= 15)"); + luaL_argcheck(L, 1 <= channel && channel <= 16, channel, + "Invalid channel range (1 <= x <= 16)"); + /* The NOTE arrays are 0-based */ + channel--; pthread_mutex_lock(&midi_mutex); /* diff --git a/applause.lua b/applause.lua index a76fb41..c701bc3 100644 --- a/applause.lua +++ b/applause.lua @@ -903,10 +903,14 @@ function NoiseStream:tick() end end +-- +-- MIDI Support +-- + -- Velocity of NOTE ON for a specific note on a channel MIDIVelocityStream = DeriveClass(Stream, function(self, note, channel) self.note = note - self.channel = channel or 0 + self.channel = channel or 1 end) -- implemented in applause.c, private! @@ -930,7 +934,7 @@ end -- The MIDI note is the lower byte and the velocity the -- upper byte of the word. MIDINoteStream = DeriveClass(Stream, function(self, channel) - self.channel = channel or 0 + self.channel = channel or 1 end) -- implemented in applause.c, private! @@ -949,7 +953,7 @@ end MIDICCStream = DeriveClass(Stream, function(self, control, channel) self.control = control - self.channel = channel or 0 + self.channel = channel or 1 end) -- implemented in applause.c, private! @@ -969,12 +973,12 @@ end -- MIDI primitives --- There are only 120 different MIDI notes, +-- There are only 128 possible MIDI notes, -- so their frequencies can and should be cached. -- We do this once instead of on-demand, so the lookup -- table consists of consecutive numbers. -local mtof_cache = table.new(120, 0) -for note = 0, 119 do +local mtof_cache = table.new(128, 0) +for note = 0, 127 do -- MIDI NOTE 69 corresponds to 440 Hz mtof_cache[note] = 440*math.pow(2, (note - 69)/12) end |