Search code examples
c++command-line-interfacemidivst

Using Midi Library To Parse Events And Store In Vector C++


I'm a PHP programmer whose decided to take the plunge into C++ by developing a simple alternative to MissWatson that would allow me to, via the command line with PHP, process a MIDI file through a VST.

I started with the Steinberg VST SDK and I've been using this MIDI library: https://ccrma.stanford.edu/software/stk/index.html.

I'm stuck on vectors, specifically the vectors that would store the MIDI events into. Here's the last bit of code to clean up (keep in mind I'm a total noob to C++ and am probably doing most of this wrong):

std::string midiPath = "C:\\creative\\midi\\m1.mid";

if (argc > 1) {
    midiPath = argv[1];
}

//MidiFileIn::MidiFileIn(midiPath);
stk::MidiFileIn::MidiFileIn(midiPath);

//std::vector<_Ty> * midiEvents;
std::vector<_Ty> midiEvents(200);

stk::MidiFileIn::getNextEvent(midiEvents, 0);
//char* midiEvents[200];
//VstEvents* midiEvents;
//processMidi(effect, VstEvents*);

const char* wavPath = argv[2];
//processAudio(effect, 0, x, x);

and here are the errors:

1>c:\users\andrew\downloads\vst_sdk2_4_rev2\vstsdk2.4\public.sdk\samples\vst2.x\minihost\source\score.cpp(370): error C2065: '_Ty' : undeclared identifier
1>c:\users\andrew\downloads\vst_sdk2_4_rev2\vstsdk2.4\public.sdk\samples\vst2.x\minihost\source\score.cpp(370): error C2514: 'std::vector' : class has no constructors
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(480) : see declaration of 'std::vector'
1>c:\users\andrew\downloads\vst_sdk2_4_rev2\vstsdk2.4\public.sdk\samples\vst2.x\minihost\source\score.cpp(372): error C2664: 'stk::MidiFileIn::getNextEvent' : cannot convert parameter 1 from 'std::vector' to 'std::vector<_Ty> *'
1>          with
1>          [
1>              _Ty=unsigned char
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>

So, how do I use the _Ty constructor? Am I on the right track or just crazy?


Solution

  • @the_mandrill is correct, but I just wanted to note that you should be using a VstEvent* type, not VstEvents. A VstEvents structure contains a list of VstEvent objects, and you probably want to break them down into the vector. So some pseudocode for you:

    // Initialization
    std::vector<VstEvent *> midiEvents();
    
    // Somewhere inside of stk::MidiFileIn::getNextEvent()
    while(youReadTheMidiEvents) {
      VstEvents *inEvents = yourReadMidiEventsFunction();
      for(int i = 0; i < inEvents->numEvents; i++) {
        midiEvents.push_back(inEvents->events[i]);
      }
    }
    
    // Somewhere much later in your destructor
    for(int i = 0; i < midiEvents.size(); i++) {
      free(midiEvents.at(i));
    }
    midiEvents.clear();
    

    I have no idea how you are actually reading MIDI events from file (hence the your* stuff), but the above code is simply assuming that you are getting back a VstEvents array somehow. So consider it just a terse overview of how you're going to have to store pointers in your vector.