home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / audio / tracker / str32.c < prev    next >
Encoding:
C/C++ Source or Header  |  2014-05-19  |  11.7 KB  |  436 lines

  1. /* str32.c */
  2.  
  3. /* plays sound/noisetracker files on Sparc, silicon graphics.
  4.  * Authors  : Liam Corner - zenith@dcs.warwick.ac.uk
  5.  *            Marc Espie - espie@dmi.ens.fr
  6.  *
  7.  * Usage    : tracker <filename> 
  8.  *  this version plays compressed files and simple zoo archives
  9.  *  as well.
  10.  */
  11.  
  12. /* $Author: espie $
  13.  * $Id: str32.c,v 2.9 1991/12/04 08:28:53 espie Exp espie $
  14.  * $Revision: 2.9 $
  15.  * $Log: str32.c,v $
  16.  * Revision 2.9  1991/12/04  08:28:53  espie
  17.  * Separated mix/stereo stuff.
  18.  *
  19.  * Revision 2.8  1991/12/03  23:03:39  espie
  20.  * Added transpose feature.
  21.  *
  22.  * Revision 2.7  1991/12/03  20:43:46  espie
  23.  * Added possibility to get back to MONO for the sgi.
  24.  *
  25.  * Revision 2.6  1991/12/03  18:07:38  espie
  26.  * Added stereo capabilities to the indigo version.
  27.  *
  28.  * Revision 2.5  1991/12/03  17:10:11  espie
  29.  * Added recovery and reread for automatic recognition
  30.  * of old/new tracker files.
  31.  *
  32.  * Revision 2.4  1991/11/20  20:46:35  espie
  33.  * Minor correction.
  34.  *
  35.  * Revision 2.3  1991/11/18  01:23:30  espie
  36.  * Added two level of fault tolerancy.
  37.  *
  38.  * Revision 2.2  1991/11/18  01:10:45  espie
  39.  * Added more rational options.
  40.  *
  41.  * Revision 2.1  1991/11/17  23:07:58  espie
  42.  * Moved almost everything to audio and automaton.
  43.  *
  44.  * Revision 2.0  1991/11/17  21:42:08  espie
  45.  * Structured part of the code, especially replay ``automaton''
  46.  * and setting up of effects.
  47.  *
  48.  * Revision 1.26  1991/11/17  17:09:53  espie
  49.  * Added missing prototypes.
  50.  *
  51.  * Revision 1.25  1991/11/17  16:30:48  espie
  52.  * Corrected cosmetic details, mostly.
  53.  *
  54.  * Revision 1.24  1991/11/16  16:57:01  espie
  55.  * Forgot that read_song only returned NULL on read problem.
  56.  * Should test that before printing Ok.
  57.  *
  58.  * Revision 1.23  1991/11/16  16:54:19  espie
  59.  * Some more info while loading files.
  60.  * Added FAULT env variable, FAULT resistant playing,
  61.  * for playing modules which are not quite correct.
  62.  *
  63.  * Revision 1.22  1991/11/16  15:42:43  espie
  64.  * tabs.
  65.  *
  66.  * Revision 1.21  1991/11/15  20:57:34  espie
  67.  * Minor cleanup, mostly comments.
  68.  *
  69.  * Revision 1.20  1991/11/15  18:22:10  espie
  70.  * Serious bug: dochangespeed was not reset all the time.
  71.  * Check all these parameters, they MUST be reset for
  72.  * each new song.
  73.  *
  74.  * Revision 1.19  1991/11/09  20:01:56  espie
  75.  * Fixed a stupid bug: when env variable LOOPING was
  76.  * undefined, we got a segv on strcmp.
  77.  * Now we just test for its existence, since this is
  78.  * about all we want...
  79.  *
  80.  * Revision 1.18  1991/11/09  17:47:33  espie
  81.  * Bug correction: when doing arpeggio, there might not
  82.  * be a new note, so we have to save the old note value
  83.  * and do the arppeggio on that note.
  84.  *
  85.  * Revision 1.17  1991/11/08  14:47:52  espie
  86.  * Completely added control with OVERSAMPLE and FREQUENCY.
  87.  *
  88.  * Revision 1.15  1991/11/08  13:35:57  espie
  89.  * Added control flow.
  90.  *
  91.  * Revision 1.14  1991/11/08  12:37:37  espie
  92.  * Added pipe decompression, so that now you can do
  93.  * str32 file.Z directly.
  94.  * stdin may go away.
  95.  *
  96.  * Looping is now controlled by an environment variable.
  97.  *
  98.  * Revision 1.13  1991/11/07  21:40:16  espie
  99.  * Added arpeggio.
  100.  *
  101.  * Revision 1.12  1991/11/07  20:12:34  espie
  102.  * Minor problem with version id.
  103.  *
  104.  * Revision 1.11  1991/11/07  20:09:06  espie
  105.  * Added embedded version id.
  106.  *
  107.  * Revision 1.10  1991/11/07  20:05:53  espie
  108.  * Added vibslide and portaslide.
  109.  *
  110.  * Revision 1.9  1991/11/07  15:27:02  espie
  111.  * Added speed command.
  112.  * Added signal control.
  113.  * Error checking: there shouldn't be that many
  114.  * segv signals any more.
  115.  *
  116.  * Revision 1.8  1991/11/06  09:46:06  espie
  117.  * Moved every command to commands.c.
  118.  *
  119.  * Revision 1.7  1991/11/06  00:03:11  espie
  120.  * Added effect #9, change offset in sample.
  121.  * It seems to work quite correctly (example: sampled annie).
  122.  *
  123.  * Revision 1.6  1991/11/05  22:49:03  espie
  124.  * Added some debug code for showing the full
  125.  * sequence for a file.
  126.  * Corrected the bug in volume slide: there is
  127.  * no default value, i.e., if it is 0, it is 0,
  128.  * as stupid as it may seem.
  129.  * Added vibrato.
  130.  * Added fastskip/corrected skip.
  131.  *
  132.  * Revision 1.5  1991/11/05  11:14:59  espie
  133.  * Modified control flow of the player till
  134.  * it looks like something reasonable (i.e.,
  135.  * the structure is more natural and reflects
  136.  * the way stuff is played actually...)
  137.  *
  138.  * Added some #define for a slightly higher level
  139.  * of abstraction ( :-)    ).
  140.  *
  141.  * Revision 1.4  1991/11/04  20:27:05  espie
  142.  * Do not restart the sound when we change instruments
  143.  * on the fly. A bit strange, but it works that way.
  144.  *
  145.  * Revision 1.3  1991/11/04  13:23:59  espie
  146.  * Modified main to use new data structures.
  147.  * The sound player is MUCH cleaner, it uses now
  148.  * a 3-state automaton for each voice.
  149.  *
  150.  * Revision 1.2  1991/11/04  08:01:20  espie
  151.  * Corrected ruckus with data type of sample.
  152.  *
  153.  * Revision 1.1  1991/11/03  22:47:05  espie
  154.  * Initial revision
  155.  *
  156.  *
  157.  */
  158.      
  159. static char *id = "$Id: str32.c,v 2.9 1991/12/04 08:28:53 espie Exp espie $";
  160.  
  161. #include <stdio.h>
  162. #include <signal.h>
  163. #include <stdlib.h>
  164. #include <string.h>
  165.      
  166. /* this should be in getopt.h, but sparc doesn't know it... */
  167.  
  168. extern int getopt();
  169. extern char *optarg;
  170. extern int optind, opterr;
  171.  
  172. #include "extern.h"
  173. #include "machine.h"
  174. #include "song.h"
  175. #include "pref.h"
  176.      
  177.      
  178. /* global variable to catch various types of errors
  179.  * and achieve the desired flow of control
  180.  */
  181. int error;
  182.  
  183. /* signal handlers */
  184.  
  185. void goodbye(sig)
  186. int sig;
  187.     {
  188.     printf("\nSignal %d\n", sig);
  189.     exit(10);
  190.     }
  191.  
  192. void nextsong(sig)
  193. int sig;
  194.     {
  195.     printf("\nSignal %d\n", sig);
  196.     signal(sig, nextsong);
  197.     error = NEXT_SONG;
  198.     }
  199.  
  200. /* v = read_env(name, default): reads the value v
  201.  * in the environment, supplies a defaults.
  202.  */
  203. int read_env(name, def)
  204. char *name;
  205. int def;
  206.     {
  207.     char *var;
  208.     int value;
  209.  
  210.     var = getenv(name);
  211.     if (!var)
  212.         return def;
  213.     if (sscanf(var, "%d", &value) == 1)
  214.         return value;
  215.     else
  216.         return def;
  217.     }
  218.  
  219. /***
  220.  *
  221.  *  Handling compressed modules.
  222.  *
  223.  ***/
  224.  
  225. int check_Z(s)
  226. char *s;
  227.     {
  228.     int i;
  229.  
  230.     i = strlen(s);
  231.     if (i < 2)
  232.         return 0;
  233.     return s[i-1] == 'Z' && s[i-2] == '.';
  234.     }
  235.  
  236. int check_zoo(s)
  237. char *s;
  238.     {
  239.     int i;
  240.  
  241.     i = strlen(s);
  242.     if (i < 4)
  243.         return 0;
  244.     return s[i-1] == 'o' && s[i-2] == 'o' && s[i-3] == 'z' && s[i-4] == '.';
  245.     }
  246.  
  247. /* small hack for transposing songs on the fly */
  248. static int transpose;
  249.  
  250. struct song *do_read_song(s, type)
  251. char *s;
  252. int type;
  253.     {
  254.     struct song *song;
  255.     FILE *fp;
  256.  
  257.     printf("(%s)...", s);
  258.     fflush(stdout);
  259.  
  260.     if (check_Z(s))
  261.         {
  262.         char pipe[200];
  263.  
  264.         sprintf(pipe, "zcat %s", s);
  265.         fp = popen(pipe, "r");
  266.         if (fp == NULL)
  267.             {
  268.             fprintf(stderr, "Unable to open tune file %s\n", s);
  269.             return NULL;
  270.             }
  271.         song = read_song(fp, type, transpose); 
  272.         pclose(fp); 
  273.         }
  274.     else if (check_zoo(s))
  275.         {
  276.         char pipe[200];
  277.  
  278.         sprintf(pipe, "zoo xpq %s", s);
  279.         fp = popen(pipe, "r");
  280.         if (fp == NULL)
  281.             {
  282.             fprintf(stderr, "Unable to open tune file %s\n", s);
  283.             return NULL;
  284.             }
  285.         song = read_song(fp, type, transpose); 
  286.         pclose(fp); 
  287.         }
  288.     else
  289.         {
  290.         fp = fopen(s, "r");
  291.         if (fp == NULL)
  292.             {
  293.             fprintf(stderr, "Unable to open tune file %s\n", s);
  294.             return NULL;
  295.             }
  296.         song = read_song(fp, type, transpose); 
  297.         fclose(fp); 
  298.         }
  299.     if (song)
  300.         printf("Ok!\n");
  301.     return song;
  302.     }
  303.  
  304. int optvalue(def)
  305. int def;
  306.     {
  307.     int d;
  308.         if (sscanf(optarg, "%d", &d) == 1)
  309.             return d;
  310.         else
  311.             {
  312.             optind--;
  313.             return def;
  314.             }
  315.     }
  316.  
  317. int main (argc, argv)
  318. int argc;
  319. char **argv;
  320.     {
  321.  
  322.     int frequency;
  323.     int oversample;
  324.  
  325.     struct pref pref;
  326.     struct song *song;
  327.  
  328.     int index;
  329.     int c;
  330.     int default_type;
  331.  
  332.     signal(2, nextsong);
  333.     signal(3, goodbye);
  334.  
  335.     frequency = read_env("FREQUENCY", 0);
  336.     oversample = read_env("OVERSAMPLE", 1);
  337.     transpose = read_env("TRANSPOSE", 0);
  338.  
  339.     frequency = open_audio(frequency);
  340.  
  341.     init_player(oversample, frequency);
  342.  
  343.         /* check the command name for default reading type */
  344.     if (strcmp(argv[0], "otracker") == 0)
  345.         default_type = OLD;
  346.     else if (strcmp(argv[0], "ntracker") == 0)
  347.         default_type = NEW;
  348.     else
  349.         default_type = BOTH;
  350.  
  351.     pref.type = default_type;
  352.     pref.repeats = 1;
  353.     pref.speed = 50;
  354.     pref.tolerate = 1;
  355.  
  356.     for (optind = 1; optind < argc; optind++)
  357.         {
  358.         while ((c = getopt(argc, argv, "onhbr:s:f:m:" )) != -1)
  359.             switch(c)
  360.                 {
  361.             case 'o':   /* old tracker type */
  362.                 pref.type = OLD;
  363.                 break;
  364.             case 'n':   /* new tracker type */
  365.                 pref.type = NEW;
  366.                 break;
  367.             case 'b':   /* both tracker types */
  368.                 pref.type = BOTH;
  369.                 break;
  370.             case 'r':   /* number of repeats */
  371.                 pref.repeats = optvalue(0);
  372.                 break;
  373.             case 's':   /* speed */
  374.                 pref.speed = optvalue(50);
  375.                 break;
  376.             case 'f':   /* level of fault */
  377.                 pref.tolerate = optvalue(2);
  378.                 break;
  379.             case 'm':   /* % of channel mix. 
  380.                          * 0->full stereo, 100->mono */
  381.                 set_mix(optvalue(30));
  382.                 break;
  383.             case 'h':   /* template */
  384.                 fprintf(stderr, "%s options:\n", argv[0]);
  385.                 fprintf(stderr, "\to  : old tracker format");
  386.                 if (default_type == OLD)
  387.                     fprintf(stderr, " (default)");
  388.                 fprintf(stderr, "\n\tn  : new tracker format");
  389.                 if (default_type == NEW)
  390.                     fprintf(stderr, " (default)");
  391.                 fprintf(stderr, "\n\tb  : try both formats");
  392.                 if (default_type == BOTH)
  393.                     fprintf(stderr, " (default)");
  394.                 fprintf(stderr, "\n\tr n: number of repeats (0 is forever, ");
  395.                 fprintf(stderr,     "1 is the default)\n");
  396.                 fprintf(stderr, "\ts n: speed (default 50, ");
  397.                 fprintf(stderr,     "some songs want 60)\n");
  398.                 fprintf(stderr, "\tf  : tolerate faults\n");
  399.                 fprintf(stderr, "\tf 0: abort song if problem (default)\n");
  400.                 fprintf(stderr, "\tm p: percent of mix (0 is spatial");
  401.                 fprintf(stderr, "100 is mono)\n");
  402.                 exit(2);
  403.                 }
  404.         if (optind < argc)
  405.             {
  406.             switch(pref.type)
  407.                 {
  408.             case BOTH:
  409.                 song = do_read_song(argv[optind], NEW);
  410.                 if (!song)
  411.                     song = do_read_song(argv[optind], OLD);
  412.                 break;
  413.             case OLD:
  414.                 song = do_read_song(argv[optind], pref.type);
  415.                 break;
  416.                 /* this is explicitly flagged as a new module,
  417.                  * so we don't need to look for a signature.
  418.                  */
  419.             case NEW:
  420.                 song = do_read_song(argv[optind], NEW_NO_CHECK);
  421.                 break;
  422.                 }
  423.             if (song == NULL)
  424.                 continue;
  425.  
  426.             dump_song(song); 
  427.             play_song(song, &pref);
  428.             release_song(song);
  429.             }
  430.         }
  431.  
  432.     close_audio();
  433.     return 0;
  434.     }
  435.  
  436.