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