home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / libsst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-10  |  10.5 KB  |  499 lines

  1. /* libsst.c - SPARC sound tools library
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11.  
  12. ** Hacked on by jwz for emacs.
  13.  
  14. */
  15.  
  16. #if __STDC__
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #endif
  20. #include <stdio.h>
  21. #include <fcntl.h>
  22. #include "libsst.h"
  23.  
  24. #define AUDBUF 1024
  25.  
  26. extern void usleep();
  27.  
  28. int
  29. sst_open(play_level, record_level)
  30.     int play_level, record_level;
  31.     {
  32.     int fd, i, gr, ger, gx;
  33.     struct audio_ioctl ai;
  34.     char *getenv(), *ep;
  35.  
  36.     fd = open( "/dev/audio", O_RDWR );
  37.     if ( fd < 0 )
  38.     {
  39.     perror( "sst_open: open /dev/audio" );
  40.     return( fd );
  41.     }
  42.  
  43. #ifdef AUDIOSETQSIZE /* This no longer exists as of 4.1.2. */
  44.  
  45.     /* Shrink audio device's queue size, to cut down time delay. */
  46.     i = AUDBUF;
  47.     if ( ioctl( fd, AUDIOSETQSIZE, &i ) < 0 )
  48.     {
  49.     perror( "sst_open: SETQSIZE" );
  50.     return( fd );
  51.     }
  52. #endif /* AUDIOSETQSIZE */
  53.  
  54.     /* Set gains.  -10 <= ger <= 18,  -18 <= gr <= 12,  -18 <= gx <= 12. */
  55.     if (!play_level) 
  56.     {
  57.     play_level = 75;
  58.     if ( (ep = getenv( "SST_PLAY" )) != NULL )
  59.     {
  60.         play_level = atoi( ep );
  61.         if ( play_level < 0 || play_level > 99 )
  62.         {
  63.         warn( "sst_open: SST_PLAY must be between 0 and 99" );
  64.         return( -1 );
  65.         }
  66.     }
  67.     }
  68.     if (!record_level) 
  69.     {
  70.     record_level = 75;
  71.     if ( (ep = getenv( "SST_RECORD" )) != NULL )
  72.     {
  73.         record_level = atoi( ep );
  74.         if ( record_level < 0 || record_level > 99 )
  75.         {
  76.         warn( "sst_open: SST_RECORD must be between 0 and 99" );
  77.         return( -1 );
  78.         }
  79.     }
  80.     }
  81.  
  82.     play_level = play_level * 59 / 100 - 28;
  83.     ger = play_level / 2;
  84.     gr = play_level - ger;
  85.     if ( ger < -10 )
  86.     {
  87.     ger = -10;
  88.     gr = play_level - ger;
  89.     }
  90.     if ( gr > 12 )
  91.     {
  92.     gr = 12;
  93.     ger = play_level - gr;
  94.     }
  95.     gx = record_level * 31 / 100 - 18;
  96.     sst_set_gr( fd, gr );
  97.     sst_set_ger( fd, ger );
  98.     sst_set_gx( fd, gx );
  99.  
  100.     /*  Initialize the MMR2 register to send the output to either
  101.     **  the speaker or the earphone jack, depending on SST_EARPHONES.
  102.     */
  103.     ai.control = AUDIO_MAP_MMR2;
  104.     if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
  105.     {
  106.     perror( "sst_open: GETREG MMR2" );
  107.     return( -1 );
  108.     }
  109.     if ( (ep = getenv( "SST_EARPHONES" )) != NULL )
  110.     ai.data[0] &= ~AUDIO_MMR2_BITS_LS;
  111.     else
  112.     ai.data[0] |= AUDIO_MMR2_BITS_LS;
  113.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  114.     {
  115.     perror( "sst_open: SETREG MMR2" );
  116.     return( fd );
  117.     }
  118.  
  119.     return fd;
  120.     }
  121.  
  122. void
  123. sst_close( fd )
  124. int fd;
  125.     {
  126.     struct audio_ioctl ai;
  127.  
  128.     ai.control = AUDIO_MAP_MMR1;
  129.     ai.data[0] = 0;
  130.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  131.     {
  132.     perror( "sst_close: SETREG MMR1" );
  133.     }
  134.     ai.control = AUDIO_MAP_MMR2;
  135.     ai.data[0] = 0;
  136.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  137.     {
  138.     perror( "sst_close: SETREG MMR2" );
  139.     }
  140.     close( fd );
  141.     }
  142.  
  143. /* These are tables of values to be loaded into various gain registers.
  144. */
  145.  
  146. static unsigned char ger_table[][2] = {
  147.     0xaa,    0xaa,    /* -10db */
  148.     0x79,    0xac,
  149.     0x41,    0x99,
  150.     0x9c,    0xde,
  151.     0x74,    0x9c,    /* -6db */
  152.     0x6a,    0xae,
  153.     0xab,    0xdf,
  154.     0x64,    0xab,
  155.     0x2a,    0xbd,
  156.     0x5c,    0xce,
  157.     0x00,    0x99,    /* 0db */
  158.     0x43,    0xdd,
  159.     0x52,    0xef,
  160.     0x55,    0x42,
  161.     0x31,    0xdd,
  162.     0x43,    0x1f,
  163.     0x40,    0xdd,    /* 6db */
  164.     0x44,    0x0f,
  165.     0x31,    0x1f,
  166.     0x10,    0xdd,
  167.     0x41,    0x0f,
  168.     0x60,    0x0b,
  169.     0x42,    0x10,    /* 12db */
  170.     0x11,    0x0f,
  171.     0x72,    0x00,
  172.     0x21,    0x10,
  173.     0x22,    0x00,
  174.     0x00,    0x0b,
  175.     0x00,    0x0f,    /* 18db */
  176.     };
  177.  
  178.  
  179. static unsigned char gr_gx_table[][2] = {
  180.     0x8b,    0x7c,    /* -18db */
  181.     0x8b,    0x35,
  182.     0x8b,    0x24,
  183.     0x91,    0x23,
  184.     0x91,    0x2a,
  185.     0x91,    0x3b,
  186.     0x91,    0xf9,    /* -12db */
  187.     0x91,    0xb6,
  188.     0x91,    0xa4,
  189.     0x92,    0x32,
  190.     0x92,    0xaa,
  191.     0x93,    0xb3,
  192.     0x9f,    0x91,    /* -6db */
  193.     0x9b,    0xf9,
  194.     0x9a,    0x4a,
  195.     0xa2,    0xa2,
  196.     0xaa,    0xa3,
  197.     0xbb,    0x52,
  198.     0x08,    0x08,    /* 0db */
  199.     0x3d,    0xac,
  200.     0x25,    0x33,
  201.     0x21,    0x22,
  202.     0x12,    0xa2,
  203.     0x11,    0x3b,
  204.     0x10,    0xf2,    /* 6db */
  205.     0x02,    0xca,
  206.     0x01,    0x5a,
  207.     0x01,    0x12,
  208.     0x00,    0x32,
  209.     0x00,    0x13,
  210.     0x00,    0x0e,    /* 12db */
  211.     };
  212.  
  213. void
  214. sst_set_ger( fd, value )
  215. int fd, value;
  216.     {
  217.     struct audio_ioctl ai;
  218.  
  219.     if ( ( value < -10 ) || ( value > 18 ) )
  220.     {
  221.       char buf [255];
  222.       sprintf (buf, "sst_set_ger: GER %d out of range", value);
  223.       warn(buf);
  224.       return;
  225.     }
  226.  
  227.     /*  Add 10 to the value to get the index into the table.  */
  228.     ai.control = AUDIO_MAP_GER;
  229.     ai.data[0] = ger_table[value + 10][1];
  230.     ai.data[1] = ger_table[value + 10][0];
  231.  
  232.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  233.     {
  234.     perror( "sst_set_ger: SETREG GER" );
  235.     }
  236.  
  237.     ai.control = AUDIO_MAP_MMR1;
  238.     if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
  239.     {
  240.     perror( "sst_set_ger: GETREG MMR1" );
  241.     }
  242.     ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GER;
  243.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  244.     {
  245.     perror( "sst_set_ger: SETREG MMR1" );
  246.     }
  247.     }
  248.  
  249. void
  250. sst_set_gr( fd, value )
  251. int fd, value;
  252.     {
  253.     struct audio_ioctl ai;
  254.  
  255.     if ( ( value < -18 ) || ( value > 12 ) )
  256.     {
  257.       char buf [255];
  258.       sprintf (buf,  "sst_set_gr: GR %d out of range", value);
  259.       warn (buf);
  260.       return;
  261.     }
  262.  
  263.     ai.control = AUDIO_MAP_GR;
  264.     ai.data[0] = gr_gx_table[value + 18][1];
  265.     ai.data[1] = gr_gx_table[value + 18][0];
  266.  
  267.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  268.     {
  269.     perror( "sst_set_gr: SETREG GR" );
  270.     }
  271.  
  272.     ai.control = AUDIO_MAP_MMR1;
  273.     if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
  274.     {
  275.     perror( "sst_set_gr: GETREG MMR1" );
  276.     }
  277.     ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GR;
  278.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  279.     {
  280.     perror( "sst_set_gr: SETREG MMR1" );
  281.     }
  282.     }
  283.  
  284. void
  285. sst_set_gx( fd, value )
  286. int fd, value;
  287.     {
  288.     struct audio_ioctl ai;
  289.     char buf [255];
  290.  
  291.     if ( ( value < -18 ) || ( value > 12 ) )
  292.     {
  293.       sprintf (buf, "sst_set_gx: GX %d out of range", value);
  294.       warn (buf);
  295.       return;
  296.     }
  297.  
  298.     /*  We add 18 to get the index into the table, since entry 0 represents
  299.     *  -18db.
  300.     */
  301.     ai.control = AUDIO_MAP_GX;
  302.     ai.data[0] = gr_gx_table[value + 18][1];
  303.     ai.data[1] = gr_gx_table[value + 18][0];
  304.  
  305.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  306.     {
  307.     perror( "sst_set_gx: SETREG GX" );
  308.     }
  309.  
  310.     ai.control = AUDIO_MAP_MMR1;
  311.     if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
  312.     {
  313.     perror( "sst_set_gx: GETREG MMR1" );
  314.     }
  315.     ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GX;
  316.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  317.     {
  318.     perror( "sst_set_gx: SETREG MMR1" );
  319.     }
  320.     }
  321.  
  322. void
  323. sst_tones( fd, dhz1, dhz2, thz, rhz, usec )
  324. int fd, dhz1, dhz2, thz, rhz, usec;
  325.     {
  326.     char buf [255];
  327.     struct audio_ioctl ai;
  328.     int dval1, dval2, tval, rval;
  329.     unsigned char oldmmr2, newmmr2;
  330.  
  331.     if ( dhz1 == 0 )
  332.     dval1 = 0;
  333.     else
  334.     {
  335.     dval1 = ( dhz1 * 128 + 63 ) / 1000;
  336.     if ( ( dval1 < 1 ) || ( dval1 > 255 ) )
  337.         {
  338.           sprintf(buf, "sst_tones: dhz1 %d out of range", dhz1 );
  339.           warn (buf);
  340.           return;
  341.         }
  342.     }
  343.  
  344.     if ( dhz2 == 0 )
  345.     dval2 = 0;
  346.     else
  347.     {
  348.     dval2 = ( dhz2 * 128 + 63 ) / 1000;
  349.     if ( ( dval2 < 1 ) || ( dval2 > 255 ) )
  350.         {
  351.           sprintf(buf, "sst_tones: dhz2 %d out of range", dhz2 );
  352.           warn (buf);
  353.           return;
  354.         }
  355.     }
  356.  
  357.     if ( thz == 0 )
  358.     tval = 0;
  359.     else
  360.     {
  361.     tval = ( thz * 128 + 63 ) / 2000;
  362.     if ( ( tval < 1 ) || ( tval > 255 ) )
  363.         {
  364.           sprintf(buf, "sst_tones: thz %d out of range", thz );
  365.           warn (buf);
  366.           return;
  367.         }
  368.     }
  369.  
  370.     if ( rhz == 0 )
  371.     rval = 0;
  372.     else
  373.     {
  374.     rval = ( rhz * 128 + 63 ) / 2000;
  375.     if ( ( rval < 1 ) || ( rval > 255 ) )
  376.         {
  377.           sprintf(buf, "sst_tones: rhz %d out of range", dhz2 );
  378.           warn (buf);
  379.           return;
  380.         }
  381.     }
  382.  
  383.     if ( ( dval1 != 0 || dval2 != 0 ) && ( tval != 0 || rval != 0 ) )
  384.     {
  385.       sprintf(buf, "sst_tones: cannot use DTMF and TONE or RINGER at the same time", dhz2 );
  386.       warn (buf);
  387.       return;
  388.     }
  389.  
  390.     if ( tval != 0 && rval != 0 )
  391.     {
  392.       sprintf(buf, "sst_tones: cannot use TONE and RINGER at the same time", dhz2 );
  393.       warn (buf);
  394.     return;
  395.     }
  396.  
  397.     ai.control = AUDIO_MAP_MMR2;
  398.     if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
  399.     {
  400.     perror( "sst_tones: GETREG MMR2" );
  401.     }
  402.     oldmmr2 = newmmr2 = ai.data[0];
  403.  
  404.     if ( dval1 != 0 || dval2 != 0 )
  405.     {
  406.     newmmr2 |= AUDIO_MMR2_BITS_DTMF;
  407.     ai.control = AUDIO_MAP_FTGR;
  408.     ai.data[0] = dval1;
  409.     ai.data[1] = dval2;
  410.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  411.         {
  412.         perror( "sst_tones: SETREG FTGR" );
  413.         }
  414.     }
  415.  
  416.     if ( tval != 0 )
  417.     {
  418.     newmmr2 |= AUDIO_MMR2_BITS_TONE;
  419.     ai.control = AUDIO_MAP_FTGR;
  420.     ai.data[0] = tval;
  421.     ai.data[1] = 0;
  422.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  423.         {
  424.         perror( "sst_tones: SETREG FTGR" );
  425.         }
  426.     }
  427.  
  428.     if ( rval != 0 )
  429.     {
  430.     newmmr2 |= AUDIO_MMR2_BITS_RINGER;
  431.     ai.control = AUDIO_MAP_FTGR;
  432.     ai.data[0] = rval;
  433.     ai.data[1] = 0;
  434.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  435.         {
  436.         perror( "sst_tones: SETREG FTGR" );
  437.         }
  438.     }
  439.  
  440.     ai.control = AUDIO_MAP_MMR2;
  441.     ai.data[0] = newmmr2;
  442.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  443.     {
  444.     perror( "sst_tones: SETREG MMR2" );
  445.     }
  446.  
  447.     usleep( usec );
  448.  
  449.     ai.data[0] = oldmmr2;
  450.     if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
  451.     {
  452.     perror( "sst_tones: SETREG MMR2" );
  453.     }
  454.     }
  455.  
  456. void
  457. sst_dtmf( fd, dial, usecper, usecpause )
  458. int fd, usecper, usecpause;
  459. char *dial;
  460.     {
  461.     char *cp;
  462.  
  463.     for ( cp = dial; *cp != '\0'; cp++ )
  464.     {
  465.     switch ( *cp )
  466.         {
  467.         case '1': sst_tones( fd, 703, 1211, 0, 0, usecper ); break;
  468.         case '2': sst_tones( fd, 703, 1336, 0, 0, usecper ); break;
  469.         case '3': sst_tones( fd, 703, 1492, 0, 0, usecper ); break;
  470.         case 'A': sst_tones( fd, 703, 1648, 0, 0, usecper ); break;
  471.         case '4': sst_tones( fd, 773, 1211, 0, 0, usecper ); break;
  472.         case '5': sst_tones( fd, 773, 1336, 0, 0, usecper ); break;
  473.         case '6': sst_tones( fd, 773, 1492, 0, 0, usecper ); break;
  474.         case 'B': sst_tones( fd, 773, 1648, 0, 0, usecper ); break;
  475.         case '7': sst_tones( fd, 859, 1211, 0, 0, usecper ); break;
  476.         case '8': sst_tones( fd, 859, 1336, 0, 0, usecper ); break;
  477.         case '9': sst_tones( fd, 859, 1492, 0, 0, usecper ); break;
  478.         case 'C': sst_tones( fd, 859, 1648, 0, 0, usecper ); break;
  479.         case '*': sst_tones( fd, 945, 1211, 0, 0, usecper ); break;
  480.         case '0': sst_tones( fd, 945, 1336, 0, 0, usecper ); break;
  481.         case '#': sst_tones( fd, 945, 1492, 0, 0, usecper ); break;
  482.         case 'D': sst_tones( fd, 945, 1648, 0, 0, usecper ); break;
  483.  
  484.         case ' ': case '-': case '(': case ')': case '+':
  485.         continue;    /* ignore */
  486.  
  487.         case ',': usleep( usecper ); break;    /* big pause */
  488.  
  489.         default:
  490.           {
  491.         char buf [255];
  492.         sprintf( buf, "sst_dtmf: unknown dialing code '%c'", *cp );
  493.         warn (buf);
  494.           }
  495.         }
  496.     usleep( usecpause );
  497.     }
  498.     }
  499.