ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

sdl-audiocd.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/scummsys.h"
00024 
00025 #if defined(SDL_BACKEND)
00026 
00027 #include "backends/audiocd/sdl/sdl-audiocd.h"
00028 
00029 #if !SDL_VERSION_ATLEAST(2, 0, 0)
00030 
00031 #include "common/textconsole.h"
00032 
00033 SdlAudioCDManager::SdlAudioCDManager()
00034     :
00035     _cdrom(0),
00036     _cdTrack(0),
00037     _cdNumLoops(0),
00038     _cdStartFrame(0),
00039     _cdDuration(0),
00040     _cdEndTime(0),
00041     _cdStopTime(0) {
00042 
00043 }
00044 
00045 SdlAudioCDManager::~SdlAudioCDManager() {
00046     close();
00047 }
00048 
00049 bool SdlAudioCDManager::open() {
00050     close();
00051 
00052     if (openRealCD())
00053         return true;
00054 
00055     return DefaultAudioCDManager::open();
00056 }
00057 
00058 bool SdlAudioCDManager::openCD(int drive) {
00059     if (SDL_InitSubSystem(SDL_INIT_CDROM) == -1)
00060         _cdrom = NULL;
00061     else {
00062         _cdrom = SDL_CDOpen(drive);
00063         // Did it open? Check if _cdrom is NULL
00064         if (!_cdrom) {
00065             warning("Couldn't open drive: %s", SDL_GetError());
00066         } else {
00067             _cdNumLoops = 0;
00068             _cdStopTime = 0;
00069             _cdEndTime = 0;
00070         }
00071     }
00072 
00073     return (_cdrom != NULL);
00074 }
00075 
00076 void SdlAudioCDManager::close() {
00077     DefaultAudioCDManager::close();
00078 
00079     if (_cdrom) {
00080                 SDL_CDStop(_cdrom);
00081                 SDL_CDClose(_cdrom);
00082         _cdrom = 0;
00083         }
00084 }
00085 
00086 void SdlAudioCDManager::stop() {
00087     DefaultAudioCDManager::stop();
00088 
00089     // Stop CD Audio in 1/10th of a second
00090     _cdStopTime = SDL_GetTicks() + 100;
00091     _cdNumLoops = 0;
00092 }
00093 
00094 bool SdlAudioCDManager::play(int track, int numLoops, int startFrame, int duration, bool onlyEmulate,
00095         Audio::Mixer::SoundType soundType) {
00096     // Prefer emulation
00097     if (DefaultAudioCDManager::play(track, numLoops, startFrame, duration, onlyEmulate, soundType))
00098         return true;
00099 
00100     // If we're set to only emulate, or have no CD, return here
00101     if (onlyEmulate || !_cdrom)
00102         return false;
00103 
00104     if (!numLoops && !startFrame)
00105         return false;
00106 
00107     // FIXME: Explain this.
00108     if (duration > 0)
00109         duration += 5;
00110 
00111     _cdTrack = track;
00112     _cdNumLoops = numLoops;
00113     _cdStartFrame = startFrame;
00114 
00115     SDL_CDStatus(_cdrom);
00116     if (startFrame == 0 && duration == 0)
00117         SDL_CDPlayTracks(_cdrom, track, 0, 1, 0);
00118     else
00119         SDL_CDPlayTracks(_cdrom, track, startFrame, 0, duration);
00120     _cdDuration = duration;
00121     _cdStopTime = 0;
00122     _cdEndTime = SDL_GetTicks() + _cdrom->track[track].length * 1000 / CD_FPS;
00123 
00124     return true;
00125 }
00126 
00127 bool SdlAudioCDManager::isPlaying() const {
00128     if (DefaultAudioCDManager::isPlaying())
00129         return true;
00130 
00131     if (!_cdrom)
00132         return false;
00133 
00134     return (_cdNumLoops != 0 && (SDL_GetTicks() < _cdEndTime || SDL_CDStatus(_cdrom) == CD_PLAYING));
00135 }
00136 
00137 void SdlAudioCDManager::update() {
00138     DefaultAudioCDManager::update();
00139 
00140     if (!_cdrom)
00141         return;
00142 
00143     if (_cdStopTime != 0 && SDL_GetTicks() >= _cdStopTime) {
00144         SDL_CDStop(_cdrom);
00145         _cdNumLoops = 0;
00146         _cdStopTime = 0;
00147         return;
00148     }
00149 
00150     if (_cdNumLoops == 0 || SDL_GetTicks() < _cdEndTime)
00151         return;
00152 
00153     if (_cdNumLoops != 1 && SDL_CDStatus(_cdrom) != CD_STOPPED) {
00154         // Wait another second for it to be done
00155         _cdEndTime += 1000;
00156         return;
00157     }
00158 
00159     if (_cdNumLoops > 0)
00160         _cdNumLoops--;
00161 
00162     if (_cdNumLoops != 0) {
00163         if (_cdStartFrame == 0 && _cdDuration == 0)
00164             SDL_CDPlayTracks(_cdrom, _cdTrack, 0, 1, 0);
00165         else
00166             SDL_CDPlayTracks(_cdrom, _cdTrack, _cdStartFrame, 0, _cdDuration);
00167         _cdEndTime = SDL_GetTicks() + _cdrom->track[_cdTrack].length * 1000 / CD_FPS;
00168     }
00169 }
00170 
00171 #endif // !SDL_VERSION_ATLEAST(2, 0, 0)
00172 
00173 #endif


Generated on Sat May 25 2019 05:00:53 for ResidualVM by doxygen 1.7.1
curved edge   curved edge