From f3b7df8e1a44be946bbc0cf4c2b89e2920d6d7ee Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Tue, 13 Nov 2012 15:07:39 +0000 Subject: initial asterisk hookup --- vfg/src/Asterisk.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ vfg/src/Asterisk.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++ vfg/src/music.cpp | 27 ++++++++++++++------ vfg/src/testApp.cpp | 44 +++++++++++++++++++++------------ vfg/src/testApp.h | 12 ++++++--- 5 files changed, 187 insertions(+), 28 deletions(-) create mode 100755 vfg/src/Asterisk.cpp create mode 100755 vfg/src/Asterisk.h (limited to 'vfg/src') diff --git a/vfg/src/Asterisk.cpp b/vfg/src/Asterisk.cpp new file mode 100755 index 0000000..8a2309e --- /dev/null +++ b/vfg/src/Asterisk.cpp @@ -0,0 +1,70 @@ +#include "Asterisk.h" + + +//there is no notification that there is someone in the queue + +Asterisk::Asterisk() +{ + udpConnection.Create(); + udpConnection.Bind(5000); + udpConnection.SetNonBlocking(true); + cmd("ssh 80.93.22.22 'sudo /usr/sbin/asterisk -rx \"database put GAME passcode 1234\"'"); + state=WAITING; +} + +void Asterisk::startGame(){ + //if (state=IDLE) { + cmd("ssh 80.93.22.22 'sudo /usr/sbin/asterisk -rx \"devstate change Custom:GAME NOT_INUSE\"'"); + state=STARTING; + //} +} +void Asterisk::cmd(string s) { + file = popen(s.c_str(), "r"); + int filenum=fileno(file); + //fcntl(filenum, F_SETFL, O_NONBLOCK); //doesn't seem to work, still pauses + //pclose(file); //discard the pipe THIS IS BLOCKING +} + +int Asterisk::update(){ + /* + if (state==WAITING||state==STARTING) { + char buf[100]; + ssize_t r = read(filenum, buf, 100); + //if (r == -1 && errno == EAGAIN) + //no data yet + //else + if (r > 0) + //received data + printf("%s\n",buf); + pclose(file); + state=IDLE; + ///else + //pipe closed + } + */ + //check messages and pipes, returns new keypresses + char udpMessage[10000]; + udpConnection.Receive(udpMessage,10000); + string msg=udpMessage; + if(msg!=""){ + if (msg.length()>2) { + //printf("status msg: %s\n",msg.c_str()); + if (msg.substr(0,5)=="Local") { + state=PLAYING; + return 1000; + } + return 0; + } + else { + //printf("cmd: %s\n",msg.c_str()); + return ofToInt(msg); + } + } + else return 0; +} + +Asterisk::~Asterisk() +{ + pclose(file); + //dtor +} diff --git a/vfg/src/Asterisk.h b/vfg/src/Asterisk.h new file mode 100755 index 0000000..b3c5fd5 --- /dev/null +++ b/vfg/src/Asterisk.h @@ -0,0 +1,62 @@ +#ifndef ASTERISK_H +#define ASTERISK_H + +#include "ofMain.h" +#include "ofxNetwork.h" + +#define IDLE 0 +#define WAITING 1 +#define STARTING 2 +#define PLAYING 3 + +// http://stackoverflow.com/questions/1735781/non-blocking-pipe-using-popen + +/* + +Commands for game to control PBX + +Game ready + +ssh 10.10.10.1 'sudo /usr/sbin/asterisk -rx "devstate change Custom:GAME NOT_INUSE"' + +change daily access 4 digit code + +ssh 10.10.10.1 'sudo /usr/sbin/asterisk -rx "database put GAME passcode 1234"' + + + +Set status code for current game + +ssh 10.10.10.1 'sudo /usr/sbin/asterisk -rx "database put GAME statuscode 1234"' + +Gameover Hangup Call + +ssh 10.10.10.1 'sudo /usr/sbin/asterisk -rx "hangup request Local/9999@default-00000039;2" + +Local/9999@default-00000039;2 is a channel identifier and will be send to game when AGI script connects to game controller. The channel ID is unique for each call + + +can we have a message that someone has joined the queue? +can we have a message if someone hangs up? +*/ + +class Asterisk +{ + public: + Asterisk(); + virtual ~Asterisk(); + void startGame(); + int update(); + void cmd(string s); + + int state; + + protected: + private: + int startTime; + FILE *file; + int filenum; + ofxUDPManager udpConnection; +}; + +#endif // ASTERISK_H diff --git a/vfg/src/music.cpp b/vfg/src/music.cpp index 4d62f6f..9fd88d3 100755 --- a/vfg/src/music.cpp +++ b/vfg/src/music.cpp @@ -1,8 +1,18 @@ #include "music.h" //event times & durations are absolute integer milliseconds - - + +int notemap(int n) { + //nonlinear mapping of notes to 3 columns - space 5,4,7 + //note drawing 46h - 52h + int numnotes=16; + int firstnote=70; + int note=n-firstnote; + if (note<5) return 0; + else if (note <9) return 1; + else return 2; +} +//---------------------------------------------------------------------------------------------------------- void lyricscore::draw(){ int scoreTime=ofGetElapsedTimeMillis()-startTime; map::iterator iter; @@ -108,6 +118,7 @@ void musicscore::parseMidi(string filename){ printf("processed %s: length %f, %i notes in %f seconds\n",filename.c_str(),((float)(iter1->first+iter1->second->duration)*.001f),notes.size(),ofGetElapsedTimef()-wt); } void musicscore::makeFlakes(int threshStart,int threshEnd){ + flakes.clear(); //decimate notes to generate flakes that can be interacted with map::iterator iter; note *lastNote=notes.begin()->second; @@ -118,7 +129,7 @@ void musicscore::makeFlakes(int threshStart,int threshEnd){ flakes[notes.begin()->first]=notes.begin()->second; for (iter = notes.begin(); iter != notes.end(); iter++) { float songPos=((float)iter->first)/songDuration; - if ((iter->second->num/5!=lastNote->num/5)||(iter->first-lastTime>((songPos*threshEnd)+((1.0f-songPos)*threshStart)))) { + if ((notemap(iter->second->num)!=notemap(lastNote->num))||(iter->first-lastTime>((songPos*threshEnd)+((1.0f-songPos)*threshStart)))) { flakes[iter->first]=iter->second; } lastNote=iter->second; @@ -158,13 +169,13 @@ void musicscore::drawFlakes(levelscore *levels) { float heightStep=((float)ofGetHeight())/timeframe; map::iterator iter; //draw flakes - for (iter = flakes.lower_bound(scoreStart-200); iter != flakes.upper_bound(scoreEnd); ++iter) { //extra 200ms for flake to leave screen + for (iter = flakes.lower_bound(scoreStart); iter != flakes.upper_bound(scoreEnd); iter++) { int thisnote=iter->second->num-firstnote; int thisstart=iter->first-scoreStart; int thislength=iter->second->duration; if (iter->second->activated) ofSetColor(255,255,255); else ofSetColor(ofColor::fromHsb(((float)thisnote*255)/numnotes,200,255)); - flake.draw(((thisnote/5)*300)+100,ofGetHeight()-(thisstart*heightStep),flake.getWidth()/2,flake.getHeight()/2); + flake.draw((notemap(iter->second->num)*300)+100,ofGetHeight()-(thisstart*heightStep),flake.getWidth()/2,flake.getHeight()/2); //todo - make all drawing resolution independent } @@ -172,7 +183,7 @@ void musicscore::drawFlakes(levelscore *levels) { //is it the number of flakes they can lose per segment? missedFlakes=0; missedLast=false; - for (iter = flakes.lower_bound(levels->getLowerBound(levels->getLevel(scoreStart))); iter != flakes.upper_bound(scoreStart); ++iter){ + for (iter = flakes.lower_bound(levels->getLowerBound(levels->getLevel(scoreStart))); iter != flakes.upper_bound(scoreStart); iter++){ if (!iter->second->activated) { missedFlakes++; } @@ -183,7 +194,7 @@ void musicscore::drawFlakes(levelscore *levels) { void musicscore::playerControl(int key,int threshold){ map::iterator iter; int scoreTime=ofGetElapsedTimeMillis()-startTime; - for (iter = flakes.lower_bound(scoreTime-threshold); iter != flakes.upper_bound(scoreTime+threshold); ++iter) { + for (iter = flakes.lower_bound(scoreTime-threshold); iter != flakes.upper_bound(scoreTime+threshold); iter++) { iter->second->activate(); } } @@ -195,7 +206,7 @@ song::song(string backfile,string melfile,string musfile,string lyricfile,string lyrics.load(lyricfile); levels.load(levelfile); isPlaying=false; - keyThresh=200; + keyThresh=400; notes.setNowpoint(0.8f); } void song::setTimeframe(int millis) {notes.setTimeframe(millis);} diff --git a/vfg/src/testApp.cpp b/vfg/src/testApp.cpp index 52f2a3a..928604d 100755 --- a/vfg/src/testApp.cpp +++ b/vfg/src/testApp.cpp @@ -6,9 +6,7 @@ void testApp::setup(){ //for (float i=0;i<1.2;i+=0.1) printf("%f in level %i bound %f\n",i,lives->getLevel(i),lives->getLowerBound(lives->getLevel(i))); testsong=new song("VODA_MUS_DeckTheHalls-Backing_v.1.5.mp3","VODA_MUS_DeckTheHalls-Melody_v.1.5.mp3","MIDI_DeckTheHalls_MIDI.1.5.xml","Lyrics_DeckTheHalls.1.5.xml","Levels_DeckTheHalls.1.5.xml"); - testsong->setTimeframe(2500); - testsong->setFlakeThresh(1000,100); - testsong->preRoll(250); + ofSetBackgroundAuto(false); ofBackground(0,0,0); @@ -28,7 +26,7 @@ void testApp::setup(){ penguins[2].setFrameRate(50); penguins[2].setLoop(false); - printf("blue clap: %i\n",penguins[0].getTotalFrames()); + //printf(ofSystem("ls -la").c_str()); } void testApp::exit(){ @@ -37,12 +35,19 @@ void testApp::exit(){ //-------------------------------------------------------------- void testApp::update(){ - - for (int i=0;i<3;i++) { - penguins[i].update(); - } - - + int ret=game.update(); + if (ret==1000) { + testsong->setTimeframe(2500); + testsong->setFlakeThresh(1000,100); + testsong->preRoll(250); + } + else if (ret>0) { + testsong->playerControl(ret); + penguins[ret-1].play(); + } + for (int i=0;i<3;i++) { + penguins[i].update(); + } } //-------------------------------------------------------------- @@ -51,14 +56,18 @@ void testApp::draw(){ //ofBackground(0,0,0,0.1); ofSetColor(0,0,0,100); ofRect(0,0,ofGetWidth(),ofGetHeight()); - ofSetColor(255,255,255); - testsong->drawNotes(); + + if (testsong->isPlaying) { + ofSetColor(255,255,255); + testsong->drawNotes(); + } ofSetColor(255,255,255); for (int i=0;i<3;i++) penguins[i].draw((i*300)+100,800); - testsong->draw(); - if (!testsong->isPlaying) ofDrawBitmapString("game over!", (ofGetWidth()/2)-25,(ofGetHeight()/2)-5); - - + if (testsong->isPlaying) { + ofSetColor(255,255,255); + testsong->draw(); + } + else ofDrawBitmapString("game over!", (ofGetWidth()/2)-25,(ofGetHeight()/2)-5); } @@ -74,6 +83,9 @@ void testApp::keyPressed(int key){ testsong->playerControl(key-'0'); penguins[key-'1'].play(); break; + case 's': + game.startGame(); + break; } } diff --git a/vfg/src/testApp.h b/vfg/src/testApp.h index 8304e12..8f52ef7 100755 --- a/vfg/src/testApp.h +++ b/vfg/src/testApp.h @@ -3,6 +3,7 @@ #include "ofMain.h" #include "ofxSprite.h" #include "music.h" +#include "Asterisk.h" /* this library? @@ -42,11 +43,12 @@ overlap character class- load sprites from xml- play behaviours- block animations when others are playing -*/ +//hooking up the backend +1 - start game - sends message - starts if acknowledged +2 - receive dtmf +3 - hang up -class game { - vector songs; -}; +*/ @@ -71,6 +73,8 @@ class testApp : public ofBaseApp{ song *testsong; vector penguins; + + Asterisk game; }; -- cgit v1.2.3