MidiClient.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * MidiClient.h - base-class for MIDI clients like ALSA-sequencer-client
  3. *
  4. * Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef MIDI_CLIENT_H
  25. #define MIDI_CLIENT_H
  26. #include <QtCore/QStringList>
  27. #include <QtCore/QVector>
  28. #include "MidiEvent.h"
  29. #include "MidiEventProcessor.h"
  30. #include "TabWidget.h"
  31. class MidiPort;
  32. // base-class for all MIDI-clients
  33. class MidiClient
  34. {
  35. public:
  36. MidiClient();
  37. virtual ~MidiClient();
  38. // to be implemented by sub-classes
  39. virtual void processOutEvent( const MidiEvent & _me,
  40. const MidiTime & _time,
  41. const MidiPort * _port ) = 0;
  42. // inheriting classes can re-implement this for being able to update
  43. // their internal port-structures etc.
  44. virtual void applyPortMode( MidiPort * _port );
  45. virtual void applyPortName( MidiPort * _port );
  46. virtual void addPort( MidiPort * _port );
  47. // re-implemented methods HAVE to call removePort() of base-class!!
  48. virtual void removePort( MidiPort * _port );
  49. // returns whether client works with raw-MIDI, only needs to be
  50. // re-implemented by MidiClientRaw for returning true
  51. virtual bool isRaw() const
  52. {
  53. return false;
  54. }
  55. // if not raw-client, return all readable/writable ports
  56. virtual QStringList readablePorts() const
  57. {
  58. return QStringList();
  59. }
  60. virtual QStringList writablePorts() const
  61. {
  62. return QStringList();
  63. }
  64. // return name of port which specified MIDI event came from
  65. virtual QString sourcePortName( const MidiEvent & ) const
  66. {
  67. return QString();
  68. }
  69. // (un)subscribe given MidiPort to/from destination-port
  70. virtual void subscribeReadablePort( MidiPort * _port,
  71. const QString & _dest,
  72. bool _subscribe = true );
  73. virtual void subscribeWritablePort( MidiPort * _port,
  74. const QString & _dest,
  75. bool _subscribe = true );
  76. // qobject-derived classes can use this for make a slot being
  77. // connected to signal of non-raw-MIDI-client if port-lists change
  78. virtual void connectRPChanged( QObject *, const char * )
  79. {
  80. }
  81. virtual void connectWPChanged( QObject *, const char * )
  82. {
  83. }
  84. // tries to open either MIDI-driver from config-file or (if it fails)
  85. // any other working
  86. static MidiClient * openMidiClient();
  87. protected:
  88. QVector<MidiPort *> m_midiPorts;
  89. } ;
  90. const uint32_t RAW_MIDI_PARSE_BUF_SIZE = 16;
  91. class MidiClientRaw : public MidiClient
  92. {
  93. public:
  94. MidiClientRaw();
  95. virtual ~MidiClientRaw();
  96. // we are raw-clients for sure!
  97. bool isRaw() const override
  98. {
  99. return true;
  100. }
  101. protected:
  102. // generic raw-MIDI-parser which generates appropriate MIDI-events
  103. void parseData( const unsigned char c );
  104. // to be implemented by actual client-implementation
  105. virtual void sendByte( const unsigned char c ) = 0;
  106. private:
  107. // this does MIDI-event-process
  108. void processParsedEvent();
  109. void processOutEvent( const MidiEvent& event, const MidiTime& time, const MidiPort* port ) override;
  110. // small helper function returning length of a certain event - this
  111. // is necessary for parsing raw-MIDI-data
  112. static int eventLength( const unsigned char event );
  113. // data being used for parsing
  114. struct midiParserData
  115. {
  116. uint8_t m_status; // identifies the type of event, that
  117. // is currently received ('Noteon',
  118. // 'Pitch Bend' etc).
  119. uint8_t m_channel; // The channel of the event that is
  120. // received (in case of a channel event)
  121. uint32_t m_bytes; // How many bytes have been read for
  122. // the current event?
  123. uint32_t m_bytesTotal; // How many bytes does the current
  124. // event type include?
  125. uint32_t m_buffer[RAW_MIDI_PARSE_BUF_SIZE];
  126. // buffer for incoming data
  127. MidiEvent m_midiEvent; // midi-event
  128. } m_midiParseData;
  129. } ;
  130. #endif