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

  1. /* automaton.c */
  2.  
  3. /* $Author: espie $
  4.  * $Id: automaton.c,v 2.5 1991/11/20 20:46:35 espie Exp espie $
  5.  * $Revision: 2.5 $
  6.  * $Log: automaton.c,v $
  7.  * Revision 2.5  1991/11/20  20:46:35  espie
  8.  * Minor correction.
  9.  *
  10.  * Revision 2.4  1991/11/19  16:07:19  espie
  11.  * Added comments, moved minor stuff around.
  12.  *
  13.  * Revision 2.3  1991/11/18  01:23:30  espie
  14.  * Added two level of fault tolerancy.
  15.  *
  16.  * Revision 2.2  1991/11/18  01:12:31  espie
  17.  * Minor changes.
  18.  *
  19.  * Revision 2.1  1991/11/17  23:07:58  espie
  20.  * Coming from str32.
  21.  *
  22.  *
  23.  */
  24.      
  25. static char *id = "$Id: automaton.c,v 2.5 1991/11/20 20:46:35 espie Exp espie $";
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30.      
  31. #include "extern.h"
  32. #include "machine.h"
  33. #include "song.h"
  34. #include "channel.h"
  35.      
  36.      
  37. /* updates the pattern to play in the automaton.
  38.  * Checks that the pattern actually exists.
  39.  */
  40. void set_pattern(a)
  41. struct automaton *a;
  42.     {
  43.     int p;
  44.     if (a->pattern_num >= a->info->length)
  45.         {
  46.         error = UNRECOVERABLE;
  47.         return;
  48.         }
  49.  
  50.         /* there is a level of indirection in the format,
  51.          * i.e., patterns can be repeated.
  52.          */
  53.     p = a->info->patnumber[a->pattern_num];
  54.     if (p >= a->info->maxpat)
  55.         {
  56.         error = UNRECOVERABLE;
  57.         return;
  58.         }
  59.     a->pattern = a->info->pblocks + p;
  60.     }
  61.  
  62. /* initialize all the fields of the automaton necessary
  63.  * to play a given song.
  64.  */
  65.  
  66. void init_automaton(a, song)
  67. struct automaton *a;
  68. struct song *song;
  69.     {
  70.     a->info = song->info;
  71.     a->pattern_num = 0;     /* first pattern */
  72.     a->note_num = 0;        /* first note in pattern */
  73.     a->counter = 0;         /* counter for the effect tempo */
  74.     a->speed = NORMAL_SPEED;/* this is the default effect tempo */
  75.     a->finespeed = NORMAL_FINESPEED;    
  76.                             /* this is the fine speed (100%)    */
  77.     a->do_stuff = DO_NOTHING;   
  78.                             /* some effects affect the automaton,
  79.                              * we keep them here.
  80.                              */
  81.  
  82.     error = NONE;           /* Maybe we should not reset errors at
  83.                              * this point.
  84.                              */
  85.     set_pattern(a);
  86.     }
  87.  
  88. /* Gets to the next pattern, and displays stuff */
  89.  
  90. void advance_pattern(a)
  91. struct automaton *a;
  92.     {
  93. #ifdef SHOW_SEQ
  94.     printf("\n");
  95. #endif
  96.     printf("%3d", a->pattern_num);
  97.     fflush(stdout);
  98. #ifdef SHOW_SEQ
  99.     printf("\n");
  100. #endif
  101.     if (++a->pattern_num >= a->info->length)
  102.         {
  103.         a->pattern_num = 0;
  104.         error = ENDED;
  105.         }
  106.     set_pattern(a);
  107.     a->note_num = 0;
  108.     }
  109.  
  110.         
  111.  
  112. /* process all the stuff which we need to advance in the song,
  113.  * including set_speed, set_skip and set_fastskip.
  114.  */
  115. void next_tick(a)
  116. struct automaton *a;
  117.     {
  118.     if (a->do_stuff & SET_SPEED && a->new_speed)
  119.         {
  120.             /* there are three classes of speed changes:
  121.              * 0 does nothing.
  122.              * <32 is the effect speed (resets the fine speed).
  123.              * >=32 changes the finespeed, so this is 32% to 255%
  124.              */
  125.         if (a->new_speed >= 32)
  126.             a->finespeed = a->new_speed;
  127.         else 
  128.             {
  129.             a->speed = a->new_speed;
  130.             a->finespeed = 100;
  131.             }
  132.         }
  133.     if (++a->counter >= a->speed)
  134.         {
  135.         a->counter = 0;
  136. #ifdef SHOW_SEQ
  137.         printf("\n");
  138. #endif
  139.         if (a->do_stuff & SET_FASTSKIP)
  140.             {
  141.             a->pattern_num = a->new_pattern;
  142.             set_pattern(a);
  143.             a->note_num = 0;
  144.             }
  145.         else if (a->do_stuff & SET_SKIP)
  146.             {
  147.             advance_pattern(a);
  148.             a->note_num = a->new_note;
  149.             }
  150.         else
  151.             {
  152.             if (++a->note_num >= BLOCK_LENGTH)
  153.                 advance_pattern(a);
  154.             }
  155.         a->do_stuff = DO_NOTHING;
  156.         }
  157.     }
  158.  
  159.  
  160.