mt-32-display.cpp
author František Kučera <franta-hg@frantovo.cz>
Tue, 19 May 2020 17:31:26 +0200
branchv_0
changeset 3 51a8362261a9
parent 2 a84830179027
permissions -rw-r--r--
SysEx documentation

/**
 * SysEx message encoder for Roland MT-32
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program 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, version 3 of the License.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#include <iostream>
#include <iomanip>

/**
 * Translates text from standard input to a hex-formatted System Exclusive (SysEx) message for Roland MT-32,
 * which instructs the unit to show given text on the display.
 * 
 * Roland MT-32 is capable to display 20 characters.
 * Longer messages are silently truncated by the MT-32 unit (this software does not check the length).
 * 
 * The SysEx message contains a checksum.
 * If the checksum is wrong, the MT-32 unit shows the "Exc. Checksum error" message for few seconds
 * and then returns back to the default screen.
 * 
 * Only printable ASCII characters are supported. Other characters (bytes) are replaced by "."
 * 
 * Some characters are displayed differently:
 *   ASCII   MT-32
 *       ~   →
 *       \   ¥
 * 
 * Usage examples:
 *     amidi --port="hw:2,0,0" --send-hex="$(echo -n '   Run GNU/Linux    ' | ./mt-32-display )"
 *     while read message; do amidi --port="hw:2,0,0" --send-hex="$(echo -n "$message" | ./mt-32-display )"; done
 * 
 * @param argc
 * @param argv
 * @return 
 */
int main(int argc, char**argv) {
	std::cout << "f0 41 10 16 12 20 00 00 ";
	// f0 = start of SysEx message
	// 41 = Roland manufacturer ID, see https://www.midi.org/specifications-old/item/manufacturer-id-numbers
	// 10 = device ID
	// 16 = model ID
	// 12 = command ID
	// 20 00 00 = show message on the display

	// The start and end of the SysEx message and the manufacturer ID are part of the MIDI standard.
	// The rest of the bytes is Roland specific.

	// TODO: allow changing device ID to support controlling multiple devices in daisy-chain
	// TODO: allow custom replacement character ('.')

	std::cout << std::hex << std::setfill('0');

	int sum = 0;

	// 20 00 00 = display message
	sum += 0x20;
	sum += 0x00;
	sum += 0x00;

	for (char ch; std::cin.read(&ch, 1).good();) {
		if (ch < 32 || ch > 126) ch = '.';
		std::cout << std::setw(2) << ((int) ch) << " ";
		sum += ch;
	}

	sum %= 128;
	sum = 128 - sum;
	std::cout << std::setw(2) << sum;

	std::cout << " f7";
	// f7 = end of SysEx message

	std::cout << std::endl;
	return 0;
}