sysex2smf.cpp
branchv_0
changeset 1 b3c075114b95
parent 0 dcdd12e654da
equal deleted inserted replaced
0:dcdd12e654da 1:b3c075114b95
    16  */
    16  */
    17 #include <iostream>
    17 #include <iostream>
    18 #include <sstream>
    18 #include <sstream>
    19 #include <smf.h>
    19 #include <smf.h>
    20 
    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  */
    21 int main(int argc, char**argv) {
    35 int main(int argc, char**argv) {
       
    36 	int exitCode = 0;
    22 	if (argc == 2) {
    37 	if (argc == 2) {
    23 		smf_t* smf = smf_new();
    38 		smf_t* smf = smf_new();
    24 		smf_track_t* track = smf_track_new();
    39 		smf_track_t* track = smf_track_new();
    25 		smf_add_track(smf, track);
    40 		smf_add_track(smf, track);
    26 
    41 
    27 		std::stringstream data;
    42 		std::stringstream data;
    28 		for (char ch; std::cin.read(&ch, 1).good();) {
    43 		for (char ch; std::cin.read(&ch, 1).good();) {
    29 			data.put(ch);
    44 			data.put(ch);
    30 		}
    45 		}
    31 
    46 
       
    47 		// TODO: review the (void*) – it works but…
    32 		smf_event_t* event = smf_event_new_from_pointer((void*) data.str().c_str(), data.tellp());
    48 		smf_event_t* event = smf_event_new_from_pointer((void*) data.str().c_str(), data.tellp());
    33 		smf_track_add_event_pulses(track, event, 0);
    49 		smf_track_add_event_pulses(track, event, 0);
    34 
    50 
    35 
    51 		// TODO: check whether file exists?
    36 		int res = smf_save(smf, argv[1]);
    52 		int res = smf_save(smf, argv[1]);
    37 		if (res) std::cerr << "unable to save MIDI file: " << res << std::endl;
    53 		if (res) {
       
    54 			std::cerr << "Error: Unable to save MIDI file: " << argv[1] << " Error code: " << res << std::endl;
       
    55 			exitCode = 1;
       
    56 		}
    38 		smf_delete(smf);
    57 		smf_delete(smf);
    39 	} else {
    58 	} else {
    40 		std::cerr << "Usage: " << argv[0] << " 'output-file.mid'" << std::endl;
    59 		std::cerr << "Usage: " << argv[0] << " 'output-file.mid'" << std::endl;
       
    60 		exitCode = 1;
    41 	}
    61 	}
       
    62 	return exitCode;
    42 }
    63 }