123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- /* -*- mode: c++; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
- //
- // Copyright (c) 2008, Wei Mingzhi <whistler_wmz@users.sf.net>.
- // All rights reserved.
- // Modified by Lou Yihua <louyihua@21cn.com>, 2015.
- //
- // This file is part of SDLPAL.
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- #include <math.h>
- #include "global.h"
- #include "players.h"
- #include "sound.h"
- #include "resampler.h"
- #include "adplug/opl.h"
- #include "adplug/demuopl.h"
- #include "adplug/dbemuopl.h"
- #if PAL_HAS_MAME
- #include "adplug/emuopl.h"
- #endif
- #include "adplug/surroundopl.h"
- #include "adplug/rix.h"
- extern "C" BOOL g_fNoMusic;
- typedef struct tagRIXPLAYER :
- public MUSICPLAYER
- {
- Copl *opl;
- CrixPlayer *rix;
- void *resampler[2];
- BYTE buf[(PAL_MAX_SAMPLERATE + 69) / 70 * sizeof(short) * 2];
- LPBYTE pos;
- INT iCurrentMusic; // current playing music number
- INT iNextMusic; // the next music number to switch to
- DWORD dwStartFadeTime;
- INT iTotalFadeOutSamples;
- INT iTotalFadeInSamples;
- INT iRemainingFadeSamples;
- enum { NONE, FADE_IN, FADE_OUT } FadeType; // fade in or fade out ?
- BOOL fLoop;
- BOOL fNextLoop;
- BOOL fReady;
- } RIXPLAYER, *LPRIXPLAYER;
- static VOID
- RIX_FillBuffer(
- VOID *object,
- LPBYTE stream,
- INT len
- )
- /*++
- Purpose:
- Fill the background music into the sound buffer. Called by the SDL sound
- callback function only (sound.c: SOUND_FillAudio).
- Parameters:
- [OUT] stream - pointer to the stream buffer.
- [IN] len - Length of the buffer.
- Return value:
- None.
- --*/
- {
- LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
- const INT max_volume = gConfig.iVolume * 3 / 4;
- if (pRixPlayer == NULL || !pRixPlayer->fReady)
- {
- //
- // Not initialized
- //
- return;
- }
- while (len > 0)
- {
- INT volume, delta_samples = 0, vol_delta = 0;
- //
- // fading in or fading out
- //
- switch (pRixPlayer->FadeType)
- {
- case RIXPLAYER::FADE_IN:
- if (pRixPlayer->iRemainingFadeSamples <= 0)
- {
- pRixPlayer->FadeType = RIXPLAYER::NONE;
- volume = max_volume;
- }
- else
- {
- volume = (INT)(max_volume * (1.0 - (double)pRixPlayer->iRemainingFadeSamples / pRixPlayer->iTotalFadeInSamples));
- delta_samples = pRixPlayer->iTotalFadeInSamples / max_volume; vol_delta = 1;
- }
- break;
- case RIXPLAYER::FADE_OUT:
- if (pRixPlayer->iTotalFadeOutSamples == pRixPlayer->iRemainingFadeSamples && pRixPlayer->iTotalFadeOutSamples > 0)
- {
- UINT now = SDL_GetTicks();
- INT passed_samples = ((INT)(now - pRixPlayer->dwStartFadeTime) > 0) ? (INT)((now - pRixPlayer->dwStartFadeTime) * SOUND_GetAudioSpec()->freq / 1000) : 0;
- pRixPlayer->iRemainingFadeSamples -= passed_samples;
- }
- if (pRixPlayer->iCurrentMusic == -1 || pRixPlayer->iRemainingFadeSamples <= 0)
- {
- //
- // There is no current playing music, or fading time has passed.
- // Start playing the next one or stop playing.
- //
- if (pRixPlayer->iNextMusic > 0)
- {
- pRixPlayer->iCurrentMusic = pRixPlayer->iNextMusic;
- pRixPlayer->iNextMusic = -1;
- pRixPlayer->fLoop = pRixPlayer->fNextLoop;
- pRixPlayer->FadeType = RIXPLAYER::FADE_IN;
- if (pRixPlayer->iCurrentMusic > 0)
- pRixPlayer->dwStartFadeTime += pRixPlayer->iTotalFadeOutSamples * 1000 / gConfig.iSampleRate;
- else
- pRixPlayer->dwStartFadeTime = SDL_GetTicks();
- pRixPlayer->iTotalFadeOutSamples = 0;
- pRixPlayer->iRemainingFadeSamples = pRixPlayer->iTotalFadeInSamples;
- pRixPlayer->rix->rewind(pRixPlayer->iCurrentMusic);
- if (pRixPlayer->resampler[0]) resampler_clear(pRixPlayer->resampler[0]);
- if (pRixPlayer->resampler[1]) resampler_clear(pRixPlayer->resampler[1]);
- continue;
- }
- else
- {
- pRixPlayer->iCurrentMusic = -1;
- pRixPlayer->FadeType = RIXPLAYER::NONE;
- return;
- }
- }
- else
- {
- volume = (INT)(max_volume * ((double)pRixPlayer->iRemainingFadeSamples / pRixPlayer->iTotalFadeOutSamples));
- delta_samples = pRixPlayer->iTotalFadeOutSamples / max_volume; vol_delta = -1;
- }
- break;
- default:
- if (pRixPlayer->iCurrentMusic <= 0)
- {
- //
- // No current playing music
- //
- return;
- }
- else
- {
- volume = max_volume;
- }
- }
- //
- // Fill the buffer with sound data
- //
- int buf_max_len = gConfig.iSampleRate / 70 * gConfig.iAudioChannels * sizeof(short);
- bool fContinue = true;
- while (len > 0 && fContinue)
- {
- if (pRixPlayer->pos == NULL || pRixPlayer->pos - pRixPlayer->buf >= buf_max_len)
- {
- pRixPlayer->pos = pRixPlayer->buf;
- if (!pRixPlayer->rix->update())
- {
- if (!pRixPlayer->fLoop)
- {
- //
- // Not loop, simply terminate the music
- //
- pRixPlayer->iCurrentMusic = -1;
- if (pRixPlayer->FadeType != RIXPLAYER::FADE_OUT && pRixPlayer->iNextMusic == -1)
- {
- pRixPlayer->FadeType = RIXPLAYER::NONE;
- }
- return;
- }
- pRixPlayer->rix->rewind(pRixPlayer->iCurrentMusic, false);
- if (!pRixPlayer->rix->update())
- {
- //
- // Something must be wrong
- //
- pRixPlayer->iCurrentMusic = -1;
- pRixPlayer->FadeType = RIXPLAYER::NONE;
- return;
- }
- }
- int sample_count = gConfig.iSampleRate / 70;
- if (pRixPlayer->resampler[0])
- {
- unsigned int samples_written = 0;
- short *finalBuf = (short*)pRixPlayer->buf;
- while (sample_count)
- {
- int to_write = resampler_get_free_count(pRixPlayer->resampler[0]);
- if (to_write)
- {
- short *tempBuf = (short*)alloca(to_write * gConfig.iAudioChannels * sizeof(short));
- pRixPlayer->opl->update(tempBuf, to_write);
- for (int i = 0; i < to_write; i++)
- for (int j = 0; j < gConfig.iAudioChannels; j++)
- resampler_write_sample(pRixPlayer->resampler[j], tempBuf[i * gConfig.iAudioChannels + j]);
- }
- int to_get = resampler_get_sample_count(pRixPlayer->resampler[0]);
- if (to_get > sample_count) to_get = sample_count;
- for (int i = 0; i < to_get; i++)
- for (int j = 0; j < gConfig.iAudioChannels; j++)
- finalBuf[samples_written++] = resampler_get_and_remove_sample(pRixPlayer->resampler[j]);
- sample_count -= to_get;
- }
- }
- else
- {
- pRixPlayer->opl->update((short *)(pRixPlayer->buf), sample_count);
- }
- }
- int l = buf_max_len - (pRixPlayer->pos - pRixPlayer->buf);
- l = (l > len) ? len / sizeof(short) : l / sizeof(short);
- //
- // Put audio data into buffer and adjust volume
- // WARNING: for signed 16-bit little-endian only
- //
- SHORT* ptr = (SHORT*)stream;
- if (pRixPlayer->FadeType == RIXPLAYER::NONE)
- {
- for (int i = 0; i < l; i++)
- {
- *ptr++ = SDL_SwapLE16((short)((int)(*(SHORT *)(pRixPlayer->pos)) * volume / SDL_MIX_MAXVOLUME));
- pRixPlayer->pos += sizeof(SHORT);
- }
- }
- else
- {
- for (int i = 0; i < l && pRixPlayer->iRemainingFadeSamples > 0; volume += vol_delta)
- {
- for (int j = 0; i < l && j < delta_samples; i++, j++)
- {
- *ptr++ = SDL_SwapLE16((short)((int)(*(SHORT *)(pRixPlayer->pos)) * volume / SDL_MIX_MAXVOLUME));
- pRixPlayer->pos += sizeof(SHORT);
- }
- pRixPlayer->iRemainingFadeSamples -= delta_samples;
- }
- fContinue = (pRixPlayer->iRemainingFadeSamples > 0);
- }
- len -= (LPBYTE)ptr - stream;
- stream = (LPBYTE)ptr;
- }
- }
- }
- static VOID
- RIX_Shutdown(
- VOID *object
- )
- /*++
- Purpose:
- Shutdown the RIX player subsystem.
- Parameters:
- None.
- Return value:
- None.
- --*/
- {
- if (object != NULL)
- {
- LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
- pRixPlayer->fReady = FALSE;
- for (int i = 0; i < gConfig.iAudioChannels; i++)
- if (pRixPlayer->resampler[i])
- resampler_delete(pRixPlayer->resampler[i]);
- delete pRixPlayer->rix;
- delete pRixPlayer->opl;
- delete pRixPlayer;
- }
- }
- static BOOL
- RIX_Play(
- VOID *object,
- INT iNumRIX,
- BOOL fLoop,
- FLOAT flFadeTime
- )
- /*++
- Purpose:
- Start playing the specified music.
- Parameters:
- [IN] iNumRIX - number of the music. 0 to stop playing current music.
- [IN] fLoop - Whether the music should be looped or not.
- [IN] flFadeTime - the fade in/out time when switching music.
- Return value:
- None.
- --*/
- {
- LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
- //
- // Check for NULL pointer.
- //
- if (pRixPlayer == NULL)
- {
- return FALSE;
- }
- //
- // Stop the current CD music.
- //
- SOUND_PlayCDA(-1);
- if (iNumRIX == pRixPlayer->iCurrentMusic && pRixPlayer->iNextMusic == -1)
- {
- /* Will play the same music without any pending play changes,
- just change the loop attribute */
- pRixPlayer->fLoop = fLoop;
- return TRUE;
- }
- if (pRixPlayer->FadeType != RIXPLAYER::FADE_OUT)
- {
- if (pRixPlayer->FadeType == RIXPLAYER::FADE_IN && pRixPlayer->iTotalFadeInSamples > 0 && pRixPlayer->iRemainingFadeSamples > 0)
- {
- pRixPlayer->dwStartFadeTime = SDL_GetTicks() - (int)((float)pRixPlayer->iRemainingFadeSamples / pRixPlayer->iTotalFadeInSamples * flFadeTime * (1000 / 2));
- }
- else
- {
- pRixPlayer->dwStartFadeTime = SDL_GetTicks();
- }
- pRixPlayer->iTotalFadeOutSamples = (int)round(flFadeTime / 2.0f * gConfig.iSampleRate) * gConfig.iAudioChannels;
- pRixPlayer->iRemainingFadeSamples = pRixPlayer->iTotalFadeOutSamples;
- pRixPlayer->iTotalFadeInSamples = pRixPlayer->iTotalFadeOutSamples;
- }
- else
- {
- pRixPlayer->iTotalFadeInSamples = (int)round(flFadeTime / 2.0f * gConfig.iSampleRate) * gConfig.iAudioChannels;
- }
- pRixPlayer->iNextMusic = iNumRIX;
- pRixPlayer->FadeType = RIXPLAYER::FADE_OUT;
- pRixPlayer->fNextLoop = fLoop;
- pRixPlayer->fReady = TRUE;
- return TRUE;
- }
- LPMUSICPLAYER
- RIX_Init(
- LPCSTR szFileName
- )
- /*++
- Purpose:
- Initialize the RIX player subsystem.
- Parameters:
- [IN] szFileName - Filename of the mus.mkf file.
- Return value:
- 0 if success, -1 if cannot allocate memory, -2 if file not found.
- --*/
- {
- LPRIXPLAYER pRixPlayer = new RIXPLAYER;
- if (pRixPlayer == NULL)
- {
- return NULL;
- }
- else
- {
- memset(pRixPlayer, 0, sizeof(RIXPLAYER));
- pRixPlayer->FillBuffer = RIX_FillBuffer;
- pRixPlayer->Shutdown = RIX_Shutdown;
- pRixPlayer->Play = RIX_Play;
- }
- if (gConfig.fUseSurroundOPL)
- {
- switch (gConfig.eOPLType)
- {
- case OPL_DOSBOX_OLD:
- pRixPlayer->opl = new CSurroundopl(
- new CDemuopl(gConfig.iOPLSampleRate, true, false),
- new CDemuopl(gConfig.iOPLSampleRate, true, false),
- true, gConfig.iOPLSampleRate, gConfig.dSurroundOPLOffset);
- break;
- case OPL_DOSBOX:
- pRixPlayer->opl = new CSurroundopl(
- new CDBemuopl(gConfig.iOPLSampleRate, true, false),
- new CDBemuopl(gConfig.iOPLSampleRate, true, false),
- true, gConfig.iOPLSampleRate, gConfig.dSurroundOPLOffset);
- break;
- #if PAL_HAS_MAME
- case OPL_MAME:
- pRixPlayer->opl = new CSurroundopl(
- new CEmuopl(gConfig.iOPLSampleRate, true, false),
- new CEmuopl(gConfig.iOPLSampleRate, true, false),
- true, gConfig.iOPLSampleRate, gConfig.dSurroundOPLOffset);
- #endif
- break;
- }
- }
- else
- {
- switch (gConfig.eOPLType)
- {
- case OPL_DOSBOX_OLD:
- pRixPlayer->opl = new CDemuopl(gConfig.iOPLSampleRate, true, gConfig.iAudioChannels == 2);
- break;
- case OPL_DOSBOX:
- pRixPlayer->opl = new CDBemuopl(gConfig.iOPLSampleRate, true, gConfig.iAudioChannels == 2);
- break;
- #if PAL_HAS_MAME
- case OPL_MAME:
- pRixPlayer->opl = new CEmuopl(gConfig.iOPLSampleRate, true, gConfig.iAudioChannels == 2);
- break;
- #endif
- }
- }
- if (pRixPlayer->opl == NULL)
- {
- delete pRixPlayer;
- return NULL;
- }
- pRixPlayer->rix = new CrixPlayer(pRixPlayer->opl);
- if (pRixPlayer->rix == NULL)
- {
- delete pRixPlayer->opl;
- delete pRixPlayer;
- return NULL;
- }
- //
- // Load the MKF file.
- //
- if (!pRixPlayer->rix->load(szFileName, CProvider_Filesystem()))
- {
- delete pRixPlayer->rix;
- delete pRixPlayer->opl;
- delete pRixPlayer;
- pRixPlayer = NULL;
- return NULL;
- }
- if (gConfig.iOPLSampleRate != gConfig.iSampleRate)
- {
- for (int i = 0; i < gConfig.iAudioChannels; i++)
- {
- pRixPlayer->resampler[i] = resampler_create();
- resampler_set_quality(pRixPlayer->resampler[i], SOUND_IsIntegerConversion(gConfig.iOPLSampleRate) ? RESAMPLER_QUALITY_MIN : gConfig.iResampleQuality);
- resampler_set_rate(pRixPlayer->resampler[i], (double)gConfig.iOPLSampleRate / (double)gConfig.iSampleRate);
- }
- }
- #if USE_RIX_EXTRA_INIT
- if (gConfig.pExtraFMRegs && gConfig.pExtraFMVals)
- {
- pRixPlayer->rix->set_extra_init(gConfig.pExtraFMRegs, gConfig.pExtraFMVals, gConfig.dwExtraLength);
- }
- #endif
- //
- // Success.
- //
- pRixPlayer->FadeType = RIXPLAYER::NONE;
- pRixPlayer->iCurrentMusic = pRixPlayer->iNextMusic = -1;
- pRixPlayer->pos = NULL;
- pRixPlayer->fLoop = FALSE;
- pRixPlayer->fNextLoop = FALSE;
- pRixPlayer->fReady = FALSE;
- return pRixPlayer;
- }
|