38 |
SamplerChannel::SamplerChannel(Sampler* pS) { |
SamplerChannel::SamplerChannel(Sampler* pS) { |
39 |
pSampler = pS; |
pSampler = pS; |
40 |
pEngineChannel = NULL; |
pEngineChannel = NULL; |
|
pMidiInputDevice = NULL; |
|
41 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
42 |
midiPort = 0; |
pMidiInputDevice = NULL; |
43 |
midiChannel = MidiInputPort::midi_chan_all; |
iMidiPort = 0; |
44 |
|
midiChannel = midi_chan_all; |
45 |
iIndex = -1; |
iIndex = -1; |
46 |
} |
} |
47 |
|
|
48 |
SamplerChannel::~SamplerChannel() { |
SamplerChannel::~SamplerChannel() { |
49 |
if (pEngineChannel) { |
if (pEngineChannel) { |
50 |
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel()); |
51 |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
52 |
if (pEngineChannel) { |
if (pEngineChannel) { |
53 |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
54 |
delete pEngineChannel; |
EngineChannelFactory::Destroy(pEngineChannel); |
55 |
} |
} |
56 |
} |
} |
57 |
} |
} |
63 |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
64 |
if (!pNewEngineChannel) throw LinuxSamplerException("Unknown engine type"); |
if (!pNewEngineChannel) throw LinuxSamplerException("Unknown engine type"); |
65 |
|
|
66 |
|
//FIXME: hack to allow fast retrieval of engine channel's sampler channel index |
67 |
|
pNewEngineChannel->iSamplerChannelIndex = Index(); |
68 |
|
|
69 |
// dereference midi input port. |
// dereference midi input port. |
70 |
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
71 |
// disconnect old engine |
// disconnect old engine channel |
72 |
if (pEngineChannel) { |
if (pEngineChannel) { |
73 |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
74 |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
75 |
delete pEngineChannel; |
EngineChannelFactory::Destroy(pEngineChannel); |
76 |
} |
} |
77 |
|
|
78 |
// connect new engine channel |
// connect new engine channel |
79 |
pEngineChannel = pNewEngineChannel; |
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); |
|
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, this->midiChannel); |
|
80 |
if (pAudioOutputDevice) { |
if (pAudioOutputDevice) { |
81 |
pNewEngineChannel->Connect(pAudioOutputDevice); |
pNewEngineChannel->Connect(pAudioOutputDevice); |
82 |
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
83 |
} |
} |
84 |
|
pEngineChannel = pNewEngineChannel; |
85 |
|
|
86 |
|
// from now on get MIDI device and port from EngineChannel object |
87 |
|
this->pMidiInputDevice = NULL; |
88 |
|
this->iMidiPort = 0; |
89 |
|
|
90 |
|
pEngineChannel->StatusChanged(true); |
91 |
|
|
92 |
dmsg(2,("OK\n")); |
dmsg(2,("OK\n")); |
93 |
} |
} |
94 |
|
|
104 |
} |
} |
105 |
|
|
106 |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
107 |
SetMidiInput(pDevice, this->midiPort, this->midiChannel); |
SetMidiInput(pDevice, 0, GetMidiInputChannel()); |
108 |
} |
} |
109 |
|
|
110 |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
111 |
SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel); |
SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel()); |
112 |
} |
} |
113 |
|
|
114 |
void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) { |
115 |
SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel); |
SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel); |
116 |
} |
} |
117 |
|
|
118 |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) { |
119 |
// dereference old midi input port. |
if (!pDevice) throw LinuxSamplerException("No MIDI input device assigned."); |
120 |
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
|
121 |
|
// get old and new midi input port |
122 |
|
MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
123 |
|
MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort); |
124 |
|
|
125 |
// disconnect old device port |
// disconnect old device port |
126 |
if (pMidiInputPort && pEngineChannel) pMidiInputPort->Disconnect(pEngineChannel); |
if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel); |
127 |
// new device, port and channel |
// remember new device, port and channel if not engine channel yet created |
128 |
pMidiInputDevice = pDevice; |
if (!pEngineChannel) { |
129 |
this->midiPort = iMidiPort; |
this->pMidiInputDevice = pDevice; |
130 |
this->midiChannel = MidiChannel; |
this->iMidiPort = iMidiPort; |
131 |
|
this->midiChannel = MidiChannel; |
132 |
|
} |
133 |
|
|
134 |
// connect new device port |
// connect new device port |
135 |
pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel); |
|
if (pMidiInputPort && pEngineChannel) pMidiInputPort->Connect(pEngineChannel, MidiChannel); |
|
136 |
// Ooops. |
// Ooops. |
137 |
if (pMidiInputPort == NULL) |
if (pNewMidiInputPort == NULL) |
138 |
throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); |
throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); |
139 |
} |
} |
140 |
|
|
142 |
return pEngineChannel; |
return pEngineChannel; |
143 |
} |
} |
144 |
|
|
145 |
MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() { |
midi_chan_t SamplerChannel::GetMidiInputChannel() { |
146 |
|
if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel(); |
147 |
return this->midiChannel; |
return this->midiChannel; |
148 |
} |
} |
149 |
|
|
150 |
int SamplerChannel::GetMidiInputPort() { |
int SamplerChannel::GetMidiInputPort() { |
151 |
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL; |
152 |
return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1); |
if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber(); |
153 |
|
return iMidiPort; |
154 |
} |
} |
155 |
|
|
156 |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
158 |
} |
} |
159 |
|
|
160 |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
161 |
|
if (pEngineChannel) |
162 |
|
pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL; |
163 |
return pMidiInputDevice; |
return pMidiInputDevice; |
164 |
} |
} |
165 |
|
|
177 |
throw LinuxSamplerException("Internal error: SamplerChannel index not found"); |
throw LinuxSamplerException("Internal error: SamplerChannel index not found"); |
178 |
} |
} |
179 |
|
|
180 |
MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) { |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
181 |
MidiInputPort* pMidiInputPort = NULL; |
MidiInputPort* pMidiInputPort = NULL; |
182 |
|
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
183 |
if (pMidiInputDevice) |
if (pMidiInputDevice) |
184 |
pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); |
pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); |
185 |
return pMidiInputPort; |
return pMidiInputPort; |
206 |
if (!mSamplerChannels.size()) { |
if (!mSamplerChannels.size()) { |
207 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
208 |
mSamplerChannels[0] = pChannel; |
mSamplerChannels[0] = pChannel; |
209 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, 1)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); |
210 |
return pChannel; |
return pChannel; |
211 |
} |
} |
212 |
|
|
221 |
// we found an unused index, so insert the new channel there |
// we found an unused index, so insert the new channel there |
222 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
223 |
mSamplerChannels[i] = pChannel; |
mSamplerChannels[i] = pChannel; |
224 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, i)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); |
225 |
return pChannel; |
return pChannel; |
226 |
} |
} |
227 |
throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index."); |
throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index."); |
230 |
// we have not reached the index limit so we just add the channel past the highest index |
// we have not reached the index limit so we just add the channel past the highest index |
231 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
232 |
mSamplerChannels[lastIndex + 1] = pChannel; |
mSamplerChannels[lastIndex + 1] = pChannel; |
233 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, lastIndex + 1)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1)); |
234 |
return pChannel; |
return pChannel; |
235 |
} |
} |
236 |
|
|
248 |
if (iterChan->second == pSamplerChannel) { |
if (iterChan->second == pSamplerChannel) { |
249 |
mSamplerChannels.erase(iterChan); |
mSamplerChannels.erase(iterChan); |
250 |
delete pSamplerChannel; |
delete pSamplerChannel; |
251 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, mSamplerChannels.size())); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); |
252 |
return; |
return; |
253 |
} |
} |
254 |
} |
} |