Commit a0102f93 authored by Pila's avatar Pila

Attempted to fix laggs

parent 0de68976
......@@ -8,16 +8,12 @@
#include "midifile/midifile.h"
#define STEAM_CONTROLLER_MAGIC_PERIOD_RATIO 495483.0
#define CHANNEL_COUNT 2
using namespace std;
double midiFrequency[128] = {8.1758, 8.66196, 9.17702, 9.72272, 10.3009, 10.9134, 11.5623, 12.2499, 12.9783, 13.75, 14.5676, 15.4339, 16.3516, 17.3239, 18.354, 19.4454, 20.6017, 21.8268, 23.1247, 24.4997, 25.9565, 27.5, 29.1352, 30.8677, 32.7032, 34.6478, 36.7081, 38.8909, 41.2034, 43.6535, 46.2493, 48.9994, 51.9131, 55, 58.2705, 61.7354, 65.4064, 69.2957, 73.4162, 77.7817, 82.4069, 87.3071, 92.4986, 97.9989, 103.826, 110, 116.541, 123.471, 130.813, 138.591, 146.832, 155.563, 164.814, 174.614, 184.997, 195.998, 207.652, 220, 233.082, 246.942, 261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995, 415.305, 440, 466.164, 493.883, 523.251, 554.365, 587.33, 622.254, 659.255, 698.456, 739.989, 783.991, 830.609, 880, 932.328, 987.767, 1046.5, 1108.73, 1174.66, 1244.51, 1318.51, 1396.91, 1479.98, 1567.98, 1661.22, 1760, 1864.66, 1975.53, 2093, 2217.46, 2349.32, 2489.02, 2637.02, 2793.83, 2959.96, 3135.96, 3322.44, 3520, 3729.31, 3951.07, 4186.01, 4434.92, 4698.64, 4978.03, 5274.04, 5587.65, 5919.91, 6271.93, 6644.88, 7040, 7458.62, 7902.13, 8372.02, 8869.84, 9397.27, 9956.06, 10548.1, 11175.3, 11839.8, 12543.9};
void delay_ms(unsigned int ms){
for(; ms > 0 ; ms --)
usleep(1000);
}
libusb_device_handle* SteamController_OpenAndClaim(int *interface_num){
libusb_device_handle* dev_handle;
//Open Steam Controller device
......@@ -121,57 +117,64 @@ void playSong(libusb_device_handle *steamcontroller_handle, char* songfile){
}
//Waiting for user to press enter
cout << "Ready, press ENTER to start" << endl;
cout << "Ready, press ENTER to start";
std::cin.ignore();
//Get current time point, will be used to know elapsed time
std::chrono::steady_clock::time_point tOrigin = std::chrono::steady_clock::now();
//Iterate through events
MidiFileEvent_t currentEvent;
for ( currentEvent = MidiFile_getFirstEvent(midifile) ; currentEvent != NULL ; currentEvent = MidiFileEvent_getNextEventInFile(currentEvent)) {
if(MidiFileEvent_getType(currentEvent) != MIDI_FILE_EVENT_TYPE_META){
MidiFileEvent_t currentEvent = MidiFile_getFirstEvent(midifile);
while(currentEvent != NULL){
usleep(10000);
//We now need to play all events with tick < currentTime
long currentTick = MidiFile_getTickFromTime(midifile,timeElapsedSince(tOrigin));
//This will contain the events to send to the Steam Controller
MidiFileEvent_t eventsToPlay[CHANNEL_COUNT] = {NULL};
//Retrieving event time
long currentEventTick = MidiFileEvent_getTick(currentEvent);
float currentEventTime = MidiFile_getTimeFromTick(midifile,currentEventTick);
//Iterate through all events until the current time
for( ; currentEvent != NULL && MidiFileEvent_getTick(currentEvent) < currentTick ; currentEvent = MidiFileEvent_getNextEventInFile(currentEvent)){
//If required, wait for event
int waitTime = (currentEventTime - timeElapsedSince(tOrigin)) * 1000.0f;
if (waitTime > 0) {
delay_ms(waitTime);
}
//Only process Note Start events
if (!MidiFileEvent_isNoteStartEvent(currentEvent)) continue;
//If it is a note start event
if (MidiFileEvent_isNoteStartEvent(currentEvent)){
//Look for the matching note end event since we need to now how long to play
MidiFileEvent_t endEvent = MidiFileNoteStartEvent_getNoteEndEvent(currentEvent);
//Get channel event
int eventChannel = MidiFileNoteStartEvent_getChannel(currentEvent);
//If not found, skip this event
if(endEvent == NULL)continue;
//If channel is other than 0 or 1, skip this event, we cannot play it with only 1 steam controller
if(eventChannel < 0 || !(eventChannel < CHANNEL_COUNT)) continue;
//If no end event, skip this event, we won't know when to stop
if(MidiFileNoteStartEvent_getNoteEndEvent(currentEvent) == NULL) continue;
//If we arrive here, this event match all conditions
eventsToPlay[eventChannel]=currentEvent;
}
//Get channel event
int eventChannel = MidiFileNoteStartEvent_getChannel(currentEvent);
//Now play the last events found
for(int i = 0 ; i < CHANNEL_COUNT ; i++){
MidiFileEvent_t selectedEvent = eventsToPlay[i];
//If channel is other than 0 or 1, skip this event, we cannot play it with only 1 steam controller
if(eventChannel < 0 || eventChannel > 1) continue;
//If no event available on the channel skip it
if(selectedEvent == NULL) continue;
//Get end event time
long endEventTick = MidiFileEvent_getTick(endEvent);
float endEventTime = MidiFile_getTimeFromTick(midifile,endEventTick);
//Look for the matching note end event since we need to now how long to play
MidiFileEvent_t endEvent = MidiFileNoteStartEvent_getNoteEndEvent(selectedEvent);
//Compute note duration
float duration = endEventTime - currentEventTime;
//Get duration
float selectedEventTime = MidiFile_getTimeFromTick(midifile,MidiFileEvent_getTick(selectedEvent));
float endEventTime = MidiFile_getTimeFromTick(midifile,MidiFileEvent_getTick(endEvent));
float duration = endEventTime - selectedEventTime;
//Get event note
int eventNote = MidiFileNoteStartEvent_getNote(currentEvent);
//Get event note
int eventNote = MidiFileNoteStartEvent_getNote(selectedEvent);
//Finally drive the Steam Controller
duration-= 0.02f; //This is to deal with wireless lagg happening sometimes
SteamController_PlayNote(steamcontroller_handle,eventChannel,eventNote,duration);
duration-= 0.02f; //This is to deal with wireless lagg happening sometimes
SteamController_PlayNote(steamcontroller_handle,i,eventNote,duration);
cout << ((eventChannel == 0) ? "RIGHT" : "LEFT ") << " haptic : note " << eventNote << " for "<< (int)(duration*1000) << " ms" <<endl;
}
cout << ((i == 0) ? "RIGHT" : "LEFT ") << " haptic : note " << eventNote << " for "<< (int)(duration*1000) << " ms" <<endl;
}
}
......
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