home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / cthugha5 / cthu5src / audiosdk.c < prev    next >
C/C++ Source or Header  |  1994-08-19  |  7KB  |  373 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. #include <string.h>
  18. #include <ctype.h>
  19.  
  20. #include "\sbpro\msc\include\sbc.h"
  21. #include "\sbpro\msc\include\sbcvoice.h"
  22.  
  23. #include "audio.h"
  24. #include "cthugha.h"
  25. #include "sb.h"
  26.  
  27. extern void draw_text(int xpos, int ypos, int size, int colour, char *tbuf);
  28.  
  29. int SDKok=0;
  30.  
  31. int sample_stereo;
  32. extern char     far * near voice_drv ;
  33. extern int in_vol;
  34.  
  35.  
  36. unsigned SbIOaddr;
  37. unsigned SbIRQ;
  38. unsigned SbDMAchan;
  39. extern int SBtype;
  40.  
  41. static int Sbpro_Get_Params(void);
  42.  
  43.  
  44. extern int (*init_audio)(void);
  45. extern void (*close_audio)(void);
  46. extern int (*audio_firsttime)(void);
  47. extern int (*audio_everytime)(int wait);
  48.  
  49. extern int  (*get_level)(int channel);
  50. extern void (*set_level)(int channel, int level);
  51. extern int  (*level_incr)(int channel);
  52.  
  53. extern void (*set_input)(enum device_list device);
  54.  
  55.  
  56. void SDK_close(void);
  57. int SDK_firsttime(void);
  58. int SDK_everytime(int wait);
  59. int SDK_get_level(int channel);
  60. static int Sbpro_Get_Params(void);
  61. int SDK_set_input(enum device_list device);
  62. int SDK_set_level(int channel, int level);
  63. int SDK_level_incr(int channel);
  64.  
  65. int SDK_init(void)
  66. {
  67.     Sbpro_Get_Params();
  68.     ct_io_addx = SbIOaddr;
  69.     buffer_size=2*BUFF_WIDTH+200;
  70. //    buffer_size=0x20000L;
  71.  
  72.     if (sbc_check_card() & 4) {
  73.         if (sbc_scan_int()) {
  74.  
  75.             if (!ctvm_init()) {
  76.  
  77.                 if (!_dos_allocmem((int)((buffer_size+15)>>4),&tempBuff)) {
  78.                     FP_SEG(voice_buffer) = tempBuff ;
  79.                     FP_OFF(voice_buffer) = 0 ;
  80.  
  81.                     voice_seg=FP_SEG(voice_buffer);
  82.  
  83.                 } else {
  84.                     ctvm_terminate() ;
  85.                     return -2;
  86.  
  87.                 }
  88.  
  89.  
  90.                 SDKok=1;
  91.  
  92.                 close_audio = SDK_close;
  93.                 audio_firsttime = SDK_firsttime;
  94.                 audio_everytime = SDK_everytime;
  95.  
  96.                 set_input = SDK_set_input;
  97.  
  98.                 get_level = SDK_get_level;
  99.                 set_level = SDK_set_level;
  100.  
  101.                 level_incr = SDK_level_incr;
  102.  
  103.                 return 0;
  104.  
  105.             }else {
  106.                 printf("Failed to init driver\n");
  107.             }
  108.         }
  109.         return 1;
  110.     } else {
  111.         printf("Sound Blaster card not found.\n") ;
  112.         return -1;
  113.     }
  114. }
  115.  
  116. void SDK_close(void)
  117. {
  118.     if (SDKok) {
  119.         ctvm_stop();
  120.         _dos_freemem(FP_SEG(voice_buffer));
  121.         ctvm_speaker(0);
  122.         ctvm_terminate();
  123.     }
  124. }
  125.  
  126.  
  127. int SDK_firsttime(void)
  128. {
  129.     set_input(device);
  130.  
  131.     ctvm_speaker(0) ;
  132.     ctvm_input(voice_buffer,(long)(2*BUFF_WIDTH+80),sample_rate);
  133.  
  134.     last_rate = sample_rate;
  135.  
  136.     return 0;
  137. }
  138.  
  139. int SDK_everytime(int wait)
  140. {
  141.     int x,y;
  142.     static int prev=128;
  143.     unsigned char far *sbuff ;
  144.     unsigned char far *sbuff2 ;
  145.     int noisy=0,last=0;
  146.     static int firsttime=1;
  147.     int ln,hn;
  148.     int v1,v2;
  149.  
  150.     while (ct_voice_status) {
  151.         if (kbhit())
  152.             ctvm_stop() ;
  153.     }
  154.  
  155.     sbuff=voice_buffer;
  156.     sbuff+=6;
  157.  
  158.     last=*sbuff;
  159.     sbuff++;
  160.     for (x=0; x<70; x+=2) {
  161.         //align...
  162.         if ((*sbuff)>=prev && last<prev-1) {
  163.             break;
  164.         } else {
  165.             last= *sbuff;
  166.         }
  167.     }
  168.  
  169.     prev=*sbuff;
  170.  
  171.     for (y=0; y<2; y++)
  172.         for (x=0; x<BUFF_WIDTH; x++) {
  173.             stereo[x][y]=*sbuff++;
  174.         }
  175.  
  176.     ctvm_speaker(0) ;
  177.  
  178.     ctvm_input(voice_buffer,(long)(2*BUFF_WIDTH+80),sample_rate);
  179.  
  180.     return 1;
  181.  
  182. }
  183.  
  184. #define MASTER_VOL  0x22
  185. #define VOC_VOL     0x04
  186. #define LINE_VOL    0x2E
  187. #define FM_VOL        0x26
  188. #define CD_VOL        0x28
  189. #define MIC_VOL        0x0A
  190. #define RECORD_SRC  0x0C
  191.  
  192. #define MIC        0x11
  193. #define LINE        0x17
  194. #define CD        0x13
  195.  
  196. int SDK_get_level(int channel)
  197. {
  198.     int vol,left,right;
  199.  
  200.     switch (channel) {
  201.         case MIXline:
  202.             outp(SbIOaddr+4, 0x2e);
  203.             vol = inp(SbIOaddr+5);
  204.  
  205.             left = vol >> 4;
  206.             right = vol & 0x0f;
  207.             break;
  208.         case MIXmicrophone:
  209.             outp(SbIOaddr+4, 0x0A );
  210.             vol = inp(SbIOaddr+5);
  211.  
  212.             left = vol >> 4;
  213.             right = vol & 0x0f;
  214.             break;
  215.         case MIXmaster:
  216.             outp(SbIOaddr+4, 0x22);
  217.             vol = inp(SbIOaddr+5);
  218.  
  219.             left = vol >> 4;
  220.             right = vol & 0x0f;
  221.             break;
  222.         case MIXcd:
  223.             outp(SbIOaddr+4, 0x28);
  224.             vol = inp(SbIOaddr+5);
  225.  
  226.             left = vol >> 4;
  227.             right = vol & 0x0f;
  228.             break;
  229.         default:
  230.         case MIXtreble:
  231.         case MIXbass:
  232.             left=0;
  233.             right=0;
  234.             break;
  235.  
  236.     }
  237. //    printf("(%d) R:%d L:%d\n",channel,right,left);
  238.     return ((right+left)/2)<<4;
  239. //    return right;
  240. }
  241.  
  242. #if 0
  243. {
  244.     unsigned char data_buf[(2*BUFF_WIDTH)+80];
  245.  
  246.     int i,left,right;
  247.  
  248.     int lmin = 255, lmax = 0, rmin = 255, rmax = 0;
  249.  
  250.     if( last_rate != 1 ) {
  251.         last_rate = 1;
  252.         dsp_set_record(4000,sample_stereo,8,0);
  253.     }
  254.  
  255.     if( dsp_read(data_buf) > 1 )
  256.         return 0;
  257.  
  258.     for( i = 0; i < buffer_size; i += 2 )
  259.     {
  260.         lmin = data_buf[i  ] < lmin ? data_buf[i] : lmin;
  261.         lmax = data_buf[i  ] > lmax ? data_buf[i] : lmax;
  262.  
  263.         rmin = data_buf[i+1] < rmin ? data_buf[i] : rmin;
  264.         rmax = data_buf[i+1] > rmax ? data_buf[i] : rmax;
  265.     }
  266.  
  267.     left  = abs(lmax-lmin);
  268.     right = abs(rmax-rmin);
  269.  
  270.     if( (lmax==255) || (lmin==0) )
  271.         left = 255;
  272.  
  273.     if( (rmax==255) || (rmin==0) )
  274.         right = 255;
  275.  
  276.  
  277.     return ((left+right)/2)<<4;
  278. }
  279. #endif
  280.  
  281. static int Sbpro_Get_Params(void)
  282. {
  283.     char *t, *t1, *blaster;
  284.  
  285.     /* Set arguments to reasonable values (Soundblaster defaults) */
  286.     SbIOaddr = 0x220;
  287.     SbIRQ = 7;
  288.     SbDMAchan = 1;
  289.     SBtype=2;
  290.  
  291.     /* Attempt to read environment variable */
  292.     t = getenv("BLASTER");
  293.  
  294.     /* Is the environment variable set? */
  295.     if(t == NULL)
  296.         return 1;
  297.  
  298.     /* Duplicate the string so that we don't trash our environment */
  299.     blaster = strdup(t);
  300.  
  301.     /* Now parse the BLASTER variable */
  302.     t = strtok(blaster," \t");
  303.     while(t) {
  304.         switch(toupper(t[0])) {
  305.             case 'A':                               /* I/O address */
  306.                 SbIOaddr = (int)strtol(t+1,&t1,16);
  307.                 break;
  308.             case 'I':                               /* Hardware IRQ */
  309.                 SbIRQ = atoi(t+1);
  310.                 break;
  311.             case 'D':                               /* DMA channel */
  312.                 SbDMAchan = atoi(t+1);
  313.                 break;
  314.             case 'T':                               /* Soundblaster type */
  315.                 SBtype = atoi(t+1);
  316.                 break;
  317.             default:
  318.                 printf("Unknown BLASTER option %c\n",t[0]);
  319.                 break;
  320.         }
  321.         t = strtok(NULL," \t");
  322.     }
  323.     free(blaster);
  324.     return 0;
  325. }
  326.  
  327.  
  328. int SDK_set_input(enum device_list device)
  329. {
  330.  
  331.     switch (device) {
  332.         case CDInput:
  333.             writemixer(RECORD_SRC,CD);
  334.             break;
  335.         case MicInput:
  336.             writemixer(RECORD_SRC,MIC);
  337.             break;
  338.         default:
  339.         case LineInput:
  340.             writemixer(RECORD_SRC,LINE);
  341.             break;
  342.     }
  343. }
  344.  
  345. int SDK_set_level(int channel, int level)
  346. {
  347.     int temp;
  348.  
  349.     temp=(level & 0xf0) + (level>>4);
  350.  
  351. //    printf("Set(%d) %d -> %d\n",channel,level,temp);
  352.     switch (channel) {
  353.         case MIXline:
  354.             writemixer(0x2e,temp);  // LINE 
  355.             break;
  356.         case MIXmicrophone:
  357.             writemixer(0x0A,temp>>1);      // MIC  
  358.             break;
  359.         case MIXcd:
  360.             writemixer(0x28,temp);      // CD   
  361.             break;
  362.         case MIXmaster:
  363.             writemixer(0x22,temp);      // Master
  364.             break;
  365.     }
  366.     return 0;
  367. }
  368.  
  369. int SDK_level_incr(int channel)
  370. {
  371.     return( channel == MIXmicrophone ? 64 : 32 );
  372. }
  373.