comparison src/psf2/plugin.c @ 2737:62cc6d667119

Import a bunch of stuff for new psf2 plugin.
author William Pitcock <nenolod@atheme.org>
date Mon, 30 Jun 2008 20:20:53 -0500
parents
children d0011cde16b7
comparison
equal deleted inserted replaced
2731:324f950774cb 2737:62cc6d667119
1 /*
2 Audio Overload SDK - main driver. for demonstration only, not user friendly!
3
4 Copyright (c) 2007-2008 R. Belmont and Richard Bannister.
5
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
12 * Neither the names of R. Belmont and Richard Bannister nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "ao.h"
32 #include "eng_protos.h"
33
34 /* file types */
35 static uint32 type;
36
37 static struct
38 {
39 uint32 sig;
40 char *name;
41 int32 (*start)(uint8 *, uint32);
42 int32 (*gen)(int16 *, uint32);
43 int32 (*stop)(void);
44 int32 (*command)(int32, int32);
45 uint32 rate;
46 int32 (*fillinfo)(ao_display_info *);
47 } types[] = {
48 { 0x50534602, "Sony PlayStation 2 (.psf2)", psf2_start, psf2_gen, psf2_stop, psf2_command, 60, psf2_fill_info },
49 { 0xffffffff, "", NULL, NULL, NULL, NULL, 0, NULL }
50 };
51
52 static char *path;
53
54 /* ao_get_lib: called to load secondary files */
55 int ao_get_lib(char *filename, uint8 **buffer, uint64 *length)
56 {
57 uint8 *filebuf;
58 uint32 size;
59 FILE *auxfile;
60
61 auxfile = fopen(filename, "rb");
62 if (!auxfile)
63 {
64 char buf[PATH_MAX];
65 snprintf(buf, PATH_MAX, "%s/%s", dirname(path), filename);
66 auxfile = fopen(buf, "rb");
67
68 if (!auxfile)
69 {
70 printf("Unable to find auxiliary file %s\n", buf);
71 return AO_FAIL;
72 }
73 }
74
75 fseek(auxfile, 0, SEEK_END);
76 size = ftell(auxfile);
77 fseek(auxfile, 0, SEEK_SET);
78
79 filebuf = malloc(size);
80
81 if (!filebuf)
82 {
83 fclose(auxfile);
84 printf("ERROR: could not allocate %d bytes of memory\n", size);
85 return AO_FAIL;
86 }
87
88 fread(filebuf, size, 1, auxfile);
89 fclose(auxfile);
90
91 *buffer = filebuf;
92 *length = (uint64)size;
93
94 return AO_SUCCESS;
95 }
96
97 int main(int argv, char *argc[])
98 {
99 FILE *file;
100 uint8 *buffer;
101 uint32 size, filesig;
102
103 path = strdup(argc[1]);
104 file = fopen(argc[1], "rb");
105
106 if (!file)
107 {
108 printf("ERROR: could not open file %s\n", argc[1]);
109 return -1;
110 }
111
112 // get the length of the file by seeking to the end then reading the current position
113 fseek(file, 0, SEEK_END);
114 size = ftell(file);
115 // reset the pointer
116 fseek(file, 0, SEEK_SET);
117
118 buffer = malloc(size);
119
120 if (!buffer)
121 {
122 fclose(file);
123 printf("ERROR: could not allocate %d bytes of memory\n", size);
124 return -1;
125 }
126
127 // read the file
128 fread(buffer, size, 1, file);
129 fclose(file);
130
131 // now try to identify the file
132 type = 0;
133 filesig = buffer[0]<<24 | buffer[1]<<16 | buffer[2]<<8 | buffer[3];
134 while (types[type].sig != 0xffffffff)
135 {
136 if (filesig == types[type].sig)
137 {
138 break;
139 }
140 else
141 {
142 type++;
143 }
144 }
145
146 // now did we identify it above or just fall through?
147 if (types[type].sig != 0xffffffff)
148 {
149 printf("File identified as %s\n", types[type].name);
150 }
151 else
152 {
153 printf("ERROR: File is unknown, signature bytes are %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
154 free(buffer);
155 return -1;
156 }
157
158 if ((*types[type].start)(buffer, size) != AO_SUCCESS)
159 {
160 free(buffer);
161 printf("ERROR: Engine rejected file!\n");
162 return -1;
163 }
164
165 m1sdr_Init(44100);
166 m1sdr_SetCallback(psf2_gen);
167 m1sdr_PlayStart();
168
169 printf("\n\nPlaying. Press CTRL-C to stop.\n");
170
171 while (1)
172 {
173 m1sdr_TimeCheck();
174 }
175
176 free(buffer);
177
178 return 1;
179 }
180