home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / source / xmedia.cpp < prev    next >
C/C++ Source or Header  |  1998-04-06  |  9KB  |  373 lines

  1. #define INCL_OS2MM
  2. #define INCL_MMIOOS2
  3. #define INCL_MMIO_CODEC
  4. #define INCL_MCIOS2
  5. #define INCL_MACHDR
  6.  
  7. #include "XVideo.h"
  8. #include "XFrmWnd.h"
  9. #include "XMsgBox.h"
  10. #include "XSound.h"
  11. #include "XCntrEvn.h"
  12. #include "xexcept.h"
  13. #include "os2me.h"
  14. #include "xcd.h"
  15.  
  16. #include <stdlib.h>
  17.  
  18. //defines for sound-class, it can handle *.mid and *.wav
  19. #define MIDI 1
  20. #define WAVE 2
  21.  
  22.  
  23. XMediaWindow :: XMediaWindow(const XFrameWindow * res):XObjectWindow(res)
  24. {
  25.    mediaId = 0;
  26. }
  27.  
  28.  
  29. XMediaWindow :: ~XMediaWindow()
  30. {
  31.    if(mediaId)
  32.       CloseDevice();
  33. }
  34.  
  35.  
  36. void XMediaWindow :: CloseDevice()
  37. {
  38.    _MCI_GENERIC_PARMS close;
  39.    mciSendCommand( mediaId, MCI_CLOSE, MCI_WAIT, &close, 0);
  40.    mediaId = 0;
  41. }
  42.  
  43. /*@
  44. @class XMediaWindow
  45. @type overview
  46. @symbol _
  47. */
  48.  
  49. /*@
  50. @class XVideo
  51. @parent XMediaWindow
  52. @type overview
  53. @symbol _
  54. */
  55.  
  56.  
  57. /*@ XVideo :: XVideo(const XFrameWindow * ow)
  58. @group constructors/destructors
  59. @remarks Construct a video-window. To use this control you must invoke the multimedia-library!
  60. @parameters XFrameWindow * owner    the owner of the video
  61. */
  62. XVideo :: XVideo(const XFrameWindow * ow):XMediaWindow(ow)
  63. {
  64.    _MCI_OPEN_PARMS open;
  65.    open.pszDeviceType = (PSZ) "digitalvideo";
  66.    ULONG rc = mciSendCommand(0, MCI_OPEN, MCI_WAIT | MCI_OPEN_SHAREABLE, &open, 0);
  67.    if( rc == 0 )
  68.       mediaId = open.usDeviceID;
  69.    else
  70.       OOLThrow( "cannot open digita-video", rc);
  71. }
  72.  
  73.  
  74. /*@
  75. @class XCDPlayer
  76. @parent XMediaWindow
  77. @type overview
  78. @symbol _
  79. */
  80.  
  81. /*@ XCDPlayer::XCDPlayer( const XFrameWindow )
  82. @group constructors/destructors
  83. @remarks Opens the CD-Player. To play a record from the CD you must call ReadRecordList()
  84. first, then call PlayRecord()!
  85. @parameters XFrameWindow* owning window
  86. @exceptions If an error ocures an exception of the type XException is thrown
  87. */
  88. XCDPlayer :: XCDPlayer( const XFrameWindow * w ):XMediaWindow(w)
  89. {
  90.    count = 0;
  91.    records = NULL;
  92.  
  93.    _MCI_OPEN_PARMS open;
  94.    open.pszDeviceType = (PSZ) "cdaudio";
  95.    ULONG rc = mciSendCommand(0, MCI_OPEN, MCI_WAIT | MCI_OPEN_SHAREABLE, &open, 0);
  96.    if( rc == 0 )
  97.       mediaId = open.usDeviceID;
  98.    else
  99.       OOLThrow( "cannot open cd-player", rc);
  100.  
  101. }
  102.  
  103.  
  104. XCDPlayer :: ~XCDPlayer()
  105. {
  106.    if(records)
  107.       free(records);
  108. }
  109.  
  110.  
  111. /*@ XCDPlayer :: ReadRecordList()
  112. @group misc
  113. @remarks Load the list of records on the current CD.
  114. @exceptions If an error ocures an exception of the type XException is thrown
  115. */
  116. void XCDPlayer :: ReadRecordList()
  117. {
  118.    if(records)
  119.       free(records);
  120.    records = NULL;
  121.    count = 0;
  122.  
  123.    MCI_TOC_REC tokens[100];
  124.    _MCI_TOC_PARMS params;
  125.  
  126.    memset( &tokens, 0, sizeof(tokens));
  127.    params.pBuf = tokens;
  128.    params.ulBufSize = sizeof(tokens);
  129.  
  130.    ULONG rc = mciSendCommand(mediaId, MCI_GETTOC, MCI_WAIT, ¶ms, 0);
  131.  
  132.    if( rc == MCIERR_DEVICE_NOT_READY)
  133.       OOLThrow( "device is not ready!", rc );
  134.    else if( rc != 0)
  135.       OOLThrow( "cannot get informations about cd-player", rc);
  136.  
  137.    while( tokens[count].TrackNum > 0 )
  138.       count++;
  139.    records = (cdRecord*) malloc( count * sizeof(cdRecord));
  140.    for(int i = 0; i < count; i++)
  141.    {
  142.       records[i].startAddr = tokens[i].ulStartAddr;
  143.       records[i].endAddr = tokens[i].ulEndAddr;
  144.       records[i].trackNumber = tokens[i].TrackNum;
  145.    }
  146. }
  147.  
  148.  
  149. /*@ XCDPlayer :: PlayRecord( USHORT record )
  150. @group misc
  151. @remarks Plays a record
  152. @parameters USHORT zero based index
  153. @returns ULONG errorcode
  154. */
  155. ULONG XCDPlayer :: PlayRecord( const USHORT record )
  156. {
  157.    if( record < count )
  158.       return Play( records[record].startAddr, records[record].endAddr );
  159.    else
  160.       return 0;
  161. }
  162.  
  163.  
  164. /*@ XCDPlayer::GetRecordLength( USHORT )
  165. @group misc
  166. @remarks Get the length of a record
  167. @parameters USHORT zero based index
  168. @returns ULONG time in milliseconds
  169. */
  170. ULONG XCDPlayer :: GetRecordLength( const USHORT i)
  171. {
  172.    if( i < count )
  173.       return (records[i].endAddr - records[i].startAddr) / 3;
  174.    else
  175.       return 0;
  176. }
  177.  
  178.  
  179. LONG XVideo::Restore(void) const
  180. {
  181.    _MCI_RESTORE_PARMS restore;
  182.    mciSendCommand( mediaId, MCI_RESTORE, MCI_WAIT, &restore, 0);
  183.    return 0;
  184.  
  185. }
  186.  
  187.  
  188. /*@ XVideo::SetViewPort(const XFrameWindow * w)
  189. @group misc
  190. @remarks Set a framewindow as the viewport where videos are displayed
  191. @parameters XFrameWindow * window   the window to show the video
  192. @returns LONG resultcode
  193. */
  194. LONG XVideo::SetViewPort(const XFrameWindow * w) const
  195. {
  196.    _MCI_VID_WINDOW_PARMS window;
  197.    window.hwndDest = owner->GetHandle();
  198.    mciSendCommand( mediaId, MCI_WINDOW, MCI_WAIT | MCI_DGV_WINDOW_HWND, &window, 0);
  199.    return 0;
  200. }
  201.  
  202.  
  203. /*@ XMediaWindow::SetVolume(const USHORT volumePercent)
  204. @group misc
  205. @remarks Set volume
  206. @parameters USHORT volume  volume in percent
  207. @returns LONG resultcode
  208. */
  209. LONG XMediaWindow::SetVolume(const USHORT volumePercent, const USHORT chanel)
  210. {
  211.    _MCI_SET_PARMS set;
  212.    memset(&set, 0, sizeof(set));
  213.    set.ulLevel = volumePercent;
  214.    set.ulAudio = chanel;//MCI_SET_AUDIO_ALL
  215.    return mciSendCommand( mediaId, MCI_SET, MCI_WAIT | MCI_SET_AUDIO, &set, 0);
  216. }
  217.  
  218. /*@ XMediaWindow::SetDataFile(const char *p)
  219. @group misc
  220. @remarks Play a new file
  221. @parameters char * the file to play
  222. @returns LONG resultcode
  223. */
  224. LONG XMediaWindow::SetDataFile(const char *p)
  225. {
  226.    _MCI_LOAD_PARMS load;
  227.    load.pszElementName = (PSZ) p;
  228.    return mciSendCommand( mediaId, MCI_LOAD, MCI_WAIT | MCI_OPEN_ELEMENT | MCI_READONLY, &load, 0);
  229. }
  230.  
  231.  
  232. /*@ XMediaWindow::Play(void)
  233. @group play functions
  234. @remarks Play the file loaded by SetDataFile
  235. @parameters
  236. ULONG from  position to start (default is 0 = current position)
  237. ULONG from  position to end (default is 0, the whole file/chunk will be played)
  238. @returns LONG resultcode
  239. */
  240. LONG XMediaWindow::Play( ULONG from, ULONG to )
  241. {
  242.    ULONG code = MCI_NOTIFY;
  243.  
  244.    _MCI_PLAY_PARMS play;
  245.    play.hwndCallback = owner->GetHandle();
  246.    if( from )
  247.    {
  248.       code |= MCI_FROM;
  249.       play.ulFrom = from;
  250.    }
  251.    if( to )
  252.    {
  253.       code |= MCI_TO;
  254.       play.ulTo = to;
  255.    }
  256.  
  257.    mciSendCommand( mediaId, MCI_PLAY, code, &play, MEDIA_PLAYED);
  258.    return 0;
  259. }
  260.  
  261.  
  262. /*@ XMediaWindow::Pause(void)
  263. @group play functions
  264. @remarks Pause
  265. @returns LONG resultcode
  266. */
  267. LONG XMediaWindow::Pause(void)
  268. {
  269.    _MCI_GENERIC_PARMS pause;
  270.    pause.hwndCallback = owner->GetHandle();
  271.    return mciSendCommand( mediaId, MCI_PAUSE, MCI_NOTIFY, &pause, MEDIA_PAUSED);
  272. }
  273.  
  274.  
  275. /*@ XMediaWindow::Rewind(void)
  276. @group play functions
  277. @remarks Seek to the beginning
  278. @returns LONG resultcode
  279. */
  280. LONG XMediaWindow::Rewind(void)
  281. {
  282.    _MCI_SEEK_PARMS seek;
  283.    seek.hwndCallback = owner->GetHandle();
  284.    return mciSendCommand( mediaId, MCI_SEEK, MCI_NOTIFY | MCI_TO_START, &seek, MEDIA_REWINDED);
  285. }
  286.  
  287.  
  288. LONG XMediaWindow::Stop(void)
  289. {
  290.    _MCI_GENERIC_PARMS rewinded;
  291.    rewinded.hwndCallback = owner->GetHandle();
  292.    return mciSendCommand( mediaId, MCI_STOP, MCI_NOTIFY, &rewinded, MEDIA_STOPED);
  293. }
  294.  
  295.  
  296. LONG XMediaWindow::Acquire(void)
  297. {
  298. /**
  299.    strcpy(commandString, "acquire ");
  300.    strcat(commandString, idString);
  301.    return SendMCIString();
  302. ***/
  303.    return 0;
  304. }
  305.  
  306.  
  307. /*@
  308. @class XSound
  309. @parent XMediaWindow
  310. @type overview
  311. @symbol _
  312. */
  313.  
  314.  
  315. LONG XSound::SetDataFile(const char *p)
  316. {
  317.    if (((strstr(p, ".MID") || strstr(p, ".mid")) && type != MIDI) || ((strstr(p, ".WAV") || strstr(p, ".wav")) && type != WAVE))
  318.    {
  319.       if (type != 0)
  320.       {
  321.          type = 0;
  322.          CloseDevice();
  323.       }
  324.  
  325.       if (p)
  326.       {
  327.          if (strstr(p, ".WAV"))
  328.             type = WAVE;
  329.          else if (strstr(p, ".MID"))
  330.             type = MIDI;
  331.       }
  332.       if(type)
  333.       {
  334.          _MCI_OPEN_PARMS open;
  335.          if(type == WAVE)
  336.             open.pszDeviceType = (PSZ) "waveaudio";
  337.          if(type == MIDI)
  338.             open.pszDeviceType = (PSZ) "sequencer";
  339.  
  340.          ULONG rc = mciSendCommand(0, MCI_OPEN, MCI_WAIT | MCI_OPEN_SHAREABLE, &open, 0);
  341.          if( rc == 0 )
  342.             mediaId = open.usDeviceID;
  343.          else
  344.             OOLThrow( "cannot open audio", rc);
  345.       }
  346.    }
  347.    return XMediaWindow::SetDataFile(p);
  348. }
  349.  
  350.  
  351. /*@ XSound :: ~XSound()
  352. @group constructors/destructors
  353. @remarks The destructor MUST be called before the message-loop of an aplication is
  354. destroyed. Therefore overwrite QueryForClose() of the owning framewindow and call
  355. the destructor of XSound in that function.
  356. */
  357.  
  358.  
  359. /*@ XSound :: XSound(const XFrameWindow * w)
  360. @group constructors/destructors
  361. @remarks Construct a sound-device. XSound can play MIDI and WAV-files. To use this control you must invoke the multimedia-library!
  362. @parameters XFrameWindow * owner    the owner of the device
  363. */
  364.  
  365.  
  366. /*@ XVideo :: ~XVideo()
  367. @group construtors/destructors
  368. @remarks The destructor MUST be called before the message-loop of an aplication is
  369. destroyed. Therefore overwrite QueryForClose() of the owning framewindow and call
  370. the destructor of XVideo in that function.
  371. */
  372.  
  373.