1 /*---------------------------------------------------------------------------*
2   Project:  DLS converter for SYN
3   File:     main.c
4 
5   Copyright (C)2001-2006 Nintendo  All Rights Reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Log: main.c,v $
14   Revision 1.3  2008/05/21 07:18:23  iwai_yuma
15   For VC2005, replaced unsecure function to secure one.
16 
17   Revision 1.2  2006/02/08 06:21:14  aka
18   Changed copyright.
19 
20 
21  *---------------------------------------------------------------------------*/
22 
23 #include <windows.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <malloc.h>
28 #include "types.h"
29 #include "wt.h"
30 #include "dls.h"
31 #include "dspadpcm.h"
32 
33 /*--------------------------------------------------------------------------*/
34 static FILE *dlsFile;
35 static FILE *wtFile;
36 static FILE *pcmFile;
37 static HINSTANCE   hDll;
38 
39 typedef u32 (*lpFunc1)(u32);
40 typedef u32 (*lpFunc2)(void);
41 typedef void (*lpFunc3)(s16*, u8*, ADPCMINFO*, u32);
42 typedef void (*lpFunc4)(u8*, s16*, ADPCMINFO*, u32);
43 typedef void (*lpFunc5)(u8*, ADPCMINFO*, u32);
44 
45 lpFunc1 getBytesForAdpcmBuffer;
46 lpFunc1 getBytesForAdpcmSamples;
47 lpFunc1 getBytesForPcmBuffer;
48 lpFunc1 getNibbleAddress;
49 lpFunc2 getBytesForAdpcmInfo;
50 lpFunc3 encode;
51 lpFunc4 decode;
52 lpFunc5 getLoopContext;
53 
54 /*--------------------------------------------------------------------------*/
clean_up(void)55 void clean_up(void)
56 {
57     // close files
58     if (dlsFile)    fclose(dlsFile);
59     if (wtFile)     fclose(wtFile);
60     if (pcmFile)    fclose(pcmFile);
61     if (hDll)       FreeLibrary(hDll);
62 }
63 
64 
65 /*--------------------------------------------------------------------------*/
getDll(void)66 int getDll(void)
67 {
68     hDll = LoadLibrary("dsptool.dll");
69 
70     if (hDll)
71     {
72         if (!(getBytesForAdpcmBuffer =
73                 (lpFunc1)GetProcAddress(
74                             hDll,
75                             "getBytesForAdpcmBuffer"
76                             ))) return 1;
77 
78         if (!(getBytesForAdpcmSamples =
79                 (lpFunc1)GetProcAddress(
80                             hDll,
81                             "getBytesForAdpcmSamples"
82                             ))) return 1;
83 
84         if (!(getBytesForPcmBuffer =
85                 (lpFunc1)GetProcAddress(
86                             hDll,
87                             "getBytesForPcmBuffer"
88                             ))) return 1;
89 
90         if (!(getBytesForAdpcmInfo =
91                 (lpFunc2)GetProcAddress(
92                             hDll,
93                             "getBytesForAdpcmInfo"
94                             ))) return 1;
95 
96         if (!(encode =
97                 (lpFunc3)GetProcAddress(
98                             hDll,
99                             "encode"
100                             ))) return 1;
101 
102         if (!(decode =
103                 (lpFunc4)GetProcAddress(
104                             hDll,
105                             "decode"
106                             ))) return 1;
107 
108         if (!(getLoopContext =
109                 (lpFunc5)GetProcAddress(
110                             hDll,
111                             "getLoopContext"
112                             ))) return 1;
113 
114         return 0;
115     }
116 
117     return 1;
118 }
119 
120 
121 /*--------------------------------------------------------------------------*/
main(u32 argc,u8 * argv[])122 int main(u32 argc, u8 *argv[])
123 {
124 	#define	FILE_NAME_SIZE   1024
125 	#define	FILE_NAME_SIZE2   128
126 
127     char        fileName[ FILE_NAME_SIZE ];
128     char        fileName2[ FILE_NAME_SIZE2 ] = {0};
129     char        *p;
130     int         mode;
131     int         len;
132     int         ii;
133     char        token[]="/\\";
134 	char        *next_token;
135     char*       curr_ptr;
136     char*       prev_ptr;
137     errno_t     err;
138 
139     // initialize vars
140     dlsFile = NULL;
141     wtFile  = NULL;
142     pcmFile = NULL;
143     mode    = MODE_USE_FLAG;
144 
145     // print banner
146     printf("\nDLS1WT v1.5 - DLS Converter for SYN\n");
147     printf("Copyright 2001 Nintendo.  All rights reserved.\n\n");
148 
149     // check args
150     if (!((argc >= 2) && (argc <= 3)))
151     {
152         printf("\nUsage DLS1WT.exe <DLS file> -Options\n");
153         printf("\n    -d   use DLS F_WSMP_NO_COMPRESSION flag (default)\n");
154         printf("    -a   ADPCM compress all samples\n");
155         printf("    -p   PCM encode all samples\n");
156         clean_up();
157         return 1;
158     }
159 
160     // parse args
161     if (argc == 3)
162     {
163         char *p;
164         char ch[2];
165 
166         p = argv[2];
167 
168         ch[0] = *p;
169         p++;
170         ch[1] = *p;
171         p++;
172 
173         if ((ch[0] != '-') || (*p != 0))
174         {
175             printf("\nInvalid option %s!\n", argv[2]);
176             clean_up();
177             return 1;
178         }
179 
180         switch(ch[1])
181         {
182         case 'a':
183 
184             mode = MODE_COMPRESS_ALL;
185 
186             break;
187 
188         case 'd':
189 
190             mode = MODE_USE_FLAG;
191 
192             break;
193 
194         case 'p':
195 
196             mode = MODE_COMPRESS_NONE;
197 
198             break;
199 
200         default:
201 
202             printf("\nInvalid option %s!\n", argv[2]);
203             clean_up();
204             return 1;
205         }
206     }
207 
208     // load DLL
209     if(getDll())
210     {
211         printf("\nError loading dsptool.dll!\n");
212         clean_up();
213         return 1;
214     }
215 
216     // open DLS file
217     strcpy_s( fileName, FILE_NAME_SIZE, argv[1] );
218 //    strcpy( fileName, argv[1] );
219     prev_ptr = fileName;
220     curr_ptr = strtok_s( fileName, token, &next_token );
221 //    curr_ptr = strtok(fileName, token );
222     while (curr_ptr)
223     {
224         prev_ptr = curr_ptr;
225         curr_ptr = strtok_s( NULL, token, &next_token );
226 //        curr_ptr = strtok( NULL, token );
227     }
228 
229     len = strlen(prev_ptr);
230     strncpy_s( fileName2, FILE_NAME_SIZE2, prev_ptr, len );
231 //    strncpy( fileName2, prev_ptr, len );
232 
233     if (len < 5)
234     {
235         printf("\nInput file needs an extension \".dls\"!\n");
236         clean_up();
237         return 1;
238     }
239 
240     for (ii = 0; ii < 3; ii++)
241     {
242         fileName2[len - ii - 1] = tolower(fileName2[len - ii - 1]);
243     }
244 
245     if (strcmp(&fileName2[len - 4], ".dls"))
246     {
247         printf("\nInput file needs an extension \".dls\"!\n");
248         clean_up();
249         return 1;
250     }
251 
252     if( (err  = fopen_s( &dlsFile, argv[1], "rb" )) != 0 )
253 //    if (!(dlsFile = fopen(argv[1], "rb")))
254     {
255         printf("\nCannot open %s for reading!\n", argv[1]);
256         clean_up();
257         return 1;
258     }
259 
260     // open WT file for writing
261     strcpy_s( fileName, FILE_NAME_SIZE, fileName2 );
262 //    strcpy(fileName, fileName2);
263     p = strrchr(fileName, '.');
264     p++;
265     *p = 'w';
266     p++;
267     *p = 't';
268     p++;
269     *p = 0;
270 
271     if( (err  = fopen_s( &wtFile, fileName, "wb" )) != 0 )
272 //    if (!(wtFile = fopen(fileName, "wb")))
273     {
274         printf("\nCannot open %s for writing!\n", fileName);
275         clean_up();
276         return 1;
277     }
278 
279     // open PCM file for writing
280     strcpy_s( fileName, FILE_NAME_SIZE, fileName2 );
281 //    strcpy(fileName, fileName2);
282     p = strrchr(fileName, '.');
283     p++;
284     *p = 'p';
285     p++;
286     *p = 'c';
287     p++;
288     *p = 'm';
289     p++;
290     *p = 0;
291 
292     if( (err  = fopen_s( &pcmFile, fileName, "wb" )) != 0 )
293 //    if (!(pcmFile = fopen(fileName, "wb")))
294     {
295         printf("\nCannot open %s for writing!\n", fileName);
296         clean_up();
297         return 1;
298     }
299 
300     // export objects from DLS file
301     dls_read_file(dlsFile, wtFile, pcmFile, mode);
302 
303     clean_up();
304 
305     return 0;
306 }
307