annotate sounds/wav2h.c @ 3727:dadb43e0bcae

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