Commit 32b0abb8 authored by Pila's avatar Pila

Enhanced arguments parsing, added demo mode

parent 71282e45
......@@ -97,12 +97,10 @@ float timeElapsedSince(std::chrono::steady_clock::time_point tOrigin){
return time_span.count();
}
void playSong(libusb_device_handle *steamcontroller_handle, char* songfile, unsigned int sleepIntervalUsec){
void playSong(libusb_device_handle *steamcontroller_handle, const char* songfile, unsigned int sleepIntervalUsec){
MidiFile_t midifile;
cout << "Loading midi file " << songfile << endl;
//Open Midi File
midifile = MidiFile_load(songfile);
......@@ -118,8 +116,8 @@ void playSong(libusb_device_handle *steamcontroller_handle, char* songfile, unsi
}
//Waiting for user to press enter
cout << "Ready, press ENTER to start";
std::cin.ignore();
cout << "Starting playback of " <<songfile << endl;
sleep(1);
//Get current time point, will be used to know elapsed time
std::chrono::steady_clock::time_point tOrigin = std::chrono::steady_clock::now();
......@@ -181,49 +179,75 @@ void playSong(libusb_device_handle *steamcontroller_handle, char* songfile, unsi
cout << "Playback completed " << endl;
}
int main(int argc, char** argv)
{
libusb_device_handle *dev_handle; //a device handle
int interface_num = 0;
unsigned int intervalUSec = DEFAULT_INTERVAL_USEC;
int libusbDebugLevel = LIBUSB_LOG_LEVEL_NONE;
char* midiSong;
int r; //for return values
cout <<"Steam Controller Singer by Pila"<<endl;
if(argc < 2){
cout << "Usage : steamcontrollersinger MIDI_FILE [-dDEBUG_LEVEL] [-iINTERVAL]" << endl;
return 1;
}
//Parse arguments
midiSong = argv[1];
for(int i = 2 ; i < argc ; i++){
if(argv[i][0] != '-') continue;
struct ParamsStruct{
const char* midiSong;
unsigned int intervalUSec;
int libusbDebugLevel;
bool demoMode;
};
unsigned long int value = strtoul(&argv[i][2],NULL,10);
switch(argv[i][1]){
case 'd':
bool parseArguments(int argc, char** argv, ParamsStruct* params){
int c;
while ( (c = getopt(argc, argv, "di:l:")) != -1) {
unsigned long int value = strtoul(optarg,NULL,10);
switch(c){
case 'l':
if(value <= LIBUSB_LOG_LEVEL_DEBUG){
libusbDebugLevel = value;
params->libusbDebugLevel = value;
}
break;
case 'i':
if(value <= 1000000){
intervalUSec = value;
params->intervalUSec = value;
}
break;
case 'd':
params->demoMode = true;
break;
default:
break;
}
}
cout << "optind : "<<optind << ",argc " << argc << endl;
if(optind == argc-1 ){
params->midiSong = argv[optind];
return true;
}
else{
return false;
}
}
int main(int argc, char** argv)
{
libusb_device_handle *dev_handle; //a device handle
int interface_num = 0;
int r; //for return values
cout <<"Steam Controller Singer by Pila"<<endl;
ParamsStruct params;
params.intervalUSec = DEFAULT_INTERVAL_USEC;
params.libusbDebugLevel = LIBUSB_LOG_LEVEL_NONE;
params.demoMode = false;
params.midiSong = "\0";
//Parse arguments
if(!parseArguments(argc, argv, &params)){
cout << "Usage : steamcontrollersinger [-d][-lDEBUG_LEVEL] [-iINTERVAL] MIDI_FILE" << endl;
return 1;
}
//Initializing LIBUSB
r = libusb_init(NULL);
......@@ -233,7 +257,7 @@ int main(int argc, char** argv)
return 1;
}
libusb_set_debug(NULL, libusbDebugLevel);
libusb_set_debug(NULL, params.libusbDebugLevel);
//Gaining access to Steam Controller
dev_handle = SteamController_OpenAndClaim(&interface_num);
......@@ -242,7 +266,9 @@ int main(int argc, char** argv)
//Playing song
playSong(dev_handle,midiSong,intervalUSec);
do{
playSong(dev_handle,params.midiSong,params.intervalUSec);
}while(params.demoMode);
//Releasing access to Steam Controller
......
......@@ -351,7 +351,7 @@ static void remove_event(MidiFileEvent_t event)
* Public API
*/
MidiFile_t MidiFile_load(char *filename)
MidiFile_t MidiFile_load(const char *filename)
{
MidiFile_t midi_file;
FILE *in;
......@@ -1472,13 +1472,11 @@ long MidiFile_getTickFromMeasureBeatTick(MidiFile_t midi_file, MidiFileMeasureBe
{
MidiFileTrack_t conductor_track = MidiFile_getFirstTrack(midi_file);
MidiFileEvent_t event;
long time_signature_event_tick = 0;
float time_signature_event_beat = 0.0;
float time_signature_event_beat = 0.0;
float time_signature_event_measure = 0.0;
long time_signature_event_visible_measure = 1;
long time_signature_event_visible_beat = 1;
float time_signature_event_visible_tick = 0.0;
int numerator = 4;
int numerator = 4;
int denominator = 4;
for (event = MidiFileTrack_getFirstEvent(conductor_track); event != NULL; event = MidiFileEvent_getNextEventInTrack(event))
......@@ -1492,12 +1490,10 @@ long MidiFile_getTickFromMeasureBeatTick(MidiFile_t midi_file, MidiFileMeasureBe
long next_time_signature_event_visible_beat = (long)((next_time_signature_event_measure - (float)(next_time_signature_event_visible_measure - 1)) * numerator) + 1;
float next_time_signature_event_visible_tick = next_time_signature_event_tick - MidiFile_getTickFromBeat(midi_file, (float)((long)(next_time_signature_event_beat)));
if ((next_time_signature_event_visible_measure > MidiFileMeasureBeatTick_getMeasure(measure_beat_tick)) || ((next_time_signature_event_visible_measure == MidiFileMeasureBeatTick_getMeasure(measure_beat_tick)) && ((next_time_signature_event_visible_beat > MidiFileMeasureBeatTick_getBeat(measure_beat_tick)) || ((next_time_signature_event_visible_measure == MidiFileMeasureBeatTick_getBeat(measure_beat_tick)) && (next_time_signature_event_visible_tick >= MidiFileMeasureBeatTick_getTick(measure_beat_tick)))))) break;
time_signature_event_tick = next_time_signature_event_tick;
time_signature_event_beat = next_time_signature_event_beat;
time_signature_event_measure = next_time_signature_event_measure;
time_signature_event_visible_measure = next_time_signature_event_visible_measure;
time_signature_event_visible_beat = next_time_signature_event_visible_beat;
time_signature_event_visible_tick = next_time_signature_event_visible_tick;
numerator = MidiFileTimeSignatureEvent_getNumerator(event);
denominator = MidiFileTimeSignatureEvent_getDenominator(event);
}
......
......@@ -93,7 +93,7 @@ typedef enum
}
MidiFileEventType_t;
MidiFile_t MidiFile_load(char *filename);
MidiFile_t MidiFile_load(const char *filename);
int MidiFile_save(MidiFile_t midi_file, const char* filename);
MidiFile_t MidiFile_new(int file_format, MidiFileDivisionType_t division_type, int resolution);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment