1 /* libsst.c - SPARC sound tools library
3 ** Copyright (C) 1989 by Jef Poskanzer.
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
12 ** Hacked on by jwz for emacs.
16 /* Synched up with: Not in FSF. */
40 sst_open(play_level, record_level)
41 int play_level, record_level;
43 int fd, i, gr, ger, gx;
44 struct audio_ioctl ai;
47 fd = open( "/dev/audio", O_RDWR );
50 perror( "sst_open: open /dev/audio" );
54 #ifdef AUDIOSETQSIZE /* This no longer exists as of 4.1.2. */
56 /* Shrink audio device's queue size, to cut down time delay. */
58 if ( ioctl( fd, AUDIOSETQSIZE, &i ) < 0 )
60 perror( "sst_open: SETQSIZE" );
63 #endif /* AUDIOSETQSIZE */
65 /* Set gains. -10 <= ger <= 18, -18 <= gr <= 12, -18 <= gx <= 12. */
69 if ( (ep = getenv( "SST_PLAY" )) != NULL )
71 play_level = atoi( ep );
72 if ( play_level < 0 || play_level > 99 )
74 warn( "sst_open: SST_PLAY must be between 0 and 99" );
82 if ( (ep = getenv( "SST_RECORD" )) != NULL )
84 record_level = atoi( ep );
85 if ( record_level < 0 || record_level > 99 )
87 warn( "sst_open: SST_RECORD must be between 0 and 99" );
93 play_level = play_level * 59 / 100 - 28;
95 gr = play_level - ger;
99 gr = play_level - ger;
104 ger = play_level - gr;
106 gx = record_level * 31 / 100 - 18;
107 sst_set_gr( fd, gr );
108 sst_set_ger( fd, ger );
109 sst_set_gx( fd, gx );
111 /* Initialize the MMR2 register to send the output to either
112 ** the speaker or the earphone jack, depending on SST_EARPHONES.
114 ai.control = AUDIO_MAP_MMR2;
115 if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
117 perror( "sst_open: GETREG MMR2" );
120 if ( (ep = getenv( "SST_EARPHONES" )) != NULL )
121 ai.data[0] &= ~AUDIO_MMR2_BITS_LS;
123 ai.data[0] |= AUDIO_MMR2_BITS_LS;
124 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
126 perror( "sst_open: SETREG MMR2" );
137 struct audio_ioctl ai;
139 ai.control = AUDIO_MAP_MMR1;
141 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
143 perror( "sst_close: SETREG MMR1" );
145 ai.control = AUDIO_MAP_MMR2;
147 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
149 perror( "sst_close: SETREG MMR2" );
154 /* These are tables of values to be loaded into various gain registers.
157 static unsigned char ger_table[][2] = {
158 0xaa, 0xaa, /* -10db */
162 0x74, 0x9c, /* -6db */
168 0x00, 0x99, /* 0db */
174 0x40, 0xdd, /* 6db */
180 0x42, 0x10, /* 12db */
186 0x00, 0x0f, /* 18db */
190 static unsigned char gr_gx_table[][2] = {
191 0x8b, 0x7c, /* -18db */
197 0x91, 0xf9, /* -12db */
203 0x9f, 0x91, /* -6db */
209 0x08, 0x08, /* 0db */
215 0x10, 0xf2, /* 6db */
221 0x00, 0x0e, /* 12db */
225 sst_set_ger( fd, value )
228 struct audio_ioctl ai;
230 if ( ( value < -10 ) || ( value > 18 ) )
233 sprintf (buf, "sst_set_ger: GER %d out of range", value);
238 /* Add 10 to the value to get the index into the table. */
239 ai.control = AUDIO_MAP_GER;
240 ai.data[0] = ger_table[value + 10][1];
241 ai.data[1] = ger_table[value + 10][0];
243 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
245 perror( "sst_set_ger: SETREG GER" );
248 ai.control = AUDIO_MAP_MMR1;
249 if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
251 perror( "sst_set_ger: GETREG MMR1" );
253 ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GER;
254 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
256 perror( "sst_set_ger: SETREG MMR1" );
261 sst_set_gr( fd, value )
264 struct audio_ioctl ai;
266 if ( ( value < -18 ) || ( value > 12 ) )
269 sprintf (buf, "sst_set_gr: GR %d out of range", value);
274 ai.control = AUDIO_MAP_GR;
275 ai.data[0] = gr_gx_table[value + 18][1];
276 ai.data[1] = gr_gx_table[value + 18][0];
278 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
280 perror( "sst_set_gr: SETREG GR" );
283 ai.control = AUDIO_MAP_MMR1;
284 if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
286 perror( "sst_set_gr: GETREG MMR1" );
288 ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GR;
289 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
291 perror( "sst_set_gr: SETREG MMR1" );
296 sst_set_gx( fd, value )
299 struct audio_ioctl ai;
302 if ( ( value < -18 ) || ( value > 12 ) )
304 sprintf (buf, "sst_set_gx: GX %d out of range", value);
309 /* We add 18 to get the index into the table, since entry 0 represents
312 ai.control = AUDIO_MAP_GX;
313 ai.data[0] = gr_gx_table[value + 18][1];
314 ai.data[1] = gr_gx_table[value + 18][0];
316 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
318 perror( "sst_set_gx: SETREG GX" );
321 ai.control = AUDIO_MAP_MMR1;
322 if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
324 perror( "sst_set_gx: GETREG MMR1" );
326 ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GX;
327 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
329 perror( "sst_set_gx: SETREG MMR1" );
334 sst_tones( fd, dhz1, dhz2, thz, rhz, usec )
335 int fd, dhz1, dhz2, thz, rhz, usec;
338 struct audio_ioctl ai;
339 int dval1, dval2, tval, rval;
340 unsigned char oldmmr2, newmmr2;
346 dval1 = ( dhz1 * 128 + 63 ) / 1000;
347 if ( ( dval1 < 1 ) || ( dval1 > 255 ) )
349 sprintf(buf, "sst_tones: dhz1 %d out of range", dhz1 );
359 dval2 = ( dhz2 * 128 + 63 ) / 1000;
360 if ( ( dval2 < 1 ) || ( dval2 > 255 ) )
362 sprintf(buf, "sst_tones: dhz2 %d out of range", dhz2 );
372 tval = ( thz * 128 + 63 ) / 2000;
373 if ( ( tval < 1 ) || ( tval > 255 ) )
375 sprintf(buf, "sst_tones: thz %d out of range", thz );
385 rval = ( rhz * 128 + 63 ) / 2000;
386 if ( ( rval < 1 ) || ( rval > 255 ) )
388 sprintf(buf, "sst_tones: rhz %d out of range", dhz2 );
394 if ( ( dval1 != 0 || dval2 != 0 ) && ( tval != 0 || rval != 0 ) )
396 sprintf(buf, "sst_tones: cannot use DTMF and TONE or RINGER at the same time", dhz2 );
401 if ( tval != 0 && rval != 0 )
403 sprintf(buf, "sst_tones: cannot use TONE and RINGER at the same time", dhz2 );
408 ai.control = AUDIO_MAP_MMR2;
409 if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
411 perror( "sst_tones: GETREG MMR2" );
413 oldmmr2 = newmmr2 = ai.data[0];
415 if ( dval1 != 0 || dval2 != 0 )
417 newmmr2 |= AUDIO_MMR2_BITS_DTMF;
418 ai.control = AUDIO_MAP_FTGR;
421 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
423 perror( "sst_tones: SETREG FTGR" );
429 newmmr2 |= AUDIO_MMR2_BITS_TONE;
430 ai.control = AUDIO_MAP_FTGR;
433 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
435 perror( "sst_tones: SETREG FTGR" );
441 newmmr2 |= AUDIO_MMR2_BITS_RINGER;
442 ai.control = AUDIO_MAP_FTGR;
445 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
447 perror( "sst_tones: SETREG FTGR" );
451 ai.control = AUDIO_MAP_MMR2;
452 ai.data[0] = newmmr2;
453 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
455 perror( "sst_tones: SETREG MMR2" );
460 ai.data[0] = oldmmr2;
461 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
463 perror( "sst_tones: SETREG MMR2" );
468 sst_dtmf( fd, dial, usecper, usecpause )
469 int fd, usecper, usecpause;
474 for ( cp = dial; *cp != '\0'; cp++ )
478 case '1': sst_tones( fd, 703, 1211, 0, 0, usecper ); break;
479 case '2': sst_tones( fd, 703, 1336, 0, 0, usecper ); break;
480 case '3': sst_tones( fd, 703, 1492, 0, 0, usecper ); break;
481 case 'A': sst_tones( fd, 703, 1648, 0, 0, usecper ); break;
482 case '4': sst_tones( fd, 773, 1211, 0, 0, usecper ); break;
483 case '5': sst_tones( fd, 773, 1336, 0, 0, usecper ); break;
484 case '6': sst_tones( fd, 773, 1492, 0, 0, usecper ); break;
485 case 'B': sst_tones( fd, 773, 1648, 0, 0, usecper ); break;
486 case '7': sst_tones( fd, 859, 1211, 0, 0, usecper ); break;
487 case '8': sst_tones( fd, 859, 1336, 0, 0, usecper ); break;
488 case '9': sst_tones( fd, 859, 1492, 0, 0, usecper ); break;
489 case 'C': sst_tones( fd, 859, 1648, 0, 0, usecper ); break;
490 case '*': sst_tones( fd, 945, 1211, 0, 0, usecper ); break;
491 case '0': sst_tones( fd, 945, 1336, 0, 0, usecper ); break;
492 case '#': sst_tones( fd, 945, 1492, 0, 0, usecper ); break;
493 case 'D': sst_tones( fd, 945, 1648, 0, 0, usecper ); break;
495 case ' ': case '-': case '(': case ')': case '+':
496 continue; /* ignore */
498 case ',': usleep( usecper ); break; /* big pause */
503 sprintf( buf, "sst_dtmf: unknown dialing code '%c'", *cp );