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
296
297
298
299
|
#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<int,lyric*>::iterator iter;
iter=lyrics.upper_bound(scoreTime);
if (iter!=lyrics.begin()) {
iter--;
if ((iter->first+iter->second->duration)>scoreTime) { //outpoint of lyric previous to the one next soonest is afterwards => this lyric is visible
int alpha=((iter->first+iter->second->duration)-scoreTime)<fadeout?(int)((((float)((iter->first+iter->second->duration)-scoreTime))/((float)fadeout))*255.0f):255;
ofSetColor(255,255,255,alpha);
font.drawString(iter->second->text,(ofGetWidth()/2)-(font.stringWidth(iter->second->text)/2.0f), ypos);
}
}
}
//----------------------------------------------------------------------------------------------------------
musicscore::musicscore() {
timeframe=2000;
missedLast=false;
nowpoint=1.0f;
missedNote=-1;
snowflakes.push_back(Puppet());
snowflakes.push_back(Puppet());
snowflakes.push_back(Puppet());
snowflakes[0].load("Snowflake-Blue.xml");
snowflakes[1].load("Snowflake-Purple.xml");
snowflakes[2].load("Snowflake-Green.xml");
}
void musicscore::parseMidi(string filename){
// millis = 60000 / (BPM * PPQ)
// BPM = 60000000 / MQPN (last 3 bytes of midi tempoSet)
// http://www.lastrayofhope.com/2009/12/23/midi-delta-time-ticks-to-seconds/
// presume no change in time signature?
//2 passes:: extract notes & set abs times, then scan for
float wt=ofGetElapsedTimef();
float BPM=120.0f;
//input:: MPQN :: default 500000
float MPQN=60000000.0f/BPM;
//unknown:: ticks per quarter note
int TPQN=480;
//want:: seconds per tick in float
float SPT =(MPQN/1000000.0f)/TPQN;
float time=0; //counts up in float seconds to avoid rounding errors but converts to millis for map index
map<int,note*> events;
if( !XML.loadFile(filename) ){
printf("unable to load %s check data/ folder\n",filename.c_str());
}else{
if(XML.pushTag("MidiFile")) {
for (int i=0;i<XML.getNumTags("TrackChunk");i++) {
XML.pushTag("TrackChunk",i);
for (int i=0;i<XML.getNumTags("Event");i++) {
time+=(SPT*XML.getAttribute("Event", "DeltaTimeTicks",0,i));
if (XML.getAttribute("Event", "Label","",i)=="TempoSet") {
string data=XML.getAttribute("Event", "Data","",i);
char* endptr;
int d1=strtoul(data.substr(6,2).c_str(),&endptr,16);
int d2=strtoul(data.substr(9,2).c_str(),&endptr,16);
int d3=strtoul(data.substr(12,2).c_str(),&endptr,16);
int MPQN=(d1<<16)+(d2<<8)+d3;
SPT =(MPQN/1000000.0f)/TPQN;
//printf("Tempo change: seconds per tick now: %f\n",SPT);
}
if (XML.getAttribute("Event", "Label","",i)=="NoteOn"||XML.getAttribute("Event", "Label","",i)=="NoteOff") {
string data=XML.getAttribute("Event", "Data","",i);
char* endptr;
int d1=strtoul(data.substr(0,2).c_str(),&endptr,16);
int d2=strtoul(data.substr(3,2).c_str(),&endptr,16);
int id=strtoul(XML.getAttribute("Event", "Id","",i).c_str(),&endptr,16);
if (id==128||id==144) events[(int)(time*1000.0f)]=new note(d1,d2,id); //noteon/off
}
}
XML.popTag();
}
}
XML.popTag();
}
//iterate events and compute durations now the absolute times are established: extract to notes
map<int,note*>::iterator iter1;
map<int,note*>::iterator iter2;
int n;
bool started=false;
for (iter1 = events.begin(); iter1 != events.end(); ++iter1) {
if (iter1->second->duration==144) {
if (!started) {
n=iter1->second->num;
started=true;
}
iter1->second->duration=0;
iter2=iter1;
while (++iter2 != events.end()) {
if ((iter1->second->num==iter2->second->num)&&(iter2->second->duration==128)) {
iter1->second->duration=iter2->first-iter1->first;
n=iter1->second->num;
notes[iter1->first]=iter1->second;
//printf("%i: noteon %i %i\n",iter1->first,iter1->second->num,iter1->second->duration);
break;
}
}
}
}
iter1 = notes.end();
iter1--;
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<int,note*>::iterator iter;
note *lastNote=notes.begin()->second;
int lastTime=0;
iter = notes.end();
iter--;
float songDuration=iter->first;
flakes[notes.begin()->first]=new flake(notes.begin()->second->num,notes.begin()->second->velocity,notes.begin()->second->duration);
flakes[notes.begin()->first]->puppet=snowflakes[notemap(lastNote->num)];
for (iter = notes.begin(); iter != notes.end(); iter++) {
float songPos=((float)iter->first)/songDuration;
if ((notemap(iter->second->num)!=notemap(lastNote->num))||(iter->first-lastTime>((songPos*threshEnd)+((1.0f-songPos)*threshStart)))) {
flakes[iter->first]=new flake(iter->second->num,iter->second->velocity,iter->second->duration);
flakes[iter->first]->puppet=snowflakes[notemap(iter->second->num)];
}
lastNote=iter->second;
lastTime=iter->first;
}
missedFlake=flakes.end();
missedNote=-1;
}
void musicscore::setTimeframe(int millis) {timeframe=millis;}
void musicscore::setNowpoint(float pct) {nowpoint=pct;}
void musicscore::drawNotes(levelscore *levels) {
int scoreStart=ofGetElapsedTimeMillis()-startTime-((1.0f-nowpoint)*timeframe);
int scoreEnd=scoreStart+timeframe;
//note drawing 46h - 52h
int numnotes=16;
int firstnote=70;
float widthStep=((float)ofGetWidth())/numnotes;
float heightStep=((float)ofGetHeight())/timeframe;
map<int,note*>::iterator iter;
//draw notes for reference
for (iter = notes.lower_bound(scoreStart); iter != notes.upper_bound(scoreEnd); ++iter) {
int thisnote=iter->second->num-firstnote;
int thisstart=iter->first-scoreStart;
int thislength=iter->second->duration;
ofSetColor(ofColor::fromHsb(((float)thisnote*255)/numnotes,200,100));
ofRect(thisnote*widthStep,ofGetHeight()-(thisstart*heightStep),widthStep,-(thislength*heightStep));
}
}
void musicscore::drawFlakes(levelscore *levels) {
ofEnableAlphaBlending();
int scoreStart=ofGetElapsedTimeMillis()-startTime-((1.0f-nowpoint)*timeframe);
int scoreEnd=scoreStart+timeframe;
//note drawing 46h - 52h
int numnotes=16;
int firstnote=70;
float widthStep=((float)ofGetWidth())/numnotes;
float heightStep=((float)ofGetHeight())/timeframe;
map<int,flake*>::iterator iter;
//draw flakes
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));
//if (iter->second->activated&&(!iter->second->disintegrated)) iter->second->disintegrate();
ofSetColor(255,255,255);
iter->second->draw((notemap(iter->second->num)*300)+100,ofGetHeight()-(thisstart*heightStep));
//todo - make all drawing resolution independent
}
//check for unactivated flakes within this segment: is there a more efficient way?
//need to know when a flake has just been missed
missedFlakes=0;
missedLast=false;
map<int,flake*>::iterator missed=flakes.end();
int scoreTime=ofGetElapsedTimeMillis()-startTime;
for (iter = flakes.lower_bound(levels->getLowerBound(levels->getLevel(scoreStart))); iter != flakes.upper_bound(scoreStart); iter++){
if (!iter->second->activated) {
missedFlakes++;
missed=iter;
}
missedLast=!iter->second->activated;
}
if ((missed!=flakes.end())&&(missedFlake!=missed)) {
missedFlake=missed;
missedNote=notemap(iter->second->num);
}
else missedNote=-1;
ofDisableAlphaBlending();
}
void musicscore::playerControl(int key,int threshold){
map<int,flake*>::iterator iter;
int scoreTime=ofGetElapsedTimeMillis()-startTime;
for (iter = flakes.lower_bound(scoreTime-threshold); iter != flakes.upper_bound(scoreTime+threshold); iter++) {
if (key==notemap(iter->second->num)) iter->second->activate();
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------
song::song(string backfile,string melfile,string musfile,string lyricfile,string levelfile) {
backing.loadSound(backfile);
melody.loadSound(melfile);
notes.parseMidi(musfile);
lyrics.load(lyricfile);
levels.load(levelfile);
isPlaying=false;
keyThresh=400;
notes.setNowpoint(0.8f);
}
void song::setTimeframe(int millis) {notes.setTimeframe(millis);}
void song::setKeythresh(int millis) {keyThresh=millis;}
void song::setFlakeThresh(int tS,int tE) {
fThreshStart=tS;
fThreshEnd=tE;
}
void song::play() {
backing.play();
melody.play();
startTime=ofGetElapsedTimeMillis();
notes.start();
lyrics.start();
isPlaying=true;
notes.makeFlakes(fThreshStart,fThreshEnd);
}
void song::stop() {
backing.stop();
melody.stop();
isPlaying=false;
}
void song::preRoll(long preroll) {
startTime=ofGetElapsedTimeMillis()+preroll;
notes.start(startTime);
lyrics.start(startTime);
isPreroll=true;
isPlaying=true;
notes.makeFlakes(fThreshStart,fThreshEnd);
}
void song::drawNotes(){
notes.drawNotes(&levels);
}
void song::draw(){
int songTime=ofGetElapsedTimeMillis()-startTime;
if (isPlaying) {
if (isPreroll) {
if (startTime<ofGetElapsedTimeMillis()) {
backing.play();
melody.play();
isPreroll=false;
}
}
if (notes.missedLast) {
melody.setVolume(0.0f);
if (levels.getLives(songTime)) {
if (notes.missedFlakes>levels.getLives(songTime)) {
//work out score
stop();
}
}
}
else melody.setVolume(1.0f);
notes.drawFlakes(&levels);
lyrics.draw();
}
ofDrawBitmapString(ofToString((float)songTime/1000.0f,1)+" "+ofToString(levels.getLevel(songTime))+" "+ofToString(notes.missedFlakes)+" of "+ofToString(levels.getLives(songTime)),10,ofGetHeight()-15);
}
void song::playerControl(int key){
notes.playerControl(key,keyThresh);
}
int song::missedNote(){
return notes.missedNote;
}
|