/* analyzer.c 
	vi:ts=3 sw=3:
*/


/* read module files and output statistics on them */

/* $Id: analyzer.c,v 4.11 1995/02/15 15:26:21 espie Exp espie $
 * $Log: analyzer.c,v $
 * Revision 4.11  1995/02/15  15:26:21  espie
 * *** empty log message ***
 *
 * Revision 4.10  1995/02/14  04:02:28  espie
 * Nothing.
 *
 * Revision 4.10  1995/02/14  04:02:28  espie
 * Nothing.
 *
 * Revision 4.9  1995/02/06  14:50:47  espie
 * Changed sample_info.
 *
 * Revision 4.9  1995/02/06  14:50:47  espie
 * Changed sample_info.
 *
 * Revision 4.8  1995/02/01  20:41:45  espie
 * *** empty log message ***
 *
 * Revision 4.7  1995/02/01  16:39:04  espie
 * Includes moved to defs.h
 *
 * Revision 4.7  1995/02/01  16:39:04  espie
 * Includes moved to defs.h
 *
 * Revision 4.6  1995/01/28  09:23:59  espie
 * Need (?) a return 0 at the end.
 *
 * Revision 4.1  1994/01/12  16:10:20  espie
 * Fixed up last minute problems.
 * Lots of changes.
 * removed create_note_tables(), run_in_fg().
 * Use new pref scheme.
 * New open_file semantics.
 * Added speed check.
 * Added patch for non termio.
 */

#include "defs.h"

#include "extern.h"
#include "song.h"
#include "tags.h"
#include "prefs.h"

ID("$Id: analyzer.c,v 4.11 1995/02/15 15:26:21 espie Exp espie $")

int error;

struct song *do_read_song(name, type)
char *name;
int type;
   {
   struct song *song;
   struct exfile *file;

   file = open_file(name, "r", getenv("MODPATH"));
   if (!file)
      return NULL;
   song = read_song(file, type); 
   close_file(file);
   if (song)
      puts(name);
   return song;
   }


int use_command[16];
int use_extended[16];

void analyze_block(b)
struct block *b;
   {
   int i, j;
   struct event *e;

   for (i = 0; i < BLOCK_LENGTH; i++)
      {
      int special;

      special = 0;
      for (j = 0; j < NUMBER_TRACKS; j++)
         {
         e = &b->e[j][i];
         switch(e->effect)
            {
#if 0
         case 13: /* skip */
            return;
         case 11: /* fastskip */
            return;
#endif
         case 14:
            use_extended[HI(e->parameters)] = TRUE;
            break;
         case 15:
            if (special != 0 && e->parameters != special)
               putchar('!');
            else
               special = e->parameters;
         default:
            use_command[e->effect] = TRUE;
            }
         }
      }
   }


void analyze_song(song)
struct song *song;
   {
   int i;

   for (i = 0; i < NUMBER_SAMPLES; i++)
      {
      if (song->samples[i]->start)
         {
         if (song->samples[i]->finetune)
            printf("Sample %d: finetune is %d\n", 
               i, song->samples[i]->finetune);
         }
      }
   for (i = 0; i < 16; i++)
      {
      use_command[i] = FALSE;
      use_extended[i] = FALSE;
      }
   for (i = 0; i < song->info.maxpat; i++)
      analyze_block(song->info.pblocks+i);
   for (i = 0; i < 16; i++)
      if (use_command[i])
         printf("%3d", i);
   for (i = 0; i < 16; i++)
      if (use_extended[i])
         printf("%3dE", i);
   printf("\n");
   }

int main(argc, argv)
int argc;
char **argv;
   {
   int i;

   struct song *song;
   int default_type;

   default_type = BOTH;
   set_pref_scalar(PREF_TOLERATE, 2);

   for (i = 1; i < argc; i++)
      {
      song = do_read_song(argv[i], NEW);
      if (!song && error != NEXT_SONG)
         song = do_read_song(argv[i], OLD);
      if (song)
         {
         analyze_song(song);
         release_song(song);
         }
      }
   return 0;
   }



