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.

126 lines
3.0KB

  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. #include <sys/types.h>
  19. #include <stdio.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <iostream>
  23. #include <limits.h>
  24. #include <queue>
  25. #include <string>
  26. #ifndef MIDI
  27. #define MIDI
  28. using namespace std;
  29. //#define ALSA_MIDI
  30. #ifdef 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. static void SetDeviceName(string s) { m_DeviceName=s; }
  56. static MidiDevice *Get() { if(!m_Singleton) m_Singleton=new MidiDevice; return m_Singleton; }
  57. static void PackUpAndGoHome() { if(m_Singleton) delete m_Singleton; m_Singleton=NULL; }
  58. static void *MidiReaderCallback(void *o) { ((MidiDevice*)o)->CollectEvents(); return NULL; }
  59. MidiEvent GetEvent(int Device);
  60. void SendEvent(int Device,const MidiEvent &Event);
  61. void SetPoly(int s) { m_Poly=s; }
  62. private:
  63. MidiDevice();
  64. void Open();
  65. void Close();
  66. void CollectEvents();
  67. void AddEvent(unsigned char* midi);
  68. void ReadByte(unsigned char *c);
  69. int m_MidiFd;
  70. int m_MidiWrFd;
  71. static string m_DeviceName;
  72. int m_Poly;
  73. queue<MidiEvent> m_EventVec[16];
  74. static MidiDevice *m_Singleton;
  75. pthread_t m_MidiReader;
  76. pthread_mutex_t* m_Mutex;
  77. #ifdef ALSA_MIDI
  78. int AlsaCallback();
  79. snd_seq_t *seq_handle;
  80. snd_seq_t *AlsaOpen();
  81. #endif
  82. #if __APPLE__
  83. MIDIClientRef mMIDIClient;
  84. MIDIEndpointRef mMIDISource;
  85. MIDIEndpointRef mMIDIDestination;
  86. #define midi_ReadSize 4096
  87. unsigned char m_ReadBuffer[midi_ReadSize];
  88. volatile int m_ReadFillIndex;
  89. volatile int m_ReadReadIndex;
  90. void MidiDevice::AppleOpen();
  91. void MidiDevice::AppleClose();
  92. int MidiDevice::AppleWrite(int dummy, unsigned char *outbuffer, int maxlen);
  93. int MidiDevice::AppleRead(int dummy, unsigned char *outbuffer, int maxlen);
  94. static void MidiDevice::sMIDIRead(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon);
  95. #endif
  96. };
  97. #endif