home *** CD-ROM | disk | FTP | other *** search
/ Audio Plus 10-94 / AUDIOPLUS.ISO / unix / playrs / soundinf / soundinf.c < prev   
C/C++ Source or Header  |  1993-02-10  |  4KB  |  189 lines

  1. **********************************************************************/
  2. *                                                                    */
  3. *  File:          soundinfo.c                                        */
  4. *  Author:        Andrew W. Scherpbier                               */
  5. *  Version:       1.00                                               */
  6. *  Created:       18 Nov 1992                                   */
  7. *                                                                    */
  8. *  Copyright (c) 1991, 1992 Andrew Scherpbier                        */
  9. *                All Rights Reserved.                                */
  10. *                                                                    */
  11. *                                                                    */
  12. *--------------------------------------------------------------------*/
  13. *  Description:  Display or modify the info field of an audio        */
  14. *          file header                                         */
  15. *                                                                    */
  16. **********************************************************************/
  17.  
  18. include <stdio.h>
  19. include <fcntl.h>
  20. include <multimedia/libaudio.h>
  21. include <multimedia/audio_hdr.h>
  22.  
  23.  
  24. define    PUBLIC
  25. define    PRIVATE        static
  26.  
  27. define    TRUE        (1)
  28. define    FALSE        (0)
  29.  
  30. define    OK        (0)
  31. define    NOTOK        (-1)
  32.  
  33. define    when        break;case
  34. define    orwhen        case
  35. define    otherwise    break;default
  36.  
  37.  
  38. * Private routines
  39.  * ================
  40.  */
  41. RIVATE void set_info(char *soundfile, char *info);
  42. RIVATE void sound_info(char *soundfile);
  43.  
  44.  
  45. * Private variables
  46.  * =================
  47.  */
  48.  
  49.  
  50. * Public routines
  51.  * ===============
  52.  */
  53.  
  54.  
  55. * Public variables
  56.  * ================
  57.  */
  58.  
  59.  
  60. **************************************************************************
  61.  * PUBLIC main(int ac, char **av)
  62.  */
  63. UBLIC main(int ac, char **av)
  64.  
  65.     int        c;
  66.     int        do_set_info = 0;
  67.     extern int    optind;
  68.     extern char    *optarg;
  69.     char        infobuf[2048];
  70.  
  71.     while ((c = getopt(ac, av, "i:")) != EOF)
  72.     {
  73.         switch (c)
  74.         {
  75.             when 'i':
  76.                 do_set_info++;
  77.                 strcpy(infobuf, optarg);
  78.             otherwise:
  79.                 fprintf(stderr, "usage: soundinfo [-i info] soundfile ...\n");
  80.                 exit(1);
  81.         }
  82.     }
  83.  
  84.     if (do_set_info && optind < ac)
  85.     {
  86.         set_info(av[optind], infobuf);
  87.         exit(0);
  88.     }
  89.  
  90.     for (; optind < ac; optind++)
  91.     {
  92.         printf("%24s -- ", av[optind]);
  93.         sound_info(av[optind]);
  94.     }
  95.  
  96.  
  97.  
  98. **************************************************************************
  99.  * PRIVATE void sound_info(char *soundfile)
  100.  * PURPOSE:
  101.  *   Display the info field from the header of this soundfile
  102.  */
  103. RIVATE void sound_info(char *soundfile)
  104.  
  105.     int        fd;
  106.     char        infop[2048];
  107.     Audio_hdr    hp;
  108.  
  109.     if (!audio_isaudiofile(soundfile))
  110.     {
  111.         printf("Not an audio file\n");
  112.         return;
  113.     }
  114.     fd = open(soundfile, O_RDONLY);
  115.     if (fd < 0)
  116.     {
  117.         printf("no such file\n");
  118.         return;
  119.     }
  120.  
  121.     if (audio_read_filehdr(fd, &hp, infop, 2048) == AUDIO_SUCCESS)
  122.     {
  123.         printf("%s\n", infop);
  124.     }
  125.     else
  126.     {
  127.         printf("Invalid sound file header\n");
  128.     }
  129.     close(fd);
  130.  
  131.  
  132.  
  133.  
  134. **************************************************************************
  135.  * PRIVATE void set_info(char *soundfile, char *info)
  136.  * PURPOSE:
  137.  *   Set the info field of an audio file
  138.  */
  139. RIVATE void set_info(char *soundfile, char *info)
  140.  
  141.     char        *buf;
  142.     int        fd;
  143.     char        infop[2048];
  144.     Audio_hdr    hp;
  145.  
  146.     if (!audio_isaudiofile(soundfile))
  147.     {
  148.         fprintf(stderr, "%s is not an audio file\n", soundfile);
  149.         exit(1);
  150.     }
  151.     fd = open(soundfile, O_RDONLY);
  152.     if (fd < 0)
  153.     {
  154.         perror(soundfile);
  155.         exit(1);
  156.     }
  157.  
  158.     if (audio_read_filehdr(fd, &hp, infop, 2048) != AUDIO_SUCCESS)
  159.     {
  160.         fprintf(stderr, "Invalid sound file header in %s\n", soundfile);
  161.         exit(1);
  162.     }
  163.  
  164.     /*
  165.      * Read the whole file into memory
  166.      */
  167.     buf = (char *)malloc(hp.data_size);
  168.     read(fd, buf, hp.data_size);
  169.     close(fd);
  170.  
  171.     fd = open(soundfile, O_WRONLY | O_TRUNC);
  172.     if (fd < 0)
  173.     {
  174.         perror(soundfile);
  175.         exit(1);
  176.     }
  177.  
  178.     if (audio_write_filehdr(fd, &hp, info, strlen(info) + 1) != AUDIO_SUCCESS)
  179.     {
  180.         fprintf(stderr, "Unable to write back the audio file\n");
  181.         exit(1);
  182.     }
  183.     write(fd, buf, hp.data_size);
  184.     close(fd);
  185.  
  186.  
  187.  
  188.  
  189.