Browse Source

Add MIDI channel to MIDI Interface

tags/v0.3.0
Andrew Belt 7 years ago
parent
commit
3fcbc62f79
3 changed files with 101 additions and 52 deletions
  1. +3
    -10
      src/app/Toolbar.cpp
  2. +9
    -17
      src/core/AudioInterface.cpp
  3. +89
    -25
      src/core/MidiInterface.cpp

+ 3
- 10
src/app/Toolbar.cpp View File

@@ -37,10 +37,10 @@ struct OpenItem : MenuItem {

struct FileChoice : ChoiceButton {
void onAction() {
MenuOverlay *overlay = new MenuOverlay();
Menu *menu = new Menu();
Menu *menu = gScene->createMenu();
menu->box.pos = getAbsolutePos().plus(Vec(0, box.size.y));
menu->box.size.x = box.size.x;

{
MenuItem *newItem = new NewItem();
newItem->text = "New";
@@ -58,8 +58,6 @@ struct FileChoice : ChoiceButton {
saveAsItem->text = "Save As";
menu->pushChild(saveAsItem);
}
overlay->addChild(menu);
gScene->setOverlay(overlay);
}
};

@@ -73,8 +71,7 @@ struct SampleRateItem : MenuItem {

struct SampleRateChoice : ChoiceButton {
void onAction() {
MenuOverlay *overlay = new MenuOverlay();
Menu *menu = new Menu();
Menu *menu = gScene->createMenu();
menu->box.pos = getAbsolutePos().plus(Vec(0, box.size.y));
menu->box.size.x = box.size.x;

@@ -86,9 +83,6 @@ struct SampleRateChoice : ChoiceButton {
item->sampleRate = sampleRates[i];
menu->pushChild(item);
}

overlay->addChild(menu);
gScene->setOverlay(overlay);
}
void step() {
text = stringf("%.0f Hz", gSampleRate);
@@ -116,7 +110,6 @@ Toolbar::Toolbar() {
SampleRateChoice *srChoice = new SampleRateChoice();
srChoice->box.pos = Vec(xPos, margin);
srChoice->box.size.x = 100;
srChoice->text = "";
addChild(srChoice);
xPos += srChoice->box.size.x;
}


+ 9
- 17
src/core/AudioInterface.cpp View File

@@ -300,9 +300,9 @@ struct AudioItem : MenuItem {
struct AudioChoice : ChoiceButton {
AudioInterface *audioInterface;
void onAction() {
MenuOverlay *overlay = new MenuOverlay();
Menu *menu = new Menu();
Menu *menu = gScene->createMenu();
menu->box.pos = getAbsolutePos().plus(Vec(0, box.size.y));
menu->box.size.x = box.size.x;

int deviceCount = audioInterface->getDeviceCount();
{
@@ -319,8 +319,6 @@ struct AudioChoice : ChoiceButton {
audioItem->text = audioInterface->getDeviceName(deviceId);
menu->pushChild(audioItem);
}
overlay->addChild(menu);
gScene->setOverlay(overlay);
}
void step() {
std::string name = audioInterface->getDeviceName(audioInterface->deviceId);
@@ -340,9 +338,9 @@ struct SampleRateItem : MenuItem {
struct SampleRateChoice : ChoiceButton {
AudioInterface *audioInterface;
void onAction() {
MenuOverlay *overlay = new MenuOverlay();
Menu *menu = new Menu();
Menu *menu = gScene->createMenu();
menu->box.pos = getAbsolutePos().plus(Vec(0, box.size.y));
menu->box.size.x = box.size.x;

const float sampleRates[6] = {44100, 48000, 88200, 96000, 176400, 192000};
int sampleRatesLen = sizeof(sampleRates) / sizeof(sampleRates[0]);
@@ -353,9 +351,6 @@ struct SampleRateChoice : ChoiceButton {
item->text = stringf("%.0f Hz", sampleRates[i]);
menu->pushChild(item);
}

overlay->addChild(menu);
gScene->setOverlay(overlay);
}
void step() {
this->text = stringf("%.0f Hz", audioInterface->sampleRate);
@@ -374,9 +369,9 @@ struct BlockSizeItem : MenuItem {
struct BlockSizeChoice : ChoiceButton {
AudioInterface *audioInterface;
void onAction() {
MenuOverlay *overlay = new MenuOverlay();
Menu *menu = new Menu();
Menu *menu = gScene->createMenu();
menu->box.pos = getAbsolutePos().plus(Vec(0, box.size.y));
menu->box.size.x = box.size.x;

const int blockSizes[] = {64, 128, 256, 512, 1024, 2048, 4096};
int blockSizesLen = sizeof(blockSizes) / sizeof(blockSizes[0]);
@@ -387,9 +382,6 @@ struct BlockSizeChoice : ChoiceButton {
item->text = stringf("%d", blockSizes[i]);
menu->pushChild(item);
}

overlay->addChild(menu);
gScene->setOverlay(overlay);
}
void step() {
this->text = stringf("%d", audioInterface->blockSize);
@@ -423,7 +415,7 @@ AudioInterfaceWidget::AudioInterfaceWidget() {
AudioChoice *choice = new AudioChoice();
choice->audioInterface = dynamic_cast<AudioInterface*>(module);
choice->box.pos = Vec(margin, yPos);
choice->box.size.x = box.size.x - 10;
choice->box.size.x = box.size.x - 2*margin;
addChild(choice);
yPos += choice->box.size.y + margin;
}
@@ -438,7 +430,7 @@ AudioInterfaceWidget::AudioInterfaceWidget() {
SampleRateChoice *choice = new SampleRateChoice();
choice->audioInterface = dynamic_cast<AudioInterface*>(module);
choice->box.pos = Vec(margin, yPos);
choice->box.size.x = box.size.x - 10;
choice->box.size.x = box.size.x - 2*margin;
addChild(choice);
yPos += choice->box.size.y + margin;
}
@@ -453,7 +445,7 @@ AudioInterfaceWidget::AudioInterfaceWidget() {
BlockSizeChoice *choice = new BlockSizeChoice();
choice->audioInterface = dynamic_cast<AudioInterface*>(module);
choice->box.pos = Vec(margin, yPos);
choice->box.size.x = box.size.x - 10;
choice->box.size.x = box.size.x - 2*margin;
addChild(choice);
yPos += choice->box.size.y + margin;
}


+ 89
- 25
src/core/MidiInterface.cpp View File

@@ -30,15 +30,23 @@ struct MidiInterface : Module {
NUM_INPUTS
};
enum OutputIds {
GATE_OUTPUT,
PITCH_OUTPUT,
GATE_OUTPUT,
MOD_OUTPUT,
PITCHWHEEL_OUTPUT,
NUM_OUTPUTS
};

int portId = -1;
PortMidiStream *stream = NULL;
std::list<int> notes;
/** Filter MIDI channel
-1 means all MIDI channels
*/
int channel = -1;
bool pedal = false;
int note = 60; // C4, most modules should use 261.626 Hz
int mod = 0;
int pitchWheel = 64;
bool retrigger = false;
bool retriggered = false;
@@ -62,6 +70,8 @@ MidiInterface::MidiInterface() {
inputs.resize(NUM_INPUTS);
outputs.resize(NUM_OUTPUTS);
midiInit();

printf("<<<%d>>>\n", getPortCount());
}

MidiInterface::~MidiInterface() {
@@ -77,6 +87,9 @@ void MidiInterface::step() {
}
}

if (outputs[PITCH_OUTPUT]) {
*outputs[PITCH_OUTPUT] = ((note - 64)) / 12.0;
}
if (outputs[GATE_OUTPUT]) {
bool gate = pedal || !notes.empty();
if (retrigger && retriggered) {
@@ -85,8 +98,11 @@ void MidiInterface::step() {
}
*outputs[GATE_OUTPUT] = gate ? 5.0 : 0.0;
}
if (outputs[PITCH_OUTPUT]) {
*outputs[PITCH_OUTPUT] = ((note - 64) + 2.0*(pitchWheel - 64) / 64.0) / 12.0;
if (outputs[MOD_OUTPUT]) {
*outputs[MOD_OUTPUT] = mod;
}
if (outputs[PITCHWHEEL_OUTPUT]) {
*outputs[PITCHWHEEL_OUTPUT] = (pitchWheel - 64) / 64.0 / 12.0;
}
}

@@ -121,6 +137,7 @@ void MidiInterface::openPort(int portId) {
return;
}
}
this->portId = portId;
}

void MidiInterface::pressNote(int note) {
@@ -202,9 +219,9 @@ struct MidiItem : MenuItem {
struct MidiChoice : ChoiceButton {
MidiInterface *midiInterface;
void onAction() {
MenuOverlay *overlay = new MenuOverlay();
Menu *menu = new Menu();
Menu *menu = gScene->createMenu();
menu->box.pos = getAbsolutePos().plus(Vec(0, box.size.y));
menu->box.size.x = box.size.x;

int portCount = midiInterface->getPortCount();
{
@@ -221,8 +238,45 @@ struct MidiChoice : ChoiceButton {
midiItem->text = midiInterface->getPortName(portId);
menu->pushChild(midiItem);
}
overlay->addChild(menu);
gScene->setOverlay(overlay);
}
void step() {
std::string name = midiInterface->getPortName(midiInterface->portId);
text = name.empty() ? "(no device)" : ellipsize(name, 14);
}
};

struct ChannelItem : MenuItem {
MidiInterface *midiInterface;
int channel;
void onAction() {
midiInterface->channel = channel;
}
};

struct ChannelChoice : ChoiceButton {
MidiInterface *midiInterface;
void onAction() {
Menu *menu = gScene->createMenu();
menu->box.pos = getAbsolutePos().plus(Vec(0, box.size.y));
menu->box.size.x = box.size.x;

{
ChannelItem *channelItem = new ChannelItem();
channelItem->midiInterface = midiInterface;
channelItem->channel = -1;
channelItem->text = "All";
menu->pushChild(channelItem);
}
for (int channel = 0; channel < 16; channel++) {
ChannelItem *channelItem = new ChannelItem();
channelItem->midiInterface = midiInterface;
channelItem->channel = channel;
channelItem->text = stringf("%d", channel + 1);
menu->pushChild(channelItem);
}
}
void step() {
text = (midiInterface->channel >= 0) ? stringf("%d", midiInterface->channel + 1) : "All";
}
};

@@ -239,42 +293,52 @@ MidiInterfaceWidget::MidiInterfaceWidget() {
}

float margin = 5;
float labelHeight = 15;
float yPos = margin;

{
Label *label = new Label();
label->box.pos = Vec(margin, yPos);
label->text = "MIDI Interface";
label->text = "MIDI device";
addChild(label);
yPos += label->box.size.y + margin;
}
yPos += labelHeight + margin;

{
MidiChoice *midiChoice = new MidiChoice();
midiChoice->midiInterface = dynamic_cast<MidiInterface*>(module);
midiChoice->text = "MIDI device";
midiChoice->box.pos = Vec(margin, yPos);
midiChoice->box.size.x = box.size.x - 10;
addChild(midiChoice);
yPos += midiChoice->box.size.y + margin;
}

{
Label *label = new Label();
label->box.pos = Vec(margin, yPos);
label->text = "MIDI channel";
addChild(label);
yPos += labelHeight + margin;

ChannelChoice *channelChoice = new ChannelChoice();
channelChoice->midiInterface = dynamic_cast<MidiInterface*>(module);
channelChoice->box.pos = Vec(margin, yPos);
channelChoice->box.size.x = box.size.x - 10;
addChild(channelChoice);
yPos += channelChoice->box.size.y + margin;
}

yPos += 5;
addOutput(createOutput<PJ3410Port>(Vec(20, yPos), module, MidiInterface::PITCH_OUTPUT));
addOutput(createOutput<PJ3410Port>(Vec(70, yPos), module, MidiInterface::GATE_OUTPUT));
yPos += 25 + margin;
yPos += 37 + margin;

{
Label *pitchLabel = new Label();
pitchLabel->box.pos = Vec(25-12, yPos);
pitchLabel->text = "Pitch";
addChild(pitchLabel);
yPos += 5;
addOutput(createOutput<PJ3410Port>(Vec(20, yPos), module, MidiInterface::GATE_OUTPUT));
yPos += 37 + margin;

Label *gateLabel = new Label();
gateLabel->box.pos = Vec(75-12, yPos);
gateLabel->text = "Gate";
addChild(gateLabel);
yPos += 5;
addOutput(createOutput<PJ3410Port>(Vec(20, yPos), module, MidiInterface::MOD_OUTPUT));
yPos += 37 + margin;

yPos += pitchLabel->box.size.y + margin;
}
yPos += 5;
addOutput(createOutput<PJ3410Port>(Vec(20, yPos), module, MidiInterface::PITCHWHEEL_OUTPUT));
yPos += 37 + margin;
}

Loading…
Cancel
Save