home *** CD-ROM | disk | FTP | other *** search
/ Groovy Bytes: Behind the Moon / groovybytes.iso / GROOVY / SND_TOOL / HSC15ECR.ZIP / PAS_HSC.ZIP / PLAYHSC.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-05-23  |  10.0 KB  |  362 lines

  1. UNIT PLAYHSC;
  2.  
  3. (*
  4.                            HSC Player Unit V1.0
  5.                            --------------------
  6.  
  7.   Written in 1994 by GLAMOROUS RAY^RADiCAL RHYTHMS
  8.   Original code and .OBJ by CHiCKEN and ZWERG ZWACK^ELECTRONiC RATS
  9.  
  10.   This code is FREE WARE. This means that you can copy and distribute it
  11.   as you like, but you may not charge any money for its distribution. If
  12.   you intend to use this code in a commercial product than you have to
  13.   get written permission from NEO Software Produktions GmbH Austria.
  14.  
  15.  
  16.   Introduction:
  17.   -------------
  18.  
  19.   What is PLAYHSC? This unit is intended for use with the HSC tracker
  20.   written by Zwerg Zwack and Chicken on low level support from NEO
  21.   Software <g>.
  22.  
  23.   This unit supports an easy and fast to use way to play sound, so
  24.   not all general functions of the player are included. It is not
  25.   possible to do the polling - this is done by the player automatically,
  26.   it links into the timer interrupt.
  27.  
  28.  
  29.   General overview:
  30.   -----------------
  31.  
  32.   Below follows a description how to play sound files from your program,
  33.   calling this unit. There are two ways:
  34.  
  35.   1st: Play a sound file, loading from disk
  36.   2nd: Play a sound file, directly from included data.
  37.  
  38.   The first method is very easy and simple, the second one more
  39.   cool ;).
  40.  
  41.   The player is build up as an object, so it is initialized on your heap
  42.   when starting and deinstalled when finishing. Please note that the
  43.   player does _no_ check for memory. So it's up to you to check if there's
  44.   enough memory (heap) free to load the music - the player won't just do
  45.   anything in that case.
  46.  
  47.   To get the object to work, you have to declare it as a variable. This
  48.   is normally done this way:
  49.  
  50.   VAR
  51.     Music : HSC_obj;
  52.  
  53.   Now the object is declared and has to be used and called by _you_.
  54.   An object's variables and procedures a called like a record so you
  55.   can do the following:
  56.  
  57.   Music.Init(0);
  58.   Music.Done;
  59.  
  60.  
  61.   Software implementation:
  62.   ------------------------
  63.  
  64.   CONSTRUCTOR HSC_obj.Init (AdlibAddress : WORD);
  65.   -> Init the player and the object. You must supply a base address for
  66.      the adlib card. If you want to use the player's autodetection
  67.      routines, simply use 0 as the base.
  68.  
  69.   PROCEDURE HSC_Obj.Start;
  70.   -> Start music file, located at HSC_Obj.Address. The address is
  71.      set by either HSC_Obj.LoadMem or HSC_Obj.LoadFile;
  72.  
  73.   FUNCTION HSC_Obj.LoadFile (FileName):BOOLEAN;
  74.   -> Load a file from disk into memory. If the file you tend to load
  75.      is invalid or simply not there, the player won't play a single
  76.      voice. The function returns if it has loaded the music or not. If
  77.      not, there maybe was not enough memory or the file could not be
  78.      loaded (due to non-existence etc.).
  79.  
  80.   PROCEDURE HSC_Obj.LoadMem (Music_Address : Pointer);
  81.   -> "Load" music from disk. This has to be done to tell the player
  82.      that the music is _not_ loaded from disk so the memory is not freed
  83.      up when playing the next file.
  84.  
  85.   PROCEDURE HSC_Obj.Stop;
  86.   -> Stop music (if playing). This has to be done if you want to stop
  87.      the music. If you want to unload the music, simplay call HSC_Obj.Done
  88.      as this (of course) stops the music, too!
  89.  
  90.   PROCEDURE HSC_Obj.Fade;
  91.   -> Fade out music. This takes up to 4 seconds, so be sure to wait
  92.      in you program for fade-out, otherwise you will "kill" the sound
  93.      when calling HSC_Obj.Done (this will do a very nasty "pop").
  94.  
  95.   DESTRUCTOR HSC_Obj.Done;
  96.   -> Deinit object <g>. This frees up all memory allocated and stops
  97.      the music.
  98.  
  99.  
  100.   Hints & Tips section:
  101.   ---------------------
  102.  
  103.   Make sure your program does not exit without calling the destructor
  104.   or at least stopping the music. If you don't stop the music, your
  105.   system will hang on loading a new program (This is because of the
  106.   memory usage - the player still plays while the memory is already
  107.   freed up and used by another program - "Zack!"). It is a wise idea
  108.   to put the command into the @Exitproc.
  109.  
  110.   If you want to play more than one music file - okay - do it! ;)
  111.  
  112.  
  113.   Examples:
  114.   ---------
  115.  
  116.   Please see the enclosed file TSTHSC.PAS for more information.
  117.  
  118.  
  119.   Including music into .EXE files:
  120.   --------------------------------
  121.  
  122.   All you need for doing this, is your sound file (*.HSC) and the
  123.   program BINOBJ.EXE which came with your pascal package. Run the
  124.   program like this:
  125.  
  126.   BINOBJ.EXE MUSIC.HSC MUSIC.OBJ MUSIC_DATA
  127.  
  128.   This converts the file MUSIC.HSC into MUSIC.OBJ with the public name
  129.   set to MUSIC_DATA. Now all you have to do is to declare this object
  130.   file within you program. This has to be done like this:
  131.  
  132.   {$F+}
  133.   {$L MUSIC.OBJ}
  134.   PROCEDURE MUSIC_DATA; EXTERNAL;
  135.   {$F-}
  136.  
  137.   To play this file, try:
  138.  
  139.   HSC_Obj.LoadMem (@MUSIC_DATA);
  140.  
  141.   That's it!
  142.  
  143.  
  144.   Last words:
  145.   -----------
  146.  
  147.   First, I'd like to comment on my code. I hope, you're able to under-
  148.   stand this mess <g>. Second, don't bother me for the way it was
  149.   written. I just know: it works. And it will work on other machines
  150.   because it is written _cleanly_. If you think, you can do better:
  151.   please do! (And send me a copy! :)) )
  152.  
  153.   Greetings go to all RADiCAL RHYTHMS members for being an amazing posse.
  154.   Last, but not least, thanks to Chicken and Zwerg Zwack for their rellay
  155.   neat program.
  156.  
  157.   If you find any bugs, or have ideas for add-ons, please contact me:
  158.  
  159.   via modem : The UnderCover BBS - +49 2323 450 850
  160.              (9600+, 8N1)
  161.  
  162.   via fido  : Christian Bartsch@2:2445/321.49 (classic)
  163.  
  164.   via phone : +49 2323 460 525
  165.  
  166.  
  167.  
  168.   May, 23rd 1994 - GLAMOROUS RAY! C U in cyberspace...
  169.  
  170. *)
  171.  
  172. INTERFACE
  173.  
  174. TYPE
  175.   HSC_obj = OBJECT
  176.  
  177.     (* The following variables are _internal_ variables and should not be
  178.        changed from outside!                                             *)
  179.  
  180.     Address    : Pointer;
  181.     Music_Load : BOOLEAN;
  182.     Music_Size : LONGINT;
  183.     Music_Run  : BOOLEAN;
  184.     Music_Fade : BOOLEAN;
  185.  
  186.     (* The following procedures/cuntions are for your use. Please read
  187.        the information above on how to use them!                        *)
  188.  
  189.     CONSTRUCTOR Init (AdlibAddress : WORD);
  190.     PROCEDURE Start;
  191.     PROCEDURE Stop;
  192.     PROCEDURE Fade;
  193.     PROCEDURE LoadMem (Music_Address : Pointer);
  194.     FUNCTION LoadFile (FileName : STRING):BOOLEAN;
  195.     DESTRUCTOR Done;
  196.  
  197.   END;
  198.  
  199. IMPLEMENTATION
  200.  
  201. USES CRT;
  202.  
  203.  
  204.  
  205. (*-------------------------------------------------------------------------*)
  206. (*                SECTION: Sub-routines used for by HSC_obj                *)
  207. (*-------------------------------------------------------------------------*)
  208.  
  209.  
  210.  
  211. {$F+}                               (* Include                         *)
  212. {$L HSCOBJ.OBJ}                     (*        external                 *)
  213. PROCEDURE _HscPlayer; EXTERNAL;     (*                  player         *)
  214. {$F-}                               (*                          object *)
  215.  
  216. FUNCTION  DetectAdlib (SuggestedPort : WORD) : WORD; ASSEMBLER;
  217.   ASM
  218.     MOV  AH,4
  219.     MOV  BX,SuggestedPort
  220.     CALL _HscPlayer
  221.     JNC  @GoOn
  222.     MOV  AX,0FFh
  223.   @GoOn:
  224.   END;
  225.  
  226. PROCEDURE GetPlayerState (VAR Destination); ASSEMBLER;
  227.   ASM
  228.     MOV  AH,7
  229.     LES  SI,DWORD PTR Destination
  230.     CALL _HscPlayer
  231.   END;
  232.  
  233. PROCEDURE StartMusic (Song : POINTER; Polling, OldIRQ : BOOLEAN); ASSEMBLER;
  234.   ASM
  235.     MOV  AH,0
  236.     MOV  BL,Polling
  237.     MOV  BH,OldIRQ
  238.     CMP  BH,1
  239.     JE   @Invert
  240.     MOV  BH,1
  241.     JMP  @GoOn
  242.   @Invert:
  243.     XOR  BH,BH
  244.   @GoOn:
  245.     LES  SI,DWORD PTR Song
  246.     CALL _HscPlayer
  247.   END;
  248.  
  249. PROCEDURE StopMusic; ASSEMBLER;
  250.   ASM
  251.     MOV  AH,2
  252.     CALL _HscPlayer
  253.   END;
  254.  
  255.  
  256.  
  257. (*-------------------------------------------------------------------------*)
  258. (*                      SECTION: HSC_obj implementation                    *)
  259. (*-------------------------------------------------------------------------*)
  260.  
  261.  
  262.  
  263. CONSTRUCTOR HSC_obj.Init (AdlibAddress : WORD);
  264. VAR
  265.   Dummy : WORD;
  266. BEGIN
  267.   Music_Load := FALSE;
  268.   Music_Run  := FALSE;
  269.   Music_Fade := FALSE;
  270.   Address    := NIL;
  271.  
  272.   Dummy := DetectAdlib (0);
  273.   Delay (30);
  274. END;
  275.  
  276. PROCEDURE HSC_obj.Start;
  277. BEGIN
  278.   IF NOT Music_Run THEN BEGIN
  279.     IF Address <> NIL THEN BEGIN
  280.       StartMusic (Address,FALSE,TRUE);
  281.       Music_Run := TRUE;
  282.     END;
  283.   END;
  284. END;
  285.  
  286. PROCEDURE HSC_obj.Stop;
  287. BEGIN
  288.   IF Music_Run THEN BEGIN
  289.     StopMusic;
  290.     Music_Run := FALSE;
  291.   END;
  292. END;
  293.  
  294. PROCEDURE HSC_obj.Fade;
  295. BEGIN
  296.   IF Music_Run THEN BEGIN
  297.     ASM
  298.       MOV  AH,3
  299.       CALL _HscPlayer
  300.     END;
  301.     Music_Fade := TRUE;
  302.     Music_Run  := FALSE;
  303.   END;
  304. END;
  305.  
  306. PROCEDURE HSC_Obj.LoadMem (Music_Address : Pointer);
  307. BEGIN
  308.   IF Music_Fade or Music_Run THEN BEGIN
  309.     StopMusic;
  310.     Music_Run  := FALSE;
  311.     Music_Fade := FALSE;
  312.     IF Music_Load THEN FreeMem (Address,Music_Size);
  313.   END;
  314.   Music_Load := FALSE;
  315.   Address    := Music_Address;
  316. END;
  317.  
  318. FUNCTION HSC_Obj.LoadFile (Filename : STRING):BOOLEAN;
  319. VAR
  320.   f : FILE;
  321. BEGIN
  322.   IF FileName <> '' THEN BEGIN
  323.     Assign (F,FileName);
  324.     {$I-} RESET (F,1); {$I+}
  325.   END;
  326.  
  327.   IF (IORESULT <> 0) OR (FileName = '') THEN BEGIN
  328.     Music_Load := FALSE;
  329.     LoadFile   := FALSE;
  330.   END ELSE BEGIN
  331.     IF Music_Fade or Music_Run THEN BEGIN
  332.       StopMusic;
  333.       Music_Run  := FALSE;
  334.       Music_Fade := FALSE;
  335.       IF Music_Load THEN FreeMem (Address,Music_Size);
  336.     END;
  337.     Music_Size := FileSize (F);
  338.     IF MaxAvail < Music_Size THEN BEGIN
  339.       LoadFile   := FALSE;
  340.       Music_Load := FALSE;
  341.     END ELSE BEGIN
  342.       GetMem (Address,Music_Size);
  343.       BlockRead (f,Address^,Music_Size);
  344.       LoadFile   := TRUE;
  345.       Music_Load := TRUE;
  346.     END;
  347.   Close (f);
  348.   END;
  349. END;
  350.  
  351. DESTRUCTOR HSC_obj.Done;
  352. BEGIN
  353.   IF Music_Run OR Music_Fade THEN StopMusic;
  354.   IF Music_Load THEN FREEMEM (Address,Music_Size);
  355. END;
  356.  
  357. END.
  358.  
  359. (*-------------------------------------------------------------------------*)
  360. (*                         SECTION: END OF FILE ;)                         *)
  361. (*-------------------------------------------------------------------------*)
  362.