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

  1. /* resample.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: resample.c,v 4.5 1994/11/15 16:11:01 espie Exp espie $
  6.  * $Log: resample.c,v $
  7.  * Revision 4.5  1994/11/15  16:11:01  espie
  8.  * *** empty log message ***
  9.  *
  10.  * Revision 4.4  1994/08/23  18:19:46  espie
  11.  * New sampling function.
  12.  * Finally corrected irksome probleme with MAX_PITCH.
  13.  * First changes to augment the number of channels.
  14.  * Added one level of abstraction. Look through player.c for the
  15.  * old high-level functions.
  16.  * Optimized output and fixed up some details.
  17.  * Unrolled code for oversample = 1 to be more efficient at
  18.  * higher frequency (since a high frequency is better than
  19.  * a higher oversample).
  20.  *
  21.  * Revision 2.14  1992/11/06  19:31:53  espie
  22.  * Fixed missing parameter type.
  23.  * fix_xxx for better speed.
  24.  * set_volume.
  25.  * Added possibility to get back to MONO for the sgi.
  26.  * Added stereo capabilities to the indigo version.
  27.  * Minor bug: a SAMPLE_FAULT is a minor error,
  28.  * we should first check that there was no other
  29.  * error before setting it.
  30.  * New resample function coming from the player.
  31.  * Added more notes.
  32.  *
  33.  * Revision 2.1  1991/11/17  23:07:58  espie
  34.  * Just computes some frequency-related parameters.
  35.  *
  36.  *
  37.  */
  38.  
  39. #include <math.h>
  40. #ifndef __386BSD__
  41. #include <malloc.h>
  42. #else
  43. #include <stdlib.h>
  44. #endif
  45. #include <stdio.h>
  46.  
  47. #include "defs.h"
  48. #include "song.h"
  49. #include "channel.h"
  50. #include "tags.h"
  51. #include "extern.h"
  52.      
  53. ID("$Id: resample.c,v 4.5 1994/11/15 16:11:01 espie Exp espie $")
  54.  
  55.  
  56. /* DO_NOTHING is also used for the automaton */
  57. #define DO_NOTHING 0
  58. #define PLAY 1
  59. #define REPLAY 2
  60.  
  61. #define MAX_CHANNELS 8
  62.  
  63. LOCAL struct audio_channel
  64.    {
  65.    struct sample_info *samp;
  66.    int mode;
  67.    unsigned long pointer;
  68.    unsigned long step;
  69.    int volume;
  70.    int pitch;
  71.    } chan[MAX_CHANNELS];
  72.  
  73. LOCAL struct sample_info dummy =
  74.    {
  75.    NULL,
  76.    0,
  77.    0,
  78.    0,
  79.    0,
  80.    0,
  81.    0,
  82.    0,
  83.    NULL,
  84.    NULL
  85.    };
  86.  
  87. LOCAL int allocated = 0;
  88.  
  89. struct audio_channel *new_channel_tag_list(prop)
  90. struct tag *prop;
  91.    {
  92.    struct audio_channel *new;
  93.    new = &chan[allocated++];
  94.    new->mode = DO_NOTHING;
  95.    new->pointer = 0;
  96.    new->step = 0;
  97.    new->pitch = 0;
  98.    new->volume = 0;
  99.    new->samp = &dummy;
  100.    return new;
  101.    }
  102.  
  103. void release_audio_channels()
  104.    {
  105.    allocated = 0;
  106.    }
  107.  
  108. /* Have to get some leeway for vibrato (since we don't bound pitch with
  109.  * vibrato). This is conservative.
  110.  */
  111. #define VIB_MAXDEPTH 150
  112.  
  113.  
  114. #define C fix_to_int(ch->pointer)
  115.  
  116. LOCAL unsigned long step_table[REAL_MAX_PITCH + VIB_MAXDEPTH];  
  117.                   /* holds the increment for finding the next sampled
  118.                    * byte at a given pitch (see resample() ).
  119.                    */
  120.  
  121. /* creates a table for converting ``amiga'' pitch
  122.  * to a step rate at a given resampling frequency.
  123.  * For accuracy, we don't use floating point, but
  124.  * instead fixed point ( << ACCURACY).
  125.  * IMPORTANT NOTES:
  126.  * - we need to make it fit within 32 bits (long), which
  127.  * must be enough for ACCURACY + log2(max sample length)
  128.  * - for the linear resampling to work correctly, we need 
  129.  * sample size (8 bit) + volume size (6 bit) + ACCURACY to fit within a
  130.  * long. Appropriate steps will have to be taken when we switch to 16 bit
  131.  * samples...
  132.  * - never forget that samples are SIGNED numbers. If you have unsigned 
  133.  * samples, you have to convert them SOMEWHERE.
  134.  */
  135.  
  136. LOCAL void create_step_table(oversample, output_fr)
  137. int oversample;     /* we sample oversample i for each byte output */
  138. int output_fr;      /* output frequency */
  139.    {
  140.    double note_fr; /* note frequency (in Hz) */
  141.    double step;
  142.    int pitch;      /* amiga pitch */
  143.  
  144.         /* special case: oversample of 0 means linear resampling */
  145.     if (oversample == 0)
  146.         oversample = 1;
  147.    step_table[0] = 0;
  148.    for (pitch = 1; pitch < REAL_MAX_PITCH + VIB_MAXDEPTH; pitch++)
  149.       {
  150.       note_fr = AMIGA_CLOCKFREQ / pitch;
  151.          /* int_to_fix(1) is the normalizing factor */
  152.       step = note_fr / output_fr * int_to_fix(1) / oversample;
  153.       step_table[pitch] = (long)step;
  154.       }
  155.    }
  156.          
  157. LOCAL void readjust_pitch()
  158.    {
  159.    int i;
  160.    for (i = 0; i < allocated; i++)
  161.       chan[i].step = step_table[chan[i].pitch];
  162.    }
  163.  
  164. void init_tables(oversample, frequency)
  165. int oversample, frequency;
  166.    {
  167.    create_step_table(oversample, frequency);
  168.    readjust_pitch();
  169.    }
  170.  
  171.  
  172. /* The playing mechanism itself.
  173.  * According to the current channel automaton,
  174.  * we resample the instruments in real time to
  175.  * generate output.
  176.  */
  177. void resample(oversample, number)
  178. int oversample;
  179. int number;
  180.    {
  181.    int i;            /* sample counter */
  182.    int channel;      /* channel counter */
  183.    int sampling;     /* oversample counter */
  184.     int step;         /* fractional part for linear resampling */
  185.    long value[NUMBER_TRACKS];
  186.                      /* recombinations of the various data */
  187.    struct audio_channel *ch;
  188.  
  189.       /* safety check: we can't have a segv there, provided
  190.        * chan points to a valid sample.
  191.        * For `empty' samples, what is needed is fix_length = 0
  192.        * and rp_start = NULL
  193.        */
  194.  
  195.         /* do the resampling, i.e., actually play sounds */
  196.         /* code unfolding for special cases */
  197.     switch(oversample)
  198.         {
  199.     case 0:    /* linear resampling */
  200.       for (i = 0; i < number; i++) 
  201.          {
  202.          for (channel = 0; channel < allocated; channel++)
  203.             {
  204.             ch = chan + channel;
  205.             switch(ch->mode)
  206.                {
  207.             case DO_NOTHING:
  208.                value[channel] = 0;
  209.                break;
  210.             case PLAY:
  211.                   /* Since we now have fix_length, we can
  212.                    * do that check with improved performance
  213.                    */
  214.                if (ch->pointer < ch->samp->fix_length)
  215.                   {
  216.                         step = fractional_part(ch->pointer);
  217.                         value[channel] = 
  218.                             ( (ch->samp->start[C] * (total_step - step) +
  219.                                ch->samp->start[C+1] * step)
  220.                              * ch->volume
  221.                             ) >> ACCURACY ;
  222.                   ch->pointer += ch->step;
  223.                   break;
  224.                   }
  225.                else
  226.                   {
  227.                   ch->mode = REPLAY;
  228.                   ch->pointer -= ch->samp->fix_length;
  229.                   /* FALLTHRU */
  230.                   }
  231.             case REPLAY:
  232.                      /* is there a replay ? */
  233.                if (!ch->samp->rp_start)
  234.                   {
  235.                   ch->mode = DO_NOTHING;
  236.                   break;
  237.                   }
  238.                while (ch->pointer >= ch->samp->fix_rp_length)
  239.                   ch->pointer -= ch->samp->fix_rp_length;
  240.                     step = fractional_part(ch->pointer);
  241.                     value[channel] = 
  242.                          ( (ch->samp->rp_start[C] * (total_step - step) +
  243.                             ch->samp->rp_start[C+1] * step)
  244.                          * ch->volume 
  245.                         ) >> ACCURACY;
  246.                ch->pointer += ch->step;
  247.                break;
  248.                }
  249.             } 
  250.          output_samples(value[0]+value[3], value[1]+value[2]);
  251.          }
  252.         break;
  253.  
  254.     case 1:        /* no oversampling at all */
  255.       for (i = 0; i < number; i++) 
  256.          {
  257.          for (channel = 0; channel < allocated; channel++)
  258.             {
  259.             ch = chan + channel;
  260.             switch(ch->mode)
  261.                {
  262.             case DO_NOTHING:
  263.                value[channel] = 0;
  264.                break;
  265.             case PLAY:
  266.                   /* Since we now have fix_length, we can
  267.                    * do that check with improved performance
  268.                    */
  269.                if (ch->pointer < ch->samp->fix_length)
  270.                   {
  271.                   value[channel] = ch->samp->start[C] * ch->volume; 
  272.                   ch->pointer += ch->step;
  273.                   break;
  274.                   }
  275.                else
  276.                   {
  277.                   ch->mode = REPLAY;
  278.                   ch->pointer -= ch->samp->fix_length;
  279.                   /* FALLTHRU */
  280.                   }
  281.             case REPLAY:
  282.                      /* is there a replay ? */
  283.                if (!ch->samp->rp_start)
  284.                   {
  285.                   ch->mode = DO_NOTHING;
  286.                   break;
  287.                   }
  288.                while (ch->pointer >= ch->samp->fix_rp_length)
  289.                   ch->pointer -= ch->samp->fix_rp_length;
  290.                value[channel] = ch->samp->rp_start[C] * ch->volume; 
  291.                ch->pointer += ch->step;
  292.                break;
  293.                     }
  294.             } 
  295.          output_samples(value[0]+value[3], value[1]+value[2]);
  296.          }
  297.         break;
  298.     default:        /* standard oversampling code */
  299.       for (i = 0; i < number; i++) 
  300.          {
  301.          for (channel = 0; channel < allocated; channel++)
  302.             {
  303.             value[channel] = 0;
  304.             for (sampling = 0; sampling < oversample; sampling++)
  305.                {
  306.                ch = chan + channel;
  307.                switch(ch->mode)
  308.                   {
  309.                case DO_NOTHING:
  310.                   break;
  311.                case PLAY:
  312.                      /* Since we now have fix_length, we can
  313.                       * do that check with improved performance
  314.                       */
  315.                   if (ch->pointer < ch->samp->fix_length)
  316.                      {
  317.                      value[channel] += ch->samp->start[C] * ch->volume;
  318.                      ch->pointer += ch->step;
  319.                      break;
  320.                      }
  321.                   else
  322.                      {
  323.                      ch->mode = REPLAY;
  324.                      ch->pointer -= ch->samp->fix_length;
  325.                      /* FALLTHRU */
  326.                      }
  327.                case REPLAY:
  328.                         /* is there a replay ? */
  329.                   if (!ch->samp->rp_start)
  330.                      {
  331.                      ch->mode = DO_NOTHING;
  332.                      break;
  333.                      }
  334.                   while (ch->pointer >= ch->samp->fix_rp_length)
  335.                      ch->pointer -= ch->samp->fix_rp_length;
  336.                   value[channel] += ch->samp->rp_start[C] * ch->volume;
  337.                   ch->pointer += ch->step;
  338.                   break;
  339.                   }
  340.                } 
  341.             }
  342.          output_samples((value[0]+value[3])/oversample, 
  343.             (value[1]+value[2])/oversample);
  344.          }   
  345.       }
  346.  
  347.    flush_buffer();
  348.    }
  349.  
  350.  
  351. /* setting up a given note */
  352.  
  353. void play_note(au, samp, pitch)
  354. struct audio_channel *au;
  355. struct sample_info *samp;
  356. int pitch;
  357.    {
  358.    au->pointer = 0;
  359.    au->pitch = pitch;
  360.    au->step = step_table[pitch];
  361.    if (samp)
  362.       {
  363.       au->samp = samp;
  364.       au->mode = PLAY;
  365.       }
  366.    else
  367.       au->mode = DO_NOTHING;
  368.    }
  369.  
  370. /* changing the current pitch (value
  371.  * may be temporary, and not stored
  372.  * in channel pitch, for instance vibratos.
  373.  */
  374. void set_play_pitch(au, pitch)
  375. struct audio_channel *au;
  376. int pitch;
  377.    {
  378.       /* save current pitch in case we want to change
  379.        * the step table on the run
  380.        */
  381.    au->pitch = pitch;
  382.    au->step = step_table[pitch];
  383.    }
  384.  
  385. /* changing the current volume. You HAVE to get through
  386.  * there so that it will work on EVERY machine.
  387.  */
  388. void set_play_volume(au, volume)
  389. struct audio_channel *au;
  390. int volume;
  391.    {
  392.    au->volume = volume;
  393.    }
  394.  
  395. void set_play_position(au, pos)
  396. struct audio_channel *au;
  397. int pos;
  398.    {
  399.    au->pointer = int_to_fix(pos);
  400.    }
  401.  
  402.