3631
|
1 #include <windows.h>
|
|
2 #include <stdio.h>
|
|
3 #include <conio.h>
|
|
4
|
|
5 #define BUF_SIZE 10
|
|
6
|
|
7 unsigned long load_file( char* filename, char** wavePtr ) {
|
|
8 HANDLE inHandle;
|
|
9 unsigned long waveSize, action;
|
|
10
|
|
11 /* Open the WAVE file */
|
|
12 if (INVALID_HANDLE_VALUE != (inHandle = CreateFile( filename, GENERIC_READ,
|
|
13 FILE_SHARE_READ, 0, OPEN_EXISTING,
|
|
14 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN, 0))) {
|
|
15 /* I'm going to skip checking that this is a WAVE file. I'll assume that it is.
|
|
16 * Normally, you'd check it's RIFF header.
|
|
17 */
|
|
18
|
|
19 /* Get the size of the file */
|
|
20 if(0xFFFFFFFF == (waveSize = GetFileSize(inHandle, 0)) && waveSize) {
|
|
21 printf("Bad wave file size\n");
|
|
22 exit(1);
|
|
23 }
|
|
24
|
|
25 /* Allocate some memory to load the file */
|
|
26 if ((*wavePtr = (char *)VirtualAlloc(0, waveSize, MEM_COMMIT, PAGE_READWRITE))) {
|
|
27 /* Read in WAVE file */
|
|
28 if (ReadFile(inHandle, *wavePtr, waveSize, &action, 0) && waveSize == action) {
|
|
29 return waveSize;
|
|
30 }
|
|
31 else {
|
|
32 printf("Error loading WAVE!\r\n");
|
|
33 }
|
|
34
|
|
35 /* Free the memory */
|
|
36 VirtualFree(*wavePtr, waveSize, MEM_FREE);
|
|
37 }
|
|
38 else {
|
|
39 printf("Can't get memory!\r\n");
|
|
40 }
|
|
41 }
|
|
42 else {
|
|
43 printf("Bad WAVE size!\r\n");
|
|
44 }
|
|
45
|
|
46 /* Close the WAVE file */
|
|
47 CloseHandle(inHandle);
|
|
48 return 0;
|
|
49 }
|
|
50
|
|
51 void usage() {
|
|
52 printf("Usage: wav2h NameOfFile.wav\n");
|
|
53 exit(1);
|
|
54 }
|
|
55
|
|
56 int main(int argc, char **argv) {
|
|
57 FILE *f_in=0;
|
|
58 FILE *f_out=0;
|
|
59 int res, i, j, ext=0;
|
|
60 char *wavedata=0;
|
|
61 int wavesize=0;
|
|
62 char dataname[100];
|
|
63 char filename[100];
|
|
64 char outfile[100];
|
|
65
|
|
66 if(argc<2 || argc>2) {
|
|
67 usage();
|
|
68 }
|
|
69
|
|
70 for(i=0;i<strlen(argv[1]);i++) {
|
|
71 if( argv[1][i] == '/' || argv[1][i] == ':' ) {
|
|
72 printf("You must run wav2h from the directory containing the wav files.\n");
|
|
73 usage();
|
|
74 }
|
|
75 else if( argv[1][i] == '.' ) {
|
|
76 if( i+3 >= strlen(argv[1]) ||
|
|
77 !(argv[1][i+1] == 'w' && argv[1][i+2] == 'a' && argv[1][i+3] == 'v') ||
|
|
78 i+4 != strlen(argv[1])) {
|
|
79 printf("Error: wav2h only works with wav files (with wav extension)\n");
|
|
80 usage();
|
|
81 }
|
|
82 dataname[i] = '\0';
|
|
83 ext=1;
|
|
84 break;
|
|
85 }
|
|
86 else
|
|
87 dataname[i] = argv[1][i];
|
|
88 }
|
|
89
|
|
90 if(!ext) {
|
|
91 printf("Error: wav2h only works with wav files (with wav extension)\n");
|
|
92 usage();
|
|
93 }
|
|
94
|
|
95 sprintf(filename, ".\\%s", argv[1]);
|
|
96 sprintf(outfile, ".\\%s.h", dataname);
|
|
97
|
|
98 if((wavesize = load_file( filename, &wavedata )) == 0) {
|
|
99 printf("Error loading file to memory\n");
|
|
100 exit(1);
|
|
101 }
|
|
102 f_out = fopen(outfile, "w+");
|
|
103 if (!f_out) {
|
|
104 perror("fopen");
|
|
105 exit(1);
|
|
106 }
|
|
107
|
|
108 fprintf(f_out, "static unsigned char %s[] = {\n", dataname);
|
|
109
|
|
110 for(i=0,j=0;i<wavesize;i++,j++) {
|
|
111 fprintf(f_out, "%#x, ", wavedata[i] & 0xff);
|
|
112 if(j==BUF_SIZE) {
|
|
113 j=0;
|
|
114 fprintf(f_out, "\n");
|
|
115 }
|
|
116 }
|
|
117
|
|
118 fprintf(f_out,"};\n");
|
|
119 fclose(f_out);
|
|
120 VirtualFree(wavedata, wavesize, MEM_FREE);
|
|
121
|
|
122 return(0);
|
|
123 }
|