qmk_firmware/tmk_core/protocol/midi/midi.h

488 lines
13 KiB
C
Raw Permalink Normal View History

2019-08-30 18:19:03 +00:00
// midi for embedded chips,
// Copyright 2010 Alex Norman
2015-08-20 04:42:28 +00:00
//
2019-08-30 18:19:03 +00:00
// This file is part of avr-midi.
2015-08-20 04:42:28 +00:00
//
2019-08-30 18:19:03 +00:00
// avr-midi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
2015-08-20 04:42:28 +00:00
//(at your option) any later version.
//
2019-08-30 18:19:03 +00:00
// avr-midi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
2015-08-20 04:42:28 +00:00
//
2019-08-30 18:19:03 +00:00
// You should have received a copy of the GNU General Public License
// along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
2015-08-20 04:42:28 +00:00
/**
* @file
* @brief The main midi functions
*
* This file includes all of the functions you need to set up and process a
* midi device, send midi, and register midi callbacks.
*
*/
#pragma once
2015-08-20 04:42:28 +00:00
#ifdef __cplusplus
extern "C" {
2019-08-30 18:19:03 +00:00
#endif
2015-08-20 04:42:28 +00:00
#include "midi_device.h"
#include "midi_function_types.h"
/**
* @defgroup midi_device_setup_process Device initialization and processing
* @brief These are method that you must use to initialize and run a device
*
* @{
*/
/**
* @brief Initialize a device
*
* You must call this before using the device in question.
*
* @param device the device to initialize
2019-08-30 18:19:03 +00:00
*/
void midi_device_init(MidiDevice* device); // [implementation in midi_device.c]
2015-08-20 04:42:28 +00:00
/**
* @brief Process input data
*
* This method drives the input processing, you must call this method frequently
* if you expect to have your input callbacks called.
*
* @param device the device to process
2019-08-30 18:19:03 +00:00
*/
void midi_device_process(MidiDevice* device); // [implementation in midi_device.c]
2015-08-20 04:42:28 +00:00
/**@}*/
/**
* @defgroup send_functions Midi send functions
* @brief These are the functions you use to send midi data through a device.
* @{
*/
/**
* @brief Send a control change message (cc) via the given device.
*
* @param device the device to use for sending
* @param chan the channel to send on, 0-15
* @param num the cc num
* @param val the value of that cc num
2019-08-30 18:19:03 +00:00
*/
void midi_send_cc(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t val);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a note on message via the given device.
*
* @param device the device to use for sending
* @param chan the channel to send on, 0-15
* @param num the note number
* @param vel the note velocity
2019-08-30 18:19:03 +00:00
*/
void midi_send_noteon(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a note off message via the given device.
*
* @param device the device to use for sending
* @param chan the channel to send on, 0-15
* @param num the note number
* @param vel the note velocity
2019-08-30 18:19:03 +00:00
*/
void midi_send_noteoff(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel);
2015-08-20 04:42:28 +00:00
/**
* @brief Send an after touch message via the given device.
*
* @param device the device to use for sending
* @param chan the channel to send on, 0-15
* @param note_num the note number
* @param amt the after touch amount
2019-08-30 18:19:03 +00:00
*/
void midi_send_aftertouch(MidiDevice* device, uint8_t chan, uint8_t note_num, uint8_t amt);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a pitch bend message via the given device.
*
* @param device the device to use for sending
* @param chan the channel to send on, 0-15
* @param amt the bend amount range: -8192..8191, 0 means no bend
2019-08-30 18:19:03 +00:00
*/
void midi_send_pitchbend(MidiDevice* device, uint8_t chan, int16_t amt); // range -8192, 8191
2015-08-20 04:42:28 +00:00
/**
* @brief Send a program change message via the given device.
*
* @param device the device to use for sending
* @param chan the channel to send on, 0-15
* @param num the program to change to
2019-08-30 18:19:03 +00:00
*/
void midi_send_programchange(MidiDevice* device, uint8_t chan, uint8_t num);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a channel pressure message via the given device.
*
* @param device the device to use for sending
* @param chan the channel to send on, 0-15
* @param amt the amount of channel pressure
2019-08-30 18:19:03 +00:00
*/
void midi_send_channelpressure(MidiDevice* device, uint8_t chan, uint8_t amt);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a clock message via the given device.
*
* @param device the device to use for sending
*/
2019-08-30 18:19:03 +00:00
void midi_send_clock(MidiDevice* device);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a tick message via the given device.
*
* @param device the device to use for sending
*/
2019-08-30 18:19:03 +00:00
void midi_send_tick(MidiDevice* device);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a start message via the given device.
*
* @param device the device to use for sending
*/
2019-08-30 18:19:03 +00:00
void midi_send_start(MidiDevice* device);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a continue message via the given device.
*
* @param device the device to use for sending
*/
2019-08-30 18:19:03 +00:00
void midi_send_continue(MidiDevice* device);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a stop message via the given device.
*
* @param device the device to use for sending
*/
2019-08-30 18:19:03 +00:00
void midi_send_stop(MidiDevice* device);
2015-08-20 04:42:28 +00:00
/**
* @brief Send an active sense message via the given device.
*
* @param device the device to use for sending
*/
2019-08-30 18:19:03 +00:00
void midi_send_activesense(MidiDevice* device);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a reset message via the given device.
*
* @param device the device to use for sending
*/
2019-08-30 18:19:03 +00:00
void midi_send_reset(MidiDevice* device);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a tc quarter frame message via the given device.
*
* @param device the device to use for sending
* @param time the time of this quarter frame, range 0..16383
*/
2019-08-30 18:19:03 +00:00
void midi_send_tcquarterframe(MidiDevice* device, uint8_t time);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a song position message via the given device.
*
* @param device the device to use for sending
* @param pos the song position
*/
2019-08-30 18:19:03 +00:00
void midi_send_songposition(MidiDevice* device, uint16_t pos);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a song select message via the given device.
*
* @param device the device to use for sending
* @param song the song to select
*/
2019-08-30 18:19:03 +00:00
void midi_send_songselect(MidiDevice* device, uint8_t song);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a tune request message via the given device.
*
* @param device the device to use for sending
*/
2019-08-30 18:19:03 +00:00
void midi_send_tunerequest(MidiDevice* device);
2015-08-20 04:42:28 +00:00
/**
* @brief Send a byte via the given device.
*
* This is a generic method for sending data via the given midi device.
* This would be useful for sending sysex data or messages that are not
* implemented in this API, if there are any. Please contact the author
* if you find some so we can add them.
*
* @param device the device to use for sending
* @param b the byte to send
*/
2019-08-30 18:19:03 +00:00
void midi_send_byte(MidiDevice* device, uint8_t b);
2015-08-20 04:42:28 +00:00
/**
* @brief Send up to 3 bytes of data
*
* % 4 is applied to count so that you can use this to pass sysex through
*
* @param device the device to use for sending
* @param count the count of bytes to send, %4 is applied
* @param byte0 the first byte
* @param byte1 the second byte, ignored if cnt % 4 != 2
* @param byte2 the third byte, ignored if cnt % 4 != 3
*/
2019-08-30 18:19:03 +00:00
void midi_send_data(MidiDevice* device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2);
2015-08-20 04:42:28 +00:00
/**
* @brief Send an array of formatted midi data.
*
* Can be used for sysex.
*
* @param device the device to use for sending
* @param count the count of bytes to send
* @param array the array of bytes
*/
2019-08-30 18:19:03 +00:00
void midi_send_array(MidiDevice* device, uint16_t count, uint8_t* array);
2015-08-20 04:42:28 +00:00
/**@}*/
/**
* @defgroup input_callback_reg Input callback registration functions
2019-08-30 18:19:03 +00:00
*
2015-08-20 04:42:28 +00:00
* @brief These are the functions you use to register your input callbacks.
*
* The functions are called when the appropriate midi message is matched on the
* associated device's input.
*
* @{
*/
2019-08-30 18:19:03 +00:00
// three byte funcs
2015-08-20 04:42:28 +00:00
/**
* @brief Register a control change message (cc) callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_cc_callback(MidiDevice* device, midi_three_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register a note on callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_noteon_callback(MidiDevice* device, midi_three_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register a note off callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_noteoff_callback(MidiDevice* device, midi_three_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register an after touch callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_aftertouch_callback(MidiDevice* device, midi_three_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register a pitch bend callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_pitchbend_callback(MidiDevice* device, midi_three_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register a song position callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_songposition_callback(MidiDevice* device, midi_three_byte_func_t func);
2015-08-20 04:42:28 +00:00
2019-08-30 18:19:03 +00:00
// two byte funcs
2015-08-20 04:42:28 +00:00
/**
* @brief Register a program change callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_progchange_callback(MidiDevice* device, midi_two_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register a channel pressure callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_chanpressure_callback(MidiDevice* device, midi_two_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register a song select callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_songselect_callback(MidiDevice* device, midi_two_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register a tc quarter frame callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_tc_quarterframe_callback(MidiDevice* device, midi_two_byte_func_t func);
2015-08-20 04:42:28 +00:00
2019-08-30 18:19:03 +00:00
// one byte funcs
2015-08-20 04:42:28 +00:00
/**
* @brief Register a realtime callback.
*
* The callback will be called for all of the real time message types.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_realtime_callback(MidiDevice* device, midi_one_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register a tune request callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_tunerequest_callback(MidiDevice* device, midi_one_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register a sysex callback.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_sysex_callback(MidiDevice* device, midi_sysex_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register fall through callback.
*
* This is only called if a more specific callback is not matched and called.
* For instance, if you don't register a note on callback but you get a note on message
* the fall through callback will be called, if it is registered.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_fallthrough_callback(MidiDevice* device, midi_var_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**
* @brief Register a catch all callback.
*
* If registered, the catch all callback is called for every message that is
* matched, even if a more specific or the fallthrough callback is registered.
*
* @param device the device associate with
* @param func the callback function to register
*/
2019-08-30 18:19:03 +00:00
void midi_register_catchall_callback(MidiDevice* device, midi_var_byte_func_t func);
2015-08-20 04:42:28 +00:00
/**@}*/
/**
* @defgroup midi_util Device independent utility functions.
* @{
*/
/**
* \enum midi_packet_length_t
*
* An enumeration of the possible packet length values.
*/
2019-08-30 18:19:03 +00:00
typedef enum { UNDEFINED = 0, ONE = 1, TWO = 2, THREE = 3 } midi_packet_length_t;
2015-08-20 04:42:28 +00:00
/**
* @brief Test to see if the byte given is a status byte
* @param theByte the byte to test
* @return true if the byte given is a midi status byte
*/
bool midi_is_statusbyte(uint8_t theByte);
/**
* @brief Test to see if the byte given is a realtime message
* @param theByte the byte to test
* @return true if it is a realtime message, false otherwise
*/
bool midi_is_realtime(uint8_t theByte);
/**
* @brief Find the length of the packet associated with the status byte given
* @param status the status byte
* @return the length of the packet, will return UNDEFINED if the byte is not
* a status byte or if it is a sysex status byte
*/
midi_packet_length_t midi_packet_length(uint8_t status);
/**@}*/
/**
* @defgroup defines Midi status and miscellaneous utility #defines
*
* @{
*/
#define SYSEX_BEGIN 0xF0
#define SYSEX_END 0xF7
2019-08-30 18:19:03 +00:00
// if you and this with a byte and you get anything non-zero
// it is a status message
2015-08-20 04:42:28 +00:00
#define MIDI_STATUSMASK 0x80
2019-08-30 18:19:03 +00:00
// if you and this with a status message that contains channel info,
// you'll get the channel
2015-08-20 04:42:28 +00:00
#define MIDI_CHANMASK 0x0F
#define MIDI_CC 0xB0
#define MIDI_NOTEON 0x90
#define MIDI_NOTEOFF 0x80
#define MIDI_AFTERTOUCH 0xA0
#define MIDI_PITCHBEND 0xE0
#define MIDI_PROGCHANGE 0xC0
#define MIDI_CHANPRESSURE 0xD0
2019-08-30 18:19:03 +00:00
// midi realtime
2015-08-20 04:42:28 +00:00
#define MIDI_CLOCK 0xF8
#define MIDI_TICK 0xF9
#define MIDI_START 0xFA
#define MIDI_CONTINUE 0xFB
#define MIDI_STOP 0xFC
#define MIDI_ACTIVESENSE 0xFE
#define MIDI_RESET 0xFF
#define MIDI_TC_QUARTERFRAME 0xF1
#define MIDI_SONGPOSITION 0xF2
#define MIDI_SONGSELECT 0xF3
#define MIDI_TUNEREQUEST 0xF6
2019-08-30 18:19:03 +00:00
// This ID is for educational or development use only
2015-08-20 04:42:28 +00:00
#define SYSEX_EDUMANUFID 0x7D
/**@}*/
#ifdef __cplusplus
}
#endif