Beispiel #1
0
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;
    }

}
Beispiel #2
0
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;
}