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

  1. /* commands.c */
  2.  
  3. /* $Author: espie $
  4.  * $Id: commands.c,v 2.4 1991/12/03 13:23:10 espie Exp espie $
  5.  * $Revision: 2.4 $
  6.  * $Log: commands.c,v $
  7.  * Revision 2.4  1991/12/03  13:23:10  espie
  8.  * Defensive programming: check the range of each note
  9.  * for arpeggio setup.
  10.  *
  11.  * Revision 2.3  1991/11/19  16:07:19  espie
  12.  * Added comments, moved minor stuff around.
  13.  *
  14.  * Revision 2.2  1991/11/18  01:12:31  espie
  15.  * Minor changes.
  16.  *
  17.  * Revision 2.1  1991/11/17  23:07:58  espie
  18.  * Used some constants.
  19.  *
  20.  * Revision 2.0  1991/11/17  21:42:08  espie
  21.  * Structured part of the code, especially replay ``automaton''
  22.  * and setting up of effects.
  23.  *
  24.  * Revision 1.9  1991/11/17  17:09:53  espie
  25.  * Added missing prototypes.
  26.  *
  27.  * Revision 1.8  1991/11/16  15:42:43  espie
  28.  * tabs.
  29.  *
  30.  * Revision 1.7  1991/11/08  14:25:55  espie
  31.  * Dynamic oversample and frequency.
  32.  *
  33.  * Revision 1.6  1991/11/07  21:40:16  espie
  34.  * Added arpeggio.
  35.  *
  36.  * Revision 1.5  1991/11/07  20:12:34  espie
  37.  * Minor problem with version id.
  38.  *
  39.  * Revision 1.4  1991/11/07  20:11:10  espie
  40.  * Added embedded version id.
  41.  *
  42.  * Revision 1.3  1991/11/07  20:05:53  espie
  43.  * Fixed up vibrato depth.
  44.  * Added vibslide and portaslide.
  45.  *
  46.  * Revision 1.2  1991/11/07  15:27:02  espie
  47.  * Added command 9.
  48.  *
  49.  * Revision 1.1  1991/11/06  09:46:06  espie
  50.  * Initial revision
  51.  *
  52.  *
  53.  */
  54.  
  55. #include <stdio.h>
  56. #include "extern.h"
  57. #include "channel.h"
  58. #include "machine.h"
  59. #include "song.h"
  60.      
  61. static char *id = "$Id: commands.c,v 2.4 1991/12/03 13:23:10 espie Exp espie $";
  62.  
  63. /* sine table for the vibrato effect (could be much more precise) */
  64.  
  65. int vibrato_table[32] = 
  66.    {  0,  25,  49,  71,  90, 106, 117, 125, 127, 125, 117, 106,  90,
  67.      71,  49,  25,   0, -25, -49, -71, -90,-106,-117,-125,-127,-125,
  68.    -117,-106, -90, -71, -49, -25};
  69.  
  70.  
  71. /***
  72.  *
  73.  *
  74.  *  setting up effects/doing effects.
  75.  *  The set_xxx gets called while parsing the effect,
  76.  *  the do_xxx gets called each tick, and update the
  77.  *  sound parameters while playing it.
  78.  *
  79.  *
  80.  ***/
  81.  
  82.  
  83. void do_nothing(ch)
  84. struct channel *ch;
  85.     {
  86.     }
  87.  
  88. void set_nothing(a, ch)
  89. struct automaton *a;
  90. struct channel *ch;
  91.     {
  92.     }
  93.  
  94. /* slide pitch (up or down) */
  95.  
  96. void do_slide(ch)
  97. struct channel *ch;
  98.     {
  99.     ch->pitch += ch->slide;
  100.     ch->pitch = MIN(ch->pitch, MAX_PITCH);
  101.     ch->pitch = MAX(ch->pitch, MIN_PITCH);
  102.     set_current_pitch(ch, ch->pitch);
  103.     }
  104.  
  105. void set_upslide(a, ch)
  106. struct automaton *a;
  107. struct channel *ch;
  108.     {
  109.     ch->adjust = do_slide;
  110.     if (a->para)
  111.         ch->slide = a->para;
  112.     }
  113.  
  114. void set_downslide(a, ch)
  115. struct automaton *a;
  116. struct channel *ch;
  117.     {
  118.     ch->adjust = do_slide;
  119.     if (a->para)
  120.         ch->slide = -a->para;
  121.     }
  122.  
  123. /* modulating the pitch with vibrato */
  124.  
  125. void do_vibrato(ch)
  126. struct channel *ch;
  127.     {
  128.     int offset;
  129.  
  130.         /* this is a literal transcription of the protracker
  131.          * code. I should rescale the vibrato table at some point
  132.          */
  133.     ch->viboffset += ch->vibrate;
  134.     ch->viboffset %= 64;
  135.     offset = (vibrato_table[ch->viboffset >> 1] * ch->vibdepth)/64;
  136.         /* temporary update of only the step value,
  137.          * note that we do not change the saved pitch.
  138.          */
  139.     set_current_pitch(ch, ch->pitch + offset);
  140.     }
  141.  
  142. void set_vibrato(a, ch)
  143. struct automaton *a;
  144. struct channel *ch;
  145.     {
  146.     ch->adjust = do_vibrato;
  147.     if (a->para)
  148.         {
  149.         ch->vibrate = HI(a->para);
  150.         ch->vibdepth = LOW(a->para);
  151.         }
  152.     }
  153.  
  154. /* arpeggio looks a bit like chords: we alternate between two
  155.  * or three notes very fast.
  156.  * Issue: we are able to re-generate real chords. Would that be
  157.  * better ? To try.
  158.  */
  159. void do_arpeggio(ch)
  160. struct channel *ch;
  161.     {
  162.     if (++ch->arpindex >= MAX_ARP)
  163.         ch->arpindex =0;
  164.     set_current_pitch(ch, ch->arp[ch->arpindex]);
  165.     }
  166.  
  167. void set_arpeggio(a, ch)
  168. struct automaton *a;
  169. struct channel *ch;
  170.     {
  171.         /* normal play is arpeggio with 0/0 */
  172.     if (!a->para)
  173.         return;
  174.         /* arpeggio can be installed relative to the
  175.          * previous note, so we have to check that there
  176.          * actually is a current(previous) note
  177.          */
  178.     if (ch->note == NO_NOTE)
  179.         {
  180.         fprintf(stderr,
  181.             "No note present for arpeggio");
  182.         error = FAULT;
  183.         }
  184.     else
  185.         {
  186.         int note;
  187.  
  188.         ch->arp[0] = pitch_table[ch->note];
  189.         note = ch->note + HI(a->para);
  190.         if (note < NUMBER_NOTES)
  191.             ch->arp[1] = pitch_table[note];
  192.         else
  193.             {
  194.             fprintf(stderr,
  195.                 "Arpeggio note out of range");
  196.             error = FAULT;
  197.             }
  198.         note = ch->note + LOW(a->para);
  199.         if (note < NUMBER_NOTES)
  200.             ch->arp[2] = pitch_table[note];
  201.         else
  202.             {
  203.             fprintf(stderr,
  204.                 "Arpeggio note out of range");
  205.             error = FAULT;
  206.             }
  207.         ch->arpindex = 0;
  208.         ch->adjust = do_arpeggio;
  209.         }
  210.     }
  211.  
  212. /* volume slide. Mostly used to simulate waveform control.
  213.  * (attack/decay/sustain).
  214.  */
  215. void do_slidevol(ch)
  216. struct channel *ch;
  217.     {
  218.     ch->volume += ch->volumerate;
  219.     ch->volume = MIN(ch->volume, MAX_VOLUME);
  220.     ch->volume = MAX(ch->volume, MIN_VOLUME);
  221.     }
  222.  
  223. /* note that volumeslide does not have a ``take default''
  224.  * behavior. If para is 0, this is truly a 0 volumeslide.
  225.  * Issue: is the test really necessary ? Can't we do
  226.  * a HI(para) - LOW(para).
  227.  */
  228. void parse_slidevol(ch, para)
  229. struct channel *ch;
  230. int para;
  231.     {
  232.     if (LOW(para))
  233.         ch->volumerate = -LOW(para);
  234.     else
  235.         ch->volumerate = HI(para);
  236.     }
  237.  
  238. void set_slidevol(a, ch)
  239. struct automaton *a;
  240. struct channel *ch;
  241.     {
  242.     ch->adjust = do_slidevol;
  243.     parse_slidevol(ch, a->para);
  244.     }
  245.  
  246. /* portamento: gets from a given pitch to another.
  247.  * We can simplify the routine by cutting it in
  248.  * a pitch up and pitch down part while setting up
  249.  * the effect.
  250.  */
  251. void do_portamento(ch)
  252. struct channel *ch;
  253.     {
  254.     if (ch->pitch < ch->pitchgoal)
  255.         {
  256.         ch->pitch += ch->pitchrate;
  257.         ch->pitch = MIN(ch->pitch, ch->pitchgoal);
  258.         }
  259.     else if (ch->pitch > ch->pitchgoal)
  260.         {
  261.         ch->pitch -= ch->pitchrate;
  262.         ch->pitch = MAX(ch->pitch, ch->pitchgoal);
  263.         }
  264.     set_current_pitch(ch, ch->pitch);
  265.     }
  266.  
  267. /* if para and pitch are 0, this is obviously a continuation
  268.  * of the previous portamento.
  269.  */
  270. void set_portamento(a, ch)
  271. struct automaton *a;
  272. struct channel *ch;
  273.     {
  274.     ch->adjust = do_portamento;
  275.     if (a->para)
  276.         ch->pitchrate = a->para;
  277.     if (a->pitch)
  278.         ch->pitchgoal = a->pitch;
  279.     }
  280.  
  281. /*
  282.  * combined commands.
  283.  */
  284.  
  285. void do_portaslide(ch)
  286. struct channel *ch;
  287.     {
  288.     do_portamento(ch);
  289.     do_slidevol(ch);
  290.     }
  291.  
  292. void set_portaslide(a, ch)
  293. struct automaton *a;
  294. struct channel *ch;
  295.     {
  296.     ch->adjust = do_portaslide;
  297.     parse_slidevol(ch, a->para);
  298.     }
  299.  
  300. void do_vibratoslide(ch)
  301. struct channel *ch;
  302.     {
  303.     do_vibrato(ch);
  304.     do_slidevol(ch);
  305.     }
  306.  
  307. void set_vibratoslide(a, ch)
  308. struct automaton *a;
  309. struct channel *ch;
  310.     {
  311.     ch->adjust = do_vibratoslide;
  312.     parse_slidevol(ch, a->para);
  313.     }
  314.  
  315. /***
  316.  *
  317.  *  effects that just need a setup part
  318.  *
  319.  ***/
  320.  
  321. /* IMPORTANT: because of the special nature of
  322.  * the player, we can't process each effect independently,
  323.  * we have to merge effects from the four channel before
  324.  * doing anything about it. For instance, there can be 
  325.  * several speed changein the same note,
  326.  * only the last one takes effect.
  327.  */
  328.  
  329. void set_speed(a, ch)
  330. struct automaton *a;
  331. struct channel *ch;
  332.     {
  333.     a->new_speed = a->para;
  334.     a->do_stuff |= SET_SPEED;
  335.     }
  336.  
  337. void set_skip(a, ch)
  338. struct automaton *a;
  339. struct channel *ch;
  340.     {
  341.         /* yep, this is BCD. */
  342.     a->new_note = HI(a->para) * 10 + LOW(a->para);
  343.     a->do_stuff |= SET_SKIP;
  344.     }
  345.  
  346. void set_fastskip(a, ch)
  347. struct automaton *a;
  348. struct channel *ch;
  349.     {
  350.     a->new_pattern = a->para;
  351.     a->do_stuff |= SET_FASTSKIP;
  352.     }
  353.  
  354. /* immediate effect: starts the sample somewhere
  355.  * off the start.
  356.  */
  357. void set_offset(a, ch)
  358. struct automaton *a;
  359. struct channel *ch;
  360.     {
  361.     ch->pointer = int_to_fix(a->para * 256);
  362.     }
  363.  
  364. /* change the volume of the current channel.
  365.  * Is effective until there is a new set_volume,
  366.  * slide_volume, or an instrument is reloaded 
  367.  * explicitly by giving its number. Obviously, if
  368.  * you load an instrument and do a set_volume in the
  369.  * same note, the set_volume will take precedence.
  370.  */
  371. void set_volume(a, ch)
  372. struct automaton *a;
  373. struct channel *ch;
  374.     {
  375.     ch->volume = a->para;
  376.     }
  377.  
  378.  
  379.  
  380.  
  381.  
  382. /* Initialize the whole effect table */
  383.  
  384. void init_effects(table)
  385. void (*table[])();
  386.     {
  387.     table[0] = set_arpeggio;
  388.     table[15] = set_speed;
  389.     table[13] = set_skip;
  390.     table[11] = set_fastskip;
  391.     table[12] = set_volume;
  392.     table[10] = set_slidevol;
  393.     table[9] = set_offset;
  394.     table[3] = set_portamento;
  395.     table[5] = set_portaslide;
  396.     table[2] = set_upslide;
  397.     table[1] = set_downslide;
  398.     table[4] = set_vibrato;
  399.     table[6] = set_vibratoslide;
  400.     table[14] = set_nothing;
  401.     table[7] = set_nothing;
  402.     table[8] = set_nothing;
  403.     }
  404.  
  405.