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.

139 lines
5.5KB

  1. /* SpiralSound
  2. * Copyleft (C) 2002 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 CHANNEL_HANDLER
  19. #define CHANNEL_HANDLER
  20. #include <pthread.h>
  21. #include <string>
  22. #include <map>
  23. #include <iostream>
  24. class ChannelHandler
  25. {
  26. public:
  27. enum Type{INPUT,OUTPUT,OUTPUT_REQUEST};
  28. ChannelHandler();
  29. ~ChannelHandler();
  30. // only call these from the audio thread
  31. // don't call Register*() when the audio thread is running
  32. void RegisterData(const std::string &ID, Type t, void *pData, int size);
  33. void Register(const std::string &ID, bool* pData, Type t=ChannelHandler::INPUT)
  34. { RegisterData(ID, t,(bool*)pData,sizeof(bool)); }
  35. void Register(const std::string &ID, char* pData, Type t=ChannelHandler::INPUT)
  36. { RegisterData(ID, t,(char*)pData,sizeof(char)); }
  37. void Register(const std::string &ID, int* pData, Type t=ChannelHandler::INPUT)
  38. { RegisterData(ID, t,(int*)pData,sizeof(int)); }
  39. void Register(const std::string &ID, long* pData, Type t=ChannelHandler::INPUT)
  40. { RegisterData(ID, t,(long*)pData,sizeof(long)); }
  41. void Register(const std::string &ID, short* pData, Type t=ChannelHandler::INPUT)
  42. { RegisterData(ID, t,(short*)pData,sizeof(short)); }
  43. void Register(const std::string &ID, float* pData, Type t=ChannelHandler::INPUT)
  44. { RegisterData(ID, t,(float*)pData,sizeof(float)); }
  45. void Register(const std::string &ID, double* pData, Type t=ChannelHandler::INPUT)
  46. { RegisterData(ID, t,(double*)pData,sizeof(double)); }
  47. void UpdateDataNow();
  48. bool IsCommandWaiting() { return m_Command[0]; }
  49. char GetCommand() { return m_Command[0]; }
  50. // use bulk transfers to copy variable and large sized data to the gui
  51. void SetupBulkTransfer(void *source) { m_BulkSrc=source; }
  52. // only call these from the gui thread
  53. // the ID comes from the order the channel is registered in
  54. void GetData(const std::string &ID, void *data);
  55. const bool GetBool(const std::string &ID) { bool t; GetData(ID,&t); return t; }
  56. const char GetChar(const std::string &ID) { char t; GetData(ID,&t); return t; }
  57. const int GetInt(const std::string &ID) { int t; GetData(ID,&t); return t; }
  58. const long GetLong(const std::string &ID) { long t; GetData(ID,&t); return t; }
  59. const short GetShort(const std::string &ID) { short t; GetData(ID,&t); return t; }
  60. const float GetFloat(const std::string &ID) { float t; GetData(ID,&t); return t; }
  61. const double GetDouble(const std::string &ID) { double t; GetData(ID,&t); return t; }
  62. void SetData(const std::string &ID, void *s);
  63. void Set(const std::string &ID, const bool& s) { SetData(ID,(void*)&s); }
  64. void Set(const std::string &ID, const char& s) { SetData(ID,(void*)&s); }
  65. void Set(const std::string &ID, const int& s) { SetData(ID,(void*)&s); }
  66. void Set(const std::string &ID, const long& s) { SetData(ID,(void*)&s); }
  67. void Set(const std::string &ID, const short& s) { SetData(ID,(void*)&s); }
  68. void Set(const std::string &ID, const float& s) { SetData(ID,(void*)&s); }
  69. void Set(const std::string &ID, const double& s) { SetData(ID,(void*)&s); }
  70. void ReplaceData(const std::string &ID, void *pData, int size);
  71. void SetCommand(char command);
  72. // initialises the data from the audio side to the internal buffers
  73. void FlushChannels();
  74. ////////////////////////////////////////////
  75. // This is useful for getting data from the audio side immediately,
  76. // as it will block until complete, and for larger amounts of data, as
  77. // it won't be doing the copy every update.
  78. void RequestChannelAndWait(const std::string &ID);
  79. ///////////////////////////////////////////
  80. // Use for copying large variable sized data very infrequently.
  81. // uses an OUTPUT_REQUEST channel as a transfer buffer.
  82. // Send the size needed to the gui down another channel.
  83. // This can be a slowww function, as it can take many audio updates
  84. // to complete, depending on the size of the buffer channel and the
  85. // total size of the transfer.
  86. void BulkTransfer(const std::string &ID, void *dest, int size);
  87. // delays for one or more updates. use for syncing commands
  88. void Wait();
  89. private:
  90. class Channel
  91. {
  92. public:
  93. Channel(Type t) { type=t; }
  94. Type type;
  95. void *data_buf;
  96. int size;
  97. void *data;
  98. bool requested;
  99. bool updated;
  100. };
  101. std::map<std::string,Channel*> m_ChannelMap;
  102. char m_Command[2];
  103. bool m_UpdateIndicator;
  104. void *m_BulkSrc;
  105. int m_BulkSize;
  106. int m_BulkPos;
  107. std::string m_BulkID;
  108. pthread_mutex_t* m_Mutex;
  109. };
  110. #endif