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.

101 lines
2.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. #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 KEYBOARD_SUPPORT
  30. class MidiEvent
  31. {
  32. public:
  33. enum type{NONE,ON,OFF,AFTERTOUCH,PARAMETER,CHANNELPRESSURE,PITCHBEND};
  34. MidiEvent() {m_Type=NONE;}
  35. MidiEvent(type t, int note, float v)
  36. {m_Type=t; m_Note=note; m_Volume=v;}
  37. type GetType() const {return m_Type;}
  38. float GetVolume() const {return m_Volume;}
  39. int GetNote() const {return m_Note;}
  40. private:
  41. float m_Volume;
  42. type m_Type;
  43. int m_Note;
  44. };
  45. class MidiDevice
  46. {
  47. public:
  48. ~MidiDevice();
  49. static void SetDeviceName(string s) { m_DeviceName=s; }
  50. static MidiDevice *Get() { if(!m_Singleton) m_Singleton=new MidiDevice; return m_Singleton; }
  51. static void PackUpAndGoHome() { if(m_Singleton) delete m_Singleton; m_Singleton=NULL; }
  52. static void *MidiReaderCallback(void *o) { ((MidiDevice*)o)->CollectEvents(); return NULL; }
  53. MidiEvent GetEvent(int Device);
  54. void SendEvent(int Device,const MidiEvent &Event);
  55. void SetPoly(int s) { m_Poly=s; }
  56. private:
  57. MidiDevice();
  58. void Open();
  59. void Close();
  60. void CollectEvents();
  61. void AddEvent(unsigned char* midi);
  62. void ReadByte(unsigned char *c);
  63. int m_MidiFd;
  64. int m_MidiWrFd;
  65. static string m_DeviceName;
  66. int m_Poly;
  67. queue<MidiEvent> m_EventVec[16];
  68. static MidiDevice *m_Singleton;
  69. pthread_t m_MidiReader;
  70. pthread_mutex_t* m_Mutex;
  71. #ifdef KEYBOARD_SUPPORT
  72. void CheckKeyboard();
  73. char m_KeyVoice[256];
  74. int m_Oct;
  75. int m_CurrentVoice;
  76. #endif
  77. };
  78. #endif