home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mmoshead.zip / samples / cdtoc / cdtoc.cc next >
Text File  |  1994-02-23  |  4KB  |  158 lines

  1. ////////////////////////////////
  2. // cdtoc - audio CD toc sample
  3. //
  4. // using emx 0.8h, mm4emx 1.0
  5. //
  6. //
  7. // Marc E.E. van Woerkom, 2/94
  8. //
  9.  
  10.  
  11. #include <os2.h>
  12.  
  13. #define  INCL_OS2MM
  14. #define  INCL_MCIOS2
  15. #include <os2me.h>
  16.  
  17. #include <iostream.h>
  18. #include <iomanip.h>
  19.  
  20.  
  21. //
  22. // mci_err: translate the MCI return code into an error string
  23. //
  24.  
  25. void mci_err(ULONG rc)
  26. {
  27.     const rsize = 128;
  28.     char rbuff[rsize];
  29.  
  30.     ULONG rc2 = mciGetErrorString(rc,      // error code
  31.                                   rbuff,   // return buffer
  32.                                   rsize);  // rbuff size
  33.  
  34.     if (rc2 == MCIERR_SUCCESS)
  35.         cerr << "MCI error: " << rbuff << "\n\n";
  36.     else
  37.         cerr << "error #" << rc << " has occured!\n\n";
  38. }
  39.  
  40.  
  41. //
  42. // print_mmtime: print time given in MMTIME as hh.mm.ss
  43. //
  44.  
  45. void print_mmtime(ULONG mmtime)
  46. {
  47.     ULONG hms = HMSFROMMM(mmtime);
  48.  
  49.     // hms packing is: |--|ss|mm|hh|
  50.  
  51.     int hour = int(ULONG_LWLB(hms));
  52.     int min  = int(ULONG_LWHB(hms));
  53.     int sec  = int(ULONG_HWLB(hms));
  54.  
  55.     if (hour)
  56.         cout << setw(4) << setfill('0')
  57.              << hour << '.'
  58.              << setfill('0');
  59.     else
  60.         cout << setfill(' ');  // I believe this shouldn't be neccessary
  61.  
  62.     cout << setw(2) << min << '.';
  63.  
  64.     cout << setw(2) << setfill('0')
  65.          << sec
  66.          << setfill(' ');       // this neither
  67. }
  68.  
  69.  
  70. //
  71. // main
  72. //
  73.  
  74. int main()
  75. {
  76.     cout << "cdtoc -- Audio CD Table of Contents\n\n";
  77.  
  78.  
  79.     // open the audio CD device
  80.  
  81.     MCI_OPEN_PARMS mop;
  82.  
  83.     mop.hwndCallback = 0;
  84.     mop.usDeviceID = 0;
  85.     mop.pszDeviceType = MCI_DEVTYPE_CD_AUDIO_NAME;
  86.     mop.pszElementName = 0;
  87.  
  88.     ULONG rc = mciSendCommand(0,
  89.                               MCI_OPEN,                       // open message
  90.                               MCI_WAIT | MCI_OPEN_SHAREABLE,  // message flags
  91.                               &mop,                           // parameters
  92.                               0);
  93.  
  94.     if (rc != MCIERR_SUCCESS) {
  95.         mci_err(rc);
  96.         return 1;
  97.     }
  98.  
  99.  
  100.     // ask for the table of contents
  101.  
  102.     const MAXTOCRECS = 99;
  103.     MCI_TOC_REC mtr[MAXTOCRECS];
  104.  
  105.     MCI_TOC_PARMS mtp;
  106.  
  107.     mtp.hwndCallback = 0;
  108.     mtp.pBuf = mtr;
  109.     mtp.ulBufSize = sizeof(mtr);
  110.  
  111.     rc = mciSendCommand(mop.usDeviceID,  // device ID
  112.                         MCI_GETTOC,      // get toc message
  113.                         MCI_WAIT,        // message flags
  114.                         &mtp,            // parameters
  115.                         0);
  116.  
  117.     if (rc != MCIERR_SUCCESS) mci_err(rc);
  118.  
  119.  
  120.     // close the device
  121.  
  122.     MCI_GENERIC_PARMS mgp;
  123.     mgp.hwndCallback = 0;
  124.  
  125.     ULONG rc2 = mciSendCommand(mop.usDeviceID, MCI_CLOSE, MCI_WAIT, &mgp, 0);
  126.  
  127.     if (rc2 != MCIERR_SUCCESS) mci_err(rc2);
  128.  
  129.  
  130.     // now show the TOC, if been successful
  131.  
  132.     if (rc == MCIERR_SUCCESS) {
  133.         int i = 0;
  134.  
  135.         while (mtr[i].TrackNum) {
  136.             cout << "Track" << setw(3);
  137.             cout << int(mtr[i].TrackNum)
  138.                  << ":  Length ";
  139.             print_mmtime(mtr[i].ulEndAddr - mtr[i].ulStartAddr);
  140.             cout << "  [";
  141.             print_mmtime(mtr[i].ulStartAddr);
  142.             cout << " to ";
  143.             print_mmtime(mtr[i].ulEndAddr);
  144.             cout << "]  Control " << int(mtr[i].Control)
  145.                  << ", Country " << mtr[i].usCountry
  146.                  << ", Owner " << mtr[i].ulOwner
  147.                  << ", #" << mtr[i].ulSerialNum << "\n";
  148.             i++;
  149.         }
  150.     }
  151.     
  152.  
  153.     // that's all folks!
  154.  
  155.     return 0;
  156. }
  157.  
  158.