# HG changeset patch # User Matti Hamalainen # Date 1353442428 -7200 # Node ID 0e60e5d56fddc3188e3c411fee561c064b3042f8 # Parent fbcd069663e529bb4a2c738c3fb394bd6c4aef2b Change how the backend emulator library is initialized for libSIDPlay2 and FP, as it seems the engine configuration has some persistence despite reconfiguration between loaded files if same engine object is retained. This caused, for example, 2SID stereo tunes being played "mono" if played after a normal 1-SID tune. Duh. diff -r fbcd069663e5 -r 0e60e5d56fdd src/xs_sidplay2.cpp --- a/src/xs_sidplay2.cpp Tue Nov 20 21:29:51 2012 +0200 +++ b/src/xs_sidplay2.cpp Tue Nov 20 22:13:48 2012 +0200 @@ -101,11 +101,76 @@ */ gboolean xs_sidplay2_init(XSEngineState * state) { + + + return TRUE; +} + + +/* Close SIDPlay2 engine + */ +void xs_sidplay2_close(XSEngineState * state) +{ + XSSIDPlay2 *engine = (XSSIDPlay2 *) state->internal; + + XSDEBUG("SIDPlay2 backend shutdown.\n"); + + xs_sidplay2_delete(state); +} + + +/* Initialize current song and sub-tune + */ +gboolean xs_sidplay2_initsong(XSEngineState * state) +{ + XSSIDPlay2 *engine = (XSSIDPlay2 *) state->internal; + + if (!engine) + return FALSE; + + if (!engine->tune.selectSong(state->currSong)) + { + xs_error("[SIDPlay2] tune.selectSong() failed\n"); + return FALSE; + } + + if (engine->emu.load(&(engine->tune)) < 0) + { + xs_error("[SIDPlay2] emu.load() failed\n"); + return FALSE; + } + + if (engine->emu.config(engine->config) < 0) + { + xs_error("[SIDPlay2] Emulator engine configuration failed!\n"); + return FALSE; + } + + return TRUE; +} + + +/* Emulate and render audio data to given buffer + */ +guint xs_sidplay2_fillbuffer(XSEngineState * state, gchar * audioBuffer, guint audioBufSize) +{ + XSSIDPlay2 *engine = (XSSIDPlay2 *) state->internal; + + if (!engine) + return 0; + + return engine->emu.play(audioBuffer, audioBufSize); +} + + +/* Load a given SID-tune file + */ +gboolean xs_sidplay2_load(XSEngineState * state, gchar * filename) +{ XSSIDPlay2 *engine; - sid_filter_t tmpFilter; - xs_sid_filter_t *f; + guint8 *buf = NULL; + size_t bufSize; gint i; - assert(state); XSDEBUG("SIDPlay2 backend initializing.\n"); @@ -113,7 +178,10 @@ engine = new XSSIDPlay2(); state->internal = engine; if (!engine) - return FALSE; + { + xs_error("Allocating XSSIDPlay2 compound backend object failed.\n"); + goto error; + } /* Get current configuration */ XSDEBUG("SIDPlay2 emulation configuration\n"); @@ -205,23 +273,6 @@ break; } - /* Convert filter */ - f = &(xs_cfg.sid2Filter); - XSDEBUG("using filter '%s', %d points\n", f->name, f->npoints); - if (f->npoints > XS_SIDPLAY2_NFPOINTS) - { - xs_error("[SIDPlay2] Invalid number of filter curve points (%d > %d)\n", - f->npoints, XS_SIDPLAY2_NFPOINTS); - f->npoints = XS_SIDPLAY2_NFPOINTS; - } - - tmpFilter.points = f->npoints; - for (i = 0; i < f->npoints; i++) - { - tmpFilter.cutoff[i][0] = f->points[i].x; - tmpFilter.cutoff[i][1] = f->points[i].y; - } - /* Clockspeed settings */ switch (xs_cfg.clockSpeed) { @@ -313,99 +364,9 @@ return FALSE; } -#if 0 - // Setup filter - engine->config.sidEmulation->filter(xs_cfg.emulateFilters); - if (!*(engine->config.sidEmulation)) - { - xs_error("builder->filter(%d) failed.\n", xs_cfg.emulateFilters); - return FALSE; - } -#endif - XSDEBUG("%s\n", engine->config.sidEmulation->credits()); - return TRUE; -} - - -/* Close SIDPlay2 engine - */ -void xs_sidplay2_close(XSEngineState * state) -{ - XSSIDPlay2 *engine = (XSSIDPlay2 *) state->internal; - - XSDEBUG("SIDPlay2 backend shutdown.\n"); - - xs_sidplay2_delete(state); - - if (engine) - { - delete engine; - engine = NULL; - } - - state->internal = NULL; -} - - -/* Initialize current song and sub-tune - */ -gboolean xs_sidplay2_initsong(XSEngineState * state) -{ - XSSIDPlay2 *engine = (XSSIDPlay2 *) state->internal; - - if (!engine) - return FALSE; - - if (!engine->tune.selectSong(state->currSong)) - { - xs_error("[SIDPlay2] tune.selectSong() failed\n"); - return FALSE; - } - - if (engine->emu.load(&(engine->tune)) < 0) - { - xs_error("[SIDPlay2] emu.load() failed\n"); - return FALSE; - } - - if (engine->emu.config(engine->config) < 0) - { - xs_error("[SIDPlay2] Emulator engine configuration failed!\n"); - return FALSE; - } - - return TRUE; -} - - -/* Emulate and render audio data to given buffer - */ -guint xs_sidplay2_fillbuffer(XSEngineState * state, gchar * audioBuffer, guint audioBufSize) -{ - XSSIDPlay2 *engine = (XSSIDPlay2 *) state->internal; - - if (!engine) - return 0; - - return engine->emu.play(audioBuffer, audioBufSize); -} - - -/* Load a given SID-tune file - */ -gboolean xs_sidplay2_load(XSEngineState * state, gchar * filename) -{ - XSSIDPlay2 *engine = (XSSIDPlay2 *) state->internal; - gboolean res = FALSE; - guint8 *buf = NULL; - size_t bufSize; - - if (!engine) - return FALSE; - if (!xs_fload_buffer(filename, &buf, &bufSize, XS_SIDBUF_SIZE, TRUE)) goto error; @@ -429,8 +390,11 @@ */ void xs_sidplay2_delete(XSEngineState * state) { - (void) state; -// XSSIDPlay2 *engine = (XSSIDPlay2 *) state->internal; + XSSIDPlay2 *engine = (XSSIDPlay2 *) state->internal; + + if (engine) + delete engine; + state->internal = NULL; } diff -r fbcd069663e5 -r 0e60e5d56fdd src/xs_sidplayfp.cpp --- a/src/xs_sidplayfp.cpp Tue Nov 20 21:29:51 2012 +0200 +++ b/src/xs_sidplayfp.cpp Tue Nov 20 22:13:48 2012 +0200 @@ -47,7 +47,6 @@ #ifdef HAVE_SIDPLAYFP_V1 sidplayfp emu; SidConfig config; - guint8 *romImages[XS_C64_ROM_IMAGES]; #else sidplay2 emu; sid2_config_t config; @@ -58,6 +57,7 @@ virtual ~XSSIDPlayFP(void); }; +static guint8 *xs_rom_imagedata[XS_C64_ROM_IMAGES]; #ifdef HAVE_RESID_FP_BUILDER # include @@ -138,16 +138,90 @@ */ gboolean xs_sidplayfp_init(XSEngineState * state) { - XSSIDPlayFP *engine; assert(state); XSDEBUG("SIDPlayFP backend initializing.\n"); + memset(xs_rom_imagedata, 0, sizeof(xs_rom_imagedata)); + if (!xs_load_rom_images(xs_rom_imagedata)) + return FALSE; + + return TRUE; +} + + +/* Close SIDPlayFP engine + */ +void xs_sidplayfp_close(XSEngineState * state) +{ + XSSIDPlayFP *engine = (XSSIDPlayFP *) state->internal; + + XSDEBUG("SIDPlayFP backend shutdown.\n"); + + xs_sidplayfp_delete(state); +} + + +/* Initialize current song and sub-tune + */ +gboolean xs_sidplayfp_initsong(XSEngineState * state) +{ + XSSIDPlayFP *engine = (XSSIDPlayFP *) state->internal; + + if (!engine) + return FALSE; + + if (!engine->tune.selectSong(state->currSong)) + { + xs_error("[SIDPlayFP] tune.selectSong() failed\n"); + return FALSE; + } + + if (engine->emu.load(&(engine->tune)) < 0) + { + xs_error("[SIDPlayFP] emu.load() failed\n"); + return FALSE; + } + + if (engine->emu.config(engine->config) < 0) + { + xs_error("[SIDPlayFP] Emulator engine configuration failed!\n"); + return FALSE; + } + + return TRUE; +} + + +/* Emulate and render audio data to given buffer + */ +guint xs_sidplayfp_fillbuffer(XSEngineState * state, gchar * audioBuffer, guint audioBufSize) +{ + XSSIDPlayFP *engine = (XSSIDPlayFP *) state->internal; + + if (!engine) + return 0; + + return engine->emu.play((short *) audioBuffer, audioBufSize / sizeof(short)) * sizeof(short); +} + + +/* Load a given SID-tune file + */ +gboolean xs_sidplayfp_load(XSEngineState * state, gchar * filename) +{ + XSSIDPlayFP *engine; + guint8 *buf = NULL; + size_t bufSize; + /* Allocate internal structures */ engine = new XSSIDPlayFP(); state->internal = engine; if (!engine) - return FALSE; + { + xs_error("Allocating XSSIDPlayFP compound backend object failed.\n"); + goto error; + } /* Get current configuration */ XSDEBUG("SIDPlayFP emulation configuration\n"); @@ -215,17 +289,17 @@ if (rs && rs->getStatus()) { engine->config.sidEmulation = rs; - if (!rs->getStatus()) return FALSE; + if (!rs->getStatus()) goto error; rs->create((engine->emu.info()).maxsids()); - if (!rs->getStatus()) return FALSE; + if (!rs->getStatus()) goto error; } #else if (rs && *rs) { engine->config.sidEmulation = rs; - if (!*rs) return FALSE; + if (!*rs) goto error; rs->create((engine->emu.info()).maxsids); - if (!*rs) return FALSE; + if (!*rs) goto error; } #endif rs->bias(0.0f); @@ -242,17 +316,17 @@ if (rs && rs->getStatus()) { engine->config.sidEmulation = rs; - if (!rs->getStatus()) return FALSE; + if (!rs->getStatus()) goto error; rs->create((engine->emu.info()).maxsids()); - if (!rs->getStatus()) return FALSE; + if (!rs->getStatus()) goto error; } #else if (rs && *rs) { engine->config.sidEmulation = rs; - if (!*rs) return FALSE; + if (!*rs) goto error; rs->create((engine->emu.info()).maxsids); - if (!*rs) return FALSE; + if (!*rs) goto error; } #endif // rs->filter6581Curve(0.0); @@ -270,13 +344,13 @@ if (hs && hs->getStatus()) { hs->create((engine->emu.info()).maxsids()); - if (!hs->getStatus()) return FALSE; + if (!hs->getStatus()) goto error; } #else if (hs && *hs) { hs->create((engine->emu.info()).maxsids); - if (!*hs) return FALSE; + if (!*hs) goto error; } #endif } @@ -285,13 +359,14 @@ default: xs_error("[SIDPlayFP] Invalid or unsupported builder selected.\n"); - return FALSE; + goto error; } + if (!engine->config.sidEmulation) { xs_error("[SIDPlayFP] Could not initialize SIDBuilder object.\n"); - return FALSE; + goto error; } // Setup filter @@ -303,98 +378,16 @@ #endif { xs_error("builder->filter(%d) failed.\n", xs_cfg.emulateFilters); - return FALSE; + goto error; } XSDEBUG("%s\n", engine->config.sidEmulation->credits()); #ifdef HAVE_SIDPLAYFP_V1 - memset(engine->romImages, 0, sizeof(engine->romImages)); - if (!xs_load_rom_images(engine->romImages)) - return FALSE; - - engine->emu.setRoms(engine->romImages[0], engine->romImages[1], engine->romImages[2]); + engine->emu.setRoms(xs_rom_imagedata[0], xs_rom_imagedata[1], xs_rom_imagedata[2]); #endif - - return TRUE; -} - - -/* Close SIDPlayFP engine - */ -void xs_sidplayfp_close(XSEngineState * state) -{ - XSSIDPlayFP *engine = (XSSIDPlayFP *) state->internal; - - XSDEBUG("SIDPlayFP backend shutdown.\n"); - - xs_sidplayfp_delete(state); - - if (engine) - { - delete engine; - engine = NULL; - } - - state->internal = NULL; -} -/* Initialize current song and sub-tune - */ -gboolean xs_sidplayfp_initsong(XSEngineState * state) -{ - XSSIDPlayFP *engine = (XSSIDPlayFP *) state->internal; - - if (!engine) - return FALSE; - - if (!engine->tune.selectSong(state->currSong)) - { - xs_error("[SIDPlayFP] tune.selectSong() failed\n"); - return FALSE; - } - - if (engine->emu.load(&(engine->tune)) < 0) - { - xs_error("[SIDPlayFP] emu.load() failed\n"); - return FALSE; - } - - if (engine->emu.config(engine->config) < 0) - { - xs_error("[SIDPlayFP] Emulator engine configuration failed!\n"); - return FALSE; - } - - return TRUE; -} - - -/* Emulate and render audio data to given buffer - */ -guint xs_sidplayfp_fillbuffer(XSEngineState * state, gchar * audioBuffer, guint audioBufSize) -{ - XSSIDPlayFP *engine = (XSSIDPlayFP *) state->internal; - - if (!engine) - return 0; - - return engine->emu.play((short *) audioBuffer, audioBufSize / sizeof(short)) * sizeof(short); -} - - -/* Load a given SID-tune file - */ -gboolean xs_sidplayfp_load(XSEngineState * state, gchar * filename) -{ - XSSIDPlayFP *engine = (XSSIDPlayFP *) state->internal; - gboolean res = FALSE; - guint8 *buf = NULL; - size_t bufSize; - - if (!engine) - return FALSE; if (!xs_fload_buffer(filename, &buf, &bufSize, XS_SIDBUF_SIZE, TRUE)) goto error; @@ -411,11 +404,15 @@ goto error; } - res = TRUE; + g_free(buf); + return TRUE; error: + if (engine) + delete engine; + state->internal = NULL; g_free(buf); - return res; + return FALSE; } @@ -423,8 +420,11 @@ */ void xs_sidplayfp_delete(XSEngineState * state) { - (void) state; -// XSSIDPlayFP *engine = (XSSIDPlayFP *) state->internal; + XSSIDPlayFP *engine = (XSSIDPlayFP *) state->internal; + + if (engine) + delete engine; + state->internal = NULL; }