diff options
Diffstat (limited to 'futuregael/src/show.h')
| -rw-r--r-- | futuregael/src/show.h | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/futuregael/src/show.h b/futuregael/src/show.h new file mode 100644 index 0000000..7261d8d --- /dev/null +++ b/futuregael/src/show.h @@ -0,0 +1,143 @@ +#include "ofMain.h" +#include "ofxCsv.h" +#include "vectortext.h" + +class ScriptLine{ +public: + ScriptLine(SVGFont &font,string audiofile,string cols,string message){ + + vector<string> colours=ofSplitString(cols,","); + for (auto c: colours){ + palette.push_back(ofColor::fromHex(ofHexToInt(c))); + } + + clear(); + vector<string> m=ofSplitString(message," "); + for (auto& word: m){ + glyphWord w; + int pos=0; + for (auto& c: word){ + string uniglyph = ofUTF8Substring(word, pos, 1); + if (c<0){ + //ofLog()<<"got unicode glyph, "<<uniglyph; + } + w.glyphs.push_back(font.getGlyph(uniglyph, + palette.size()? + palette[ofRandom(palette.size())]: + //112->231 hue sat 0->255 brightness 255 + ofColor::fromHsb(ofRandom(119)+112,ofRandom(255),255) + )); + pos++; + } + words.push_back(w); + } + + if (audio.load(audiofile)){ + //requires https://github.com/arturoc/openFrameworks/tree/feature-soundPlayerDuration + duration = ((float)audio.getDurationMS())/1000.0f; + + avgWordDuration = duration / words.size(); + + ofLog()<<"Line "<<audiofile<<", duration "<<duration<<"s, "<<words.size()<<" words avg "<<avgWordDuration<<"s"; + } + else duration=0.0f; + + } + void clear(){ + words.clear(); + } + + vector<ofColor> palette; + vector<glyphWord> words; + ofSoundPlayer audio; + float duration; + float avgWordDuration; + +}; + + +class Show{ +public: + bool loadFont(filesystem::path fontpath){ + return font.load(fontpath); + } + Show(){ + isPlaying=false; + } + Show(filesystem::path fontpath){ + font.load(fontpath); + Show(); + } + bool load(string file){ + + if (!font.isLoaded()){ + ofLogError()<<"cannot load show without a font"; + return false; + } + + // Load a CSV File. + if(csv.load(file,"|")) { + + for (auto row:csv){ + if (row.size()<2){ + ofLog()<<"Error, found row with "<<row.size()<<" elements"; + } + else script.push_back(ScriptLine(font,row[0],row[1],row[2])); + } + + ofLog()<<"Loaded show, "<<csv.getNumRows()<<" lines"; + + playline=script.begin(); + + return true; + } + + playline=script.end(); + + return false; + } + + void play(){ + if (playline==script.end()) return; + + if (playline->audio.isLoaded()){ + playline->audio.play(); + startTime=ofGetElapsedTimef(); + isPlaying=true; + ofLog()<<"Line playing "<<playline->duration; + } + } + + void stop(){ + if (isPlaying){ + playline->audio.stop(); + isPlaying=false; + ofLog()<<"Line stopped"; + } + } + + void update(){ + if (isPlaying){ + if (ofGetElapsedTimef()-startTime>playline->duration){ + playline->audio.stop(); + isPlaying=false; + ofLog()<<"Line finished! "<<playline->duration;; + playline++; + if (playline==script.end()){ + ofLog()<<"Show finished!"; + } + } + } + } + + ofxCsv csv; + + vector<ScriptLine> script; + + vector<ScriptLine>::iterator playline; + + float startTime; + bool isPlaying; + + SVGFont font; +};
\ No newline at end of file |
