Randy Jones
Randy Jones

Reputation: 1

How to make MusicDeviceMIDIEvent in MusicDevice.h respond to note durations?

I'm trying to make a melody using the following code. The problems is trying to make the sequence play the rhythmic values in the rhythmArray. I basically want to play the note, and the velocity for a specified time then turn off and play the next notes in the sequence.

Any help would be greatly appreciated.

int playMidi()
{
 //pitch array with constants defined for pitch numbers: ex. C4 = 60
 int pitchArray[11] {C4, A5, E4, F3, D4, FS5, BF4, CS3, FS5, C3, DF4}; /

//velocity array with constants defined for velocity: ex. QN = 1, EN = 0.5
 int velocityArray[11] {FORTE, PIANISSIMO, MEZZO_FORTE, PIANISSIMO, FORTISSIMO,
 MEZZO_PIANO, PIANISSIMO, FORTE, MEZZO_PIANO,FORTE, FORTISSIMO};

//rhythm array with constants defined for rhythm values: ex.  = 110
double rhythmArray[11] {EN, SN, SN, EN, QN, EN, EN, EN, TSN,TSN, SN};


for(int i=0; i<11; i++)
{

    UInt32 noteOnCommand =  kMidiMessage_NoteOn << 4 | midiChannelInUse;


        std::cout<< "The current pitch is: " 
                 <<pitchArray[i]  << "The velocity is: " <<velocityArray[i]
                 << " and the rhythmic value is: "<<rhythmArray[i]<<  "\n";

        MusicDeviceMIDIEvent(synthUnit, 
                             noteOnCommand, 
                             pitchArray[i], 
                             velocityArray[i], 
                             0);

        // sleep for a second
        sleep(1);





}
 return 0;
}

Upvotes: 0

Views: 1530

Answers (2)

John
John

Reputation: 1

I believe the last argument in MusicDeviceMIDIEvent is the time offset in samples. So if your sample rate is 44.1k, then you have 44,100 samples per second. This would mean that to play two consecutive notes one second apart you would call this method 4 times. First with time: 0, then note off, then with time: 44,100, then note off.

Upvotes: 0

spring
spring

Reputation: 18487

If you are simply wanting to play midi notes in sequence, you would likely make more progress using the built in MusicPlayer than trying to create similar functionality from scratch. It is a very flexible framework enabling a developer to create multiple MusicTracks and MusicSequences on the fly, alter track and note properties and even save out a .mid file. The endpoint can be either a midi device or an AUGraph using soundfonts.

Upvotes: 3

Related Questions