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
franta-hg@0
     1
/**
franta-hg@0
     2
 * SysEx to SMF convertor
franta-hg@0
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
franta-hg@0
     4
 *
franta-hg@0
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@0
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@0
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@0
     8
 *
franta-hg@0
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@0
    12
 * GNU General Public License for more details.
franta-hg@0
    13
 *
franta-hg@0
    14
 * You should have received a copy of the GNU General Public License
franta-hg@0
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@0
    16
 */
franta-hg@0
    17
#include <iostream>
franta-hg@0
    18
#include <sstream>
franta-hg@0
    19
#include <smf.h>
franta-hg@0
    20
franta-hg@1
    21
/**
franta-hg@1
    22
 * Translates System Exclusive (SysEx) message (binary not HEX) from standard input to a Standard MIDI file (SMF).
franta-hg@1
    23
 * The file path must be passed as a CLI argument.
franta-hg@1
    24
 * 
franta-hg@1
    25
 * Usage examples:
franta-hg@1
    26
 *     cat test.syx | ./sysex2smf test.mid
franta-hg@1
    27
 * 
franta-hg@1
    28
 * Dependencies:
franta-hg@1
    29
 *     libsmf (in Debian-based distributions do: apt install libsmf-dev)
franta-hg@1
    30
 * 
franta-hg@1
    31
 * @param argc
franta-hg@1
    32
 * @param argv
franta-hg@1
    33
 * @return 
franta-hg@1
    34
 */
franta-hg@0
    35
int main(int argc, char**argv) {
franta-hg@1
    36
	int exitCode = 0;
franta-hg@0
    37
	if (argc == 2) {
franta-hg@0
    38
		smf_t* smf = smf_new();
franta-hg@0
    39
		smf_track_t* track = smf_track_new();
franta-hg@0
    40
		smf_add_track(smf, track);
franta-hg@0
    41
franta-hg@0
    42
		std::stringstream data;
franta-hg@0
    43
		for (char ch; std::cin.read(&ch, 1).good();) {
franta-hg@0
    44
			data.put(ch);
franta-hg@0
    45
		}
franta-hg@0
    46
franta-hg@1
    47
		// TODO: review the (void*) – it works but…
franta-hg@0
    48
		smf_event_t* event = smf_event_new_from_pointer((void*) data.str().c_str(), data.tellp());
franta-hg@0
    49
		smf_track_add_event_pulses(track, event, 0);
franta-hg@0
    50
franta-hg@1
    51
		// TODO: check whether file exists?
franta-hg@0
    52
		int res = smf_save(smf, argv[1]);
franta-hg@1
    53
		if (res) {
franta-hg@1
    54
			std::cerr << "Error: Unable to save MIDI file: " << argv[1] << " Error code: " << res << std::endl;
franta-hg@1
    55
			exitCode = 1;
franta-hg@1
    56
		}
franta-hg@0
    57
		smf_delete(smf);
franta-hg@0
    58
	} else {
franta-hg@0
    59
		std::cerr << "Usage: " << argv[0] << " 'output-file.mid'" << std::endl;
franta-hg@1
    60
		exitCode = 1;
franta-hg@0
    61
	}
franta-hg@1
    62
	return exitCode;
franta-hg@0
    63
}