123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /* -*- mode: c++; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
- //
- // Copyright (c) 2009, 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 "util.h"
- #include "global.h"
- #include "palcfg.h"
- #if PAL_HAS_MP3
- #include "audio.h"
- #include "players.h"
- #include "resampler.h"
- #include "libmad/music_mad.h"
- typedef struct tagMP3PLAYER
- {
- AUDIOPLAYER_COMMONS;
- mad_data *pMP3;
- INT iMusic;
- BOOL fLoop;
- } MP3PLAYER, *LPMP3PLAYER;
- static VOID MP3_Close(
- LPMP3PLAYER player
- )
- {
- if (player->pMP3)
- {
- mad_stop(player->pMP3);
- mad_closeFile(player->pMP3);
- player->pMP3 = NULL;
- }
- }
- static VOID
- MP3_FillBuffer(
- VOID *object,
- LPBYTE stream,
- INT len
- )
- {
- LPMP3PLAYER player = (LPMP3PLAYER)object;
- if (player->pMP3) {
- if (!mad_isPlaying(player->pMP3) && player->fLoop)
- {
- mad_seek(player->pMP3, 0);
- mad_start(player->pMP3);
- }
- if (mad_isPlaying(player->pMP3))
- mad_getSamples(player->pMP3, stream, len);
- }
- }
- static VOID
- MP3_Shutdown(
- VOID *object
- )
- {
- if (object)
- {
- MP3_Close((LPMP3PLAYER)object);
- free(object);
- }
- }
- static BOOL
- MP3_Play(
- VOID *object,
- INT iNum,
- BOOL fLoop,
- FLOAT flFadeTime
- )
- {
- LPMP3PLAYER player = (LPMP3PLAYER)object;
- //
- // Check for NULL pointer.
- //
- if (player == NULL)
- {
- return FALSE;
- }
- player->fLoop = fLoop;
- if (iNum == player->iMusic)
- {
- return TRUE;
- }
- MP3_Close(player);
- if (iNum > 0)
- {
- if ((player->pMP3 = mad_openFile(va("%smp3/%.2d.mp3", gConfig.pszGamePath, iNum), AUDIO_GetDeviceSpec(), gConfig.iResampleQuality)) == NULL)
- {
- player->pMP3 = mad_openFile(va("%sMP3/%.2d.MP3", gConfig.pszGamePath, iNum), AUDIO_GetDeviceSpec(), gConfig.iResampleQuality);
- }
- if (player->pMP3)
- {
- player->iMusic = iNum;
- mad_start(player->pMP3);
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- else
- {
- return TRUE;
- }
- }
- LPAUDIOPLAYER
- MP3_Init(
- LPCSTR szFileName
- )
- {
- LPMP3PLAYER player;
- if (player = (LPMP3PLAYER)malloc(sizeof(MP3PLAYER)))
- {
- player->FillBuffer = MP3_FillBuffer;
- player->Play = MP3_Play;
- player->Shutdown = MP3_Shutdown;
- player->pMP3 = NULL;
- player->iMusic = -1;
- player->fLoop = FALSE;
- return (LPAUDIOPLAYER)player;
- }
- else
- {
- return NULL;
- }
- }
- #endif
|