home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / STK100.ZIP / PLAYDWD.PAS < prev    next >
Pascal/Delphi Source File  |  1995-01-12  |  12KB  |  452 lines

  1. (******************************************************************************
  2. File:                          playdwd.pas
  3. Tab stops:                 every 2 collumns
  4. Project:                     DWD Player
  5. Copyright:                 1994 DiamondWare, Ltd.  All rights reserved.*
  6. Written:                     Keith Weiner & Erik Lorenzen
  7. Pascal Conversion: David A. Johndrow
  8. Purpose:                     Contains simple example code to show how to load/play a
  9.                                      .DWD file
  10. History:                     KW 10/21/94 Started playdwd.c
  11.                                      DJ 11/12/94 Translated to Pascal
  12.                                      EL 01/12/95 Finalized
  13.  
  14. Notes
  15. -----
  16.  
  17. The bulk of this file is error checking logic.
  18.  
  19. However, this code isn't really robust when it comes to standard error checking
  20. and particularly recovery, software engineering technique, etc. The STK will
  21. handle songs larger than 64K (but not digitized sounds).    Also, exitting
  22. and cleanup is not handled robustly in this code.  The code below can
  23. only be validated by extremely careful scrutiny to make sure each case is
  24. handled properly.
  25.  
  26. But all such code would make this example file less clear; its purpose was
  27. to illustrate how to call the STK, not how to write QA-proof software.
  28.  
  29.  
  30. *Permission is expressely granted to use DisplayError or any derivitive made
  31.  from it to registered users of the STK.
  32. ******************************************************************************)
  33.  
  34.  
  35.  
  36. Program PlayDWD;
  37.  
  38. uses crt,dws;
  39.  
  40.  
  41.  
  42. var
  43.     ExitSave: pointer;
  44.  
  45.     ch:                 char;
  46.     fp:                 file;
  47.     dov:                dws_DOPTR;
  48.     dres:             dws_DRPTR;
  49.     ideal:            dws_IDPTR;
  50.     dplay:            dws_DPPTR;
  51.     errno:            word;
  52.     input:            integer;
  53.     sound:            pointer;
  54.     result:         word;
  55.     soundsize:    longint;
  56.     DWDInitted: boolean;
  57.     KeepGoing:    boolean;
  58.  
  59.  
  60.  
  61. Function Exist(FileName: string): boolean;
  62. Var
  63.     Fil: File;
  64. begin
  65.     Assign(Fil,FileName);
  66.     {*$I- }
  67.     Reset(Fil);
  68.     Close(Fil);
  69.     {$I+ }
  70.  
  71.     Exist := (IOResult = 0);
  72. end;
  73.  
  74.  
  75. Procedure DisplayError;
  76. begin
  77.     case dws_ErrNo of
  78.  
  79.         dws_EZERO:
  80.         begin
  81.             (*
  82.              . This should not have happened, considering how we got here!
  83.             *)
  84.             writeln('I am confused!  Where am I?  HOW DID I GET HERE????');
  85.             writeln('The ERROR number is:',dws_ErrNo);
  86.         end;
  87.  
  88.         dws_NOTINITTED:
  89.         begin
  90.             (*
  91.              . If we get here, it means you haven't called dws_Init().
  92.              . The STK needs to initialize itself and the hardware before
  93.              . it can do anything.
  94.             *)
  95.             writeln('The STK was not initialized');
  96.         end;
  97.  
  98.         dws_ALREADYINITTED:
  99.         begin
  100.             (*
  101.              . If we get here, it means you've called dws_Init() already.  Calling
  102.              . dws_DetectHardWare() at this point would cause zillions of
  103.              . problems if we let the call through.
  104.             *)
  105.             writeln('The STK was already initialized');
  106.         end;
  107.  
  108.         dws_NOTSUPPORTED:
  109.         begin
  110.             (*
  111.              . If we get here, it means that either the user's machine does not
  112.              . support the function you just called, or the STK was told not to
  113.              . support it in dws_Init.
  114.             *)
  115.             writeln('Function not supported');
  116.         end;
  117.  
  118.         dws_DetectHardware_UNSTABLESYSTEM:
  119.         begin
  120.             (*
  121.              . Please report it to DiamondWare if you get here!
  122.              .
  123.              . Ideally, you would disable control-C here, so that the user can't
  124.              . hit control-alt-delete, causing SmartDrive to flush its (possibly
  125.              . currupt) buffers.
  126.             *)
  127.             writeln('The system is unstable!');
  128.             writeln('Please power down now!');
  129.  
  130.             while (1 = 1) do
  131.             begin
  132.             end;
  133.         end;
  134.  
  135.         (*
  136.          . The following three errors are USER/PROGRAMMER errors.  You forgot
  137.          . to fill the cardtyp struct full of -1's (except in those fields
  138.          . you intended to override, or the user (upon the unlikly event that
  139.          . the STK was unable to find a card) gave you a bad overide value.
  140.         *)
  141.         dws_DetectHardware_BADBASEPORT:
  142.         begin
  143.             (*
  144.              . You set dov.baseport to a bad value, or
  145.              . didn't fill it with a -1.
  146.             *)
  147.             writeln('Bad port address');
  148.         end;
  149.  
  150.         dws_DetectHardware_BADDMA:
  151.         begin
  152.             (*
  153.              . You set dov.digdma to a bad value, or
  154.              . didn't fill it with a -1.
  155.             *)
  156.             writeln('Bad DMA channel');
  157.         end;
  158.  
  159.         dws_DetectHardware_BADIRQ:
  160.         begin
  161.             (*
  162.              . You set dov.digirq to a bad value, or
  163.              . didn't fill it with a -1.
  164.             *)
  165.             writeln('Bad IRQ level');
  166.         end;
  167.  
  168.         dws_Kill_CANTUNHOOKISR:
  169.         begin
  170.             (*
  171.              . The STK points the interrupt vector for the sound card's IRQ
  172.              . to its own code in dws_Init.
  173.              .
  174.              . dws_Kill was unable to restore the vector to its original
  175.              . value because other code has hooked it after the STK
  176.              . initialized(!)  This is really bad.    Make the user get rid
  177.              . of it and call dws_Kill again.
  178.             *)
  179.             writeln('Get rid of your TSR, pal!');
  180.             writeln('(Press any key)');
  181.             repeat
  182.             until (keypressed);
  183.         end;
  184.  
  185.         dws_X_BADINPUT:
  186.         begin
  187.             (*
  188.              . The mixer funtion's can only accept volumes between 0 & 255,
  189.              . the volume will remain unchanged.
  190.             *)
  191.             writeln('Bad mixer level');
  192.         end;
  193.  
  194.         dws_D_NOTADWD:
  195.         begin
  196.             (* You passed the STK a pointer to something which is not a .DWD file! *)
  197.             writeln('The file you are attempting to play is not a .DWD');
  198.         end;
  199.  
  200.         dws_D_NOTSUPPORTEDVER:
  201.         begin
  202.             (*
  203.              . The STK can't play a .DWD converted using a version of VOC2DWD.EXE
  204.              . newer than itself.  And, although we'll try to maintain backwards
  205.              . compatibility, we may not be able to guarantee that newer versions
  206.              . of the code will be able to play older .DWD files.  In any event,
  207.              . it's a good idea to always convert .VOC files with the utility
  208.              . which comes with the library you're linking into your application.
  209.             *)
  210.             writeln('Please reconvert this file using the VOC2DWD.EXE which came with this library');
  211.         end;
  212.  
  213.         dws_D_INTERNALERROR:
  214.         begin
  215.             (*
  216.              . This error should never occur and probably will not affect sound
  217.              . play(?).  If it happens please contact DiamondWare.
  218.             *)
  219.             writeln('An internal error has occured');
  220.             writeln('Please contact DiamondWare');
  221.         end;
  222.  
  223.         dws_DPlay_NOSPACEFORSOUND:
  224.         begin
  225.             (*
  226.              . This error is more like a warning, though it may happen on a
  227.              . regular basis, depending on how many sounds you told the STK
  228.              . to allow in dws_Init, how you chose to prioritize sounds and
  229.              . how many sounds are currently being played.
  230.             *)
  231.             writeln('No more room for new digitized sounds right now');
  232.         end;
  233.  
  234.         dws_DSetRate_FREQTOLOW:
  235.         begin
  236.             (*
  237.              . The STK will set rate as close as possible to the indicated rate
  238.              . but cannot set a rate that low.
  239.             *)
  240.             writeln('Playback frequency too low');
  241.         end;
  242.  
  243.         dws_DSetRate_FREQTOHIGH:
  244.         begin
  245.             (*
  246.              . The STK will set rate as close as possible to the indicated rate
  247.              . but cannot set a rate that high.
  248.             *)
  249.             writeln('Playback frequency too high');
  250.         end;
  251.  
  252.         dws_MPlay_NOTADWM:
  253.         begin
  254.             (* You passed the STK a pointer to something which is not a .DWM file! *)
  255.             writeln('The file you are attempting to play is not a .DWM');
  256.         end;
  257.  
  258.         dws_MPlay_NOTSUPPORTEDVER:
  259.         begin
  260.             (*
  261.              . The STK can't play a .DWM converted using a version of VOC2DWM.EXE
  262.              . newer than itself.  And, although we'll try to maintain backwards
  263.              . compatibility, we may not be able to guarantee that newer versions
  264.              . of the code will be able to play older .DWM files.  In any event,
  265.              . it's a good idea to always convert .MID files with the utility
  266.              . which comes with the library you're linking into your application.
  267.             *)
  268.             writeln('Please reconvert this file using the MID2DWM.EXE which came with this library');
  269.         end;
  270.  
  271.         dws_MPlay_INTERNALERROR:
  272.         begin
  273.             (*
  274.              . This error should never occur and probably will not affect sound
  275.              . play(?). If it happens please contact DiamondWare.
  276.             *)
  277.             writeln('An internal error has occured.');
  278.             writeln('Please contact DiamondWare');
  279.         end;
  280.  
  281.         else
  282.         begin
  283.             (*
  284.              . This should never occur and probably will not affect sound
  285.              . play(?). If it happens please contact DiamondWare.
  286.             *)
  287.             writeln('I am confused!  Where am I?  HOW DID I GET HERE????');
  288.             writeln('The ERROR number is:',dws_ErrNo);
  289.         end;
  290.     end;
  291. end;
  292.  
  293.  
  294. procedure ExitPlay; far;
  295.  
  296. label TryToKillAgain;
  297.  
  298. begin
  299.     ExitProc := ExitSave;
  300.  
  301.     TryToKillAgain:
  302.  
  303.     if (dws_Kill <> 1) then
  304.     begin
  305.         (*
  306.          . If an error occurs here, it's either dws_Kill_CANTUNHOOKISR
  307.          . or dws_NOTINITTED.  If it's dws_Kill_CANTUNHOOKISR the user
  308.          . must remove his tsr, and dws_Kill must be called again.    If it's
  309.          . dws_NOTINITTED, there's nothing to worry about at this point.
  310.         *)
  311.         DisplayError;
  312.  
  313.         if (dws_ErrNo = dws_Kill_CANTUNHOOKISR) then
  314.         begin
  315.             goto TryToKillAgain;
  316.         end;
  317.     end;
  318.  
  319.     if (sound <> nil) then
  320.     begin
  321.         freemem(sound, soundsize);
  322.     end;
  323.  
  324.     dispose(dplay);
  325.     dispose(ideal);
  326.     dispose(dres);
  327.     dispose(dov);
  328. end;
  329.  
  330.  
  331.  
  332. begin
  333.     ExitSave := ExitProc;
  334.     ExitProc := @ExitPlay;
  335.  
  336.     writeln;
  337.     writeln('PLAYDWD is Copyright 1994, DiamondWare, Ltd.');
  338.     writeln('All rights reserved.');
  339.     writeln;
  340.     writeln;
  341.  
  342.     new(dov);
  343.     new(dres);
  344.     new(ideal);
  345.     new(dplay);
  346.  
  347.     sound := nil;
  348.  
  349.     if (ParamCount = 0) then
  350.     begin
  351.         writeln('Usage PLAYDWD <dwd-file>');
  352.         halt(65535);
  353.     end;
  354.  
  355.     if Exist(ParamStr(1)) then
  356.     begin
  357.         Assign(fp, ParamStr(1));
  358.         Reset(fp,1);
  359.         soundsize := filesize(fp);
  360.  
  361.         (* Please note we don't check to see if we get the memory we need. *)
  362.         Getmem(sound, soundsize);
  363.         BlockRead(fp,sound^,soundsize);
  364.         Close(fp);
  365.     end
  366.     else
  367.     begin
  368.         writeln('Unable to open '+ParamStr(1));
  369.         halt(65535);
  370.     end;
  371.  
  372.     (*
  373.      . We need to set every field to -1 in dws_DETECTOVERRIDES record; this
  374.      . tells the STK to autodetect everything.    Any other value
  375.      . overrides the autodetect routine, and will be accepted on
  376.      . faith, though the STK will verify it if possible.
  377.     *)
  378.     dov^.baseport := 65535;
  379.     dov^.digdma     := 65535;
  380.     dov^.digirq     := 65535;
  381.  
  382.     if (dws_DetectHardWare(dov, dres) = 0) then
  383.     begin
  384.         DisplayError;
  385.         halt(65535);
  386.     end;
  387.  
  388.     (*
  389.      . The "ideal" record tells the STK how you'd like it to initialize the
  390.      . sound hardware.    In all cases, if the hardware won't support your
  391.      . request, the STK will go as close as possible.  For example, not all
  392.      . sound boards will support al sampling rates (some only support 5 or
  393.      . 6 discrete rates).
  394.     *)
  395.     ideal^.musictyp     := 0;         (*0=No music, 1=OPL2*)
  396.     ideal^.digtyp         := 8;         (*0=No Dig, 8=8bit*)
  397.     ideal^.digrate        := 11000; (*sampling rate, in Hz*)
  398.                                                             (*we could have called dws_DGetRateFromDWD*)
  399.                                                             (*before dws_Init to get the correct rate*)
  400.     ideal^.dignvoices := 1;         (*number of voices (up to 16)*)
  401.     ideal^.dignchan     := 1;         (*1=mono*)
  402.  
  403.     if (dws_Init(dres, ideal) = 0) then
  404.     begin
  405.         DisplayError;
  406.         halt(65535);
  407.     end;
  408.  
  409.     (*
  410.      . Set master volume to about 4/5ths max
  411.     *)
  412.     if (dws_XMaster(200) = 0) then
  413.     begin
  414.         DisplayError;
  415.     end;
  416.  
  417.     dplay^.snd            := sound;
  418.     dplay^.count        := 1;             (* 0=infinite loop, 1-N num times to play sound *)
  419.     dplay^.priority := 1000;
  420.     dplay^.presnd     := 0;
  421.  
  422.     if (dws_DGetRateFromDWD(sound, @ideal^.digrate) = 0) then
  423.     begin
  424.         DisplayError;
  425.         halt(65535);
  426.     end;
  427.  
  428.     if (dws_DSetRate(ideal^.digrate) = 0) then
  429.     begin
  430.         DisplayError;
  431.         halt(65535);
  432.     end;
  433.  
  434.     if (dws_DPlay(dplay) = 0) then
  435.     begin
  436.         DisplayError;
  437.         halt(65535);
  438.     end;
  439.  
  440.     repeat
  441.     begin
  442.         if(dws_DSoundStatus(dplay^.soundnum, @result) = 0) then
  443.         begin
  444.             DisplayError;
  445.             halt(65535);
  446.         end;
  447.     end;
  448.     until (result = 0) or (keypressed);
  449.  
  450.     halt(65535);
  451. end.
  452.