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. */
36 sst_open(play_level, record_level)
37 int play_level, record_level;
39 int fd, i, gr, ger, gx;
40 struct audio_ioctl ai;
43 fd = open( "/dev/audio", O_RDWR );
46 perror( "sst_open: open /dev/audio" );
50 #ifdef AUDIOSETQSIZE /* This no longer exists as of 4.1.2. */
52 /* Shrink audio device's queue size, to cut down time delay. */
54 if ( ioctl( fd, AUDIOSETQSIZE, &i ) < 0 )
56 perror( "sst_open: SETQSIZE" );
59 #endif /* AUDIOSETQSIZE */
61 /* Set gains. -10 <= ger <= 18, -18 <= gr <= 12, -18 <= gx <= 12. */
65 if ( (ep = getenv( "SST_PLAY" )) != NULL )
67 play_level = atoi( ep );
68 if ( play_level < 0 || play_level > 99 )
70 warn( "sst_open: SST_PLAY must be between 0 and 99" );
78 if ( (ep = getenv( "SST_RECORD" )) != NULL )
80 record_level = atoi( ep );
81 if ( record_level < 0 || record_level > 99 )
83 warn( "sst_open: SST_RECORD must be between 0 and 99" );
89 play_level = play_level * 59 / 100 - 28;
91 gr = play_level - ger;
95 gr = play_level - ger;
100 ger = play_level - gr;
102 gx = record_level * 31 / 100 - 18;
103 sst_set_gr( fd, gr );
104 sst_set_ger( fd, ger );
105 sst_set_gx( fd, gx );
107 /* Initialize the MMR2 register to send the output to either
108 ** the speaker or the earphone jack, depending on SST_EARPHONES.
110 ai.control = AUDIO_MAP_MMR2;
111 if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
113 perror( "sst_open: GETREG MMR2" );
116 if ( (ep = getenv( "SST_EARPHONES" )) != NULL )
117 ai.data[0] &= ~AUDIO_MMR2_BITS_LS;
119 ai.data[0] |= AUDIO_MMR2_BITS_LS;
120 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
122 perror( "sst_open: SETREG MMR2" );
133 struct audio_ioctl ai;
135 ai.control = AUDIO_MAP_MMR1;
137 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
139 perror( "sst_close: SETREG MMR1" );
141 ai.control = AUDIO_MAP_MMR2;
143 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
145 perror( "sst_close: SETREG MMR2" );
150 /* These are tables of values to be loaded into various gain registers.
153 static unsigned char ger_table[][2] = {
154 0xaa, 0xaa, /* -10db */
158 0x74, 0x9c, /* -6db */
164 0x00, 0x99, /* 0db */
170 0x40, 0xdd, /* 6db */
176 0x42, 0x10, /* 12db */
182 0x00, 0x0f, /* 18db */
186 static unsigned char gr_gx_table[][2] = {
187 0x8b, 0x7c, /* -18db */
193 0x91, 0xf9, /* -12db */
199 0x9f, 0x91, /* -6db */
205 0x08, 0x08, /* 0db */
211 0x10, 0xf2, /* 6db */
217 0x00, 0x0e, /* 12db */
221 sst_set_ger( fd, value )
224 struct audio_ioctl ai;
226 if ( ( value < -10 ) || ( value > 18 ) )
229 sprintf (buf, "sst_set_ger: GER %d out of range", value);
234 /* Add 10 to the value to get the index into the table. */
235 ai.control = AUDIO_MAP_GER;
236 ai.data[0] = ger_table[value + 10][1];
237 ai.data[1] = ger_table[value + 10][0];
239 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
241 perror( "sst_set_ger: SETREG GER" );
244 ai.control = AUDIO_MAP_MMR1;
245 if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
247 perror( "sst_set_ger: GETREG MMR1" );
249 ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GER;
250 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
252 perror( "sst_set_ger: SETREG MMR1" );
257 sst_set_gr( fd, value )
260 struct audio_ioctl ai;
262 if ( ( value < -18 ) || ( value > 12 ) )
265 sprintf (buf, "sst_set_gr: GR %d out of range", value);
270 ai.control = AUDIO_MAP_GR;
271 ai.data[0] = gr_gx_table[value + 18][1];
272 ai.data[1] = gr_gx_table[value + 18][0];
274 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
276 perror( "sst_set_gr: SETREG GR" );
279 ai.control = AUDIO_MAP_MMR1;
280 if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
282 perror( "sst_set_gr: GETREG MMR1" );
284 ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GR;
285 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
287 perror( "sst_set_gr: SETREG MMR1" );
292 sst_set_gx( fd, value )
295 struct audio_ioctl ai;
298 if ( ( value < -18 ) || ( value > 12 ) )
300 sprintf (buf, "sst_set_gx: GX %d out of range", value);
305 /* We add 18 to get the index into the table, since entry 0 represents
308 ai.control = AUDIO_MAP_GX;
309 ai.data[0] = gr_gx_table[value + 18][1];
310 ai.data[1] = gr_gx_table[value + 18][0];
312 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
314 perror( "sst_set_gx: SETREG GX" );
317 ai.control = AUDIO_MAP_MMR1;
318 if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
320 perror( "sst_set_gx: GETREG MMR1" );
322 ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GX;
323 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
325 perror( "sst_set_gx: SETREG MMR1" );
330 sst_tones( fd, dhz1, dhz2, thz, rhz, usec )
331 int fd, dhz1, dhz2, thz, rhz, usec;
334 struct audio_ioctl ai;
335 int dval1, dval2, tval, rval;
336 unsigned char oldmmr2, newmmr2;
342 dval1 = ( dhz1 * 128 + 63 ) / 1000;
343 if ( ( dval1 < 1 ) || ( dval1 > 255 ) )
345 sprintf(buf, "sst_tones: dhz1 %d out of range", dhz1 );
355 dval2 = ( dhz2 * 128 + 63 ) / 1000;
356 if ( ( dval2 < 1 ) || ( dval2 > 255 ) )
358 sprintf(buf, "sst_tones: dhz2 %d out of range", dhz2 );
368 tval = ( thz * 128 + 63 ) / 2000;
369 if ( ( tval < 1 ) || ( tval > 255 ) )
371 sprintf(buf, "sst_tones: thz %d out of range", thz );
381 rval = ( rhz * 128 + 63 ) / 2000;
382 if ( ( rval < 1 ) || ( rval > 255 ) )
384 sprintf(buf, "sst_tones: rhz %d out of range", dhz2 );
390 if ( ( dval1 != 0 || dval2 != 0 ) && ( tval != 0 || rval != 0 ) )
392 sprintf(buf, "sst_tones: cannot use DTMF and TONE or RINGER at the same time", dhz2 );
397 if ( tval != 0 && rval != 0 )
399 sprintf(buf, "sst_tones: cannot use TONE and RINGER at the same time", dhz2 );
404 ai.control = AUDIO_MAP_MMR2;
405 if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
407 perror( "sst_tones: GETREG MMR2" );
409 oldmmr2 = newmmr2 = ai.data[0];
411 if ( dval1 != 0 || dval2 != 0 )
413 newmmr2 |= AUDIO_MMR2_BITS_DTMF;
414 ai.control = AUDIO_MAP_FTGR;
417 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
419 perror( "sst_tones: SETREG FTGR" );
425 newmmr2 |= AUDIO_MMR2_BITS_TONE;
426 ai.control = AUDIO_MAP_FTGR;
429 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
431 perror( "sst_tones: SETREG FTGR" );
437 newmmr2 |= AUDIO_MMR2_BITS_RINGER;
438 ai.control = AUDIO_MAP_FTGR;
441 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
443 perror( "sst_tones: SETREG FTGR" );
447 ai.control = AUDIO_MAP_MMR2;
448 ai.data[0] = newmmr2;
449 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
451 perror( "sst_tones: SETREG MMR2" );
456 ai.data[0] = oldmmr2;
457 if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
459 perror( "sst_tones: SETREG MMR2" );
464 sst_dtmf( fd, dial, usecper, usecpause )
465 int fd, usecper, usecpause;
470 for ( cp = dial; *cp != '\0'; cp++ )
474 case '1': sst_tones( fd, 703, 1211, 0, 0, usecper ); break;
475 case '2': sst_tones( fd, 703, 1336, 0, 0, usecper ); break;
476 case '3': sst_tones( fd, 703, 1492, 0, 0, usecper ); break;
477 case 'A': sst_tones( fd, 703, 1648, 0, 0, usecper ); break;
478 case '4': sst_tones( fd, 773, 1211, 0, 0, usecper ); break;
479 case '5': sst_tones( fd, 773, 1336, 0, 0, usecper ); break;
480 case '6': sst_tones( fd, 773, 1492, 0, 0, usecper ); break;
481 case 'B': sst_tones( fd, 773, 1648, 0, 0, usecper ); break;
482 case '7': sst_tones( fd, 859, 1211, 0, 0, usecper ); break;
483 case '8': sst_tones( fd, 859, 1336, 0, 0, usecper ); break;
484 case '9': sst_tones( fd, 859, 1492, 0, 0, usecper ); break;
485 case 'C': sst_tones( fd, 859, 1648, 0, 0, usecper ); break;
486 case '*': sst_tones( fd, 945, 1211, 0, 0, usecper ); break;
487 case '0': sst_tones( fd, 945, 1336, 0, 0, usecper ); break;
488 case '#': sst_tones( fd, 945, 1492, 0, 0, usecper ); break;
489 case 'D': sst_tones( fd, 945, 1648, 0, 0, usecper ); break;
491 case ' ': case '-': case '(': case ')': case '+':
492 continue; /* ignore */
494 case ',': usleep( usecper ); break; /* big pause */
499 sprintf( buf, "sst_dtmf: unknown dialing code '%c'", *cp );