platformRedBook.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "platform/platformRedBook.h"
  25. //------------------------------------------------------------------------------
  26. // Class: RedBookDevice
  27. //------------------------------------------------------------------------------
  28. RedBookDevice::RedBookDevice()
  29. {
  30. mAcquired = false;
  31. mDeviceName = 0;
  32. }
  33. RedBookDevice::~RedBookDevice()
  34. {
  35. delete [] mDeviceName;
  36. }
  37. //------------------------------------------------------------------------------
  38. // Class: RedBook
  39. //------------------------------------------------------------------------------
  40. Vector<RedBookDevice *> RedBook::smDeviceList(__FILE__, __LINE__);
  41. RedBookDevice * RedBook::smCurrentDevice;
  42. char RedBook::smLastError[1024];
  43. //------------------------------------------------------------------------------
  44. void RedBook::init()
  45. {
  46. }
  47. void RedBook::destroy()
  48. {
  49. close();
  50. for( Vector<RedBookDevice*>::iterator i = smDeviceList.begin( ); i != smDeviceList.end( ); i++ ) {
  51. delete *i;
  52. }
  53. smDeviceList.clear( );
  54. }
  55. //------------------------------------------------------------------------------
  56. void RedBook::installDevice(RedBookDevice * device)
  57. {
  58. smDeviceList.push_back(device);
  59. }
  60. RedBookDevice * RedBook::getCurrentDevice()
  61. {
  62. return(smCurrentDevice);
  63. }
  64. U32 RedBook::getDeviceCount()
  65. {
  66. return(smDeviceList.size());
  67. }
  68. const char * RedBook::getDeviceName(U32 idx)
  69. {
  70. if(idx >= getDeviceCount())
  71. {
  72. setLastError("Invalid device index");
  73. return("");
  74. }
  75. return(smDeviceList[idx]->mDeviceName);
  76. }
  77. void RedBook::setLastError(const char * error)
  78. {
  79. if(!error || dStrlen(error) >= sizeof(smLastError))
  80. setLastError("Invalid error string passed");
  81. else
  82. dStrcpy(smLastError, error);
  83. }
  84. const char * RedBook::getLastError()
  85. {
  86. return(smLastError);
  87. }
  88. void RedBook::handleCallback(U32 type)
  89. {
  90. switch(type)
  91. {
  92. case PlayFinished:
  93. Con::executef("RedBookCallback", "PlayFinished");
  94. break;
  95. }
  96. }
  97. //------------------------------------------------------------------------------
  98. bool RedBook::open(const char * deviceName)
  99. {
  100. if(!deviceName)
  101. {
  102. setLastError("Invalid device name");
  103. return(false);
  104. }
  105. for(U32 i = 0; i < smDeviceList.size(); i++)
  106. if(!dStricmp(deviceName, smDeviceList[i]->mDeviceName))
  107. return(open(smDeviceList[i]));
  108. setLastError("Failed to find device");
  109. return(false);
  110. }
  111. bool RedBook::open(RedBookDevice * device)
  112. {
  113. if(!device)
  114. {
  115. setLastError("Invalid device passed");
  116. return(false);
  117. }
  118. close();
  119. smCurrentDevice = device;
  120. return(smCurrentDevice->open());
  121. }
  122. bool RedBook::close()
  123. {
  124. if(smCurrentDevice)
  125. {
  126. bool ret = smCurrentDevice->close();
  127. smCurrentDevice = 0;
  128. return(ret);
  129. }
  130. setLastError("No device is currently open");
  131. return(false);
  132. }
  133. bool RedBook::play(U32 track)
  134. {
  135. if(!smCurrentDevice)
  136. {
  137. setLastError("No device is currently open");
  138. return(false);
  139. }
  140. return(smCurrentDevice->play(track));
  141. }
  142. bool RedBook::stop()
  143. {
  144. if(!smCurrentDevice)
  145. {
  146. setLastError("No device is currently open");
  147. return(false);
  148. }
  149. return(smCurrentDevice->stop());
  150. }
  151. bool RedBook::getTrackCount(U32 * trackCount)
  152. {
  153. if(!smCurrentDevice)
  154. {
  155. setLastError("No device is currently open");
  156. return(false);
  157. }
  158. return(smCurrentDevice->getTrackCount(trackCount));
  159. }
  160. bool RedBook::getVolume(F32 * volume)
  161. {
  162. if(!smCurrentDevice)
  163. {
  164. setLastError("No device is currently open");
  165. return(false);
  166. }
  167. return(smCurrentDevice->getVolume(volume));
  168. }
  169. bool RedBook::setVolume(F32 volume)
  170. {
  171. if(!smCurrentDevice)
  172. {
  173. setLastError("No device is currently open");
  174. return(false);
  175. }
  176. return(smCurrentDevice->setVolume(volume));
  177. }
  178. //------------------------------------------------------------------------------
  179. // console methods
  180. //------------------------------------------------------------------------------
  181. ConsoleFunctionGroupBegin( Redbook, "Control functions for Redbook audio (ie, CD audio).");
  182. ConsoleFunction(redbookOpen, bool, 1, 2, "(string device=NULL)"
  183. "@brief Deprecated\n\n"
  184. "@internal")
  185. {
  186. if(argc == 1)
  187. return(RedBook::open(RedBook::getDeviceName(0)));
  188. else
  189. return(RedBook::open(argv[1]));
  190. }
  191. ConsoleFunction(redbookClose, bool, 1, 1, "Close the current Redbook device."
  192. "@brief Deprecated\n\n"
  193. "@internal")
  194. {
  195. return(RedBook::close());
  196. }
  197. ConsoleFunction( redbookPlay, bool, 2, 2, "(int track) Play the selected track."
  198. "@brief Deprecated\n\n"
  199. "@internal")
  200. {
  201. return(RedBook::play(dAtoi(argv[1])));
  202. }
  203. ConsoleFunction( redbookStop, bool, 1, 1, "Stop playing."
  204. "@brief Deprecated\n\n"
  205. "@internal")
  206. {
  207. return(RedBook::stop());
  208. }
  209. ConsoleFunction(redbookGetTrackCount, S32, 1, 1, "Return the number of tracks."
  210. "@brief Deprecated\n\n"
  211. "@internal")
  212. {
  213. U32 trackCount;
  214. if(!RedBook::getTrackCount(&trackCount))
  215. return(0);
  216. return(trackCount);
  217. }
  218. ConsoleFunction(redbookGetVolume, F32, 1, 1, "Get the volume."
  219. "@brief Deprecated\n\n"
  220. "@internal")
  221. {
  222. F32 vol;
  223. if(!RedBook::getVolume(&vol))
  224. return(0.f);
  225. else
  226. return(vol);
  227. }
  228. ConsoleFunction(redbookSetVolume, bool, 2, 2, "(float volume) Set playback volume."
  229. "@brief Deprecated\n\n"
  230. "@internal")
  231. {
  232. return(RedBook::setVolume(dAtof(argv[1])));
  233. }
  234. ConsoleFunction( redbookGetDeviceCount, S32, 1, 1, "get the number of redbook devices."
  235. "@brief Deprecated\n\n"
  236. "@internal")
  237. {
  238. return(RedBook::getDeviceCount());
  239. }
  240. ConsoleFunction( redbookGetDeviceName, const char *, 2, 2, "(int index) Get name of specified Redbook device."
  241. "@brief Deprecated\n\n"
  242. "@internal")
  243. {
  244. return(RedBook::getDeviceName(dAtoi(argv[1])));
  245. }
  246. ConsoleFunction( redbookGetLastError, const char*, 1, 1, "Get a string explaining the last redbook error."
  247. "@brief Deprecated\n\n"
  248. "@internal")
  249. {
  250. return(RedBook::getLastError());
  251. }
  252. ConsoleFunctionGroupEnd( Redbook );