summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/muse/midirc.cpp
blob: 14b69d558bd8ea5daf643f1b7145b8532e74a455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//=============================================================================
//  MusE
//  Linux Music Editor
//  $Id:$
//
//  Copyright (C) 2002-2006 by Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#include "midirc.h"
#include "song.h"
#include "midi.h"

MidiRCList midiRCList;

//---------------------------------------------------------
//   isActive
//---------------------------------------------------------

bool MidiRCList::isActive(int action)
      {
      for (iMidiRC i = begin(); i != end(); ++i) {
            if (i->action == action)
                  return true;
            }
      return false;
      }

//---------------------------------------------------------
//   setAction
//---------------------------------------------------------

void MidiRCList::setAction(const MidiEvent& e, int action)
      {
      //
      //  TODO: check for already used events
      //
      for (iMidiRC i = begin(); i != end(); ++i) {
            if (i->action == action) {
//      printf("replace action %d: ", action);
//      i->event.dump();
                  i->event = e;
                  return;
                  }
            }
      push_back(MidiRC(e, action));
//      printf("add action %d: ", action);
      e.dump();
      }

//---------------------------------------------------------
//   emitAction
//---------------------------------------------------------

void MidiRCList::emitAction(int action) const
      {
//      printf("emit action %d\n", action);
      switch(action) {
            case RC_STOP:
                  song->setStop(true);
                  break;
            case RC_PLAY:
                  song->setPlay(true);
                  break;
            case RC_RECORD:
                  song->setRecord(true);
                  break;
            case RC_GOTO_LEFT_MARK:
                  song->setPos(0, song->lpos(), true, true, true);
                  break;
            }
      }

//---------------------------------------------------------
//   doAction
//    emit action associated with event e
//    return true if action found
//---------------------------------------------------------

bool MidiRCList::doAction(const MidiEvent& e)
      {
//      printf("MidiRCList::doAction ");
//      e.dump();
      for (iMidiRC i = begin(); i != end(); ++i) {
            if ((i->event.type()& 0xf0) == (e.type() & 0xf0)) {
                  //
                  // for note on events only compare pitch, not velocity
                  // ignore note off events (note on with velicity zero
                  //
                  if ((e.type() == ME_NOTEON)
                     && i->event.dataA() == e.dataA()
                     && e.dataB() != 0) {
                        emitAction(i->action);
                        return true;
                        }
                  //
                  // compare controller and controller value
                  // TODO: maybe we need a special option to
                  //       ignore the controller value
                  //
                  if ((e.type() == ME_CONTROLLER)
                     && i->event.dataA() == e.dataA()
                     && i->event.dataB() == e.dataB()) {
                        emitAction(i->action);
                        return true;
                        }
                  }
            }
      return false;
      }

//---------------------------------------------------------
//   read
//---------------------------------------------------------

void MidiRCList::read(QDomNode node)
      {
      int action = 0;
      MidiEvent event;
      for (node = node.firstChild(); !node.isNull(); node = node.nextSibling()) {
            QDomElement e = node.toElement();
            if (e.isNull())
                  continue;
            if (e.tagName() == "action") {
                  action = e.attribute("id", "0").toInt();
                  for (QDomNode n = node.firstChild(); !n.isNull(); n = n.nextSibling()) {
                        QDomElement e1 = node.toElement();
                        if (e1.isNull())
                              continue;
                        if (e1.tagName() == "noteOn") {
                              event.setType(ME_NOTEON);
                              event.setA(e1.attribute("pitch","0").toInt());
                              }
                        else if (e1.tagName() == "controller") {
                              event.setType(ME_CONTROLLER);
                              event.setA(e1.attribute("no","0").toInt());
                              event.setB(e1.attribute("val","0").toInt());
                              }
                        else
                              printf("MusE:midiRC:action: unknown tag %s\n", e1.tagName().toLatin1().data());
                        }
                  setAction(event, action);
                  }
            else
                  printf("MusE:midiRC: unknown tag %s\n", e.tagName().toLatin1().data());
            }
      }

//---------------------------------------------------------
//   write
//---------------------------------------------------------

void MidiRCList::write(Xml& xml)
      {
      xml.stag("midiRC");
      for (iMidiRC i = begin(); i != end(); ++i) {
            xml.stag(QString("action id=\"%1\"").arg(i->action));
            if (i->event.type() == ME_NOTEON)
                  xml.stag(QString("noteOn pitch=\"%1\"").arg(i->event.dataA()));
            else if (i->event.type() == ME_CONTROLLER)
                  xml.stag(QString("controller no=\"%1\" val=\"%2\"")
                     .arg(i->event.dataA()).arg(i->event.dataB()));
            else
                  printf("remote event type %d not supported\n", i->event.type());
            xml.etag("action");
            }
      xml.etag("midiRC");
      }