comparison cdcnv.c @ 0:e1a1a181c0d7

initial import
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Thu, 16 Dec 2010 20:30:11 +0900
parents
children 05cc06e88a57
comparison
equal deleted inserted replaced
-1:000000000000 0:e1a1a181c0d7
1 /* cdcnv.c ---------------------------------------------
2 $Id: cdcnv.c,v 1.1 2002/12/21 01:13:27 tosy Exp $
3
4 (以前の履歴は rcctl.c 参照)
5 v0.30 97.12.13 コード変換部(cdcnv.c)分離
6 v0.31 98.03.07 Mac21対応
7 v0.31a 98.03.11 Syncom対応(要rc_send修正)
8 v0.31b 98.03.15 ALISA3(L)対応
9 v0.32 98.11.29 鉄人,Σ,NET7000対応
10 v0.40 02.12.15 B-kara,HyperJOY対応,Σバグ修正
11 ------------------------------------------------------*/
12
13 #include <string.h>
14 #if defined(__FreeBSD__)||defined(linux)
15 #define stricmp(s, c) strcasecmp(s, c)
16 #endif
17
18 const char *mks = "XUGJHBPDSNKMALZTCI";
19 #define M_X2 0 /* X2000 */
20 #define M_UK 1 /* U-kara */
21 #define M_GI 2 /* GIGA */
22 #define M_JO 3 /* JOYSOUND */
23 #define M_HJ 4 /* HyperJOY */
24 #define M_BT 5 /* BeMax'S(T) */
25 #define M_PR 6 /* Prologue21 */
26 #define M_DA 7 /* DAM */
27 #define M_SY 8 /* Syncom/孫悟空 */
28 #define M_N7 9 /* NET7000 */
29 #define M_BK 10 /* B-kara */
30 #define M_MA 11 /* Mac21 */
31 #define M_A3 12 /* ALISA3(A) */
32 #define M_AL 13 /* ALISA3(L) */
33 #define M_AZ 14 /* ALISA3(Z) */
34 #define M_TS 15 /* 東映システム */
35 #define M_SI 16 /* Σシステム */
36 #define M_TJ 17 /* カラオケの鉄人 */
37
38 #define K_SE 3
39 #define K_KU 4
40 #define K_KD 5
41 #define K_SP 6
42 #define K_ST 7
43 #define K_CL 8
44 #define K_NM 9
45
46 int cvt[][21] = {
47 /* cd, IDL, IDH, SE, KU, KD, SP, ST, CL, */
48 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B*/
49 {0x30, 0x2e, 0xd1, 0x17, 0x02, 0x03, 0x0c, 0x1c, 0x0d, /* X2000 */
50 0x13, 0x10, 0x14, 0x18, 0x11, 0x15, 0x19, 0x12, 0x16, 0x1a, -1, -1 },
51 {0x30, 0x98, 0x67, 0x0c, 0x13, 0x12, 0x0f, 0x0e, 0x0d, /* U-kara/-2 */
52 0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, -1, -1 },
53 {0x10, 0x84, 0x10, -1, 0x04, 0x05, 0x0f, 0x01, -1, /* GIGA */
54 0xf0, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, -1, -1 },
55 {0x40, 0x81, 0x16, 0x1f, 0x54, 0x0b, 0x1d, 0x5c, 0x1c, /* JOYSOUND */
56 0x00, 0x07, 0x0a, 0x58, 0x03, 0x06, 0x50, 0x01, 0x0e, 0x5d, -1, -1 },
57 {0x20, 0x81, 0x16, 0x1f, 0x54, 0x0b, 0x1d, 0x5c, 0x1c, /* HyperJOY */
58 0x00, 0x07, 0x0a, 0x58, 0x03, 0x06, 0x50, 0x01, 0x0e, 0x5d, -1, -1 },
59 {0x20, 0xac, 0x53, 0x9d, 0xcc, 0xca, 0x96, 0xdc, 0x9a, /* BeMax'S (T) */
60 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, -1, -1 },
61 {0x10, 0x81, 0xb0, 0x33, 0xd3, 0xb3, 0x93, 0x13, -1, /* Prologue21 */
62 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e, 0x01, 0x09, -1, -1 },
63 {0x10, 0xd1, 0x2d, 0x09, 0x04, 0x05, 0x00, 0x01, 0x07, /* DAM */
64 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b },
65 {0x60, 0xc3, 0xc3, 0x05, 0x2c, 0x2d, 0x04, 0xc7, 0x02, /* Syncom */
66 0x0f, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, -1, -1 },
67 {0x30, 0x84, 0x05, 0x0f, 0x0e, 0x0c, 0x07, -1, 0x1e, /* NET7000 */
68 0x1c, 0x10, 0x11, 0x12, 0x14, 0x15, 0x16, 0x18, 0x19, 0x1a, -1, -1 },
69 {0x10, 0x2e, 0xd1, 0x4c, 0x35, 0x33, 0x4f, 0x4e, 0x4d, /* B-kara */
70 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b },
71 {0x30, 0x80, 0x00, 0x03, 0x18, 0x14, 0x1e, -1, 0x07, /* Mac21 */
72 0x00, 0x0c, 0x0d, 0x0e, 0x08, 0x09, 0x0a, 0x04, 0x05, 0x06, 0x01, 0x02 },
73 {0x30, 0x87, 0x56, 0x12, 0x43, 0x47, 0x11, -1, 0x42, /* ALISA3 */
74 0x0d, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x0f },
75 {0x30, 0x87, 0x56, 0x12, 0x43, 0x47, 0x11, -1, 0x42, /* ALISA3 */
76 0x0d, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x0f },
77 {0x30, 0x87, 0x56, 0x12, 0x43, 0x47, 0x11, -1, 0x42, /* ALISA3 */
78 0x0d, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x0f },
79 {0x30, 0x00, 0xff, 0x41, 0x08, 0x09, 0x4c, 0x45, 0x44, /* TOEI sys */
80 0x13, 0x0f, 0x4f, 0x4e, 0x07, 0x47, 0x46, 0x03, 0x43, 0x42, 0x53, 0x52 },
81 {0x10, 0x55, 0xaa, 0x3e, 0x08, 0x09, 0x5c, -1, -1, /* Σ System */
82 0x13, 0x0f, 0x4f, 0x4e, 0x07, 0x47, 0x46, 0x03, 0x43, 0x42, 0x53, 0x52 },
83 {0x40, 0xae, 0x51, 0xdc, 0x03, 0x02, 0x96, -1, 0xdd, /* 鉄人 */
84 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0xda, 0xdb }
85 };
86
87 int tsc[] = {0x00, 0x04, 0x0c, 0x10, 0x01, 0x05, 0x0d, 0x11};
88 int tjc[] = {0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xde, 0xd0, 0xd1, 0xd2, 0xd3};
89
90 char *cverrstr[] = {
91 "Invalid maker code.",
92 "Invalid music number.",
93 "Invalid function code."
94 };
95
96 int cdcnv(int buf[], char *mak, char *cod)
97 {
98 int maker, i, c, n, *q;
99
100 if ((maker = (int)strchr(mks, *mak & 0xdf)) == NULL)
101 return -1;
102 maker -= (int)mks;
103
104 q = buf;
105
106 *q++ = 0x80; /* wake-up code */
107 *q++ = cvt[maker][0];
108 *q++ = cvt[maker][1];
109 *q++ = cvt[maker][2];
110
111 /* 統合機種等でのメーカー名指定 */
112 if ((c = *++mak) != 0) {
113 if ((maker == M_A3)&&('0' <= c)&&(c <= '4'))
114 cvt[maker][K_SE] = 0x12 + (c - '0');
115 else if ((maker == M_AZ)&&('1' <= c)&&(c <= '4'))
116 *q++ = 0x13 + (c - '1');
117 else if ((maker == M_AL)&&('0' <= c)&&(c <= '9')) {
118 *q++ = cvt[maker][K_NM + (c - '0')];
119 *q++ = 0x10;
120 }
121 else if ((maker == M_TS)&&('1' <= c)&&(c <= '8'))
122 *q++ = tsc[c - '1'];
123 else if ((maker == M_TJ)&&('0' <= c)&&(c <= '9'))
124 *q++ = tjc[c - '1'];
125 else if ((maker == M_UK)&&('2' == c)) {
126 buf[1] = 0x70;
127 *q++ = 0xf0;
128 }
129 }
130 if (maker == M_BK)
131 *q++ = 0xf0;
132
133 if (('0' <= *cod)&&(*cod <= '9')) {
134 if (maker == M_DA)
135 *q++ = 0x08;
136 else if (maker == M_MA)
137 *q++ = 0x10; /* Req.1 */
138 else if (maker == M_SI) {
139 /* [1-9A-F] -> '01','02',...,'15' , NULL -> '00' */
140 if (*mak == 0) {
141 *q++ = cvt[maker][K_NM + 0];
142 *q++ = cvt[maker][K_NM + 0];
143 }
144 else {
145 if (mak[1] != 0)
146 *q++ = cvt[maker][K_NM + (*mak++ - '0')];
147 else
148 *q++ = cvt[maker][K_NM + ((*mak >= 'A')? 1: 0)];
149 *q++ = cvt[maker][K_NM + ((*mak % 0x41) & 0x0f)];
150 }
151 }
152
153 /* 曲コード変換 */
154 while((c = *cod++) != '\0') {
155 if (('0' <= c)&&(c <= '9'))
156 *q = cvt[maker][c - '0' + K_NM];
157 else if ((c == 'A')||(c == 'a'))
158 *q = cvt[maker][10 + K_NM];
159 else if ((c == 'B')||(c == 'b'))
160 *q = cvt[maker][11 + K_NM];
161 else if (c == '-') {
162 if (maker == M_TJ)
163 *q = cvt[maker][K_SE];
164 else
165 continue;
166 }
167 else
168 *q = -1;
169 if (*q++ == -1)
170 return -2;
171 }
172
173 /* 送信直前処理 */
174 switch(maker) {
175 case M_X2:
176 /* X2000: 曲コードの末尾 2 桁の直前にも SET を送信 */
177 q -= 2;
178 q[3] = cvt[maker][K_SE];
179 q[2] = q[1];
180 q[1] = q[0];
181 q[0] = cvt[maker][K_SE];
182 q += 4;
183 break;
184
185 case M_GI:
186 /* GIGA: 曲コードが 6 桁以下の場合は最後に'0'を埋める */
187 while(q < &buf[10])
188 *q++ = cvt[maker][K_NM];
189 break;
190
191 case M_PR:
192 /* Prologue21: */
193 for( i=0; i<3; i++ ) {
194 buf[10-i] = 0;
195 if (q > &buf[4])
196 buf[10-i] = *--q << 4;
197 if (q > &buf[4])
198 buf[10-i] |= *--q;
199 }
200 q = &buf[11];
201 *q++ = 0x33;
202 buf[4] = buf[8];
203 buf[5] = buf[9];
204 buf[6] = buf[10];
205 buf[7] = buf[11];
206 break; /* q=&buf[8] にしないとバグのような… */
207
208 case M_SI:
209 /* Σシステム: コード長 10 バイト(<Maker[2]>,<Code[7]>,<SET>)固定 */
210 case M_DA:
211 /* DAM: コード長 9 バイト(0x08,<Code[7]>,<SET>)固定 */
212 case M_BK:
213 /* B-kara: コード長 9 バイト(0xf0,<Code[7]>,<SET>)固定 */
214 n = (maker == M_SI)? 12: 11; /* 数字の最終桁を buf[n] とした値 */
215 for( i=0; i<7; i++ ) {
216 c = *(q-1);
217 if ((i == 1)&&((c == cvt[maker][K_NM+10])||(c == cvt[maker][K_NM+11])))
218 /* 曲コード中の'A','B'以降の桁数が1桁なら'0'を挿入 */
219 buf[n-i] = cvt[maker][K_NM + 0];
220 else if ((i == 2)&&(c!=cvt[maker][K_NM+10])&&(c!=cvt[maker][K_NM+11]))
221 /* 曲コードの末尾 3 文字めが'A','B'でなければ'-'を設定 */
222 buf[n-i] = (maker == M_BK)? 0x3d: ((maker == M_SI)? 0x51: 0x3c);
223 else if (q > &buf[n-6])
224 buf[n-i] = *--q;
225 else
226 /* 桁数の足らない部分には'0'を設定 */
227 buf[n-i] = cvt[maker][K_NM + 0];
228 }
229 q = &buf[n+1];
230 default:
231 *q++ = cvt[maker][K_SE];
232 break;
233 }
234 }
235 /* 制御コードの処理 */
236 else {
237 *q = -1;
238 if (!stricmp(cod, "SE"))
239 *q = cvt[maker][K_SE];
240 else if (!stricmp(cod, "KU"))
241 *q = cvt[maker][K_KU];
242 else if (!stricmp(cod, "KD"))
243 *q = cvt[maker][K_KD];
244 else if (!stricmp(cod, "SP"))
245 *q = cvt[maker][K_SP];
246 else if (!stricmp(cod, "ST"))
247 *q = cvt[maker][K_ST];
248 else if (!stricmp(cod, "CL"))
249 *q = cvt[maker][K_CL];
250 if (*q == -1)
251 return -3;
252 q++;
253 }
254
255 if ((maker == M_BK)||((maker == M_UK)&&('2' == *mak)))
256 *q++ = 0xf7;
257
258 buf[1] |= q-buf-2; /* 実送信バイト数(PIC 制御用)を埋め込み */
259 return q-buf;
260 }