home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 26 / CD_ASCQ_26_1295.iso / vrac / gctv101.zip / TESTSAPI.CPP < prev    next >
C/C++ Source or Header  |  1995-09-18  |  7KB  |  268 lines

  1.  
  2. //  ------------------------------------------------------------------
  3. //  Test program for the Goldware Sound API.
  4. //  Released to the Public Domain by Odinn Sorensen.
  5. //  ------------------------------------------------------------------
  6. //  This source has been tested and compiles cleanly with Borland C++
  7. //  3.1 and Watcom C++ 10.0a.
  8. //  ------------------------------------------------------------------
  9. //  For 16-bit compilers, compile in a large memory model.
  10. //  Compile with -DDEBUG for diagnostics.
  11. //  ------------------------------------------------------------------
  12. //  See GOLDSAPI.DOC for a specification of the Goldware Sound API.
  13. //  ------------------------------------------------------------------
  14.  
  15.  
  16. //  ------------------------------------------------------------------
  17.  
  18. #include <dos.h>
  19. #include <stdio.h>
  20. #include <malloc.h>
  21. #include <string.h>
  22.  
  23.  
  24. //  ------------------------------------------------------------------
  25. //  Some typedefs
  26.  
  27. typedef unsigned char  byte;
  28. typedef unsigned short word;
  29. typedef unsigned int   uint;
  30.  
  31.  
  32. //  ------------------------------------------------------------------
  33. //  Goldware Sound API function numbers
  34.  
  35. #define GSAPI_INSTALL_CHECK       0x00
  36. #define GSAPI_OPEN_API            0x10
  37. #define GSAPI_CLOSE_API           0x11
  38. #define GSAPI_OPEN_AND_LOAD_FILE  0x12
  39. #define GSAPI_CLOSE_FILE          0x13
  40. #define GSAPI_PLAY                0x14
  41. #define GSAPI_STOP                0x15
  42. #define GSAPI_PAUSE               0x16
  43. #define GSAPI_RESUME              0x17
  44. #define GSAPI_BREAK_LOOP          0x18
  45. #define GSAPI_SPEAKER_ON_OFF      0x19
  46.  
  47.  
  48. //  ------------------------------------------------------------------
  49. //  Goldware Sound API data structure
  50.  
  51. struct gsapidata {
  52.   word driver_version;
  53.   word dsp_version;
  54.   word io_port;
  55.   byte irq_number;
  56.   byte dma_channel;
  57.   word sample_rate;
  58.   volatile word status;
  59.   word buffer_segment;
  60.   word buffer_offset;
  61.   long buffer_length;
  62.   char parameters[80];
  63. };
  64.  
  65.  
  66. //  ------------------------------------------------------------------
  67. //  The AMIS signature string
  68.  
  69. struct amis_signature {
  70.   char manufacturer[8];
  71.   char product_name[8];
  72.   char product_description[64];
  73. };
  74.  
  75.  
  76. //  ------------------------------------------------------------------
  77. //  Compiler portability defines
  78.  
  79. #if defined(__WATCOMC__)
  80.   #define x_ax w.ax
  81.   #define x_bx w.bx
  82.   #define x_cx w.cx
  83.   #define x_dx w.dx
  84.   #define x_di w.di
  85.   #if defined(__386__)
  86.     /* NOTE: This is for the DOS/4GW DOS extender! */
  87.     #define MK_FP2(s,o) ((s << 4) + o)
  88.     #define INT86 int386
  89.   #else
  90.     #define MK_FP2(s,o) MK_FP(s,o)
  91.     #define INT86 int86
  92.     #define HUGEMALLOC(n) halloc(((long)n/1024L)+1,1024)
  93.   #endif
  94. #else
  95.   #define x_ax x.ax
  96.   #define x_bx x.bx
  97.   #define x_cx x.cx
  98.   #define x_dx x.dx
  99.   #define x_di x.di
  100.   #define MK_FP2(s,o) MK_FP(s,o)
  101.   #define INT86 int86
  102.   #define HUGEMALLOC(n) farmalloc(n)
  103. #endif
  104.  
  105.  
  106. //  ------------------------------------------------------------------
  107. //  The AMIS multiplex number found for the Goldware Sound API
  108.  
  109. int mpx;
  110.  
  111.  
  112. //  ------------------------------------------------------------------
  113. //  Call a function in the API
  114.  
  115. int call_api(uint al, uint bx) {
  116.  
  117.   union REGS cpu;
  118.   cpu.h.al = al;
  119.   cpu.h.ah = mpx;
  120.   cpu.x_bx = bx;
  121.   INT86(0x2D, &cpu, &cpu);
  122.   #ifdef DEBUG
  123.   if(al == GSAPI_OPEN_AND_LOAD_FILE) {
  124.     if(cpu.x_ax == 0xFFFF)
  125.       printf("DOS error code %u\n", cpu.x_bx);
  126.   }
  127.   #endif
  128.   return cpu.x_ax;
  129. }
  130.  
  131.  
  132. //  ------------------------------------------------------------------
  133.  
  134. int main(int argc, char** argv) {
  135.  
  136.   union REGS cpu;
  137.   int key_value, apiret;
  138.   amis_signature* signature;
  139.   gsapidata* data;
  140.   #if defined(__WATCOMC__)
  141.   long memory_needed;
  142.   #if defined(__386__)
  143.   word buffer;
  144.   #else
  145.   char* buffer;
  146.   #endif
  147.   #else
  148.   char* buffer;
  149.   #endif
  150.  
  151.   #ifdef DEBUG
  152.   printf("Memory: %lu bytes\n", coreleft());
  153.   #endif
  154.  
  155.  
  156.   if(argc > 1) {
  157.  
  158.     // Installation check
  159.     for(mpx=0; mpx<256; mpx++) {
  160.       cpu.h.ah = mpx;
  161.       cpu.h.al = GSAPI_INSTALL_CHECK;
  162.       INT86(0x2D, &cpu, &cpu);
  163.       if(cpu.h.al == 0xFF) {
  164.         signature = (amis_signature*)MK_FP2(cpu.x_dx, cpu.x_di);
  165.         #ifdef DEBUG
  166.         printf("Found %8.8s version %04Xh\n", signature->product_name, cpu.x_cx);
  167.         #endif
  168.         if((cpu.x_cx >= 0x0100) && (cpu.x_cx < 0x0200))
  169.           if(strnicmp(signature->product_name, "GoldSAPI", 8) == 0)
  170.             break;
  171.       }
  172.     }
  173.  
  174.     // Return if not installed
  175.     if(mpx >= 256) {
  176.       #ifdef DEBUG
  177.       printf("GSAPI is not installed.\n");
  178.       #endif
  179.       mpx = -1;
  180.       return -1;
  181.     }
  182.  
  183.     // Call the open api function
  184.     cpu.h.al = GSAPI_OPEN_API;
  185.     cpu.h.ah = mpx;
  186.     INT86(0x2D, &cpu, &cpu);
  187.     apiret = cpu.x_ax;
  188.     if(apiret) {
  189.       #ifdef DEBUG
  190.       printf("Could not open GSAPI. Errorcode %u.\n", apiret);
  191.       #endif
  192.       return apiret;
  193.     }
  194.  
  195.     // Successfully opened the api. Now get the data pointer and key value
  196.     data = (gsapidata*)MK_FP2(cpu.x_dx, cpu.x_di);
  197.     key_value = cpu.x_bx;
  198.  
  199.     // Copy the filename to the parameters field
  200.     strcpy(data->parameters, argv[1]);
  201.  
  202.     // Now call the api to open and load the file
  203.     apiret = call_api(GSAPI_OPEN_AND_LOAD_FILE, 0);
  204.     if(apiret == 0x02) {
  205.       #if defined(__WATCOMC__) && defined(__386__)
  206.       // Determine how much is available
  207.       cpu.x_ax = 0x100;
  208.       cpu.x_bx = 65535u;
  209.       INT86(0x31, &cpu, &cpu);
  210.       memory_needed = (data->buffer_length + 16L) / 16L;
  211.       if(memory_needed <= (long)cpu.x_bx*16L) {
  212.         cpu.x_ax = 0x100;
  213.         cpu.x_bx = memory_needed;
  214.         INT86(0x31, &cpu, &cpu);
  215.         data->buffer_segment = cpu.x_ax;
  216.         data->buffer_offset = 0;
  217.         buffer = cpu.x_dx;
  218.         apiret = call_api(GSAPI_OPEN_AND_LOAD_FILE, 0);
  219.       }
  220.       else {
  221.         #ifdef DEBUG
  222.         printf("Not enough memory to load the file\n");
  223.         #endif
  224.       }
  225.       #else
  226.       buffer = (char*)HUGEMALLOC(data->buffer_length);
  227.       if(buffer) {
  228.         data->buffer_segment = FP_SEG(buffer);
  229.         data->buffer_offset = FP_OFF(buffer);
  230.         apiret = call_api(GSAPI_OPEN_AND_LOAD_FILE, 0);
  231.       }
  232.       else {
  233.         #ifdef DEBUG
  234.         printf("Not enough memory to load the file\n");
  235.         #endif
  236.       }
  237.       #endif
  238.       if(apiret == 0) {
  239.         #ifdef DEBUG
  240.         printf("Now playing %s via GSAPI\n", argv[1]);
  241.         #endif
  242.         call_api(GSAPI_PLAY, 0);
  243.         while(data->status)
  244.           /* Wait until play stops */;
  245.         call_api(GSAPI_CLOSE_FILE, 0);
  246.       }
  247.       else {
  248.         #ifdef DEBUG
  249.         printf("GSAPI returned errorcode %u while opening the file second time\n", apiret);
  250.         #endif
  251.       }
  252.     }
  253.     else {
  254.       #ifdef DEBUG
  255.       printf("GSAPI returned errorcode %u while opening the file first time\n", apiret);
  256.       #endif
  257.     }
  258.  
  259.     call_api(GSAPI_CLOSE_API, key_value);
  260.   }
  261.  
  262.   return 117;   /* Value to test errorlevel in GCTVSAPI */
  263. }
  264.  
  265.  
  266. //  ------------------------------------------------------------------
  267.  
  268.