2
0

platformRedBook.cpp 7.3 KB

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