home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / XBD2_SRC.ZIP / XBD2_SRC / SOUND.C < prev    next >
C/C++ Source or Header  |  1991-02-10  |  2KB  |  83 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*    SOUND.C : C functions for digitized sound.                             */
  4. /*                                                                          */
  5. /*                                                                          */
  6. /*    (c) Hervé Soulard, Version 1.0 - 10/01/1991                            */
  7. /*                       Version 1.1 - 01/02/1991                            */
  8. /*                                                                          */
  9. /****************************************************************************/
  10.  
  11.  
  12. #include <fcntl.h>
  13. #include <sys\types.h>
  14. #include <sys\stat.h>
  15. #include <io.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <dos.h>
  20. #include <process.h>
  21. #include <malloc.h>
  22.  
  23. #include "sound.h"
  24.  
  25. #define SIZE    16384
  26.  
  27. static byte    convTab[256];
  28.  
  29. int readSound(char *fSound, struct sound *sSound)
  30. {
  31.     int            fd;
  32.     byte _huge    *ptr;
  33.     unsigned int    part;
  34.     unsigned long    i;
  35.     char            fName[255];
  36.     
  37.     strcpy(fName, fSound);
  38.     strcat(fName, ".psf");
  39.  
  40.     if (_dos_open(fName, O_RDONLY, &fd) != 0) {
  41.         perror(fSound);
  42.         exit(1);
  43.     }
  44.     
  45.     lseek(fd, SEEK_SET, 0);
  46.     _dos_read(fd, sSound, sizeof(struct sound), &part);
  47.     
  48.     if (!strcmp(sSound->ident, IDENT)) {
  49.         for (i = 0; i < 256; i++) 
  50.             convTab[i] = (char)(((i * sSound->timer) / 256 ) + 1);
  51.  
  52.         ptr = sSound->buffer = (byte _huge *)halloc(sSound->nbValues+1, 1);
  53.         if (ptr == NULL) {
  54.             perror("Can't allocate memory");
  55.             exit(1);
  56.         }
  57.  
  58.         do {
  59.             _dos_read(fd, ptr, SIZE, &part);
  60.             ptr += part;
  61.         }
  62.         while (part == SIZE);
  63.     
  64.         ptr = sSound->buffer;
  65.  
  66.         for (i = 0; i < sSound->nbValues; i++, ptr++)
  67.             *ptr = convTab[*ptr];
  68.         *ptr = '\0';
  69.         
  70.         _dos_close(fd);
  71.         return (1);
  72.     }
  73.     else {
  74.         _dos_close(fd);
  75.         return (0);
  76.     }
  77. }
  78.  
  79.  
  80. void closeSound(struct sound *sSound)
  81. {
  82.     hfree(sSound->buffer);
  83. }