home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / SOUNDUTILS / mm1_tracker.lzh / TRACKER4.6 / commands.c < prev    next >
Text File  |  1994-11-24  |  12KB  |  549 lines

  1. /* commands.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: commands.c,v 4.3 1994/11/15 16:11:01 espie Exp espie $
  6.  * $Log: commands.c,v $
  7.  * Revision 4.3  1994/11/15  16:11:01  espie
  8.  * *** empty log message ***
  9.  *
  10.  * Revision 4.2  1994/08/23  18:19:46  espie
  11.  * Added speedmode option
  12.  * Abstracted IO.
  13.  * Some notice to status.
  14.  * play_note instead of ch->mode.
  15.  * Fixed bug with bad loops.
  16.  * Modified the way set_speed works.
  17.  * Very small bug with volume (Lawrence).
  18.  * Added bg/fg test.
  19.  * More precise vibrato table.
  20.  *
  21.  * Revision 2.12  1992/11/13  13:24:24  espie
  22.  * Added some extended commands: E12AB, and some.
  23.  * now use set_volume in audio.c. All the device-dependent operation
  24.  * is there.
  25.  * Defensive programming: check the range of each note
  26.  * for arpeggio setup.
  27.  * Structured part of the code, especially replay ``automaton''
  28.  * and setting up of effects.
  29.  *
  30.  * Revision 1.9  1991/11/17  17:09:53  espie
  31.  * Added missing prototypes.
  32.  * Dynamic oversample and frequency.
  33.  * Added arpeggio.
  34.  * Fixed up vibrato depth.
  35.  * Added vibslide and portaslide.
  36.  * Added command 9.
  37.  */
  38.  
  39. #include <stdio.h>
  40.  
  41. #include "defs.h"
  42. #include "channel.h"
  43. #include "song.h"
  44. #include "extern.h"
  45. #include "prefs.h"
  46.      
  47. ID("$Id: commands.c,v 4.3 1994/11/15 16:11:01 espie Exp espie $")
  48.  
  49. /* sine table for the vibrato effect (obtained through build.c) */
  50.  
  51. int vibrato_table[64] = 
  52.    {
  53.    0,50,100,149,196,241,284,325,362,396,426,452,473,490,502,510,512,
  54.    510,502,490,473,452,426,396,362,325,284,241,196,149,100,50,0,-49,
  55.    -99,-148,-195,-240,-283,-324,-361,-395,-425,-451,-472,-489,-501,
  56.    -509,-511,-509,-501,-489,-472,-451,-425,-395,-361,-324,-283,-240,
  57.    -195,-148,-99,-49
  58.    };
  59.  
  60. /***
  61.  *
  62.  *
  63.  * setting up effects/doing effects.
  64.  * The set_xxx gets called while parsing the effect,
  65.  * the do_xxx gets called each tick, and update the
  66.  * sound parameters while playing it.
  67.  *
  68.  *
  69.  ***/
  70.  
  71.  
  72. void do_nothing(ch)
  73. struct channel *ch;
  74.    {
  75.    }
  76.  
  77. LOCAL void set_nothing(a, ch)
  78. struct automaton *a;
  79. struct channel *ch;
  80.    {
  81.    }
  82.  
  83. /* slide pitch (up or down) */
  84. LOCAL void do_slide(ch)
  85. struct channel *ch;
  86.    {
  87.    ch->pitch += ch->slide;
  88.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  89.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  90.    set_current_pitch(ch, ch->pitch);
  91.    }
  92.  
  93. LOCAL void set_upslide(a, ch)
  94. struct automaton *a;
  95. struct channel *ch;
  96.    {
  97.    ch->adjust = do_slide;
  98.    if (a->para)
  99.       ch->slide = a->para;
  100.    }
  101.  
  102. LOCAL void set_downslide(a, ch)
  103. struct automaton *a;
  104. struct channel *ch;
  105.    {
  106.    ch->adjust = do_slide;
  107.    if (a->para)
  108.       ch->slide = -a->para;
  109.    }
  110.  
  111. /* modulating the pitch with vibrato */
  112. LOCAL void do_vibrato(ch)
  113. struct channel *ch;
  114.    {
  115.    int offset;
  116.  
  117.       /* this is no longer a literal transcription of the pt
  118.        * code. I have rescaled the vibrato table.
  119.        */
  120.    ch->viboffset += ch->vibrate;
  121.    ch->viboffset &= 63;
  122.       /* please don't use logical shift on signed values */
  123.    offset = (vibrato_table[ch->viboffset] * ch->vibdepth)/256;
  124.       /* temporary update of only the step value,
  125.        * note that we do not change the saved pitch.
  126.        */
  127.    set_current_pitch(ch, ch->pitch + offset);
  128.    }
  129.  
  130. LOCAL void set_vibrato(a, ch)
  131. struct automaton *a;
  132. struct channel *ch;
  133.    {
  134.    ch->adjust = do_vibrato;
  135.    if (HI(a->para))
  136.       ch->vibrate = HI(a->para);
  137.    if (LOW(a->para))
  138.       ch->vibdepth = LOW(a->para);
  139.    }
  140.  
  141. /* arpeggio looks a bit like chords: we alternate between two
  142.  * or three notes very fast.
  143.  */
  144. LOCAL void do_arpeggio(ch)
  145. struct channel *ch;
  146.    {
  147.    if (++ch->arpindex >= MAX_ARP)
  148.       ch->arpindex =0;
  149.    set_current_pitch(ch, ch->arp[ch->arpindex]);
  150.    }
  151.  
  152. LOCAL void set_arpeggio(a, ch)
  153. struct automaton *a;
  154. struct channel *ch;
  155.    {
  156.       /* arpeggio can be installed relative to the
  157.        * previous note, so we have to check that there
  158.        * actually is a current(previous) note
  159.        */
  160.    if (ch->note == NO_NOTE)
  161.       {
  162.       status("No note present for arpeggio");
  163.       error = FAULT;
  164.       }
  165.    else
  166.       {
  167.       int note;
  168.  
  169.       ch->arp[0] = pitch_table[ch->note][ch->finetune];
  170.       note = ch->note + HI(a->para);
  171.       if (note < NUMBER_NOTES)
  172.          ch->arp[1] = pitch_table[note][ch->finetune];
  173.       else
  174.          {
  175.          status("Arpeggio note out of range");
  176.          error = FAULT;
  177.          }
  178.       note = ch->note + LOW(a->para);
  179.       if (note < NUMBER_NOTES)
  180.          ch->arp[2] = pitch_table[note][ch->finetune];
  181.       else
  182.          {
  183.          status("Arpeggio note out of range");
  184.          error = FAULT;
  185.          }
  186.       ch->arpindex = 0;
  187.       ch->adjust = do_arpeggio;
  188.       }
  189.    }
  190.  
  191. /* volume slide. Mostly used to simulate waveform control.
  192.  * (attack/decay/sustain).
  193.  */
  194. LOCAL void do_slidevol(ch)
  195. struct channel *ch;
  196.    {
  197.    set_current_volume(ch, ch->volume + ch->volumerate);
  198.    }
  199.  
  200. /* note that volumeslide does not have a ``take default''
  201.  * behavior. If para is 0, this is truly a 0 volumeslide.
  202.  * Issue: is the test really necessary ? Can't we do
  203.  * a HI(para) - LOW(para). Answer: protracker does not.
  204.  */
  205. LOCAL void parse_slidevol(ch, para)
  206. struct channel *ch;
  207. int para;
  208.    {
  209.    if (LOW(para))
  210.       ch->volumerate = -LOW(para);
  211.    else
  212.       ch->volumerate = HI(para);
  213.    }
  214.  
  215. LOCAL void set_slidevol(a, ch)
  216. struct automaton *a;
  217. struct channel *ch;
  218.    {
  219.    ch->adjust = do_slidevol;
  220.    parse_slidevol(ch, a->para);
  221.    }
  222.  
  223. /* portamento: gets from a given pitch to another.
  224.  * We can simplify the routine by cutting it in
  225.  * a pitch up and pitch down part while setting up
  226.  * the effect.
  227.  */
  228. LOCAL void do_portamento(ch)
  229. struct channel *ch;
  230.    {
  231.    if (ch->pitch < ch->pitchgoal)
  232.       {
  233.       ch->pitch += ch->pitchrate;
  234.       ch->pitch = MIN(ch->pitch, ch->pitchgoal);
  235.       }
  236.    else if (ch->pitch > ch->pitchgoal)
  237.       {
  238.       ch->pitch -= ch->pitchrate;
  239.       ch->pitch = MAX(ch->pitch, ch->pitchgoal);
  240.       }
  241.       /* if we want to implement funk glissando, we need a change right
  242.        * there
  243.        */
  244.    set_current_pitch(ch, ch->pitch);
  245.    }
  246.  
  247. /* if para and pitch are 0, this is obviously a continuation
  248.  * of the previous portamento.
  249.  */
  250. LOCAL void set_portamento(a, ch)
  251. struct automaton *a;
  252. struct channel *ch;
  253.    {
  254.    ch->adjust = do_portamento;
  255.    if (a->para)
  256.       ch->pitchrate = a->para;
  257.    if (a->pitch)
  258.       ch->pitchgoal = a->pitch;
  259.    }
  260.  
  261. /*
  262.  * combined commands.
  263.  */
  264. LOCAL void do_portaslide(ch)
  265. struct channel *ch;
  266.    {
  267.    do_portamento(ch);
  268.    do_slidevol(ch);
  269.    }
  270.  
  271. LOCAL void set_portaslide(a, ch)
  272. struct automaton *a;
  273. struct channel *ch;
  274.    {
  275.    ch->adjust = do_portaslide;
  276.    if (a->pitch)
  277.       ch->pitchgoal = a->pitch;
  278.    parse_slidevol(ch, a->para);
  279.    }
  280.  
  281. LOCAL void do_vibratoslide(ch)
  282. struct channel *ch;
  283.    {
  284.    do_vibrato(ch);
  285.    do_slidevol(ch);
  286.    }
  287.  
  288. LOCAL void set_vibratoslide(a, ch)
  289. struct automaton *a;
  290. struct channel *ch;
  291.    {
  292.    ch->adjust = do_vibratoslide;
  293.    parse_slidevol(ch, a->para);
  294.    }
  295.  
  296. /***
  297.  *
  298.  *  effects that just need a setup part
  299.  *
  300.  ***/
  301.  
  302. /* IMPORTANT: because of the special nature of
  303.  * the player, we can't process each effect independently,
  304.  * we have to merge effects from the four channel before
  305.  * doing anything about it. For instance, there can be 
  306.  * several speed change in the same note.
  307.  */
  308. LOCAL void set_speed(a, ch)
  309. struct automaton *a;
  310. struct channel *ch;
  311.    {
  312.    if (a->para >= 32 && get_pref_scalar(PREF_SPEEDMODE) != OLD_SPEEDMODE)
  313.       {
  314.       a->new_finespeed = a->para;
  315.       a->do_stuff |= SET_FINESPEED;
  316.       }
  317.    else if (a->para)
  318.       {
  319.       a->new_speed = a->para;
  320.       a->do_stuff |= SET_SPEED;
  321.       }
  322.    }
  323.  
  324. LOCAL void set_skip(a, ch)
  325. struct automaton *a;
  326. struct channel *ch;
  327.    {
  328.       /* BCD decoding in read.c */
  329.    a->new_note = a->para;
  330.    a->do_stuff |= SET_SKIP;
  331.    }
  332.  
  333. LOCAL void set_fastskip(a, ch)
  334. struct automaton *a;
  335. struct channel *ch;
  336.    {
  337.    a->new_pattern = a->para;
  338.    a->do_stuff |= SET_FASTSKIP;
  339.    }
  340.  
  341. /* immediate effect: starts the sample somewhere
  342.  * off the start.
  343.  */
  344. LOCAL void set_offset(a, ch)
  345. struct automaton *a;
  346. struct channel *ch;
  347.    {
  348.    set_position(ch, a->para * 256);
  349.    }
  350.  
  351. /* change the volume of the current channel.
  352.  * Is effective until there is a new set_volume,
  353.  * slide_volume, or an instrument is reloaded 
  354.  * explicitly by giving its number. Obviously, if
  355.  * you load an instrument and do a set_volume in the
  356.  * same note, the set_volume will take precedence.
  357.  */
  358. LOCAL void set_volume(a, ch)
  359. struct automaton *a;
  360. struct channel *ch;
  361.    {
  362.    set_current_volume(ch, a->para);
  363.    }
  364.  
  365.  
  366.  
  367. /***
  368.  *
  369.  * EXTENDED COMMANDS
  370.  *
  371.  ***/
  372.  
  373. /* extended command: retrig note at a fast pace
  374.  */
  375. LOCAL void do_retrig(ch)
  376. struct channel *ch;
  377.    {
  378.    if (--ch->current <= 0)
  379.       {
  380.       reset_note(ch, ch->note, ch->pitch);
  381.       ch->current = ch->retrig;
  382.       }
  383.    }
  384.  
  385. LOCAL void set_retrig(a, ch)
  386. struct automaton *a;
  387. struct channel *ch;
  388.    {
  389.    ch->retrig = a->para;
  390.    ch->current = ch->retrig;
  391.    ch->adjust = do_retrig;
  392.    }
  393.  
  394. /* extended command: start note after a small
  395.  * delay
  396.  */
  397. LOCAL void do_latestart(ch)
  398. struct channel *ch;
  399.    {
  400.    if (--ch->current <= 0)
  401.       {
  402.       reset_note(ch, ch->note, ch->pitch);
  403.       ch->adjust = do_nothing;
  404.       }
  405.    }
  406.  
  407. LOCAL void set_late_start(a, ch)
  408. struct automaton *a;
  409. struct channel *ch;
  410.    {
  411.    play_note(ch->audio, NULL, 0);
  412.    ch->current = a->para;
  413.    ch->adjust = do_latestart;
  414.    }
  415.  
  416. /* extended command: cut note after some time.
  417.  * Note we only kill the volume, as protracker does...
  418.  */
  419. LOCAL void do_cut(ch)
  420. struct channel *ch;
  421.    {
  422.    if (ch->retrig)
  423.       {
  424.       if (--ch->retrig == 0)
  425.          set_current_volume(ch, 0);
  426.       }
  427.    }
  428.  
  429. LOCAL void set_note_cut(a, ch)
  430. struct automaton *a;
  431. struct channel *ch;
  432.    {
  433.    ch->retrig = a->para;
  434.    ch->adjust = do_cut;
  435.    }
  436.  
  437.  
  438. LOCAL void set_smooth_up(a, ch)
  439. struct automaton *a;
  440. struct channel *ch;
  441.    {
  442.    ch->pitch += a->para;
  443.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  444.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  445.    set_current_pitch(ch, ch->pitch);
  446.    }
  447.  
  448. LOCAL void set_smooth_down(a, ch)
  449. struct automaton *a;
  450. struct channel *ch;
  451.    {
  452.    ch->pitch -= a->para;
  453.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  454.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  455.    set_current_pitch(ch, ch->pitch);
  456.    }
  457.  
  458. LOCAL void set_change_finetune(a, ch)
  459. struct automaton *a;
  460. struct channel *ch;
  461.    {
  462.    ch->finetune = a->para;
  463.    }
  464.  
  465.  
  466. LOCAL void set_loop(a, ch)
  467. struct automaton *a;
  468. struct channel *ch;
  469.    {
  470.       /* Note: the current implementation of protracker
  471.        * does not allow for a jump from pattern to pattern,
  472.        * even though it looks like a logical extension to the current 
  473.        * format.
  474.        */
  475.    if (a->para == 0) 
  476.       a->loop_note_num = a->note_num;
  477.    else
  478.       {
  479.       if (a->loop_counter == 0)
  480.          a->loop_counter = a->para + 1;
  481.       /* We have to defer the actual count-down and note jump
  482.        * to automaton.c, because some modules include several
  483.        * loops on the same measure, which is a bit confusing
  484.        * (see don't you want me)
  485.        */
  486.       a->do_stuff |= JUMP_PATTERN;
  487.       }
  488.    }
  489.  
  490. LOCAL void set_smooth_upvolume(a, ch)
  491. struct automaton *a;
  492. struct channel *ch;
  493.    {
  494.    set_current_volume(ch, ch->volume + a->para);
  495.    }
  496.  
  497. LOCAL void set_smooth_downvolume(a, ch)
  498. struct automaton *a;
  499. struct channel *ch;
  500.    {
  501.    set_current_volume(ch, ch->volume - a->para);
  502.    }
  503.  
  504.  
  505. LOCAL void set_delay_pattern(a, ch)
  506. struct automaton *a;
  507. struct channel *ch;
  508.    {
  509.    a->counter -= (a->para + 1) * a->speed;
  510.    a->do_stuff |= DELAY_PATTERN;
  511.    }
  512.  
  513.  
  514.  
  515. /* Initialize the whole effect table */
  516.  
  517. void init_effects(table)
  518. void (*table[]) P((struct automaton *a, struct channel *ch));
  519.    {
  520.    int i;
  521.  
  522.    for (i = 0; i < NUMBER_EFFECTS; i++)
  523.       table[i] = set_nothing;
  524.    table[EFF_ARPEGGIO] = set_arpeggio;
  525.    table[EFF_SPEED] = set_speed;
  526.    table[EFF_SKIP] = set_skip;
  527.    table[EFF_FF] = set_fastskip;
  528.    table[EFF_VOLUME] = set_volume;
  529.    table[EFF_VOLSLIDE] = set_slidevol;
  530.    table[EFF_OFFSET] = set_offset;
  531.    table[EFF_PORTA] = set_portamento;
  532.    table[EFF_PORTASLIDE] = set_portaslide;
  533.    table[EFF_UP] = set_upslide;
  534.    table[EFF_DOWN] = set_downslide;
  535.    table[EFF_VIBRATO] = set_vibrato;
  536.    table[EFF_VIBSLIDE] = set_vibratoslide;
  537.    table[EFF_SMOOTH_UP] = set_smooth_up;
  538.    table[EFF_SMOOTH_DOWN] = set_smooth_down;
  539.    table[EFF_CHG_FTUNE] = set_change_finetune;
  540.    table[EFF_LOOP] = set_loop;
  541.    table[EFF_RETRIG] = set_retrig;
  542.    table[EFF_S_UPVOL] = set_smooth_upvolume;
  543.    table[EFF_S_DOWNVOL] = set_smooth_downvolume;
  544.    table[EFF_NOTECUT] = set_note_cut;
  545.    table[EFF_LATESTART] = set_late_start;
  546.    table[EFF_DELAY] = set_delay_pattern;
  547.    }
  548.  
  549.