home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / cthugha5 / cthu5src / audio.c next >
C/C++ Source or Header  |  1994-08-19  |  4KB  |  235 lines

  1. //
  2. // Cthugha - Audio Seeded Image Processing
  3. //
  4. // Zaph, Digital Aasvogel Group, Torps Productions 1993-1994
  5. //
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <dos.h>
  10. #include <io.h>
  11. #include <fcntl.h>
  12. #include <stdlib.h>
  13. #include <math.h>
  14. #include <conio.h>
  15. #include <assert.h>
  16. #include <bios.h>
  17.  
  18. #include "cthugha.h"
  19. //#include "audio.h"
  20. //#include "sb.h"
  21.  
  22. enum device_list { CDInput, LineInput, MicInput } device = CDInput;
  23.  
  24. extern void draw_text(int xpos, int ypos, int size, int colour, char *tbuf);
  25.  
  26. #if 0
  27. unsigned  tempBuff;
  28. //long      buffer_size = 0x20000L;
  29. //long buffer_size = (2*BUFF_WIDTH)+80;
  30. unsigned int buffer_size = (2*BUFF_WIDTH)+80;
  31. char      far *voice_buffer ;
  32. unsigned int voice_seg;
  33. int load_drv(void);
  34. int SBok=0;
  35. #endif
  36.  
  37. int sample_stereo=1;
  38. int display_flag;
  39. int wait_flag=1;
  40. int peaknoise=0;
  41. int peaklevel=220;
  42. int peakframes=16;
  43. int peaks=0;
  44.  
  45. extern int debug_mode;
  46. int (*init_audio)(void)=NULL;
  47. int (*close_audio)(void)=NULL;
  48. int (*audio_firsttime)(void)=NULL;
  49. int (*audio_everytime)(int wait)=NULL;
  50.  
  51. int (*get_level)(int channel)=NULL;
  52. void (*set_level)(int channel, int level)=NULL;
  53. int  (*level_incr)(int channel)=NULL;
  54.  
  55. void (*set_input)(enum device_list device)=NULL;
  56.  
  57. unsigned last_rate = 65535;
  58. unsigned actual_rate;
  59.  
  60. unsigned vu_rate = 11000;
  61.  
  62. // NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  63. //
  64. // As you probably noticed - this isn't REAL stereo :-)
  65. //
  66. // If you can get stereo with any card, let me know!!
  67. //
  68.  
  69. int get_stereo(void)
  70. {
  71.     int x,y;
  72.     static int prev=128;
  73. //    unsigned char far *sbuff ;
  74. //    unsigned char far *sbuff2 ;
  75.     int noisy=0,last=0;
  76.     static int firsttime=1;
  77.     int ln,hn;
  78.     int v1,v2;
  79.  
  80.     int do_display = 0;
  81.  
  82.     if( sample_rate != last_rate )
  83.     {
  84.         if( last_rate == 65535 )
  85.             display_flag = 1;
  86.  
  87.         if( !display_flag )
  88.             do_display = 1;
  89.  
  90.         if( sample_rate != 0 )
  91.             last_rate = sample_rate;
  92.         else
  93.             sample_rate = last_rate;
  94.  
  95.         display_flag = 0;
  96.     }
  97.  
  98.     if( sample_rate == 0 )
  99.     {
  100.         sample_rate = last_rate;
  101.         last_rate = 0;
  102.     }
  103.  
  104.     if (debug_mode) {
  105.         stereo[x][0]=128;
  106.         stereo[x][1]=128;
  107.         for (x=1; x<BUFF_WIDTH; x++) {
  108.     
  109.             v1 += rand()%7-3;
  110.             v2 += rand()%7-3;
  111.  
  112.             stereo[x][0]=stereo[x-1][0]+v1;
  113.             stereo[x][1]=stereo[x-1][1]+v2;
  114.  
  115.             if (stereo[x][0]<0) {
  116.                 stereo[x][0]=0;
  117.                 v1=1;
  118.             } else if (stereo[x][0]>255) {
  119.                 stereo[x][0]=255;
  120.                 v1=-1;
  121.             }
  122.             if (stereo[x][1]<0) {
  123.                 stereo[x][1]=0;
  124.                 v2=1;
  125.             } else if (stereo[x][1]>255) {
  126.                 stereo[x][1]=255;
  127.                 v2=-1;
  128.             }
  129.         }
  130.         return 1;
  131.     }
  132.  
  133.     if (firsttime) {
  134.  
  135.         firsttime=0;
  136.         audio_firsttime();
  137.     }
  138.  
  139.     if( audio_everytime(!wait_flag)==0 )
  140. //    if( audio_everytime(1)==0 )
  141.         return -1;
  142.  
  143.     wait_flag = 0;
  144.  
  145.     if( do_display )
  146.     {
  147.         char buf[8];
  148.  
  149.         draw_text(0,42,1,255,ltoa(actual_rate,buf,10));
  150.         draw_text(0,52,1,255,sample_stereo ? "Stereo" : "Mono" );
  151.     }
  152.  
  153.     ln=255;
  154.     hn=0;
  155.     noisy=0;
  156.     peaknoise=0;
  157.  
  158.     for (y=0; y<2; y++) {
  159.         ln=255;
  160.         hn=0;
  161.         for (x=0; x<BUFF_WIDTH; x++) {
  162.             if (stereo[x][y]>hn)
  163.                 hn=stereo[x][y];
  164.             if (stereo[x][y]<ln)
  165.                 ln=stereo[x][y];
  166.             if (!noisy)
  167.                 if ((hn-ln)>(minnoise)) {
  168.                     noisy=1;
  169.                 }
  170.             if (!peaknoise && peakframes>0)
  171.                 if ((hn-ln)>(peaklevel)) {
  172.                     peaknoise=1;
  173.                 }
  174.         }
  175.         if (noisy && peaknoise)
  176.             break;
  177.     }
  178.  
  179.     return noisy;
  180. }
  181.  
  182.  
  183. int get_levels(int *left, int *right)
  184. {
  185.     unsigned last;
  186.  
  187.     int i;
  188.  
  189.     int lmin = 255, lmax = 0, rmin = 255, rmax = 0;
  190.  
  191.     last = sample_rate;
  192.  
  193.     display_flag = 1;     // Suppress display of sample rate
  194.     wait_flag = 1;         // Don't wait for data
  195.  
  196.  
  197.     if( vu_rate > 0 )
  198.         sample_rate = vu_rate;
  199.  
  200.     if( get_stereo() == -1 )
  201.     {
  202.         sample_rate = last;
  203.         display_flag = 1;
  204.  
  205.         return 0;
  206.     }
  207.  
  208.     for( i = 0; i < BUFF_WIDTH; i++ )
  209.     {
  210.         lmin = stereo[i][0] < lmin ? stereo[i][0] : lmin;
  211.         lmax = stereo[i][0] > lmax ? stereo[i][0] : lmax;
  212.  
  213.         rmin = stereo[i][1] < rmin ? stereo[i][1] : rmin;
  214.         rmax = stereo[i][1] > rmax ? stereo[i][1] : rmax;
  215.     }
  216.  
  217.     *left  = abs(lmax-lmin);
  218.     *right = abs(rmax-rmin);
  219.  
  220.     if( (lmax==255) || (lmin==0) )
  221.         *left = 255;
  222.  
  223.     if( (rmax==255) || (rmin==0) )
  224.         *right = 255;
  225.  
  226.     if( !sample_stereo )
  227.         *left = *right = max(*left,*right);
  228.  
  229.     display_flag = 1;
  230.     sample_rate = last;
  231.  
  232.     return 1;
  233. }
  234.  
  235.