Subversion Repositories svn.mios32

Rev

Rev 1195 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
877 tk 1
/* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
2
// $Id: MidiSlider.cpp 1724 2013-03-28 20:23:40Z tk $
3
/*
4
 * Midi Slider
5
 *
6
 * ==========================================================================
7
 *
8
 *  Copyright (C) 2010 Thorsten Klose (tk@midibox.org)
9
 *  Licensed for personal non-commercial use only.
10
 *  All other rights reserved.
11
 *
12
 * ==========================================================================
13
 */
14
 
15
#include "MidiSlider.h"
878 tk 16
#include "MiosStudio.h"
877 tk 17
 
878 tk 18
 
877 tk 19
//==============================================================================
943 tk 20
static const char MidiSliderCcNames[128][9] = {
21
  // 0x00..0x0f
22
  "Bank MSB",
23
  "ModWheel",
24
  "Breath  ",
25
  "Ctrl.  3",
26
  "FootCtrl",
27
  "PortTime",
28
  "Data MSB",
29
  "Volume  ",
30
  "Balance ",
31
  "Ctrl.  9",
32
  "Panorama",
33
  "Expr.   ",
34
  "Fx#1 MSB",
35
  "Fx#2 MSB",
36
  "Ctrl. 14",
37
  "Ctrl. 15",
38
 
39
  // 0x10..0x1f
40
  "GP #1   ",
41
  "GP #2   ",
42
  "GP #3   ",
43
  "GP #4   ",
44
  "Ctrl. 20",
45
  "Ctrl. 21",
46
  "Ctrl. 22",
47
  "Ctrl. 23",
48
  "Ctrl. 24",
49
  "Ctrl. 25",
50
  "Ctrl. 26",
51
  "Ctrl. 27",
52
  "Ctrl. 28",
53
  "Ctrl. 29",
54
  "Ctrl. 30",
55
  "Ctrl. 31",
56
 
57
  // 0x20..0x2f
58
  "Bank LSB",
59
  "Mod  LSB",
60
  "BreatLSB",
61
  "Ctrl. 35",
62
  "Foot.LSB",
63
  "PortaLSB",
64
  "Data LSB",
65
  "Vol. LSB",
66
  "Bal. LSB",
67
  "Ctrl. 41",
68
  "Pan. LSB",
69
  "Expr.LSB",
70
  "Fx#1 LSB",
71
  "Fx#2 LSB",
72
  "Ctrl. 46",
73
  "Ctrl. 47",
74
 
75
  // 0x30..0x3f
76
  "Ctrl. 48",
77
  "Ctrl. 49",
78
  "Ctrl. 50",
79
  "Ctrl. 51",
80
  "Ctrl. 52",
81
  "Ctrl. 53",
82
  "Ctrl. 54",
83
  "Ctrl. 55",
84
  "Ctrl. 56",
85
  "Ctrl. 57",
86
  "Ctrl. 58",
87
  "Ctrl. 59",
88
  "Ctrl. 60",
89
  "Ctrl. 61",
90
  "Ctrl. 62",
91
  "Ctrl. 63",
92
 
93
  // 0x40..0x4f
94
  "Sustain ",
95
  "Port. ON",
96
  "Sustenu.",
97
  "SoftPed.",
98
  "LegatoSw",
99
  "Hold2   ",
100
  "Ctrl. 70",
101
  "Harmonic",
102
  "Release ",
103
  "Attack  ",
104
  "CutOff  ",
105
  "Ctrl. 75",
106
  "Ctrl. 76",
107
  "Ctrl. 77",
108
  "Ctrl. 78",
109
  "Ctrl. 79",
110
 
111
  // 0x50..0x5f
112
  "Ctrl. 80",
113
  "Ctrl. 81",
114
  "Ctrl. 82",
115
  "Ctrl. 83",
116
  "Ctrl. 84",
117
  "Ctrl. 85",
118
  "Ctrl. 86",
119
  "Ctrl. 87",
120
  "Ctrl. 88",
121
  "Ctrl. 89",
122
  "Ctrl. 90",
123
  "Reverb  ",
124
  "Tremolo ",
125
  "Chorus  ",
126
  "Celeste ",
127
  "Phaser  ",
128
 
129
  // 0x60..0x6f
130
  "Data Inc",
131
  "Data Dec",
132
  "NRPN LSB",
133
  "NRPN MSB",
134
  "RPN LSB ",
135
  "RPN MSB ",
136
  "Ctrl.102",
137
  "Ctrl.103",
138
  "Ctrl.104",
139
  "Ctrl.105",
140
  "Ctrl.106",
141
  "Ctrl.107",
142
  "Ctrl.108",
143
  "Ctrl.109",
144
  "Ctrl.110",
145
  "Ctrl.111",
146
 
147
  // 0x70..0x7f
148
  "Ctrl.112",
149
  "Ctrl.113",
150
  "Ctrl.114",
151
  "Ctrl.115",
152
  "Ctrl.116",
153
  "Ctrl.117",
154
  "Ctrl.118",
155
  "Ctrl.119",
156
  "SoundOff",
157
  "ResetAll",
158
  "Local   ",
159
  "NotesOff",
160
  "Omni Off",
161
  "Omni On ",
162
  "Mono On ",
163
  "Poly On "
164
};
165
 
166
 
167
//==============================================================================
910 tk 168
MidiSliderComponent::MidiSliderComponent(const String &componentName)
169
    : Slider(componentName)
170
    , snapsBackOnRelease(0)
171
{
172
    setRange(0, 127, 1);
173
    setTextBoxStyle(Slider::NoTextBox, true, 80, 20);
174
}
175
 
176
MidiSliderComponent::~MidiSliderComponent()
177
{
178
}
179
 
180
void MidiSliderComponent::mouseUp(const MouseEvent& e)
181
{
182
    if( snapsBackOnRelease )
183
        setValue(64);
184
}
185
 
186
//==============================================================================
909 tk 187
MidiSlider::MidiSlider(MiosStudio *_miosStudio, int _num, String _functionName, int _functionArg, int _midiChannel, int initialValue, bool _vertical)
878 tk 188
    : miosStudio(_miosStudio)
908 tk 189
    , sliderNum(_num)
909 tk 190
    , vertical(_vertical)
877 tk 191
{
943 tk 192
    addAndMakeVisible(slider = new MidiSliderComponent(T("Slider")));
910 tk 193
    slider->setSliderStyle(vertical ? MidiSliderComponent::LinearVertical : MidiSliderComponent::LinearHorizontal);
877 tk 194
    slider->addListener(this);
195
 
943 tk 196
    // only used for horizontal sliders
197
    addAndMakeVisible(sliderFunction = new ComboBox(String::empty));
198
    sliderFunction->addListener(this);
877 tk 199
 
908 tk 200
    // restore settings
1724 tk 201
    PropertiesFile *propertiesFile = MiosStudioProperties::getInstance()->getCommonSettings(true);
908 tk 202
    if( propertiesFile ) {
203
        _functionName = propertiesFile->getValue("slider" + String(sliderNum) + "FunctionName", _functionName);
204
        _functionArg = propertiesFile->getIntValue("slider" + String(sliderNum) + "FunctionArg", _functionArg);
205
        _midiChannel = propertiesFile->getIntValue("slider" + String(sliderNum) + "MidiChannel", _midiChannel);
206
        initialValue = propertiesFile->getIntValue("slider" + String(sliderNum) + "InitialValue", initialValue);
207
    }
208
 
878 tk 209
    setFunction(_functionName, _functionArg, _midiChannel, initialValue);
210
 
909 tk 211
    if( vertical )
212
        setSize(24, 18+80);
213
    else
214
        setSize(128, 24+18);
877 tk 215
}
216
 
217
MidiSlider::~MidiSlider()
218
{
949 philetaylo 219
    deleteAndZero(slider);
220
    deleteAndZero(sliderFunction);
877 tk 221
}
222
 
223
//==============================================================================
224
void MidiSlider::paint (Graphics& g)
225
{
226
    g.fillAll(Colours::white);
227
}
228
 
229
void MidiSlider::resized()
230
{
909 tk 231
    if( vertical ) {
943 tk 232
        slider->setBounds(0, 0, getWidth(), getHeight());
909 tk 233
    } else {
943 tk 234
        sliderFunction->setBounds(0, 0, 128, 18);
909 tk 235
        slider->setBounds(0, 18, 128, 24);
236
    }
877 tk 237
}
238
 
239
 
240
//==============================================================================
908 tk 241
void MidiSlider::setFunction(const String &_functionName, const int &_functionArg, const int &_midiChannel, const int &initialValue)
877 tk 242
{
243
    functionName = _functionName;
244
    functionArg = _functionArg;
878 tk 245
    midiChannel = _midiChannel;
877 tk 246
 
910 tk 247
    slider->snapsBackOnRelease = 0;
877 tk 248
    if( functionName.containsWholeWord(T("CC")) ) {
249
        String labelStr(functionArg);
949 philetaylo 250
        sliderFunction->clear(true);
943 tk 251
        for(int i=0; i<128; ++i) {
252
            sliderFunction->addItem(T("CC") + String(i) + T(": ") + String(MidiSliderCcNames[i]), i+1);
253
        }
948 tk 254
        sliderFunction->setSelectedId(functionArg+1, true);
878 tk 255
    } else if( functionName.containsWholeWord(T("PB")) ) {
256
        String labelStr(functionArg);
910 tk 257
        slider->snapsBackOnRelease = 1;
943 tk 258
        sliderFunction->clear();
877 tk 259
    } else {
943 tk 260
        sliderFunction->clear();
877 tk 261
    }
878 tk 262
 
1724 tk 263
    slider->setValue(initialValue, dontSendNotification); // don't send update message
908 tk 264
 
265
    // store settings
1724 tk 266
    PropertiesFile *propertiesFile = MiosStudioProperties::getInstance()->getCommonSettings(true);
908 tk 267
    if( propertiesFile ) {
268
        propertiesFile->setValue("slider" + String(sliderNum) + "FunctionName",_functionName);
269
        propertiesFile->setValue("slider" + String(sliderNum) + "FunctionArg", functionArg);
270
        propertiesFile->setValue("slider" + String(sliderNum) + "MidiChannel", midiChannel);
271
        propertiesFile->setValue("slider" + String(sliderNum) + "InitialValue", slider->getValue());
272
    }
877 tk 273
}
274
 
908 tk 275
 
877 tk 276
//==============================================================================
945 tk 277
void MidiSlider::setMidiChannel(const int& _midiChannel)
278
{
279
    midiChannel = _midiChannel;
280
}
281
 
282
int MidiSlider::getMidiChannel(void)
283
{
284
    return midiChannel;
285
}
286
 
287
//==============================================================================
877 tk 288
void MidiSlider::sliderValueChanged(Slider* sliderThatWasMoved)
289
{
878 tk 290
    if( functionName.containsWholeWord(T("CC")) ) {
291
        MidiMessage message = MidiMessage::controllerEvent(midiChannel, functionArg, (uint8)slider->getValue());
292
        miosStudio->sendMidiMessage(message);
293
    } else if( functionName.containsWholeWord(T("PB")) ) {
1195 tk 294
        unsigned pitchValue = (int)slider->getValue() * 128;
295
 
296
        // I've seen this handling to fake an increased resolution on Korg and Yamaha Keyboards
297
        if( pitchValue != 0x2000 )
298
            pitchValue += (int)slider->getValue();
299
 
300
        MidiMessage message = MidiMessage::pitchWheel(midiChannel, pitchValue);
878 tk 301
        miosStudio->sendMidiMessage(message);
302
    }
877 tk 303
}
943 tk 304
 
305
 
306
//==============================================================================
307
void MidiSlider::comboBoxChanged(ComboBox *comboBoxThatHasChanged)
308
{
309
    setFunction(functionName, sliderFunction->getSelectedId()-1, midiChannel, slider->getValue());
310
}
311