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