home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / univspl / reverb_w.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-15  |  3.7 KB  |  139 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "audioc.h"
  4. #include "uspl.h"
  5.  
  6. /* Reverb Wave Routine Delays an audio object by specified amounts */
  7. long reverb_wav(AudioC &Wav,float delay, float amp, AudioC &newwave)
  8. {
  9. long delay_samps;
  10. float *fbuf_l=NULL,*fbuf_r=NULL;
  11. float *tbuf;
  12. long fbuf_samps;
  13. float scale_factor;
  14. long orig_samps;
  15. long step=1;
  16. long status=1;
  17. char *cptr;
  18. short *sptr;
  19. long one=1;
  20. long cur_samp=0;
  21. float f;
  22.  
  23.    // compute scale factor
  24.    scale_factor=amp/100.0;
  25.    // compute the number of samples to delay by
  26.    f=delay/1000.0*Wav.GetSampleRate();
  27.    delay_samps=f;
  28.    
  29.    // compute max size of float buf we will use
  30.    orig_samps=Wav.GetNumSamps()/Wav.GetNumChan();
  31.    fbuf_samps=orig_samps+delay_samps*3;
  32.  
  33.    if (fbuf_samps<=0) return 0;
  34.  
  35.  
  36.    // allocate memory for temp buf
  37.    tbuf=(float*) malloc(fbuf_samps*sizeof(float));
  38.    if (tbuf==NULL) status=0;
  39.    // allocate memory for 1 chan (always have at least 1)
  40.    fbuf_l=(float*) malloc(fbuf_samps*sizeof(float));
  41.    if (fbuf_l==NULL) status=0;
  42.  
  43.    // if we have 2 channels
  44.    if (Wav.GetNumChan()==2)
  45.    {
  46.       step=2;
  47.       fbuf_r=(float*) malloc(2*fbuf_samps*sizeof(float));
  48.       if (fbuf_r==NULL) status=0;
  49.    }
  50.  
  51.    // make sure we are ok
  52.    if (status)
  53.    {
  54.       // clear the buffers
  55.       vclr_(fbuf_l,&one,&fbuf_samps);
  56.       if (step==2) vclr_(fbuf_r,&one,&fbuf_samps);
  57.       vclr_(tbuf,&one,&fbuf_samps);
  58.       sptr=(short*) Wav.GetBuffer();
  59.       cptr=(char*) sptr;
  60.  
  61.       // demux data, convert to float in temp buffer
  62.       // 1st case 8 bits/sample ie. 1 byte/samp
  63.       if (Wav.GetBitsPerSamp()==8)
  64.       {
  65.          fltb_(cptr, &step, fbuf_r, &one, &orig_samps);
  66.          // second channel if reqd
  67.          if (step==2)
  68.             fltb_(&cptr[1], &step, fbuf_l, &one, &orig_samps);
  69.       }
  70.       // 2nd case 16 bits/sample ie. 2 bytes/samp
  71.       if (Wav.GetBitsPerSamp()==16)
  72.       {
  73.          flt2_(sptr, &step, fbuf_r, &one, &orig_samps);
  74.          // second channel if reqd
  75.          if (step==2)
  76.             flt2_(&sptr[1], &step, fbuf_l, &one, &orig_samps);
  77.       }
  78.  
  79.       // while we have enough room
  80.       while (fbuf_samps-cur_samp>orig_samps)
  81.       {
  82.  
  83.          cur_samp+=delay_samps;
  84.          // do left channel
  85.          // scale data & save in temp buf
  86.          vsmul_(fbuf_l,&one,&scale_factor,tbuf,&one,&orig_samps);
  87.          // add temp buf to original data
  88.          vadd_(tbuf,&one,&fbuf_l[cur_samp],&one,&fbuf_l[cur_samp],&one,&orig_samps);
  89.  
  90.          //do right channel
  91.          if (Wav.GetNumChan()==2)
  92.          {
  93.             // scale data & save in temp buf
  94.             vsmul_(fbuf_r,&one,&scale_factor,tbuf,&one,&orig_samps);
  95.             // add temp buf to original data
  96.             vadd_(tbuf,&one,&fbuf_r[cur_samp],&one,&fbuf_r[cur_samp],&one,&orig_samps);
  97.          }
  98.          // adjust scale factor down
  99.          scale_factor*=scale_factor;
  100.  
  101.  
  102.       }
  103.  
  104.     }
  105.  
  106.    // change samples
  107.    newwave.SetNumSamps(fbuf_samps);
  108.  
  109.    // convert & mux the data
  110.    // 8 bit data
  111.    if (newwave.GetBitsPerSamp()==8)
  112.    {
  113.        cptr=(char *)newwave.GetBuffer();
  114.        fixbn_(fbuf_l,&one,(unsigned char *)cptr,&step,&fbuf_samps);
  115.        if (newwave.GetNumChan()==2)
  116.         fixbn_(fbuf_r,&one,(unsigned char *)&cptr[1],&step,&fbuf_samps);
  117.    }
  118.  
  119.    // 16 bit data
  120.    if (newwave.GetBitsPerSamp()==16)
  121.    {
  122.        sptr=(short *)newwave.GetBuffer();
  123.        fix2n_(fbuf_l,&one,sptr,&step,&fbuf_samps);
  124.        if (newwave.GetNumChan()==2)
  125.         fix2n_(fbuf_r,&one,&sptr[1],&step,&fbuf_samps);
  126.    }
  127.  
  128.    // free up old buffers
  129.    if (fbuf_l!=NULL) free(fbuf_l);
  130.    if (fbuf_r!=NULL) free(fbuf_r);
  131.    if (tbuf!=NULL) free(tbuf);
  132.  
  133.    if (status==0) return (0); else return(1);
  134.  
  135. }
  136.  
  137.  
  138.  
  139.