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

  1. /* audio.c */
  2.  
  3. /* $Author: espie $
  4.  * $Id: audio.c,v 2.8 1991/12/03 21:24:53 espie Exp espie $
  5.  * $Revision: 2.8 $
  6.  * $Log: audio.c,v $
  7.  * Revision 2.8  1991/12/03  21:24:53  espie
  8.  * Added comments.
  9.  *
  10.  * Revision 2.7  1991/12/03  20:43:46  espie
  11.  * Added possibility to get back to MONO for the sgi.
  12.  *
  13.  * Revision 2.6  1991/12/03  18:07:38  espie
  14.  * Added stereo capabilities to the indigo version.
  15.  *
  16.  * Revision 2.5  1991/12/03  13:23:10  espie
  17.  * Minor bug: a SAMPLE_FAULT is a minor error,
  18.  * we should first check that there was no other
  19.  * error before setting it.
  20.  *
  21.  * Revision 2.4  1991/11/19  16:07:19  espie
  22.  * Added comments, moved minor stuff around.
  23.  *
  24.  * Revision 2.3  1991/11/18  14:10:30  espie
  25.  * New resample function coming from the player.
  26.  *
  27.  * Revision 2.2  1991/11/18  01:12:31  espie
  28.  * Added more notes.
  29.  *
  30.  * Revision 2.1  1991/11/17  23:07:58  espie
  31.  * Just computes some frequency-related parameters.
  32.  *
  33.  *
  34.  */
  35.  
  36. #include <math.h>
  37. #include <malloc.h>
  38.  
  39. #include "extern.h"
  40. #include "machine.h"
  41. #include "song.h"
  42. #include "channel.h"
  43.      
  44. static char *id = "$Id: audio.c,v 2.8 1991/12/03 21:24:53 espie Exp espie $";
  45.  
  46.  
  47. /* creates a table for converting ``amiga'' pitch
  48.  * to a step rate at a given resampling frequency.
  49.  * For accuracy, we don't use floating point, but
  50.  * instead fixed point ( << ACCURACY).
  51.  */
  52.  
  53. #define ACCURACY 16
  54. #define AMIGA_CLOCKFREQ 3575872
  55.  
  56. int step_table[MAX_PITCH];  
  57.                     /* holds the increment for finding the next sampled
  58.                      * byte at a given pitch (see resample() ).
  59.                      */
  60.  
  61. void create_step_table(oversample, output_fr)
  62. int oversample;     /* we sample oversample i for each byte output */
  63. int output_fr;      /* output frequency */
  64.     {
  65.     double note_fr; /* note frequency (in Hz) */
  66.     double step;
  67.     int pitch;      /* amiga pitch */
  68.  
  69.     step_table[0] = 0;
  70.     for (pitch = 1; pitch < MAX_PITCH; pitch++)
  71.         {
  72.         note_fr = AMIGA_CLOCKFREQ / pitch;
  73.             /* int_to_fix(1) is the normalizing factor */
  74.         step = note_fr / output_fr * int_to_fix(1) / oversample;
  75.         step_table[pitch] = (int)step;
  76.         }
  77.     }
  78.          
  79. /* the musical notes correspond to some specific pitch.
  80.  * It's useful to be able to find them back, at least for
  81.  * arpeggii.
  82.  */
  83. int pitch_table[NUMBER_NOTES];
  84.  
  85. void create_notes_table()
  86.     {
  87.     double base, pitch;
  88.     int i;
  89.  
  90.     base = AMIGA_CLOCKFREQ/440;
  91.     for (i = 0; i < NUMBER_NOTES; i++)
  92.         {
  93.         pitch = base / pow(2.0, i/12.0);
  94.         pitch_table[i] = pitch;
  95.         }
  96.     }
  97.  
  98. void init_tables(oversample, frequency)
  99. int oversample, frequency;
  100.     {
  101.     create_step_table(oversample, frequency);
  102.     create_notes_table();
  103.     }
  104.  
  105. #define C fix_to_int(ch->pointer)
  106.  
  107. /* The playing mechanism itself.
  108.  * According to the current channel automata,
  109.  * we resample the instruments in real time to
  110.  * generate output.
  111.  */
  112.  
  113. void resample(chan, oversample, number)
  114. struct channel *chan;
  115. int oversample;
  116. int number;
  117.     {
  118.     int i;          /* sample counter */
  119.     int channel;    /* channel counter */
  120.     int sampling;   /* oversample counter */
  121.     SAMPLE sample;  /* sample from the channel */
  122.     int byte[NUMBER_TRACKS];
  123.                     /* recombinations of the various data */
  124.     struct channel *ch;
  125.  
  126.         /* check the existence of samples */
  127.     for (channel = 0; channel < NUMBER_TRACKS; channel++)
  128.         if (!chan[channel].samp->start)
  129.             {
  130.             if (!error)
  131.                 error = SAMPLE_FAULT;
  132.             chan[channel].mode = DO_NOTHING;
  133.             }
  134.             
  135.         /* do the resampling, i.e., actually play sounds */
  136.     for (i = 0; i < number; i++) 
  137.         {
  138.         for (channel = 0; channel < NUMBER_TRACKS; channel++)
  139.             {
  140.             byte[channel] = 0;
  141.             for (sampling = 0; sampling < oversample; sampling++)
  142.                 {
  143.                 ch = chan + channel;
  144.                 switch(ch->mode)
  145.                     {
  146.                 case DO_NOTHING:
  147.                     break;
  148.                 case PLAY:
  149.                         /* small liability: the sample may have
  150.                          * changed, and we may be out of range.
  151.                          * However, this routine is time-critical,
  152.                          * so we don't check for this very rare case.
  153.                          */
  154.                     sample = ch->samp->start[C];
  155.                     byte[channel] += sample * ch->volume;
  156.                     ch->pointer += ch->step;
  157.                     if (C >= ch->samp->length)
  158.                         {
  159.                             /* is there a replay ? */
  160.                         if (ch->samp->rp_start) 
  161.                             {
  162.                             ch->mode = REPLAY;
  163.                             ch->pointer -= int_to_fix(ch->samp->length);
  164.                             }
  165.                         else
  166.                             ch->mode = DO_NOTHING;
  167.                         }
  168.                     break;
  169.                 case REPLAY:
  170.                         /* small liability: the sample may have
  171.                          * changed, and we may be out of range.
  172.                          * However, this routine is time-critical,
  173.                          * so we don't check for this very rare case.
  174.                          */
  175.                     sample = ch->samp->rp_start[C];
  176.                     byte[channel] += sample * ch->volume;
  177.                     ch->pointer += ch->step;
  178.                     if (C >= ch->samp->rp_length)
  179.                         ch->pointer -= int_to_fix(ch->samp->rp_length);
  180.                     break;
  181.                     }
  182.  
  183.                 } 
  184.             }
  185.         output_samples((byte[0]+byte[3])/oversample, 
  186.             (byte[1]+byte[2])/oversample);
  187.         }   
  188.  
  189.  
  190.     flush_buffer();
  191.     }
  192.  
  193.  
  194. /* setting up a given note */
  195.  
  196. void reset_note(ch, note, pitch)
  197. struct channel *ch;
  198. int note;
  199.     {
  200.     ch->pointer = 0;
  201.     ch->mode = PLAY;
  202.     ch->pitch = pitch;
  203.     ch->step = step_table[pitch];
  204.     ch->note = note;
  205.     ch->viboffset = 0;
  206.     }
  207.  
  208. /* changing the current pitch (value
  209.  * may be temporary, and not stored
  210.  * in channel pitch, for instance vibratos.
  211.  */
  212. void set_current_pitch(ch, pitch)
  213. struct channel *ch;
  214. int pitch;
  215.     {
  216.     ch->step = step_table[pitch];
  217.     }
  218.  
  219.