Пример #1
0
//-----------------------------------------------------------------------------
// 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);
                    }
                }
            }

            
        }
    }
}
Пример #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;
}