void drawSquares(GLenum mode) { glPushName(0); if (mode == GL_SELECT) glLoadName(1); drawFretboard(); string you="Your Turn",comp="Computer's Turn"; if (mode == GL_SELECT) glLoadName(2); drawTimeline(); if (mode == GL_SELECT) glLoadName(3); drawButton(5,-62,"metronome",8,g_metronome_on); if (mode == GL_SELECT) glLoadName(4); drawButton(85,-62,"sync",8,1); if (turn_cpu) { draw_string(25,-20,0,comp,70,2.5); for (int i=0;i<g_playComputer.size();i++) { if ( g_playComputer.at(i).startTime()<g_t && g_playComputer.at(i).endTime()>g_t) playNote(g_playComputer.at(i).pitch(),1); else playNote(g_playComputer.at(i).pitch(),0); } } else { glColor4f(1,0,0,1); draw_string(38,-20,0,you,70,2.5); } drawParameter(95,-39,0,g_tempo,"tempo",mode,5); drawParameter(-10,-39,0,g_prob_rhythm,"rhythm variation",mode,7); drawParameter(25,-39,0,g_prob_melody,"melody variation",mode,9); drawParameter(65,-39,0,g_tradebars,"bars",mode,11); glPopName(); }
//----------------------------------------------------------------------------- // name: midi_callback() // desc: callback that is called every time a new MIDI message is received //----------------------------------------------------------------------------- void midi_callback( double deltatime, std::vector< unsigned char > *message, void *userData ) { unsigned int nBytes = message->size(); int midimessage=(int)message->at(0),pitch,velocity; if (nBytes > 0 ) { if (midimessage==144) { pitch = (int)message->at(1); velocity = (int)message->at(2); cout<<pitch<<" "<<velocity<<endl; fluid_synth_noteon( g_synth, 1, pitch,velocity ); if (turn_cpu == 0) { if ( velocity==0 ) { for (int i=0;i<g_current.size();i++) { if (g_current[i].pitch() == pitch) { g_current[i].setEndTime(g_t); g_playHuman.add(g_current[i]); g_current.erase(g_current.begin()+i); } } } else { MidiEvent midz(pitch,velocity,g_quarter,g_t); g_current.push_back(midz); } } } else if (midimessage == 128) { pitch = (int)message->at(1); fluid_synth_noteoff( g_synth, 1, pitch); if (turn_cpu == 0) { for (int i=0;i<g_current.size();i++) { if (g_current[i].pitch() == pitch) { g_current[i].setEndTime(g_t); g_playHuman.add(g_current[i]); g_current.erase(g_current.begin()+i); } } } } } }
void respondToClick(int name) { //metronome if (name==3) g_metronome_on = -1*g_metronome_on; //sync else if (name==4) { g_t=0; g_timeline_x=0; g_beat_played=0; g_playHuman.clear(); g_playComputer.clear(); } //rhythm else if (name==7) { g_prob_rhythm -= 0.1; if (g_prob_rhythm<0.1) g_prob_rhythm=0; } else if (name==8) { g_prob_rhythm += 0.1; if (g_prob_rhythm>1.01) g_prob_rhythm=1; } //melody else if (name==9) { g_prob_melody -= 0.1; if (g_prob_melody<0.1) g_prob_melody=0; } else if (name==10) { g_prob_melody += 0.1; if (g_prob_melody>1.01) g_prob_melody=1; } //bars else if (name==11) { g_tradebars--; if (g_tradebars<1) g_tradebars=1; g_trade=(float)g_tradebars*g_bar; g_quantization_x=(float)(g_fretboard_w+2)/(4*g_tradebars); } else if (name==12) { g_tradebars++; if (g_tradebars>16) g_tradebars=16; g_trade=g_tradebars*g_bar; g_quantization_x=(float)(g_fretboard_w+2)/(4*g_tradebars); } //tempo else if (name==5) { g_tempo-=5; if (g_tempo<40) g_tempo=40; g_beat = MY_SRATE*60/g_tempo; g_bar = 4*g_beat; g_trade=g_tradebars*g_bar; g_sixteenth=g_bar/16; g_eighth=g_bar/8; g_quarter=g_bar/4; g_half=g_bar/2; } else if (name==6) { g_tempo+=5; if (g_tempo>200) g_tempo=200; g_beat = MY_SRATE*60/g_tempo; g_bar = 4*g_beat; g_trade=g_tradebars*g_bar; g_sixteenth=g_bar/16; g_eighth=g_bar/8; g_quarter=g_bar/4; g_half=g_bar/2; } }
int audioCallback( void * outputBuffer, void * inputBuffer, unsigned int bufferSize, double streamTime, RtAudioStreamStatus status, void * userData ) { SAMPLE * out = (SAMPLE *)outputBuffer; SAMPLE acc[bufferSize*2]; fluid_synth_write_float( g_synth, bufferSize, out, 0, 2, out, 1, 2 ); for (int i=0;i<bufferSize*2;i++) acc[i]=out[i]; fluid_synth_write_float( g_metronome, bufferSize, out, 0, 2, out, 1, 2 ); for (int i=0;i<bufferSize*2;i++) out[i]+=acc[i]; //turnaround - every n bars if (g_t >= g_trade) { g_timeline_x=0; turn_cpu=!(turn_cpu); //cout<<"swap"<<" "<<turn_cpu<<endl; //if the cpu plays - process once if (turn_cpu==1) { for (int i=0;i<g_current.size();i++) { g_current[i].setEndTime(g_t-1); g_playHuman.add(g_current[i]); g_current.erase(g_current.begin()+i); } g_current.clear(); //g_playHuman.printQ(); g_playComputer = g_playHuman; g_playHuman.clear(); //g_playComputer.printQ(); g_playComputer.randomize(g_prob_melody,g_prob_rhythm,g_tradebars,g_trade); //g_playComputer.printQ(); g_drumnote=50; g_played.clear(); g_played.resize(g_playComputer.size(),0); g_stopped.clear(); g_stopped.resize(g_playComputer.size(),0); } else { g_drumnote = 51; for (int i=40;i<110;i++) fluid_synth_noteoff( g_synth, 1, i); } g_t=0; g_beat_played=0; } //if cpu, then please play if (turn_cpu && g_playComputer.size()>0 ) { // cout<<g_played.size(); for (int i=0;i<g_playComputer.size();i++) { if ( g_playComputer.at(i).startTime()<=g_t && g_played[i]==0 ) { fluid_synth_noteon( g_synth, 1, g_playComputer.at(i).pitch(),g_playComputer.at(i).velocity() ); g_played[i]=1; } if ( (g_playComputer.at(i).startTime()+g_playComputer.at(i).duration() <= g_t) && g_stopped[i]==0) { fluid_synth_noteon( g_synth, 1, g_playComputer.at(i).pitch(),0); g_stopped[i]=1; } } } if (g_t==0 && g_metronome_on == 1) fluid_synth_noteon( g_metronome, 1, g_drumnote,90 ); if (g_t/g_beat > g_beat_played && g_metronome_on == 1 ) { fluid_synth_noteon( g_metronome, 1, g_drumnote,90 ); g_beat_played++; } g_t += bufferSize; return 0; }