1 /*
2  * Copyright 1996 by Jutta Degener and Carsten Bormann, Technische
3  * Universitaet Berlin.  See the accompanying file "COPYRIGHT" for
4  * details.  THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
5  */
6 
7 /*$Header*/
8 
9 /* Generate code to pack a bit array from a name:#bits description,
10  * WAV #49 style.
11  */
12 
13 #include	<stdio.h>
14 #include	"taste.h"
15 #include	"proto.h"
16 #include	<limits.h>
17 
18 /* This module goes back to one Jeff Chilton used for his implementation
19  * of the #49 WAV GSM format.  (In his original patch 8, it replaced
20  * bitter.c.)
21  *
22  * In Microsoft's WAV #49 version of the GSM format, two 32 1/2
23  * byte GSM frames are packed together to make one WAV frame, and
24  * the GSM parameters are packed into bytes right-to-left rather
25  * than left-to-right.
26  *
27  * That is, where toast's GSM format writes
28  *
29  * 	aaaaaabb bbbbcccc cdddddee ...
30  *	___1____ ___2____ ___3____
31  *
32  *  for parameters a (6 bits), b (6 bits), c (5 bits), d (5 bits), e ..
33  *  the WAV format has
34  *
35  * 	bbaaaaaa ccccbbbb eedddddc ...
36  *	___1____ ___2____ ___3____
37  *
38  *  (This format looks a lot prettier if one pictures octets coming
39  *  in through a fifo queue from the left, rather than waiting in the
40  *  right-hand remainder of a C array.)
41  */
42 
43 #define WORD_BITS	16	/* sizeof(uword) * CHAR_BIT on the
44 				 * target architecture---if this isn't 16,
45 				 * you're in trouble with this library anyway.
46 				 */
47 
48 #define BYTE_BITS	 8	/* CHAR_BIT on the target architecture---
49 				 * if this isn't 8, you're in *deep* trouble.
50 				 */
51 
52 void write_code P2((s_spex, n_spex), struct spex * s_spex, int n_spex)
53 {
54 	struct spex	* sp = s_spex;
55 	int		  n_in = 0;
56 
57 	printf("uword sr = 0;\n");
58 
59 	for (; n_spex > 0; n_spex--, sp++) {
60 
61 		/*	insert       old
62 		 *	new var	     value     unused
63 		 *	here
64 		 *
65 		 *	[____________xxxxxx**********]
66 		 *
67 		 *	<----- n_in ------>
68 		 */
69 		printf("sr = sr >> %d | %s << %d;\n",
70 			sp->varsize,
71 			sp->var,
72 			WORD_BITS - sp->varsize);
73 
74 		n_in += sp->varsize;
75 
76 		while (n_in >= BYTE_BITS) {
77 			printf("*c++ = sr >> %d;\n",
78 				WORD_BITS - n_in);
79 			n_in -= BYTE_BITS;
80 		}
81 	}
82 
83 	while (n_in >= BYTE_BITS) {
84 		printf("*c++ = sr >> %d;\n", WORD_BITS - n_in);
85 		n_in -= BYTE_BITS;
86 	}
87 
88 	if (n_in > 0) {
89 		fprintf(stderr, "warning: %d bits left over\n", n_in);
90 	}
91 }
92