summaryrefslogtreecommitdiffstats
path: root/src/commands/studio/ModifyDeviceMappingCommand.cpp
blob: 396910c99ca1f77e8e89d45e96d860b076146919 (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
/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <[email protected]>,
        Chris Cannam        <[email protected]>,
        Richard Bown        <[email protected]>
 
    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.
 
    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/


#include "ModifyDeviceMappingCommand.h"

#include "misc/Debug.h"
#include "base/Composition.h"
#include "base/Device.h"
#include "base/Instrument.h"
#include "base/MidiProgram.h"
#include "base/Studio.h"
#include "base/Track.h"
#include "document/RosegardenGUIDoc.h"
#include <tqstring.h>


namespace Rosegarden
{

ModifyDeviceMappingCommand::ModifyDeviceMappingCommand(
    RosegardenGUIDoc *doc,
    DeviceId fromDevice,
    DeviceId toDevice):
        KNamedCommand(getGlobalName()),
        m_composition(&doc->getComposition()),
        m_studio(&doc->getStudio()),
        m_fromDevice(fromDevice),
        m_toDevice(toDevice)
{}

void
ModifyDeviceMappingCommand::execute()
{
    Composition::trackcontainer &tracks =
        m_composition->getTracks();
    Composition::trackcontainer::iterator it = tracks.begin();
    Instrument *instr = 0;
    int index = 0;

    for (; it != tracks.end(); it++) {
        instr = m_studio->getInstrumentById(it->second->getInstrument());
        if (!instr || !instr->getDevice())
            continue;

        if (instr->getDevice()->getId() == m_fromDevice) {
            // if source and target are MIDI
            if (m_studio->getDevice(m_fromDevice)->getType() ==
                    Device::Midi &&
                    m_studio->getDevice(m_toDevice)->getType() ==
                    Device::Midi) {
                // Try to match channels on the target device
                //
                MidiByte channel = instr->getMidiChannel();

                InstrumentList destList = m_studio->
                                          getDevice(m_toDevice)->getPresentationInstruments();

                InstrumentList::iterator dIt = destList.begin();

                for (; dIt != destList.end(); dIt++) {
                    if ((*dIt)->getMidiChannel() == channel) {
                        break;
                    }
                }

                // Failure to match anything and there's no Instruments
                // at all in the destination.  Skip to next source Instrument.
                //
                if (dIt == destList.end() || destList.size() == 0)
                    continue;


                RG_DEBUG << " Track " << it->first
                << ", setting Instrument to "
                << (*dIt)->getId() << endl;

                // store "to" and "from" values
                //
                m_mapping.push_back(
                    std::pair < TrackId,
                    InstrumentId >
                    (it->first,
                     instr->getId()));

                it->second->setInstrument((*dIt)->getId());
            } else // audio is involved in the mapping - use indexes
            {
                // assign by index numbers
                InstrumentList destList = m_studio->
                                          getDevice(m_toDevice)->getPresentationInstruments();

                // skip if we can't match
                //
                if (index > (int)(destList.size() - 1))
                    continue;

                m_mapping.push_back(
                    std::pair < TrackId,
                    InstrumentId >
                    (it->first,
                     instr->getId()));

                it->second->setInstrument(destList[index]->getId());
            }

            index++;
        }
    }

}

void
ModifyDeviceMappingCommand::unexecute()
{
    std::vector<std::pair<TrackId, InstrumentId> >
    ::iterator it = m_mapping.begin();
    Track *track = 0;

    for (; it != m_mapping.end(); it++) {
        track = m_composition->getTrackById(it->first);
        track->setInstrument(it->second);
    }
}

}