1
#include "midiobject.h"
3
/* -------- ------------------------------------------------------
4
Purpose: Initialize midi, and start parsing loop
5
Input: None. Automatically selects default midi input sound
8
-------- ------------------------------------------------------ */
9
MidiObject::MidiObject() {
10
int card = snd_defaults_rawmidi_card();
11
int device = snd_defaults_rawmidi_device();
14
buffer = new char[4096];
16
qDebug("Midi: Error allocating buffer");
20
// Open midi device for input
22
if ((err = snd_rawmidi_open(&handle, card, device, SND_RAWMIDI_OPEN_INPUT)) != 0) {
23
qDebug("open failed: %s.", snd_strerror(err));
27
// Set number of bytes received, before snd_rawmidi_read is woken up.
28
snd_rawmidi_params_t params;
29
params.channel = SND_RAWMIDI_CHANNEL_INPUT;
32
err = snd_rawmidi_channel_params(handle,¶ms);
38
// Start the midi thread:
42
/* -------- ------------------------------------------------------
43
Purpose: Deallocates midi buffer, and closes device
46
-------- ------------------------------------------------------ */
47
MidiObject::~MidiObject() {
49
snd_rawmidi_close(handle);
55
/* -------- ------------------------------------------------------
56
Purpose: Add a button ready to recieve midi events.
57
Input: a pointer to the button. The second argument
58
is the method in the control to call when the button
61
-------- ------------------------------------------------------ */
62
void MidiObject::addbutton(ControlPushButton* newbutton) {
63
buttons.push_back(newbutton);
65
//std::cout << "Registered midi button " << newbutton->print() << ".\n";
68
void MidiObject::removebutton(ControlPushButton* button) {
69
for (int i=0; i<buttons.size(); i++)
70
if (buttons[i] == button) ; // buttons.erase(i); **************************************
73
/* -------- ------------------------------------------------------
74
Purpose: Add a potmeter ready to recieve midi events.
75
Input: a pointer to the potmeter. The second argument
76
is the method in the control to call when the potmeter
79
-------- ------------------------------------------------------ */
80
void MidiObject::addpotmeter(ControlPotmeter* newpotmeter) {
81
potmeters.push_back(newpotmeter);
83
//std::cout << "Registered midi potmeter " << newpotmeter->print() << ".\n";
86
void MidiObject::removepotmeter(ControlPotmeter* potmeter) {
87
for (int i=0; i<potmeters.size(); i++)
88
if (potmeters[i] == potmeter); // potmeters.erase(i); ************************************'
89
qWarning("Remove of midi potmeters not yet implemented.");
92
/* -------- ------------------------------------------------------
93
Purpose: Loop for parsing midi events
96
-------- ------------------------------------------------------ */
97
void MidiObject::run() {
101
First read until we get at -79 event:
104
int no = snd_rawmidi_read(handle,&buffer[0],1);
106
std::cout << "Warning: midi recieved " << no << " bytes.\n";
107
} while (buffer[0] != -79);
109
and then get the following 2 bytes:
111
for (int i=1; i<3; i++) {
112
int no = snd_rawmidi_read(handle,&buffer[i],1);
114
std::cout << "Warning: midi recieved " << no << " bytes.\n";
117
char channel = buffer[0];
118
char midicontrol = buffer[1];
119
char midivalue = buffer[2];
121
qDebug("Received midi message: %i %i %i",(int)channel,
122
(int)midicontrol,(int)midivalue);
124
// Check the potmeters:
125
for (int i=0; i<no_potmeters; i++)
126
if (potmeters[i]->midino == midicontrol) {
127
potmeters[i]->slotSetPosition((int)midivalue);
128
qDebug("Changed potmeter %s to %i",potmeters[i]->print(),
129
(int)potmeters[i]->getValue());
133
// Check the buttons:
134
for (int i=0; i<no_buttons; i++)
135
if (buttons[i]->midino == midicontrol) {
136
// Now that we've found a button on the right gate, we try to
137
// see if the button is really changed:
138
positionType state = down;
139
if ((buttons[i]->midimask & midivalue) == 0) state = up;
140
if (buttons[i]->position != state) {
141
buttons[i]->slotSetPosition(state);
142
qDebug("Changed button %s to %s", buttons[i]->print(),
143
buttons[i]->printValue());