comparison src/xsf/plugin.c @ 2961:70b0973e7b70

Merge xsf plugin.
author William Pitcock <nenolod@atheme.org>
date Thu, 16 Oct 2008 14:45:41 -0500
parents
children 89d29615390d
comparison
equal deleted inserted replaced
2960:fe2ba1a712cd 2961:70b0973e7b70
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 <audacious/plugin.h>
32
33 #include "ao.h"
34 #include "corlett.h"
35 #include "vio2sf.h"
36
37 /* xsf_get_lib: called to load secondary files */
38 static gchar *path;
39 int xsf_get_lib(char *filename, void **buffer, unsigned int *length)
40 {
41 guchar *filebuf;
42 gsize size;
43 char buf[PATH_MAX];
44
45 snprintf(buf, PATH_MAX, "%s/%s", dirname(path), filename);
46
47 aud_vfs_file_get_contents(buf, (gchar **) &filebuf, &size);
48
49 *buffer = filebuf;
50 *length = (uint64)size;
51
52 return AO_SUCCESS;
53 }
54
55 static gint seek = 0;
56 Tuple *xsf_tuple(gchar *filename)
57 {
58 Tuple *t;
59 corlett_t *c;
60 guchar *buf;
61 gsize sz;
62
63 aud_vfs_file_get_contents(filename, (gchar **) &buf, &sz);
64
65 if (!buf)
66 return NULL;
67
68 if (corlett_decode(buf, sz, NULL, NULL, &c) != AO_SUCCESS)
69 return NULL;
70
71 t = aud_tuple_new_from_filename(filename);
72
73 aud_tuple_associate_int(t, FIELD_LENGTH, NULL, c->inf_length ? psfTimeToMS(c->inf_length) + psfTimeToMS(c->inf_fade) : -1);
74 aud_tuple_associate_string(t, FIELD_ARTIST, NULL, c->inf_artist);
75 aud_tuple_associate_string(t, FIELD_ALBUM, NULL, c->inf_game);
76 aud_tuple_associate_string(t, -1, "game", c->inf_game);
77 aud_tuple_associate_string(t, FIELD_TITLE, NULL, c->inf_title);
78 aud_tuple_associate_string(t, FIELD_COPYRIGHT, NULL, c->inf_copy);
79 aud_tuple_associate_string(t, FIELD_QUALITY, NULL, "sequenced");
80 aud_tuple_associate_string(t, FIELD_CODEC, NULL, "Nintendo DS Audio");
81 aud_tuple_associate_string(t, -1, "console", "Nintendo DS");
82
83 free(c);
84 g_free(buf);
85
86 return t;
87 }
88
89 gchar *xsf_title(gchar *filename, gint *length)
90 {
91 gchar *title = NULL;
92 Tuple *tuple = xsf_tuple(filename);
93
94 if (tuple != NULL)
95 {
96 title = aud_tuple_formatter_make_title_string(tuple, aud_get_gentitle_format());
97 *length = aud_tuple_get_int(tuple, FIELD_LENGTH, NULL);
98 aud_tuple_free(tuple);
99 }
100 else
101 {
102 title = g_path_get_basename(filename);
103 *length = -1;
104 }
105
106 return title;
107 }
108
109 void xsf_update(unsigned char *buffer, long count, InputPlayback *playback);
110
111 void xsf_play(InputPlayback *data)
112 {
113 guchar *buffer;
114 gsize size;
115 gint length;
116 gchar *title = xsf_title(data->filename, &length);
117 gint16 samples[44100*2];
118 gint seglen = 44100 / 60;
119
120 path = g_strdup(data->filename);
121 aud_vfs_file_get_contents(data->filename, (gchar **) &buffer, &size);
122
123 if (xsf_start(buffer, size) != AO_SUCCESS)
124 {
125 free(buffer);
126 return;
127 }
128
129 data->output->open_audio(FMT_S16_NE, 44100, 2);
130
131 data->set_params(data, title, length, 44100*2*2*8, 44100, 2);
132
133 data->playing = TRUE;
134 data->set_pb_ready(data);
135
136 for (;;)
137 {
138 while (data->playing && !seek)
139 {
140 xsf_gen(samples, seglen);
141 xsf_update(samples, seglen * 4, data);
142
143 if (data->output->written_time() > length)
144 data->eof = TRUE;
145 }
146
147 if (seek)
148 {
149 data->eof = FALSE;
150 data->output->flush(seek);
151
152 xsf_term();
153
154 if (xsf_start(buffer, size) == AO_SUCCESS)
155 {
156 //xsf_seek(seek);
157 seek = 0;
158 continue;
159 }
160 else
161 {
162 data->output->close_audio();
163 break;
164 }
165 }
166
167 xsf_term();
168
169 data->output->buffer_free();
170 data->output->buffer_free();
171
172 while (data->eof && data->output->buffer_playing())
173 g_usleep(10000);
174
175 data->output->close_audio();
176
177 break;
178 }
179
180 g_free(buffer);
181 g_free(path);
182 g_free(title);
183
184 data->playing = FALSE;
185 }
186
187 void xsf_update(unsigned char *buffer, long count, InputPlayback *playback)
188 {
189 const int mask = ~((((16 / 8) * 2)) - 1);
190
191 if (buffer == NULL)
192 {
193 playback->playing = FALSE;
194 playback->eof = TRUE;
195
196 return;
197 }
198
199 while (count > 0)
200 {
201 int t = playback->output->buffer_free() & mask;
202 if (t > count)
203 playback->pass_audio(playback, FMT_S16_NE, 2, count, buffer, NULL);
204 else
205 {
206 if (t)
207 playback->pass_audio(playback, FMT_S16_NE, 2, t, buffer, NULL);
208
209 g_usleep((count-t)*1000*5/441/2);
210 }
211 count -= t;
212 buffer += t;
213 }
214
215 #if 0
216 if (seek)
217 {
218 if (xsf_seek(seek))
219 {
220 playback->output->flush(seek);
221 seek = 0;
222 }
223 else
224 {
225 playback->eof = TRUE;
226 return;
227 }
228 }
229 #endif
230 }
231
232 void xsf_Stop(InputPlayback *playback)
233 {
234 playback->playing = FALSE;
235 }
236
237 void xsf_pause(InputPlayback *playback, short p)
238 {
239 playback->output->pause(p);
240 }
241
242 int xsf_is_our_fd(gchar *filename, VFSFile *file)
243 {
244 gchar magic[4];
245 aud_vfs_fread(magic, 1, 4, file);
246
247 if (!memcmp(magic, "PSF$", 4))
248 return 1;
249
250 return 0;
251 }
252
253 void xsf_Seek(InputPlayback *playback, int time)
254 {
255 seek = time * 1000;
256 }
257
258 gchar *xsf_fmts[] = { "2sf", "mini2sf", NULL };
259
260 InputPlugin xsf_ip =
261 {
262 .description = "2SF Audio Plugin",
263 .play_file = xsf_play,
264 .stop = xsf_Stop,
265 .pause = xsf_pause,
266 .seek = xsf_Seek,
267 .get_song_tuple = xsf_tuple,
268 .is_our_file_from_vfs = xsf_is_our_fd,
269 .vfs_extensions = xsf_fmts,
270 };
271
272 InputPlugin *xsf_iplist[] = { &xsf_ip, NULL };
273
274 DECLARE_PLUGIN(psf2, NULL, NULL, xsf_iplist, NULL, NULL, NULL, NULL, NULL);
275