24 |
#include "EngineChannel.h" |
#include "EngineChannel.h" |
25 |
|
|
26 |
#include "../../common/global_private.h" |
#include "../../common/global_private.h" |
27 |
|
#include "../../Sampler.h" |
28 |
|
|
29 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
30 |
|
|
31 |
EngineChannel::EngineChannel() : |
EngineChannel::EngineChannel() : |
32 |
InstrumentChangeCommandReader(InstrumentChangeCommand) { |
InstrumentChangeCommandReader(InstrumentChangeCommand), |
33 |
|
virtualMidiDevicesReader_AudioThread(virtualMidiDevices), |
34 |
|
virtualMidiDevicesReader_MidiThread(virtualMidiDevices) |
35 |
|
{ |
36 |
pMIDIKeyInfo = new midi_key_info_t[128]; |
pMIDIKeyInfo = new midi_key_info_t[128]; |
37 |
pEngine = NULL; |
pEngine = NULL; |
38 |
pInstrument = NULL; |
pInstrument = NULL; |
64 |
|
|
65 |
EngineChannel::~EngineChannel() { |
EngineChannel::~EngineChannel() { |
66 |
DisconnectAudioOutputDevice(); |
DisconnectAudioOutputDevice(); |
67 |
|
|
68 |
|
// In case the channel was removed before the instrument was |
69 |
|
// fully loaded, try to give back instrument again (see bug #113) |
70 |
|
instrument_change_command_t& cmd = ChangeInstrument(NULL); |
71 |
|
if (cmd.pInstrument) { |
72 |
|
Engine::instruments.HandBack(cmd.pInstrument, this); |
73 |
|
} |
74 |
|
/////// |
75 |
|
|
76 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
77 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
78 |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
339 |
// release the currently loaded instrument |
// release the currently loaded instrument |
340 |
Engine::instruments.HandBackInstrument(cmd.pInstrument, this, d); |
Engine::instruments.HandBackInstrument(cmd.pInstrument, this, d); |
341 |
} |
} |
342 |
|
|
343 |
if (d) delete d; |
if (d) delete d; |
344 |
|
|
345 |
// release all active dimension regions to resource |
// release all active dimension regions to resource |
462 |
} |
} |
463 |
fxSends.push_back(pFxSend); |
fxSends.push_back(pFxSend); |
464 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
465 |
fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount()); |
fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount()); |
466 |
|
|
467 |
return pFxSend; |
return pFxSend; |
468 |
} |
} |
501 |
} |
} |
502 |
} |
} |
503 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
504 |
fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount()); |
fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount()); |
505 |
} |
} |
506 |
|
|
507 |
/** |
/** |
522 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
523 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
524 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
525 |
|
// inform connected virtual MIDI devices if any ... |
526 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
527 |
|
ArrayList<VirtualMidiDevice*>& devices = |
528 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
529 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
530 |
|
); |
531 |
|
for (int i = 0; i < devices.size(); i++) { |
532 |
|
devices[i]->SendNoteOnToDevice(Key, Velocity); |
533 |
|
} |
534 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
535 |
} |
} |
536 |
} |
} |
537 |
|
|
558 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
559 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
560 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
561 |
|
// inform connected virtual MIDI devices if any ... |
562 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
563 |
|
ArrayList<VirtualMidiDevice*>& devices = |
564 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
565 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
566 |
|
); |
567 |
|
for (int i = 0; i < devices.size(); i++) { |
568 |
|
devices[i]->SendNoteOnToDevice(Key, Velocity); |
569 |
|
} |
570 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
571 |
} |
} |
572 |
} |
} |
573 |
|
|
589 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
590 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
591 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
592 |
|
// inform connected virtual MIDI devices if any ... |
593 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
594 |
|
ArrayList<VirtualMidiDevice*>& devices = |
595 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
596 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
597 |
|
); |
598 |
|
for (int i = 0; i < devices.size(); i++) { |
599 |
|
devices[i]->SendNoteOffToDevice(Key, Velocity); |
600 |
|
} |
601 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
602 |
} |
} |
603 |
} |
} |
604 |
|
|
625 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
626 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
627 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
628 |
|
// inform connected virtual MIDI devices if any ... |
629 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
630 |
|
ArrayList<VirtualMidiDevice*>& devices = |
631 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
632 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
633 |
|
); |
634 |
|
for (int i = 0; i < devices.size(); i++) { |
635 |
|
devices[i]->SendNoteOffToDevice(Key, Velocity); |
636 |
|
} |
637 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
638 |
} |
} |
639 |
} |
} |
640 |
|
|
748 |
MidiVolume = 1.0; |
MidiVolume = 1.0; |
749 |
GlobalPanLeft = 1.0f; |
GlobalPanLeft = 1.0f; |
750 |
GlobalPanRight = 1.0f; |
GlobalPanRight = 1.0f; |
751 |
|
iLastPanRequest = 64; |
752 |
GlobalTranspose = 0; |
GlobalTranspose = 0; |
753 |
// set all MIDI controller values to zero |
// set all MIDI controller values to zero |
754 |
memset(ControllerTable, 0x00, 129); |
memset(ControllerTable, 0x00, 129); |
775 |
* current audio cycle |
* current audio cycle |
776 |
*/ |
*/ |
777 |
void EngineChannel::ImportEvents(uint Samples) { |
void EngineChannel::ImportEvents(uint Samples) { |
778 |
|
// import events from pure software MIDI "devices" |
779 |
|
// (e.g. virtual keyboard in instrument editor) |
780 |
|
{ |
781 |
|
const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices |
782 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
783 |
|
VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device |
784 |
|
// as we're going to (carefully) write some status to the |
785 |
|
// synchronized struct, we cast away the const |
786 |
|
ArrayList<VirtualMidiDevice*>& devices = |
787 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>(virtualMidiDevicesReader_AudioThread.Lock()); |
788 |
|
// iterate through all virtual MIDI devices |
789 |
|
for (int i = 0; i < devices.size(); i++) { |
790 |
|
VirtualMidiDevice* pDev = devices[i]; |
791 |
|
// I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-) |
792 |
|
while (pDev->GetMidiEventFromDevice(devEvent)) { |
793 |
|
event.Type = |
794 |
|
(devEvent.Type == VirtualMidiDevice::EVENT_TYPE_NOTEON) ? |
795 |
|
Event::type_note_on : Event::type_note_off; |
796 |
|
event.Param.Note.Key = devEvent.Key; |
797 |
|
event.Param.Note.Velocity = devEvent.Velocity; |
798 |
|
event.pEngineChannel = this; |
799 |
|
// copy event to internal event list |
800 |
|
if (pEvents->poolIsEmpty()) { |
801 |
|
dmsg(1,("Event pool emtpy!\n")); |
802 |
|
goto exitVirtualDevicesLoop; |
803 |
|
} |
804 |
|
*pEvents->allocAppend() = event; |
805 |
|
} |
806 |
|
} |
807 |
|
} |
808 |
|
exitVirtualDevicesLoop: |
809 |
|
virtualMidiDevicesReader_AudioThread.Unlock(); |
810 |
|
|
811 |
|
// import events from the regular MIDI devices |
812 |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
813 |
Event* pEvent; |
Event* pEvent; |
814 |
while (true) { |
while (true) { |
854 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
855 |
} |
} |
856 |
|
|
857 |
|
void EngineChannel::Connect(VirtualMidiDevice* pDevice) { |
858 |
|
// double buffer ... double work ... |
859 |
|
{ |
860 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate(); |
861 |
|
devices.add(pDevice); |
862 |
|
} |
863 |
|
{ |
864 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig(); |
865 |
|
devices.add(pDevice); |
866 |
|
} |
867 |
|
} |
868 |
|
|
869 |
|
void EngineChannel::Disconnect(VirtualMidiDevice* pDevice) { |
870 |
|
// double buffer ... double work ... |
871 |
|
{ |
872 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate(); |
873 |
|
devices.remove(pDevice); |
874 |
|
} |
875 |
|
{ |
876 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig(); |
877 |
|
devices.remove(pDevice); |
878 |
|
} |
879 |
|
} |
880 |
|
|
881 |
float EngineChannel::Volume() { |
float EngineChannel::Volume() { |
882 |
return GlobalVolume; |
return GlobalVolume; |
883 |
} |
} |
887 |
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
888 |
} |
} |
889 |
|
|
890 |
|
float EngineChannel::Pan() { |
891 |
|
return float(iLastPanRequest - 64) / 64.0f; |
892 |
|
} |
893 |
|
|
894 |
|
void EngineChannel::Pan(float f) { |
895 |
|
int iMidiPan = int(f * 64.0f) + 64; |
896 |
|
if (iMidiPan > 127) iMidiPan = 127; |
897 |
|
else if (iMidiPan < 0) iMidiPan = 0; |
898 |
|
GlobalPanLeft = Engine::PanCurve[128 - iMidiPan]; |
899 |
|
GlobalPanRight = Engine::PanCurve[iMidiPan]; |
900 |
|
iLastPanRequest = iMidiPan; |
901 |
|
} |
902 |
|
|
903 |
uint EngineChannel::Channels() { |
uint EngineChannel::Channels() { |
904 |
return 2; |
return 2; |
905 |
} |
} |