1
|
1 /*
|
5
|
2 XMMS-SID - SIDPlay input plugin for X MultiMedia System (XMMS)
|
1
|
3
|
|
4 Main source file
|
|
5
|
5
|
6 Written by Matti "ccr" Hamalainen <ccr@tnsp.org>
|
|
7 (few bits may still be by Willem Monsuwe)
|
1
|
8
|
|
9 This program is free software; you can redistribute it and/or modify
|
|
10 it under the terms of the GNU General Public License as published by
|
|
11 the Free Software Foundation; either version 2 of the License, or
|
|
12 (at your option) any later version.
|
|
13
|
|
14 This program is distributed in the hope that it will be useful,
|
|
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 GNU General Public License for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with this program; if not, write to the Free Software
|
|
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
22 */
|
|
23
|
|
24 #include "xmms-sid.h"
|
|
25 #include <sidplay/player.h>
|
|
26 #include <sidplay/myendian.h>
|
|
27 #include <sidplay/fformat.h>
|
|
28
|
|
29 extern "C" {
|
|
30 #include <pthread.h>
|
|
31
|
|
32 #include <stdlib.h>
|
|
33 #include <string.h>
|
|
34 #include <stdio.h>
|
5
|
35 #include <errno.h>
|
1
|
36
|
|
37 #include <xmms/plugin.h>
|
|
38 #include <xmms/util.h>
|
|
39 }
|
|
40
|
|
41
|
|
42 /*
|
5
|
43 * Global variables
|
1
|
44 */
|
|
45 static struct emuConfig xs_emuConf;
|
|
46 static emuEngine xs_emuEngine;
|
|
47 static pthread_t xs_decode_thread;
|
5
|
48 struct t_xs_cfg xs_cfg;
|
|
49
|
|
50 struct {
|
|
51 int s_error, s_playing, s_songs, s_allownext;
|
|
52 sidTune *s_tune;
|
|
53 gchar *s_fname;
|
|
54 } xs_status;
|
|
55
|
|
56 pthread_mutex_t xs_mutex = PTHREAD_MUTEX_INITIALIZER;
|
1
|
57
|
|
58
|
|
59 /*
|
5
|
60 * Initialize XMMS-SID
|
1
|
61 */
|
|
62 void xs_init(void)
|
|
63 {
|
5
|
64 XSDEBUG("xs_init()\n");
|
1
|
65
|
5
|
66 /* Try to initialize libSIDPlay */
|
|
67 if (!xs_emuEngine)
|
|
68 {
|
|
69 XSERR("Couldn't start SIDPlay emulator engine!\n");
|
|
70 return;
|
1
|
71 }
|
|
72
|
5
|
73 if (!xs_emuEngine.verifyEndianess())
|
|
74 {
|
|
75 XSERR("Wrong hardware endianess (SIDPlay error)!\n");
|
|
76 return;
|
1
|
77 }
|
|
78
|
5
|
79 /* Initialize and get configuration */
|
|
80 memset(&xs_cfg, 0, sizeof(xs_cfg));
|
|
81 xs_get_configure();
|
1
|
82
|
5
|
83 xs_status.s_error = 0;
|
|
84 xs_status.s_playing = 0;
|
|
85 xs_status.s_songs = 0;
|
|
86 xs_status.s_allownext = 1; // Initialize to TRUE to allow first song
|
|
87 xs_status.s_tune = NULL;
|
|
88 xs_status.s_fname = NULL;
|
|
89
|
|
90 /* Initialize STIL structures */
|
|
91 // FIXME FIXME FIx ME
|
1
|
92
|
5
|
93 /* Create sub-song control window */
|
|
94 // FIX ME FIXME
|
1
|
95 }
|
|
96
|
|
97
|
|
98 /*
|
5
|
99 * Shut down XMMS-SID
|
|
100 */
|
|
101 void xs_close(void)
|
1
|
102 {
|
5
|
103 XSDEBUG("shutting down...\n");
|
1
|
104
|
5
|
105 /* Stop playing */
|
|
106 xmms_sid_ip.stop();
|
1
|
107
|
5
|
108 /* Free allocated memory */
|
1
|
109
|
5
|
110 // FIXME FIXME: STIL-entries, songlendb
|
1
|
111
|
|
112 }
|
|
113
|
|
114
|
|
115 /*
|
|
116 * Check whether the given file is handled by this plugin
|
|
117 */
|
5
|
118 gint xs_is_our_file(char *fileName)
|
1
|
119 {
|
5
|
120 char *pcExt;
|
|
121
|
|
122 /* Check the filename */
|
|
123 if (fileName == NULL)
|
|
124 return FALSE;
|
|
125
|
|
126 /* Try to detect via libSIDPlay's detection routine, if required */
|
|
127 if (xs_cfg.detectMagic) {
|
1
|
128
|
5
|
129 sidTune *testTune = new sidTune(fileName);
|
1
|
130
|
5
|
131 if (!testTune) return FALSE;
|
|
132 if (!testTune->getStatus())
|
|
133 {
|
|
134 delete testTune;
|
|
135 return FALSE;
|
|
136 }
|
|
137
|
|
138 delete testTune;
|
|
139 return TRUE;
|
1
|
140 }
|
|
141
|
5
|
142
|
|
143 /* Detect just by checking filename extension */
|
|
144 pcExt = strrchr(fileName, '.');
|
|
145 if (pcExt)
|
|
146 {
|
|
147 pcExt++;
|
|
148 if (!strcasecmp(pcExt, "psid")) return TRUE;
|
|
149 if (!strcasecmp(pcExt, "sid")) return TRUE;
|
|
150 if (!strcasecmp(pcExt, "dat")) return TRUE;
|
|
151 if (!strcasecmp(pcExt, "inf")) return TRUE;
|
|
152 if (!strcasecmp(pcExt, "info")) return TRUE;
|
1
|
153 }
|
5
|
154
|
|
155 return FALSE;
|
1
|
156 }
|
|
157
|
|
158
|
|
159 /*
|
|
160 * Playing thread loop function
|
|
161 */
|
5
|
162 static void *xs_play_loop(void *argPointer)
|
1
|
163 {
|
5
|
164 gchar plr_data[XMMS_SID_BUFSIZE];
|
|
165 struct sidTuneInfo plr_sidInf;
|
|
166 gchar *plr_tune_title = NULL;
|
|
167 gint plr_fxlen;
|
|
168 enum AFormat plr_fmt;
|
|
169 gint plr_tune_num;
|
|
170 gint32 plr_tune_len;
|
|
171
|
|
172
|
|
173 /* Don't allow next song to be set yet */
|
|
174 pthread_mutex_lock(&xs_mutex);
|
|
175 xs_status.s_allownext = 0;
|
|
176 pthread_mutex_unlock(&xs_mutex);
|
|
177
|
|
178
|
|
179 /* Check tune number */
|
|
180 plr_tune_num = xs_status.s_playing;
|
|
181
|
|
182 if (plr_tune_num <= 0)
|
|
183 plr_tune_num = 1;
|
|
184
|
|
185 XSDEBUG("xs_play_loop(%d, %d)\n", plr_tune_num, xs_status.s_playing);
|
|
186
|
|
187 /* Get song information */
|
|
188 xs_status.s_tune->getInfo(plr_sidInf);
|
|
189 plr_tune_len = xs_get_length(xs_status.s_fname, plr_tune_num);
|
|
190 plr_tune_title = xs_get_filetitle(&plr_sidInf, plr_tune_num);
|
1
|
191
|
5
|
192 XSDEBUG("title='%s', len=%d\n", plr_tune_title, plr_tune_len);
|
|
193
|
|
194
|
|
195 /* Initialize audio output */
|
|
196 // FIXME FIXME: FMT_S16_XXX -- different architechtures??
|
|
197 // the patch may break something ...
|
|
198 plr_fmt = (xs_emuConf.bitsPerSample == 16) ? FMT_S16_NE : FMT_U8;
|
|
199
|
1
|
200
|
5
|
201 if (!xmms_sid_ip.output->open_audio(plr_fmt, xs_emuConf.frequency, xs_emuConf.channels))
|
1
|
202 {
|
5
|
203 pthread_mutex_lock(&xs_mutex);
|
|
204 xs_status.s_error = 1;
|
|
205 if (plr_tune_title) g_free(plr_tune_title);
|
|
206 if (xs_status.s_tune) delete xs_status.s_tune;
|
|
207 xs_status.s_allownext = 1;
|
|
208 pthread_mutex_unlock(&xs_mutex);
|
|
209 return NULL;
|
|
210 }
|
|
211
|
|
212
|
|
213 /* Initialize the SIDPlay-emulator for song */
|
|
214 if (!sidEmuInitializeSong(xs_emuEngine, *xs_status.s_tune, plr_tune_num))
|
|
215 {
|
|
216 XSERR("Couldn't initialize SIDPlay emulator engine! This may be a problem with your sound settings, or possibly a bug in XMMS-SID.\n");
|
|
217 pthread_mutex_lock(&xs_mutex);
|
|
218 xs_status.s_error = 1;
|
|
219 pthread_mutex_unlock(&xs_mutex);
|
|
220 goto pl_cleanup;
|
1
|
221 }
|
|
222
|
5
|
223
|
|
224 /* Set song title information */
|
|
225 xmms_sid_ip.set_info(
|
|
226 plr_tune_title,
|
|
227 (plr_tune_len * 1000),
|
|
228 (1000 * (plr_sidInf.songSpeed ? plr_sidInf.songSpeed : (plr_sidInf.clockSpeed == SIDTUNE_CLOCK_NTSC) ? 60 : 50)),
|
|
229 xs_emuConf.frequency,
|
|
230 xs_emuConf.channels);
|
|
231
|
1
|
232
|
5
|
233 /* Run playing loop: loop as long as xs_playing is same as current tune number */
|
|
234 while (xs_status.s_playing == plr_tune_num)
|
|
235 {
|
|
236 plr_fxlen = XMMS_SID_BUFSIZE;
|
|
237
|
|
238 /* Run emulator to fill output buffer with audio data */
|
|
239 sidEmuFillBuffer(xs_emuEngine, *xs_status.s_tune, plr_data, plr_fxlen);
|
|
240
|
|
241 /* If Max Playtime option set, check playtime */
|
|
242 if (xs_cfg.playUseMaxTime)
|
|
243 {
|
|
244 if ((xmms_sid_ip.output->output_time() / 1000) >= xs_cfg.playMaxTime)
|
|
245 {
|
|
246 pthread_mutex_lock(&xs_mutex);
|
|
247 xs_status.s_playing = 0;
|
|
248 pthread_mutex_unlock(&xs_mutex);
|
|
249 }
|
|
250 }
|
1
|
251
|
5
|
252 /* Check playtime against database */
|
|
253 if (xs_cfg.playMethod == XMMS_SID_PMETHOD_DATABASE)
|
|
254 {
|
|
255 if ((xmms_sid_ip.output->output_time() / 1000) >= plr_tune_len)
|
|
256 {
|
|
257 pthread_mutex_lock(&xs_mutex);
|
|
258 xs_status.s_playing = 0;
|
|
259 pthread_mutex_unlock(&xs_mutex);
|
|
260 }
|
|
261
|
|
262 }
|
|
263 #if 0
|
|
264 else
|
1
|
265
|
5
|
266 /* Check for silence */
|
|
267 if (xs_cfg.playMethod == XMMS_SID_PMETHOD_MAXSILENCE)
|
|
268 {
|
|
269 }
|
|
270
|
|
271 /* Add static noise */
|
|
272
|
|
273 /* Muffle waveform (low-pass filter) */
|
1
|
274
|
5
|
275 #endif
|
|
276
|
|
277
|
|
278 /* Send audio data to visualization plugin */
|
1
|
279 xmms_sid_ip.add_vis_pcm(xmms_sid_ip.output->written_time(),
|
5
|
280 plr_fmt, xs_emuConf.channels, plr_fxlen, plr_data);
|
1
|
281
|
5
|
282 /* Wait for a while */
|
|
283 while ((xs_status.s_playing == plr_tune_num) && (xmms_sid_ip.output->buffer_free() < plr_fxlen))
|
1
|
284 xmms_usleep(10000);
|
|
285
|
5
|
286
|
|
287 /* If playing, send final audio data to output plugin */
|
|
288 if (xs_status.s_playing == plr_tune_num)
|
|
289 xmms_sid_ip.output->write_audio(plr_data, plr_fxlen);
|
1
|
290
|
5
|
291 } /* End of playerloop */
|
|
292
|
|
293
|
|
294 pl_cleanup:
|
|
295 XSDEBUG("cleaning up...\n");
|
|
296
|
|
297 /* Cleanup & shutdown */
|
1
|
298 xmms_sid_ip.output->close_audio();
|
|
299
|
5
|
300 if (plr_tune_title) g_free(plr_tune_title);
|
1
|
301
|
5
|
302 pthread_mutex_lock(&xs_mutex);
|
|
303 xs_status.s_playing = 0;
|
|
304 if (xs_status.s_tune) delete xs_status.s_tune;
|
|
305 xs_status.s_allownext = 1;
|
|
306 pthread_mutex_unlock(&xs_mutex);
|
1
|
307
|
5
|
308 XSDEBUG("exiting thread, bye.\n");
|
|
309 return NULL;
|
1
|
310 }
|
|
311
|
|
312
|
|
313 /*
|
|
314 * Start playing the given file
|
|
315 */
|
5
|
316 void xs_play_file(char *fileName)
|
1
|
317 {
|
5
|
318 sidTune *newTune;
|
|
319 struct sidTuneInfo sidInf;
|
|
320
|
|
321 XSDEBUG("request to start '%s'\n", fileName);
|
|
322
|
|
323 /* Wait until the previous song has finished for sure */
|
|
324 if (!xs_status.s_allownext)
|
|
325 pthread_join(xs_decode_thread, NULL);
|
1
|
326
|
5
|
327 /* Try to get the tune */
|
|
328 newTune = new sidTune(fileName);
|
|
329 if (newTune == NULL) return;
|
|
330
|
|
331 XSDEBUG("tune ok, status %i\n", xs_status.s_playing);
|
1
|
332
|
5
|
333 /* Get current configuration */
|
|
334 xs_emuEngine.getConfig(xs_emuConf);
|
|
335
|
|
336
|
|
337 /* Configure channels and stuff */
|
|
338 switch (xs_cfg.fmtChannels) {
|
1
|
339
|
|
340 case XMMS_SID_CHN_AUTOPAN:
|
|
341 xs_emuConf.channels = SIDEMU_STEREO;
|
|
342 xs_emuConf.autoPanning = SIDEMU_CENTEREDAUTOPANNING;
|
|
343 xs_emuConf.volumeControl = SIDEMU_FULLPANNING;
|
|
344 break;
|
|
345
|
|
346 case XMMS_SID_CHN_STEREO:
|
|
347 xs_emuConf.channels = SIDEMU_STEREO;
|
|
348 xs_emuConf.autoPanning = SIDEMU_NONE;
|
|
349 xs_emuConf.volumeControl = SIDEMU_NONE;
|
|
350 break;
|
|
351
|
|
352 case XMMS_SID_CHN_MONO:
|
|
353 xs_emuConf.channels = SIDEMU_MONO;
|
|
354 xs_emuConf.autoPanning = SIDEMU_NONE;
|
|
355 xs_emuConf.volumeControl = SIDEMU_NONE;
|
|
356 break;
|
|
357
|
5
|
358 default:
|
|
359 XSERR("Internal: Invalid channels setting. Possibly corrupted configuration file.\n");
|
|
360 delete newTune;
|
|
361 return;
|
|
362 }
|
1
|
363
|
5
|
364 /* Memory mode settings */
|
|
365 switch (xs_cfg.memoryMode) {
|
1
|
366 case XMMS_SID_MPU_BANK_SWITCHING:
|
|
367 xs_emuConf.memoryMode = MPU_BANK_SWITCHING;
|
|
368 break;
|
|
369
|
|
370 case XMMS_SID_MPU_TRANSPARENT_ROM:
|
|
371 xs_emuConf.memoryMode = MPU_TRANSPARENT_ROM;
|
|
372 break;
|
|
373
|
|
374 case XMMS_SID_MPU_PLAYSID_ENVIRONMENT:
|
|
375 xs_emuConf.memoryMode = MPU_PLAYSID_ENVIRONMENT;
|
|
376 break;
|
|
377
|
5
|
378 default:
|
|
379 XSERR("Internal: Invalid memoryMode setting. Possibly corrupted configuration file.\n");
|
|
380 delete newTune;
|
|
381 return;
|
|
382 }
|
1
|
383
|
|
384
|
5
|
385 /* Clockspeed settings */
|
|
386 switch (xs_cfg.clockSpeed) {
|
1
|
387 case XMMS_SID_CLOCK_PAL:
|
|
388 xs_emuConf.clockSpeed = SIDTUNE_CLOCK_PAL;
|
|
389 break;
|
|
390
|
|
391 case XMMS_SID_CLOCK_NTSC:
|
|
392 xs_emuConf.clockSpeed = SIDTUNE_CLOCK_NTSC;
|
|
393 break;
|
|
394
|
5
|
395 default:
|
|
396 XSERR("Internal: Invalid clockSpeed setting. Possibly corrupted configuration file.\n");
|
|
397 delete newTune;
|
|
398 return;
|
|
399 }
|
|
400
|
|
401
|
|
402 /* Configure rest of the emulation */
|
|
403 xs_emuConf.bitsPerSample = xs_cfg.fmtBitsPerSample;
|
|
404 xs_emuConf.frequency = xs_cfg.fmtFrequency;
|
|
405 xs_emuConf.sampleFormat = SIDEMU_SIGNED_PCM;
|
|
406 xs_emuConf.mos8580 = xs_cfg.mos8580;
|
|
407 xs_emuConf.emulateFilter = xs_cfg.emulateFilter;
|
|
408 xs_emuConf.filterFs = xs_cfg.filterFs;
|
|
409 xs_emuConf.filterFm = xs_cfg.filterFm;
|
|
410 xs_emuConf.filterFt = xs_cfg.filterFt;
|
|
411
|
|
412 XSDEBUG("configuring engine..\n");
|
|
413
|
|
414 /* Now set the emulator configuration */
|
|
415 xs_emuEngine.setConfig(xs_emuConf);
|
|
416 newTune->getInfo(sidInf);
|
|
417
|
|
418 pthread_mutex_lock(&xs_mutex);
|
|
419 xs_status.s_error = 0;
|
|
420 xs_status.s_playing = sidInf.startSong;
|
|
421 xs_status.s_songs = sidInf.songs;
|
|
422 xs_status.s_tune = newTune;
|
|
423 pthread_mutex_unlock(&xs_mutex);
|
|
424
|
|
425 XSDEBUG("starting thread!\n");
|
|
426
|
|
427 /* Start the playing thread! */
|
|
428 if (pthread_create(&xs_decode_thread, NULL, xs_play_loop, NULL) < 0)
|
|
429 {
|
|
430 XSERR("Couldn't start playing thread! Possible reason reported by system: %s\n", strerror(errno));
|
|
431 delete newTune;
|
1
|
432 }
|
|
433
|
5
|
434 /* Exit */
|
1
|
435 }
|
|
436
|
|
437
|
|
438 /*
|
5
|
439 * Stop playing
|
1
|
440 */
|
|
441 void xs_stop(void)
|
|
442 {
|
5
|
443 /* If playing, stop. */
|
|
444 if (xs_status.s_playing)
|
1
|
445 {
|
5
|
446 pthread_mutex_lock(&xs_mutex);
|
|
447 xs_status.s_playing = 0;
|
|
448 pthread_mutex_unlock(&xs_mutex);
|
|
449
|
|
450 pthread_join(xs_decode_thread, NULL);
|
1
|
451 }
|
|
452 }
|
|
453
|
|
454
|
|
455 /*
|
5
|
456 * Pause the playing
|
1
|
457 */
|
5
|
458 void xs_pause(short pauseState)
|
1
|
459 {
|
5
|
460 xmms_sid_ip.output->pause(pauseState);
|
1
|
461 }
|
|
462
|
|
463
|
|
464 /*
|
5
|
465 * Set the time-seek position
|
|
466 * (the playing thread will do the "seeking" aka song-change)
|
1
|
467 */
|
5
|
468 void xs_seek(int iTime)
|
1
|
469 {
|
5
|
470 #ifdef HAVE_SONG_POSITION
|
|
471 if ((iTime > 0) && (iTime <= xs_songs))
|
|
472 {
|
|
473 pthread_mutex_lock(&xs_mutex);
|
|
474 xs_status.s_playing = iTime;
|
|
475 pthread_mutex_unlock(&xs_mutex);
|
1
|
476 }
|
5
|
477 #endif
|
1
|
478 }
|
|
479
|
|
480
|
|
481 /*
|
5
|
482 * Return the playing "position/time"
|
1
|
483 */
|
|
484 int xs_get_time(void)
|
|
485 {
|
5
|
486 if (xs_status.s_error)
|
|
487 return -2;
|
|
488
|
|
489 if (!xs_status.s_playing)
|
|
490 return -1;
|
1
|
491
|
5
|
492 #ifdef HAVE_SONG_POSITION
|
|
493 set_song_position(xs_status.s_playing, 1, xs_status.s_songs);
|
|
494 #endif
|
1
|
495
|
5
|
496 return xmms_sid_ip.output->output_time();
|
1
|
497 }
|
|
498
|
|
499
|
5
|
500 /*
|
|
501 * Return song information
|
|
502 */
|
|
503 void xs_get_song_info(char *songFilename, char **songTitle, int *songLength)
|
|
504 {
|
|
505 struct sidTuneInfo sidInf;
|
|
506 sidTune *testTune = new sidTune(songFilename);
|
|
507
|
|
508 /* Check if the tune exists and is readable */
|
|
509 if (!testTune) return;
|
|
510 if (!testTune->getStatus())
|
|
511 {
|
|
512 delete testTune;
|
|
513 return;
|
|
514 }
|
|
515
|
|
516 /* Get general tune information */
|
|
517 testTune->getInfo(sidInf);
|
|
518 delete testTune;
|
|
519
|
|
520 /* Get titlestring */
|
|
521 *songTitle = xs_get_filetitle(&sidInf, sidInf.startSong);
|
|
522
|
|
523 /* Get song length (in seconds), negative if no known length */
|
|
524 *songLength = xs_get_length(songFilename, sidInf.startSong);
|
|
525 }
|
|
526
|