1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
#include "ofMain.h"
#include "ofxCsv.h"
#include "vectortext.h"
/*
[notice ] 1b Briseadh Bualadh Bos.wav, duration 1.709s, 3, width: 8003
[notice ] 1b Briseadh Bualadh Bos.wav, duration 1.709s, 3, width: 9137
[notice ] 1b Briseadh Bualadh Bos.wav, duration 1.709s, 3, width: 9137
the first time we load a scriptline the width comes out too short. why>>???
//this was caused becauase enspace=font.enspace was at the end of the initialiser
//how did this make it work out the 2nd time?
*/
class ScriptLine{
public:
ScriptLine(SVGFont &font,string audiofile,string cols,string message){
enspace=font.enspace;
vector<string> colours=ofSplitString(cols,",");
for (auto c: colours){
palette.push_back(ofColor::fromHex(ofHexToInt(c)));
}
clear();
vector<string> m=ofSplitString(message," ");
width=0;
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;
}
auto g=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));
w.glyphs.push_back(g);
w.width+=g.width;
pos++;
}
words.push_back(w);
width+=(w.width+enspace);
}
if (audio.load(audiofile)){
name=audiofile;
//requires https://github.com/arturoc/openFrameworks/tree/feature-soundPlayerDuration
duration = ((float)audio.getDurationMS())/1000.0f;
avgWordDuration = duration / words.size();
ofLog()<<audiofile<<", duration "<<duration<<"s, width: "<<width;
}
else duration=0.0f;
}
void clear(){
words.clear();
}
vector<colourPolyline>& getOutlines(float time){
float wordphase=time/duration;
float phase=time/avgWordDuration;
float phaseseg=fmod(phase,1.0f);
//ofLog()<<"phase "<<phase<<" phaseseg "<<phaseseg;
/*
int word1=(int)(phase+0.5f);
//segment 1 plays word 0 out for the first half
//then word 1 solid
int word2=(int)(phase+1.5f);
//segment 2 plays word 1 solid for the first half
//then word 2 in
//deal with the ends?
//extra padding words, 2 per end
float word1amt=0.5-phaseseg;
if (word1amt<0.0f) word1amt=1.0f;
float word2mat=phaseseg-0.5f;
if (word2amt<0.0f) word2amt=1.0f;
*/
//segment 1 plays word 0 solid for the first half
//then word 0 out
//at the very beginning, there's a buffer
int word1=(int)(phase)-1;
//segment 2 plays word 1 in for the first half
//then word 1 solid
int word2=word1+1;
float word1amt=2.0-(phaseseg*2.0);
if (word1amt>1.0f) word1amt=1.0f;
float word2amt=phaseseg*2.0;
if (word2amt>1.0f) word2amt=1.0f;
outlines.clear();
float s=0.1f;
float p=((ofGetWidth()/4)*s)-(width*wordphase)-((ofGetWidth()/2)*s*wordphase);
float start=p;
if (word1>-1&&word1<words.size()){
for (int i=0;i<word1;i++){
p+=words[i].width;
p+=enspace;
}
for (auto& g:words[word1].glyphs){
for (auto& o:g.outline){
auto q=o;
q.scale(s,-s);
q.translate(glm::vec3(p*s,0,0));
outlines.push_back(colourPolyline(q,g.colour*word1amt));
}
p+=g.width;
}
p+=enspace;
}
if (word2<words.size()){
for (auto& g:words[word2].glyphs){
for (auto& o:g.outline){
auto q=o;
q.scale(s,-s);
q.translate(glm::vec3(p*s,0,0));
outlines.push_back(colourPolyline(q,g.colour*word2amt));
}
p+=g.width;
}
}
if ((wordphase-1.0f)>0.0f&&!printed){
ofLog()<<"added: "<<p-start<<" width: "<<width;
printed=true;
}
return outlines;
}
string name;
vector<ofColor> palette;
vector<glyphWord> words;
ofSoundPlayer audio;
float duration;
float avgWordDuration;
vector<colourPolyline> outlines;
float enspace;
bool printed;
private:
float width;
};
class Show{
public:
bool loadFont(filesystem::path fontpath){
return font.load(fontpath);
}
Show(){
bisPlaying=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();
ofLog()<<"Selected "<<playline->name;
return true;
}
playline=script.end();
return false;
}
void play(){
if (playline==script.end()){
if (script.size()){
playline=script.begin();
ofLog()<<"Restarted!";
}
else return;
}
if (playline->audio.isLoaded()){
playline->audio.play();
playline->printed=false;
startTime=ofGetElapsedTimef();
bisPlaying=true;
ofLog()<<"Line playing "<<playline->duration;
}
}
void stop(){
if (isPlaying()){
playline->audio.stop();
bisPlaying=false;
ofLog()<<"Line stopped";
}
}
void previous(){
stop();
if (script.size()){
if (playline!=script.begin()){
playline--;
ofLog()<<"previous line selected, "<<playline->name;
}
}
}
void next(){
stop();
if (script.size()){
if (playline!=script.end()){
playline++;
ofLog()<<"next line selected, "<<playline->name;
}
}
}
void update(){
if (isPlaying()){
if (ofGetElapsedTimef()-startTime>
(playline->avgWordDuration*
playline->words.size()+1)){
playline->audio.stop();
bisPlaying=false;
playline++;
ofLog()<<"Line ended, selected "<<playline->name;
if (playline==script.end()){
ofLog()<<"Show finished!";
}
}
}
}
vector<colourPolyline>& getOutlines(){
return playline->getOutlines(ofGetElapsedTimef()-startTime);
}
ofxCsv csv;
vector<ScriptLine> script;
vector<ScriptLine>::iterator playline;
float startTime;
bool bisPlaying;
bool isPlaying() {
return bisPlaying;
}
SVGFont font;
};
|