From: Thomas Pietrzak Date: Thu, 2 Jun 2011 20:43:56 +0000 (+0000) Subject: factoring metronomes, wait for haptic to begin X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=fe545ba3a8cee02ca85d0de8ce6bbdc2bb63499e;p=hapticmetronome.git factoring metronomes, wait for haptic to begin git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@21 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- diff --git a/Haptic Metronome/Haptic Metronome.suo b/Haptic Metronome/Haptic Metronome.suo index 6080ca3..7cdbd58 100644 Binary files a/Haptic Metronome/Haptic Metronome.suo and b/Haptic Metronome/Haptic Metronome.suo differ diff --git a/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj b/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj index 1ec5140..861b312 100644 --- a/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj +++ b/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj @@ -50,9 +50,9 @@ - Level4 + Level3 Disabled - _MBCS;%(PreprocessorDefinitions);WIN32;FULLSCREEN + _MBCS;%(PreprocessorDefinitions);WIN32 4996 @@ -68,7 +68,7 @@ MaxSpeed true true - _MBCS;%(PreprocessorDefinitions);WIN32 + _MBCS;%(PreprocessorDefinitions);WIN32;FULLSCREEN false diff --git a/include/Metronome.hpp b/include/Metronome.hpp index cbcd7b5..1a92832 100644 --- a/include/Metronome.hpp +++ b/include/Metronome.hpp @@ -7,9 +7,13 @@ class Metronome { public: - Metronome(const Track *track, Uint32 nbbeats=4, Uint32 tempo=0, bool activated=false); + Metronome(const Track &track, Uint32 nbbeats=4, Uint32 tempo=0, bool activated=false); virtual ~Metronome(){} + virtual void init(){} + + virtual void checkAt(Uint32 timestamp)=0; + virtual void setActive(bool activated) { _activated = activated; } virtual void setTempo(Uint32 tempo) { _tempo = tempo; } virtual void setNbBeats(Uint32 nbbeats) {_nbbeats = nbbeats; } @@ -20,7 +24,13 @@ class Metronome Uint32 _nbbeats; Uint32 _tempo; bool _activated; - const Track *_track; + unsigned char _currentbeat; + const list > *_bips; + const map > *_tempos; + const map > *_timesignatures; + list >::const_iterator _iBips; + map >::const_iterator _iTempos; + map >::const_iterator _iTimesignature; }; #endif diff --git a/include/MetronomeAudio.hpp b/include/MetronomeAudio.hpp index 7134ffc..b277186 100644 --- a/include/MetronomeAudio.hpp +++ b/include/MetronomeAudio.hpp @@ -13,10 +13,12 @@ class MetronomeAudio : public Metronome { public: - MetronomeAudio(const Track *track, Uint32 nbbeats=4, Uint32 tempo=0, bool activated=true); + MetronomeAudio(const Track &track, Uint32 nbbeats=4, Uint32 tempo=0, bool activated=true); ~MetronomeAudio(); + void checkAt(Uint32 timestamp); + void run(); private: diff --git a/include/MetronomeHaptic.hpp b/include/MetronomeHaptic.hpp index a8a7a98..e402770 100644 --- a/include/MetronomeHaptic.hpp +++ b/include/MetronomeHaptic.hpp @@ -15,13 +15,17 @@ class MetronomeHaptic : public Metronome { public: - MetronomeHaptic(const Track *track, Uint32 nbbeats=4, Uint32 tempo=0, bool activated=true); + MetronomeHaptic(const Track &track, Uint32 nbbeats=4, Uint32 tempo=0, bool activated=true); ~MetronomeHaptic(); + void init(); + void setTempo(Uint32 tempo); void setNbBeats(Uint32 nbbeats); + void checkAt(Uint32 timestamp); + void run(); private: @@ -30,4 +34,4 @@ class MetronomeHaptic : public Metronome Serial *_comport; }; -#endif \ No newline at end of file +#endif diff --git a/src/Metronome.cpp b/src/Metronome.cpp index 5d8396e..52d9622 100644 --- a/src/Metronome.cpp +++ b/src/Metronome.cpp @@ -1,6 +1,10 @@ #include "Metronome.hpp" -Metronome::Metronome(const Track *track, Uint32 nbbeats, Uint32 tempo, bool activated) -:_track(track), _nbbeats(nbbeats), _tempo(tempo), _activated(activated) -{ +Metronome::Metronome(const Track &track, Uint32 nbbeats, Uint32 tempo, bool activated) +:_nbbeats(nbbeats), _tempo(tempo), _activated(activated), _currentbeat(0), + _bips(track.getBips()), _tempos(track.getTempos()), _timesignatures(track.getTimeSignatures()) +{ + _iBips = _bips->begin(); + _iTempos = _tempos->begin(); + _iTimesignature = _timesignatures->begin(); } diff --git a/src/MetronomeAudio.cpp b/src/MetronomeAudio.cpp index 4ab0fda..b785bbd 100644 --- a/src/MetronomeAudio.cpp +++ b/src/MetronomeAudio.cpp @@ -2,13 +2,12 @@ #include "Mixer.hpp" -extern Uint32 start; - #define BIPLENGTH 10 +extern Uint32 start; extern FILE*logfile; -MetronomeAudio::MetronomeAudio(const Track *track, Uint32 nbbeats, Uint32 tempo, bool activated) +MetronomeAudio::MetronomeAudio(const Track &track, Uint32 nbbeats, Uint32 tempo, bool activated) :Metronome(track, nbbeats, tempo, activated), thread(NULL) { Mixer::getInstance()->addSound("bip","bip10.wav"); @@ -26,52 +25,45 @@ void MetronomeAudio::run() thread = SDL_CreateThread(&MetronomeAudio::thread_func, this); } +void MetronomeAudio::checkAt(Uint32 timestamp) +{ + while (_iBips != _bips->end() && timestamp >= BIPTIMESTAMP(*_iBips)) + { + if (_currentbeat == 0) + Mixer::getInstance()->playSound("bip"); + else + Mixer::getInstance()->playSound("bip2"); + if (logfile) + fprintf(logfile, "BIP;%d;%d;%d\n", BIPTICK(*_iBips), BIPTIMESTAMP(*_iBips), timestamp); + SDL_Log("BipA %d at %d\n", BIPTIMESTAMP(*_iBips), timestamp); + + _currentbeat = (_currentbeat + 1) % _nbbeats; + _iBips++; + } + + // on avance tant que les tempos sont dépassées + while (_iTimesignature != _timesignatures->end() && (*_iTimesignature).second.getX() < timestamp) + { + //time signature change + SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Time signature change at %d : tick=%d at %d for %d beats\n", + timestamp, + (*_iTimesignature).first, + (*_iTimesignature).second.getX(), + (*_iTimesignature).second.getY()); + _nbbeats = (*_iTimesignature).second.getY(); + _iTimesignature++; + } +} + int MetronomeAudio::thread_func(void *obj) { MetronomeAudio *thismetronome = static_cast(obj); SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); - const list > *bips = thismetronome->_track->getBips(); - //const map > *tempo = thismetronome->_track->getTempos(); - const map > *timesignatures = thismetronome->_track->getTimeSignatures(); - static list >::const_iterator cBips = bips->begin(); - //static map >::const_iterator cTempo = tempo->begin(); - static map >::const_iterator cTimesignature = timesignatures->begin(); - - int currentbeat = 0; - Uint32 currenttime = 0; - float lag = 0.0; - while(thismetronome->_activated) { - Uint32 currenttime = SDL_GetTicks() - start; - - while (cBips != bips->end() && currenttime >= BIPTIMESTAMP(*cBips)) - { - if (currentbeat == 0) - Mixer::getInstance()->playSound("bip"); - else - Mixer::getInstance()->playSound("bip2"); - if (logfile) - fprintf(logfile, "BIP;%d;%d;%d\n", BIPTICK(*cBips), BIPTIMESTAMP(*cBips), currenttime); - - currentbeat = (currentbeat + 1) % thismetronome->_nbbeats; - cBips++; - } - - // on avance tant que les tempos sont dépassées - while (cTimesignature != timesignatures->end() && (*cTimesignature).second.getX() < currenttime) - { - //tempo change - SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Time signature change at %d : tick=%d at %d for %d beats\n", - currenttime, - (*cTimesignature).first, - (*cTimesignature).second.getX(), - (*cTimesignature).second.getY()); - thismetronome->_nbbeats = (*cTimesignature).second.getY(); - cTimesignature++; - } + thismetronome->checkAt(SDL_GetTicks() - start); SDL_Delay(5); } diff --git a/src/MetronomeHaptic.cpp b/src/MetronomeHaptic.cpp index 70290ef..1647169 100644 --- a/src/MetronomeHaptic.cpp +++ b/src/MetronomeHaptic.cpp @@ -2,18 +2,20 @@ #include "Mixer.hpp" +#include + #ifdef WIN32 #include "SerialWindows.hpp" #else #include "SerialLinux.hpp" #endif -extern Uint32 start; #define BEFOREDELAY 2000 +extern Uint32 start; extern FILE *logfile; -MetronomeHaptic::MetronomeHaptic(const Track *track, Uint32 nbbeats, Uint32 tempo, bool activated) +MetronomeHaptic::MetronomeHaptic(const Track &track, Uint32 nbbeats, Uint32 tempo, bool activated) :Metronome(track, nbbeats, tempo, activated), thread(NULL) { try @@ -28,10 +30,10 @@ MetronomeHaptic::MetronomeHaptic(const Track *track, Uint32 nbbeats, Uint32 temp unsigned char buffer1[3]; buffer1[0] = 'N'; buffer1[1] = 0x00; buffer1[2] = 0x01; unsigned char buffer2[6]; - buffer2[0] = 0x01; //pattern - buffer2[1] = 0; buffer2[2] = 100; //duration - buffer2[3] = 0; buffer2[4] = 250; //frequency - buffer2[5] = 255; //amplitude + buffer2[0] = 0x01; //pattern + buffer2[1] = 0; buffer2[2] = 50; //duration + buffer2[3] = 0; buffer2[4] = 250; //frequency + buffer2[5] = 255; //amplitude //Tacton 1 _comport->WriteData(buffer1, 3); @@ -76,141 +78,72 @@ void MetronomeHaptic::run() thread = SDL_CreateThread(&MetronomeHaptic::thread_func, this); } -int MetronomeHaptic::thread_func(void *obj) +void MetronomeHaptic::init() { - MetronomeHaptic *thismetronome = static_cast(obj); - - SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); - - const list > *bips = thismetronome->_track->getBips(); - //const map > *tempo = thismetronome->_track->getTempos(); - const map > *timesignatures = thismetronome->_track->getTimeSignatures(); - static list >::const_iterator cBips = bips->begin(); - //static map >::const_iterator cTempo = tempo->begin(); - static map >::const_iterator cTimesignature = timesignatures->begin(); + checkAt(BEFOREDELAY); +} - bool written = thismetronome->_comport->WriteData("S", 1); +void MetronomeHaptic::checkAt(Uint32 timestamp) +{ unsigned char outBuffer[6]; outBuffer[0] = 'P'; - unsigned char currentbeat = 0; - - while(thismetronome->_activated) + + //send first beeps in advance + while (_iBips != _bips->end() && timestamp >= BIPTIMESTAMP(*_iBips)) { - Uint32 currenttime = SDL_GetTicks() - start; - - while (cBips != bips->end() && currenttime >= BIPTIMESTAMP(*cBips)) - { -/* char outBuffer[5]; - outBuffer[0] = 'N';*/ - outBuffer[1] = currentbeat % 4; - outBuffer[2] = (BIPTIMESTAMP(*cBips) & 0xff000000) >> 24; - outBuffer[3] = (BIPTIMESTAMP(*cBips) & 0x00ff0000) >> 16; - outBuffer[4] = (BIPTIMESTAMP(*cBips) & 0x0000ff00) >> 8; - outBuffer[5] = BIPTIMESTAMP(*cBips) & 0x000000ff; - - bool written = thismetronome->_comport->WriteData(outBuffer, 6); -/* char INBUFFER[128]; - memset(INBUFFER, 0, 128); - int nbread = thismetronome->_comport->ReadData(INBUFFER, 128); - stringstream s; - s << nbread << " read:\n" << INBUFFER << endl; - OutputDebugString(s.str().c_str());*/ - - if (logfile) - fprintf(logfile, "BIP;%d;%d;%d\n", BIPTICK(*cBips), BIPTIMESTAMP(*cBips), currenttime); - - currentbeat = (currentbeat + 1) % thismetronome->_nbbeats; - cBips++; - } - - // on avance tant que les tempos sont dépassées - while (cTimesignature != timesignatures->end() && currenttime >= TIMESTAMP(*cTimesignature)) - { -/* char outBuffer[5]; - outBuffer[0] = 'B'; - outBuffer[1] = (TIMESTAMP(*cTimesignature) & 0xff000000) >> 24; - outBuffer[2] = (TIMESTAMP(*cTimesignature) & 0x00ff0000) >> 16; - outBuffer[3] = (TIMESTAMP(*cTimesignature) & 0x0000ff00) >> 8; - outBuffer[4] = TIMESTAMP(*cTimesignature) & 0x000000ff; - - bool written = thismetronome->_comport->WriteData(outBuffer, 5); - */ - //time signature change - SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Time signature change at %d : tick=%d at %d for %d beats\n", - currenttime, - (*cTimesignature).first, - (*cTimesignature).second.getX(), - (*cTimesignature).second.getY()); - thismetronome->_nbbeats = (*cTimesignature).second.getY(); - cTimesignature++; - } + outBuffer[1] = _currentbeat % 4; + outBuffer[2] = (BIPTIMESTAMP(*_iBips) & 0xff000000) >> 24; + outBuffer[3] = (BIPTIMESTAMP(*_iBips) & 0x00ff0000) >> 16; + outBuffer[4] = (BIPTIMESTAMP(*_iBips) & 0x0000ff00) >> 8; + outBuffer[5] = BIPTIMESTAMP(*_iBips) & 0x000000ff; + bool written = _comport->WriteData(outBuffer, 6); + + if (logfile) + fprintf(logfile, "BIP;%d;%d;%d\n", BIPTICK(*_iBips), BIPTIMESTAMP(*_iBips), 0); + SDL_Log("BipH %d at %d\n", timestamp, BIPTIMESTAMP(*_iBips)); + + _currentbeat = (_currentbeat + 1) % _nbbeats; + _iBips++; + } - SDL_Delay(5); + // on avance tant que les tempos sont dépassées + while (_iTimesignature != _timesignatures->end() && BEFOREDELAY >= TIMESTAMP(*_iTimesignature)) + { + //time signature change + SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Time signature change at %d : tick=%d at %d for %d beats\n", + timestamp, + (*_iTimesignature).first, + (*_iTimesignature).second.getX(), + (*_iTimesignature).second.getY()); + _nbbeats = (*_iTimesignature).second.getY(); + _iTimesignature++; } - return 0; } -/* int MetronomeHaptic::thread_func(void *obj) { MetronomeHaptic *thismetronome = static_cast(obj); - SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); +// SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); - const map > *tempo = thismetronome->_track->getTempos(); - const map > *timesignatures = thismetronome->_track->getTimeSignatures(); - static map >::const_iterator cTempo = tempo->begin(); - static map >::const_iterator cTimesignature = timesignatures->begin(); - -#ifdef WIN32 - struct { int tv_sec, tv_usec; } now, start; - DWORD t = timeGetTime (); - start.tv_sec = t / 1000; - start.tv_usec = (t % 1000) * 1000; -#else - struct timeval now, start; - gettimeofday(&start, NULL); -#endif - float currenttime = 0; + unsigned char outBuffer[6]; + outBuffer[0] = 'P'; + + bool written = thismetronome->_comport->WriteData("S", 1); while(thismetronome->_activated) { -#ifdef WIN32 - t = timeGetTime (); - now.tv_sec = t / 1000; - now.tv_usec = (t % 1000) * 1000; -#else - gettimeofday(&now, NULL); -#endif - float currenttime = (now.tv_sec - start.tv_sec) * 1000.0f + (now.tv_usec - start.tv_usec) / 1000.0f; - - // on avance tant que les tempos sont dépassées - while (cTempo != tempo->end() && currenttime > (*cTempo).second.getX()) - { - //tempo change - stringstream s; - s << "Tempo change at " << currenttime << " : tick=" << (*cTempo).first << " at " << (*cTempo).second.getX() << " for " << (60000000/(*cTempo).second.getY()) << " bpm" << endl; - OutputDebugString(s.str().c_str()); - - thismetronome->setTempo((*cTempo).second.getY()); - - cTempo++; - } - - // on avance tant que les tempos sont dépassées - while (cTimesignature != timesignatures->end() && (*cTimesignature).second.getX() < currenttime) - { - //time signature change - stringstream s; - s << "Time signature change at " << currenttime << " : tick=" << (*cTimesignature).first << " at " << (*cTimesignature).second.getX() << " for " << (*cTimesignature).second.getY() << " beats " << endl; - OutputDebugString(s.str().c_str()); - - thismetronome->setNbBeats((*cTimesignature).second.getY()); - - cTimesignature++; - } + // TEST + char INBUFFER[128]; + memset(INBUFFER, 0, 128); + int nbread = thismetronome->_comport->ReadData(INBUFFER, 128); + if (nbread > 0) + SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "%d read: %s\n", nbread, INBUFFER); + // + + thismetronome->checkAt(SDL_GetTicks() - start + BEFOREDELAY); + SDL_Delay(5); } return 0; } -*/ \ No newline at end of file diff --git a/src/PaintGame.cpp b/src/PaintGame.cpp index c94205b..9886a89 100644 --- a/src/PaintGame.cpp +++ b/src/PaintGame.cpp @@ -188,6 +188,10 @@ void init() glEnable(GL_LIGHT0); // SDL_EnableKeyRepeat(10, 100); +#ifdef FULLSCREEN + SDL_ShowCursor( SDL_DISABLE ); +#endif + fromKeyboard.setTimestamp(-1); fromKeyboard.setEndTimestamp(-1); } @@ -619,17 +623,18 @@ void loop(const Track &tr, const String &songname) init(); SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Create Metronome\n"); -// Metronome *m = new MetronomeAudio(&tr); - Metronome *m; + Metronome *m = NULL, *m2 = NULL; try { - m = new MetronomeHaptic(&tr); + //m = new MetronomeHaptic(tr); + m2 = new MetronomeAudio(tr); } catch (char *error) { // cout << error << endl; SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s\n", error); m = NULL; + m2 = NULL; } bool quitProgram = false; @@ -647,23 +652,23 @@ void loop(const Track &tr, const String &songname) if (gametype == REGULAR) Mixer::getInstance()->loadMusic(songname + "/tout.ogg"); -//must be done before paint and metronomes - start = SDL_GetTicks(); -/*#ifdef WIN32 - DWORD t = timeGetTime (); - start.tv_sec = t / 1000; - start.tv_usec = (t % 1000) * 1000; -#else - gettimeofday(&start, NULL); -#endif*/ - SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "LET'S ROCK!\n"); //m->setActive(true); + if (m) + m->init(); + if (m2) + m2->init(); + SDL_Delay(1000); if (gametype == REGULAR) Mixer::getInstance()->playMusic(); + //must be done before paint and metronomes + start = SDL_GetTicks(); if (m) m->run(); + if (m2) + m2->run(); + /* Mixer::getInstance()->playSound("guitar"); if (rhythm) Mixer::getInstance()->playSound("rhythm"); @@ -701,4 +706,5 @@ void loop(const Track &tr, const String &songname) } delete m; + delete m2; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index a72c74e..1ee3801 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,6 +40,7 @@ void handleStrokeOff() int main(int argc, char *argv[]) { + srand(time((time_t *)(NULL))); if (argc < 2) { cout << "Usage : " << argv[0] << "songname [logname] [tempo] [duration] [beatsize]" << endl; @@ -130,5 +131,7 @@ int main(int argc, char *argv[]) if (logfile) fclose(logfile); + SDL_ShowCursor( SDL_ENABLE ); + return 1; }