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