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.

136 lines
3.4KB

  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 AlsaClose ();
  82. snd_seq_t *seq_handle;
  83. snd_seq_t *AlsaOpen(Type t);
  84. #endif
  85. #ifdef USE_OSS_MIDI
  86. static void *MidiReaderCallback (void *o) { ((MidiDevice*)o)->OssCollectEvents(); return NULL; }
  87. void OssCollectEvents();
  88. void OssAddEvent(unsigned char* midi);
  89. void OssReadByte(unsigned char *c);
  90. void OssClose();
  91. bool OssOpen();
  92. static string m_DeviceName;
  93. int m_MidiFd, m_MidiWrFd;
  94. #endif
  95. #if __APPLE__
  96. MIDIClientRef mMIDIClient;
  97. MIDIEndpointRef mMIDISource;
  98. MIDIEndpointRef mMIDIDestination;
  99. #define midi_ReadSize 4096
  100. unsigned char m_ReadBuffer[midi_ReadSize];
  101. volatile int m_ReadFillIndex;
  102. volatile int m_ReadReadIndex;
  103. void MidiDevice::AppleOpen();
  104. void MidiDevice::AppleClose();
  105. int MidiDevice::AppleWrite(int dummy, unsigned char *outbuffer, int maxlen);
  106. int MidiDevice::AppleRead(int dummy, unsigned char *outbuffer, int maxlen);
  107. static void MidiDevice::sMIDIRead(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon);
  108. #endif
  109. };
  110. #endif