platformRedBook.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/strings/stringFunctions.h"
  23. #include "console/console.h"
  24. #include "console/engineAPI.h"
  25. #include "platform/platformRedBook.h"
  26. //------------------------------------------------------------------------------
  27. // Class: RedBookDevice
  28. //------------------------------------------------------------------------------
  29. RedBookDevice::RedBookDevice()
  30. {
  31. mAcquired = false;
  32. mDeviceName = 0;
  33. }
  34. RedBookDevice::~RedBookDevice()
  35. {
  36. delete [] mDeviceName;
  37. }
  38. //------------------------------------------------------------------------------
  39. // Class: RedBook
  40. //------------------------------------------------------------------------------
  41. Vector<RedBookDevice *> RedBook::smDeviceList(__FILE__, __LINE__);
  42. RedBookDevice * RedBook::smCurrentDevice;
  43. char RedBook::smLastError[1024];
  44. //------------------------------------------------------------------------------
  45. void RedBook::init()
  46. {
  47. }
  48. void RedBook::destroy()
  49. {
  50. close();
  51. for( Vector<RedBookDevice*>::iterator i = smDeviceList.begin( ); i != smDeviceList.end( ); i++ ) {
  52. delete *i;
  53. }
  54. smDeviceList.clear( );
  55. }
  56. //------------------------------------------------------------------------------
  57. void RedBook::installDevice(RedBookDevice * device)
  58. {
  59. smDeviceList.push_back(device);
  60. }
  61. RedBookDevice * RedBook::getCurrentDevice()
  62. {
  63. return(smCurrentDevice);
  64. }
  65. U32 RedBook::getDeviceCount()
  66. {
  67. return(smDeviceList.size());
  68. }
  69. const char * RedBook::getDeviceName(U32 idx)
  70. {
  71. if(idx >= getDeviceCount())
  72. {
  73. setLastError("Invalid device index");
  74. return("");
  75. }
  76. return(smDeviceList[idx]->mDeviceName);
  77. }
  78. void RedBook::setLastError(const char * error)
  79. {
  80. if(!error || dStrlen(error) >= sizeof(smLastError))
  81. setLastError("Invalid error string passed");
  82. else
  83. dStrcpy(smLastError, error);
  84. }
  85. const char * RedBook::getLastError()
  86. {
  87. return(smLastError);
  88. }
  89. void RedBook::handleCallback(U32 type)
  90. {
  91. switch(type)
  92. {
  93. case PlayFinished:
  94. Con::executef("RedBookCallback", "PlayFinished");
  95. break;
  96. }
  97. }
  98. //------------------------------------------------------------------------------
  99. bool RedBook::open(const char * deviceName)
  100. {
  101. if(!deviceName)
  102. {
  103. setLastError("Invalid device name");
  104. return(false);
  105. }
  106. for(U32 i = 0; i < smDeviceList.size(); i++)
  107. if(!dStricmp(deviceName, smDeviceList[i]->mDeviceName))
  108. return(open(smDeviceList[i]));
  109. setLastError("Failed to find device");
  110. return(false);
  111. }
  112. bool RedBook::open(RedBookDevice * device)
  113. {
  114. if(!device)
  115. {
  116. setLastError("Invalid device passed");
  117. return(false);
  118. }
  119. close();
  120. smCurrentDevice = device;
  121. return(smCurrentDevice->open());
  122. }
  123. bool RedBook::close()
  124. {
  125. if(smCurrentDevice)
  126. {
  127. bool ret = smCurrentDevice->close();
  128. smCurrentDevice = 0;
  129. return(ret);
  130. }
  131. setLastError("No device is currently open");
  132. return(false);
  133. }
  134. bool RedBook::play(U32 track)
  135. {
  136. if(!smCurrentDevice)
  137. {
  138. setLastError("No device is currently open");
  139. return(false);
  140. }
  141. return(smCurrentDevice->play(track));
  142. }
  143. bool RedBook::stop()
  144. {
  145. if(!smCurrentDevice)
  146. {
  147. setLastError("No device is currently open");
  148. return(false);
  149. }
  150. return(smCurrentDevice->stop());
  151. }
  152. bool RedBook::getTrackCount(U32 * trackCount)
  153. {
  154. if(!smCurrentDevice)
  155. {
  156. setLastError("No device is currently open");
  157. return(false);
  158. }
  159. return(smCurrentDevice->getTrackCount(trackCount));
  160. }
  161. bool RedBook::getVolume(F32 * volume)
  162. {
  163. if(!smCurrentDevice)
  164. {
  165. setLastError("No device is currently open");
  166. return(false);
  167. }
  168. return(smCurrentDevice->getVolume(volume));
  169. }
  170. bool RedBook::setVolume(F32 volume)
  171. {
  172. if(!smCurrentDevice)
  173. {
  174. setLastError("No device is currently open");
  175. return(false);
  176. }
  177. return(smCurrentDevice->setVolume(volume));
  178. }
  179. //------------------------------------------------------------------------------
  180. // console methods
  181. //------------------------------------------------------------------------------
  182. ConsoleFunctionGroupBegin( Redbook, "Control functions for Redbook audio (ie, CD audio).");
  183. DefineConsoleFunction( redbookOpen, bool, (const char * device), (""), "(string device=NULL)"
  184. "@brief Deprecated\n\n"
  185. "@internal")
  186. {
  187. if(dStrcmp(device,"")==0)
  188. return(RedBook::open(RedBook::getDeviceName(0)));
  189. else
  190. return(RedBook::open(device));
  191. }
  192. DefineConsoleFunction( redbookClose, bool, (), , "Close the current Redbook device."
  193. "@brief Deprecated\n\n"
  194. "@internal")
  195. {
  196. return(RedBook::close());
  197. }
  198. DefineConsoleFunction( redbookPlay, bool, (S32 track), , "(int track) Play the selected track."
  199. "@brief Deprecated\n\n"
  200. "@internal")
  201. {
  202. return(RedBook::play(track));
  203. }
  204. DefineConsoleFunction( redbookStop, bool, (), , "Stop playing."
  205. "@brief Deprecated\n\n"
  206. "@internal")
  207. {
  208. return(RedBook::stop());
  209. }
  210. DefineConsoleFunction( redbookGetTrackCount, S32, (), , "Return the number of tracks."
  211. "@brief Deprecated\n\n"
  212. "@internal")
  213. {
  214. U32 trackCount;
  215. if(!RedBook::getTrackCount(&trackCount))
  216. return(0);
  217. return(trackCount);
  218. }
  219. DefineConsoleFunction( redbookGetVolume, F32, (), , "Get the volume."
  220. "@brief Deprecated\n\n"
  221. "@internal")
  222. {
  223. F32 vol;
  224. if(!RedBook::getVolume(&vol))
  225. return(0.f);
  226. else
  227. return(vol);
  228. }
  229. DefineConsoleFunction( redbookSetVolume, bool, (F32 volume), , "(float volume) Set playback volume."
  230. "@brief Deprecated\n\n"
  231. "@internal")
  232. {
  233. return(RedBook::setVolume(volume));
  234. }
  235. DefineConsoleFunction( redbookGetDeviceCount, S32, (), , "get the number of redbook devices."
  236. "@brief Deprecated\n\n"
  237. "@internal")
  238. {
  239. return(RedBook::getDeviceCount());
  240. }
  241. DefineConsoleFunction( redbookGetDeviceName, const char *, (S32 index), , "(int index) Get name of specified Redbook device."
  242. "@brief Deprecated\n\n"
  243. "@internal")
  244. {
  245. return(RedBook::getDeviceName(index));
  246. }
  247. DefineConsoleFunction( redbookGetLastError, const char *, (), , "Get a string explaining the last redbook error."
  248. "@brief Deprecated\n\n"
  249. "@internal")
  250. {
  251. return(RedBook::getLastError());
  252. }
  253. ConsoleFunctionGroupEnd( Redbook );