You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.7KB

  1. /* SpiralSynth
  2. * Copyleft (C) 2000 David Griffiths <dave@pawfal.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #ifndef MIDI
  19. #define MIDI
  20. #include <sys/types.h>
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <iostream>
  25. #include <limits.h>
  26. #include <queue>
  27. #include <string>
  28. #include "../config.h"
  29. using namespace std;
  30. #ifdef USE_ALSA_MIDI
  31. #include <alsa/asoundlib.h>
  32. #endif
  33. #if __APPLE__
  34. #include <CoreMIDI/MIDIServices.h>
  35. #endif
  36. class MidiEvent
  37. {
  38. public:
  39. enum type{NONE,ON,OFF,AFTERTOUCH,PARAMETER,CHANNELPRESSURE,PITCHBEND};
  40. MidiEvent() {m_Type=NONE;}
  41. MidiEvent(type t, int note, float v)
  42. {m_Type=t; m_Note=note; m_Volume=v;}
  43. type GetType() const {return m_Type;}
  44. float GetVolume() const {return m_Volume;}
  45. int GetNote() const {return m_Note;}
  46. private:
  47. float m_Volume;
  48. type m_Type;
  49. int m_Note;
  50. };
  51. class MidiDevice
  52. {
  53. public:
  54. ~MidiDevice();
  55. enum Type{READ,WRITE};
  56. static void Init(const string &name, Type t);
  57. static void SetDeviceName(string s) {
  58. #ifdef USE_OSS_MIDI
  59. m_DeviceName=s;
  60. #endif
  61. }
  62. static MidiDevice *Get() { return m_Singleton; }
  63. static void PackUpAndGoHome() { if (m_Singleton) delete m_Singleton; m_Singleton=NULL; }
  64. MidiEvent GetEvent(int Device);
  65. void SendEvent(int Device,const MidiEvent &Event);
  66. void SetPoly(int s) { m_Poly=s; }
  67. float GetClock() { return m_Clock; }
  68. private:
  69. MidiDevice(Type t);
  70. int m_Poly;
  71. float m_Clock;
  72. int m_ClockCount;
  73. queue<MidiEvent> m_EventVec[16];
  74. static MidiDevice *m_Singleton;
  75. pthread_t m_MidiReader;
  76. pthread_mutex_t* m_Mutex;
  77. static string m_AppName;
  78. #ifdef USE_ALSA_MIDI
  79. static void *MidiReaderCallback (void *o) { ((MidiDevice*)o)->AlsaCollectEvents(); return NULL; }
  80. void AlsaCollectEvents();
  81. void AlsaSendEvent(int Device, const MidiEvent &Event);
  82. void AlsaClose ();
  83. //snd_seq_t *seq_handle;
  84. //I appears that ALsa does not support both a read and write handle
  85. //so we must have two handle one for each mode
  86. snd_seq_t *seq_rhandle;
  87. snd_seq_t *seq_whandle;
  88. void AlsaOpen();
  89. #endif
  90. #ifdef USE_OSS_MIDI
  91. static void *MidiReaderCallback (void *o) { ((MidiDevice*)o)->OssCollectEvents(); return NULL; }
  92. void OssCollectEvents();
  93. void OssAddEvent(unsigned char* midi);
  94. void OssReadByte(unsigned char *c);
  95. void OssClose();
  96. bool OssOpen();
  97. static string m_DeviceName;
  98. int m_MidiFd, m_MidiWrFd;
  99. #endif
  100. #if __APPLE__
  101. MIDIClientRef mMIDIClient;
  102. MIDIEndpointRef mMIDISource;
  103. MIDIEndpointRef mMIDIDestination;
  104. #define midi_ReadSize 4096
  105. unsigned char m_ReadBuffer[midi_ReadSize];
  106. volatile int m_ReadFillIndex;
  107. volatile int m_ReadReadIndex;
  108. void MidiDevice::AppleOpen();
  109. void MidiDevice::AppleClose();
  110. int MidiDevice::AppleWrite(int dummy, unsigned char *outbuffer, int maxlen);
  111. int MidiDevice::AppleRead(int dummy, unsigned char *outbuffer, int maxlen);
  112. static void MidiDevice::sMIDIRead(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon);
  113. #endif
  114. };
  115. #endif