summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/midiplugins/drumglue/drumglue.cpp
blob: 7a9b6ed2e3a7aeb6e9c882512d704b5536798623 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//=========================================================
//  MusE
//  Linux Music Editor
//
//    drumglue - filter
//
//  (C) Copyright 2008 Robert Jonsson (rj@spamatica.se)
//  (C) Copyright 2005- Werner Schweer (ws@seh.de)
// Copyright: See COPYING file that comes with this distribution
//=========================================================

#include "drumgluegui.h"
#include "drumglue.h"
#include "midi.h"
#include "midievent.h"



//---------------------------------------------------------
//   DrumInstrument - get next index
//     - next output instrument to use
//---------------------------------------------------------
int DrumInstrument::getNextIndex(int /*velocity*/)
{
	// for now we simply do a round robin
	//
	// future improvements are to keep track of
	// the time since the last hit and the
	// weight set for each instrument.
	// the incoming velocity should be checked that it's in range
	if (outputInstruments.size() == 0)
		return -1;
	
	if (lastOutputIndex+1 > outputInstruments.size()-1)
		return 0;
	else
		return lastOutputIndex+1;
}

//---------------------------------------------------------
//   DrumInstrument - get velocity
//     - velocity value to use
//---------------------------------------------------------
int DrumInstrument::getVelocity(int /*index*/, int velocity)
{
	// for now we just return the same velocity
	// future improvements are to allow for some
	return velocity;
}


//---------------------------------------------------------
//   DrumGlue - constructor
//---------------------------------------------------------
DrumGlue::DrumGlue(const char* name, const MempiHost* h)
   : Mempi(name, h)
      {
      gui = 0;
      saveData = NULL;
      }

//---------------------------------------------------------
//   DrumGlue - destructor
//---------------------------------------------------------
DrumGlue::~DrumGlue()
      {
      if (gui)
            delete gui;
      if (saveData)
      		delete saveData;
      }

//---------------------------------------------------------
//   init
//---------------------------------------------------------

bool DrumGlue::init()
      {
      gui = new DrumGlueGui(this, 0);
      gui->setWindowTitle("MusE: "+QString(name()));
      gui->show();
      return false;
      }

//---------------------------------------------------------
//   getGeometry
//---------------------------------------------------------

void DrumGlue::getGeometry(int* x, int* y, int* w, int* h) const
      {
      QPoint pos(gui->pos());
      QSize size(gui->size());
      *x = pos.x();
      *y = pos.y();
      *w = size.width();
      *h = size.height();
      }

//---------------------------------------------------------
//   setGeometry
//---------------------------------------------------------

void DrumGlue::setGeometry(int x, int y, int w, int h)
      {
      gui->resize(QSize(w, h));
      gui->move(QPoint(x, y));
      }

//---------------------------------------------------------
//   process
//---------------------------------------------------------

void DrumGlue::process(unsigned , unsigned , MidiEventList* il, MidiEventList* ol)
      {

      for (iMidiEvent i = il->begin(); i != il->end(); ++i) {
            MidiEvent temp=*i;
            if (temp.isNote() && !temp.isNoteOff())
                {
					foreach(DrumInstrument *di, drumInstruments) {
						if (temp.dataA() == di->inKey) {
							int inVelocity = temp.dataB();
							int instrumentIdx = di->getNextIndex(inVelocity);
							if (instrumentIdx==-1) {
								// no instrument defined, yet, skip it
								break;
							}
							int outKey = di->outputInstruments[instrumentIdx]->outKey;
							int outVelocity= di->getVelocity(instrumentIdx, inVelocity);
							printf("inKey=%d outKey =%d outVelocity=%d instrumentIdx=%d\n", di->inKey, outKey, outVelocity, instrumentIdx);
							temp.setA(outKey);
							temp.setB(outVelocity);
							
							ol->insert(temp);	// note on
							
							temp.setB(0);
							ol->insert(temp);	// note off
							
							di->lastOutputIndex = instrumentIdx;
							di->outputTime = temp.time();
							break;
							}
                    	}
                 }
            if (temp.isNoteOff()) ; // we just throw it away, we will insert noteoffs for each note on
            }
      }

//
// getInitData - return configuration to MusE
//
void DrumGlue::getInitData(int* n, const unsigned char** p) const
      {
       QString saveStr;
       
       foreach (DrumInstrument *di, drumInstruments) {
       		QString drumline = "DRUM " +di->name + " " + QString("%1").arg(di->inKey) + "\n";
       		saveStr.append(drumline);
       		foreach (DrumOutputInstrument *doi, di->outputInstruments) {
				QString outputline = "OUTPUT " + 
									QString("%1").arg(doi->outKey) + " " + 
									QString("%1").arg(doi->lowestVelocity) + " " + 
									QString("%1").arg(doi->highestVelocity) + " " + 
									QString("%1").arg(doi->prefer) + " " + 
									QString("%1").arg(doi->preferFast) + "\n";
				saveStr.append(outputline);
       			
       		}
       }
       
      *n = saveStr.length();
      
      if (saveData)
      	delete saveData;
      	
      
      saveData = new unsigned char[saveStr.length()];
      
      strncpy((char*)saveData, saveStr.toLatin1().data(), saveStr.length());
      saveData[saveStr.length()]=0;
      printf("getInitData -\n%s\n",saveData);
      
      *p = saveData;

      }

void DrumGlue::setInitData(int n, const unsigned char* p)
      {
      if (saveData)
      		delete saveData;
      saveData = new unsigned char[n+1];
      		
      strncpy((char*)saveData,(char*)p,n);
      saveData[n]=0;
      
      QString loadStr = (char*)saveData;
      printf("setInitData -\n%s\n",saveData);
      	
      QStringList loadList = loadStr.split("\n");
      	
      DrumInstrument *currentInstrument=NULL;
      foreach (QString line, loadList) {
      		QStringList splitLine = line.split(" ");
      		if (splitLine[0] == "DRUM") {
      			if (currentInstrument)
      				drumInstruments.append(currentInstrument);
      			
      			currentInstrument = new DrumInstrument();
      			currentInstrument->name = splitLine[1];
      			currentInstrument->inKey = splitLine[2].toInt();
      		}
      		if (splitLine[0] == "OUTPUT") {
				DrumOutputInstrument *doi = new DrumOutputInstrument;
				doi->outKey = splitLine[1].toInt();
				doi->lowestVelocity = splitLine[2].toInt();
				doi->highestVelocity = splitLine[3].toInt();
				doi->prefer = splitLine[4].toInt();
				doi->preferFast = splitLine[5].toInt();
				currentInstrument->outputInstruments.append(doi);
      		}
      }
      if (currentInstrument)
		drumInstruments.append(currentInstrument);
      
      if (gui)
            gui->init();
      }

//---------------------------------------------------------
//   inst
//---------------------------------------------------------

static Mempi* instantiate(const char* name, const MempiHost* h)
      {
      return new DrumGlue(name, h);
      }

extern "C" {
      static MEMPI descriptor = {
            "DrumGlue",
            "Drum instrument mux filter",
            "0.1",                  // filter version string
            MEMPI_FILTER,           // plugin type
            MEMPI_MAJOR_VERSION, MEMPI_MINOR_VERSION,
            instantiate
            };

      const MEMPI* mempi_descriptor() { return &descriptor; }
      }