命令行接口-使用Midi库分析事件并存储在矢量C++中

command line interface - Using Midi Library To Parse Events And Store In Vector C++

本文关键字:存储 C++ 事件 使用 Midi 命令行接口      更新时间:2023-10-16

我是一名PHP程序员,他决定通过开发一个简单的MissWatson替代方案来深入C++,该替代方案允许我通过PHP的命令行通过VST处理MIDI文件。

我从Steinberg VST SDK开始,一直在使用这个MIDI库:https://ccrma.stanford.edu/software/stk/index.html.

我被矢量卡住了,特别是那些将MIDI事件存储到其中的矢量。这是最后一点需要清理的代码(请记住,我对C++完全不了解,可能大部分都做错了):

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);

以下是错误:

1>c:usersandrewdownloadsvst_sdk2_4_rev2vstsdk2.4public.sdksamplesvst2.xminihostsourcescore.cpp(370): error C2065: '_Ty' : undeclared identifier
1>c:usersandrewdownloadsvst_sdk2_4_rev2vstsdk2.4public.sdksamplesvst2.xminihostsourcescore.cpp(370): error C2514: 'std::vector' : class has no constructors
1>          c:program files (x86)microsoft visual studio 10.0vcincludevector(480) : see declaration of 'std::vector'
1>c:usersandrewdownloadsvst_sdk2_4_rev2vstsdk2.4public.sdksamplesvst2.xminihostsourcescore.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>

那么,我该如何使用_Ty构造函数呢?我是走在正确的轨道上,还是只是疯了?

_Ty只是模板参数的占位符。您有一个std::vector<_Ty>,它是一个模板化的类,但您需要定义要用于它的类。在这种情况下,它将是您的MIDI事件类的任何一个-可能是VstEvents

@the_mandrill是正确的,但我只是想注意,您应该使用VstEvent*类型,而不是VstEventsVstEvents结构包含VstEvent对象的列表,您可能希望将它们分解为向量。所以给你一些伪代码:

// 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();

我不知道您实际上是如何从文件中读取MIDI事件的(因此是your*的东西),但上面的代码只是假设您正在以某种方式返回VstEvents数组。所以,把它看作是一个关于如何在向量中存储指针的简要概述。