sysex2smf.cpp
author František Kučera <franta-hg@frantovo.cz>
Tue, 19 May 2020 23:09:10 +0200
branchv_0
changeset 1 b3c075114b95
parent 0 dcdd12e654da
permissions -rw-r--r--
documentation, exit code

/**
 * SysEx to SMF convertor
 * 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 <sstream>
#include <smf.h>

/**
 * Translates System Exclusive (SysEx) message (binary not HEX) from standard input to a Standard MIDI file (SMF).
 * The file path must be passed as a CLI argument.
 * 
 * Usage examples:
 *     cat test.syx | ./sysex2smf test.mid
 * 
 * Dependencies:
 *     libsmf (in Debian-based distributions do: apt install libsmf-dev)
 * 
 * @param argc
 * @param argv
 * @return 
 */
int main(int argc, char**argv) {
	int exitCode = 0;
	if (argc == 2) {
		smf_t* smf = smf_new();
		smf_track_t* track = smf_track_new();
		smf_add_track(smf, track);

		std::stringstream data;
		for (char ch; std::cin.read(&ch, 1).good();) {
			data.put(ch);
		}

		// TODO: review the (void*) – it works but…
		smf_event_t* event = smf_event_new_from_pointer((void*) data.str().c_str(), data.tellp());
		smf_track_add_event_pulses(track, event, 0);

		// TODO: check whether file exists?
		int res = smf_save(smf, argv[1]);
		if (res) {
			std::cerr << "Error: Unable to save MIDI file: " << argv[1] << " Error code: " << res << std::endl;
			exitCode = 1;
		}
		smf_delete(smf);
	} else {
		std::cerr << "Usage: " << argv[0] << " 'output-file.mid'" << std::endl;
		exitCode = 1;
	}
	return exitCode;
}