Subversion Repositories svn.mios32

Rev

Rev 1190 | 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: MidiKeyboard.cpp 1724 2013-03-28 20:23:40Z tk $
3
/*
4
 * Midi Keyboard
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 "MidiKeyboard.h"
878 tk 16
#include "MiosStudio.h"
877 tk 17
 
18
 
19
//==============================================================================
878 tk 20
MidiKeyboard::MidiKeyboard(MiosStudio *_miosStudio)
21
    : miosStudio(_miosStudio)
877 tk 22
{
909 tk 23
    for(int i=0; i<6; ++i) {
878 tk 24
        MidiSlider *s;
25
        if( i == 0 )
909 tk 26
            s = new MidiSlider(miosStudio, i, "PB", 0, 1, 0x40, true);
27
        else if( i == 1 )
28
            s = new MidiSlider(miosStudio, i, "CC", 1, 1, 0, false);
878 tk 29
        else
943 tk 30
            s = new MidiSlider(miosStudio, i, "CC", i+16-2, 1, 0, false);
878 tk 31
 
877 tk 32
        midiSlider.push_back(s);
33
        addAndMakeVisible(s);
34
    }
35
 
36
    addAndMakeVisible(midiKeyboardComponent
37
                      = new MidiKeyboardComponent(keyboardState,
38
                                                  MidiKeyboardComponent::horizontalKeyboard));
39
 
878 tk 40
    keyboardState.addListener(this);
41
 
42
    midiKeyboardComponent->setLowestVisibleKey(24); // does match better with layout
43
 
943 tk 44
    addAndMakeVisible(midiChannelLabel = new Label(T("Chn."), T("Chn.")));
45
    midiChannelLabel->setJustificationType(Justification::centred);
46
 
47
    addAndMakeVisible(midiChannelSlider = new Slider(T("MIDI Channel")));
48
    midiChannelSlider->setRange(1, 16, 1);
49
    midiChannelSlider->setSliderStyle(Slider::IncDecButtons);
50
    midiChannelSlider->setTextBoxStyle(Slider::TextBoxAbove, false, 80, 20);
51
    midiChannelSlider->addListener(this);
52
 
908 tk 53
    // restore settings
1724 tk 54
    PropertiesFile *propertiesFile = MiosStudioProperties::getInstance()->getCommonSettings(true);
908 tk 55
    if( propertiesFile ) {
943 tk 56
        int chn = propertiesFile->getIntValue("midiKeyboardChannel", 1);
57
        midiKeyboardComponent->setMidiChannel(chn);
945 tk 58
        for(int i=0; i<midiSlider.size(); ++i)
59
            midiSlider[i]->setMidiChannel(chn);
1724 tk 60
        midiChannelSlider->setValue(chn, dontSendNotification);
908 tk 61
    }
62
 
877 tk 63
    setSize(400, 200);
64
}
65
 
66
MidiKeyboard::~MidiKeyboard()
67
{
68
}
69
 
70
//==============================================================================
71
void MidiKeyboard::paint (Graphics& g)
72
{
73
    g.fillAll(Colours::white);
74
}
75
 
76
void MidiKeyboard::resized()
77
{
943 tk 78
    // MIDI channel
79
    if( getWidth() > 764 ) {
80
        midiChannelLabel->setVisible(true);
81
        midiChannelLabel->setBounds(4, 6, 30, 12);
82
        midiChannelSlider->setVisible(true);
83
        midiChannelSlider->setBounds(4, 20, 30, 32);
84
    } else {
85
        midiChannelLabel->setVisible(false);
86
        midiChannelSlider->setVisible(false);
87
    }
909 tk 88
 
89
    // CCs
877 tk 90
    int sliderWidth = 128;
91
    int distanceBetweenSliders = 150;
909 tk 92
    int sliderArrayWidth = (midiSlider.size()-1) * distanceBetweenSliders - (distanceBetweenSliders-sliderWidth);
93
    int sliderOffset = 20 + ((getWidth()-8-sliderArrayWidth) / 2);
94
    for(int i=1; i<midiSlider.size(); ++i)
95
        midiSlider[i]->setBounds(sliderOffset + distanceBetweenSliders*(i-1), 4, sliderWidth, 42);
877 tk 96
 
943 tk 97
    int keyboardX = 4+24+4;
98
    int keyboardWidth = getWidth()-8-24-4;
99
    int keyboardY = 4+44+4;
100
    int keyboardHeight = getHeight()-8-44-4;
101
    if( getWidth() >= (1024+4+24+4+4) ) {
102
        keyboardWidth = 1024;
103
        keyboardX = 4+24+4 + ((getWidth()-keyboardWidth-4-4) / 2);
104
    }
105
 
106
    midiKeyboardComponent->setBounds(keyboardX, keyboardY, keyboardWidth, keyboardHeight);
107
    midiSlider[0]->setBounds(keyboardX-24-4, keyboardY-4, 24, keyboardHeight+8);
877 tk 108
}
878 tk 109
 
110
 
111
//==============================================================================
945 tk 112
void MidiKeyboard::setMidiChannel(const int& midiChannel)
908 tk 113
{
114
    midiKeyboardComponent->setMidiChannel(midiChannel);
909 tk 115
    midiKeyboardComponent->setMidiChannelsToDisplay(1 << midiChannel);
908 tk 116
 
945 tk 117
    for(int i=0; i<midiSlider.size(); ++i)
118
        midiSlider[i]->setMidiChannel(midiChannel);
119
 
908 tk 120
    // store setting
1724 tk 121
    PropertiesFile *propertiesFile = MiosStudioProperties::getInstance()->getCommonSettings(true);
908 tk 122
    if( propertiesFile )
123
        propertiesFile->setValue("midiKeyboardChannel", midiChannel);
124
}
125
 
126
 
127
int MidiKeyboard::getMidiChannel(void)
128
{
129
    return midiKeyboardComponent->getMidiChannel();
130
}
131
 
132
//==============================================================================
878 tk 133
void MidiKeyboard::handleNoteOn(MidiKeyboardState *source, int midiChannel, int midiNoteNumber, float velocity)
134
{
135
    MidiMessage message = MidiMessage::noteOn(midiChannel, midiNoteNumber, (uint8)(velocity*128));
136
    miosStudio->sendMidiMessage(message);
137
}
138
 
139
void MidiKeyboard::handleNoteOff(MidiKeyboardState *source, int midiChannel, int midiNoteNumber)
140
{
141
    MidiMessage message = MidiMessage::noteOn(midiChannel, midiNoteNumber, (uint8)0);
142
    miosStudio->sendMidiMessage(message);
143
}
909 tk 144
 
145
 
146
//==============================================================================
943 tk 147
void MidiKeyboard::sliderValueChanged(Slider* sliderThatWasMoved)
148
{
149
    setMidiChannel(midiChannelSlider->getValue());
150
}
151
 
152
 
153
//==============================================================================
909 tk 154
void MidiKeyboard::handleIncomingMidiMessage(const MidiMessage& message, uint8 runningStatus)
155
{
156
 
157
}
158