~mixxxdevelopers/mixxx/features_library_scanner

« back to all changes in this revision

Viewing changes to mixxx/mixxx/midiobject.cpp

  • Committer: tuehaste
  • Date: 2002-02-26 11:12:07 UTC
  • Revision ID: vcs-imports@canonical.com-20020226111207-5rly26cj9gdd19ba
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "midiobject.h"
 
2
 
 
3
/* -------- ------------------------------------------------------
 
4
   Purpose: Initialize midi, and start parsing loop
 
5
   Input:   None. Automatically selects default midi input sound
 
6
            card and device.
 
7
   Output:  -
 
8
   -------- ------------------------------------------------------ */
 
9
MidiObject::MidiObject() {
 
10
  int card = snd_defaults_rawmidi_card();
 
11
  int device = snd_defaults_rawmidi_device();
 
12
 
 
13
  // Allocate buffer
 
14
  buffer = new char[4096];
 
15
  if (buffer == 0) {
 
16
    qDebug("Midi: Error allocating buffer");
 
17
    return;
 
18
  }
 
19
 
 
20
  // Open midi device for input
 
21
  int err;
 
22
  if ((err = snd_rawmidi_open(&handle, card, device, SND_RAWMIDI_OPEN_INPUT)) != 0) {
 
23
    qDebug("open failed: %s.", snd_strerror(err));
 
24
    return;
 
25
  }
 
26
 
 
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;
 
30
  params.size    = 4096;
 
31
  params.min     = 1;
 
32
  err = snd_rawmidi_channel_params(handle,&params);
 
33
 
 
34
  // Init buttons:
 
35
  no_potmeters = 0;
 
36
  no_buttons = 0;
 
37
 
 
38
  // Start the midi thread:
 
39
  start();
 
40
};
 
41
 
 
42
/* -------- ------------------------------------------------------
 
43
  Purpose: Deallocates midi buffer, and closes device
 
44
   Input:   -
 
45
   Output:  -
 
46
   -------- ------------------------------------------------------ */
 
47
MidiObject::~MidiObject() {
 
48
  // Close device
 
49
  snd_rawmidi_close(handle);
 
50
 
 
51
  // Deallocate buffer
 
52
  delete [] buffer;
 
53
};
 
54
 
 
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
 
59
            has been moved.
 
60
   Output:  -
 
61
   -------- ------------------------------------------------------ */
 
62
void MidiObject::addbutton(ControlPushButton* newbutton) {
 
63
  buttons.push_back(newbutton);
 
64
  no_buttons++;
 
65
  //std::cout << "Registered midi button " << newbutton->print() << ".\n";
 
66
}
 
67
 
 
68
void MidiObject::removebutton(ControlPushButton* button) {
 
69
  for (int i=0; i<buttons.size(); i++)
 
70
    if (buttons[i] == button) ; // buttons.erase(i); **************************************
 
71
}
 
72
 
 
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
 
77
            has been moved.
 
78
   Output:  -
 
79
   -------- ------------------------------------------------------ */
 
80
void MidiObject::addpotmeter(ControlPotmeter* newpotmeter) {
 
81
  potmeters.push_back(newpotmeter);
 
82
  no_potmeters++;
 
83
  //std::cout << "Registered midi potmeter " << newpotmeter->print() << ".\n";
 
84
}
 
85
 
 
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.");
 
90
}
 
91
 
 
92
/* -------- ------------------------------------------------------
 
93
   Purpose: Loop for parsing midi events
 
94
   Input:   -
 
95
   Output:  -
 
96
   -------- ------------------------------------------------------ */
 
97
void MidiObject::run() {
 
98
  int stop = 0;
 
99
  while(stop == 0) {
 
100
    /*
 
101
      First read until we get at -79 event:
 
102
    */
 
103
    do {
 
104
      int no = snd_rawmidi_read(handle,&buffer[0],1);
 
105
      if (no != 1)
 
106
        std::cout << "Warning: midi recieved " << no << " bytes.\n";
 
107
    } while (buffer[0] != -79);
 
108
    /*
 
109
      and then get the following 2 bytes:
 
110
    */
 
111
    for (int i=1; i<3; i++) {
 
112
      int no = snd_rawmidi_read(handle,&buffer[i],1);
 
113
      if (no != 1)
 
114
        std::cout << "Warning: midi recieved " << no << " bytes.\n";
 
115
    }
 
116
    
 
117
    char channel = buffer[0];
 
118
    char midicontrol = buffer[1];
 
119
    char midivalue = buffer[2];
 
120
 
 
121
    qDebug("Received midi message: %i %i %i",(int)channel, 
 
122
           (int)midicontrol,(int)midivalue);
 
123
    
 
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());
 
130
        break;
 
131
      }
 
132
    
 
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());
 
144
          break;
 
145
        }
 
146
      }
 
147
  }
 
148
};