| 1 | /* ScummVM - Scumm Interpreter |
| 2 | * Copyright (C) 2003-2005 The ScummVM project |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * $Header:$ |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "sound/mod.h" |
| 23 | |
| 24 | #ifdef USE_MIKMOD |
| 25 | |
| 26 | #include "common/file.h" |
| 27 | #include "common/util.h" |
| 28 | |
| 29 | #include "sound/audiostream.h" |
| 30 | #include "sound/audiocd.h" |
| 31 | |
| 32 | #include <mikmod.h> |
| 33 | |
| 34 | |
| 35 | using Common::File; |
| 36 | |
| 37 | |
| 38 | void initMOD(Audio::Mixer *mixer) { |
| 39 | // We only use the MikMod library for decoding. Therefore, we only need |
| 40 | // the "No Sound" driver. |
| 41 | MikMod_RegisterDriver(&drv_nos); |
| 42 | MikMod_RegisterAllLoaders(); |
| 43 | |
| 44 | md_mixfreq = mixer->getOutputRate(); |
| 45 | |
| 46 | CHAR mmArgs[] = ""; |
| 47 | |
| 48 | MikMod_Init(mmArgs); |
| 49 | } |
| 50 | |
| 51 | void exitMOD() { |
| 52 | MikMod_Exit(); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | #pragma mark - |
| 57 | #pragma mark --- MOD Audio CD emulation --- |
| 58 | #pragma mark - |
| 59 | |
| 60 | class MODTrackInfo : public DigitalTrackInfo { |
| 61 | private: |
| 62 | File *_file; |
| 63 | MODULE *_module; |
| 64 | bool _error_flag; |
| 65 | |
| 66 | public: |
| 67 | MODTrackInfo(File *file); |
| 68 | ~MODTrackInfo(); |
| 69 | bool openTrack(); |
| 70 | bool error() { return _error_flag; } |
| 71 | void play(Audio::Mixer *mixer, Audio::SoundHandle *handle, int startFrame, int duration); |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | // These are wrapper functions to allow using a File object to provide data to |
| 76 | // the MOD reader |
| 77 | |
| 78 | struct MFILEREADER { |
| 79 | MREADER core; |
| 80 | MODULE *module; |
| 81 | File *file; |
| 82 | }; |
| 83 | |
| 84 | static int mm_eof_wrapper(struct MREADER *reader) { |
| 85 | return ((MFILEREADER *)reader)->file->eof(); |
| 86 | } |
| 87 | |
| 88 | static BOOL mm_read_wrapper(struct MREADER *reader, void *dest, size_t length) { |
| 89 | return ((MFILEREADER *)reader)->file->read(dest, length); |
| 90 | } |
| 91 | |
| 92 | static int mm_get_wrapper(struct MREADER *reader) { |
| 93 | File *file = ((MFILEREADER *)reader)->file; |
| 94 | |
| 95 | if (file->eof()) |
| 96 | return EOF; |
| 97 | |
| 98 | return file->readByte(); |
| 99 | } |
| 100 | |
| 101 | static BOOL mm_seek_wrapper(struct MREADER *reader, long offset, int whence) { |
| 102 | ((MFILEREADER *)reader)->file->seek(offset, whence); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static long mm_tell_wrapper(struct MREADER *reader) { |
| 107 | return ((MFILEREADER *)reader)->file->pos(); |
| 108 | } |
| 109 | |
| 110 | static MODULE *loadModuleFromFile(File *file) { |
| 111 | assert(file); |
| 112 | |
| 113 | MFILEREADER *reader = (MFILEREADER *)malloc(sizeof(MFILEREADER)); |
| 114 | |
| 115 | assert(reader); |
| 116 | |
| 117 | reader->core.Eof = &mm_eof_wrapper; |
| 118 | reader->core.Read = &mm_read_wrapper; |
| 119 | reader->core.Get = &mm_get_wrapper; |
| 120 | reader->core.Seek = &mm_seek_wrapper; |
| 121 | reader->core.Tell = &mm_tell_wrapper; |
| 122 | |
| 123 | reader->file = file; |
| 124 | |
| 125 | MODULE *module = Player_LoadGeneric((MREADER *)reader, 64, 0); |
| 126 | |
| 127 | if (!module) { |
| 128 | free(reader); |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | file->incRef(); |
| 133 | |
| 134 | Player_Start(module); |
| 135 | Player_SetPosition(0); |
| 136 | |
| 137 | return module; |
| 138 | } |
| 139 | |
| 140 | MODTrackInfo::MODTrackInfo(File *file) { |
| 141 | _file = file; |
| 142 | |
| 143 | if (openTrack()) { |
| 144 | warning("Invalid file format"); |
| 145 | _error_flag = true; |
| 146 | _file = 0; |
| 147 | } else { |
| 148 | _error_flag = false; |
| 149 | _file->incRef(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | MODTrackInfo::~MODTrackInfo() { |
| 154 | if (!_error_flag) { |
| 155 | Player_Free(_module); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | bool MODTrackInfo::openTrack() { |
| 160 | assert(_file); |
| 161 | |
| 162 | _module = loadModuleFromFile(_file); |
| 163 | |
| 164 | if (!_module) { |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | void MODTrackInfo::play(Audio::Mixer *mixer, Audio::SoundHandle *handle, int startFrame, int duration) { |
| 172 | // TODO: We don't handle position or duration at all |
| 173 | |
| 174 | bool err = openTrack(); |
| 175 | assert(!err); |
| 176 | |
| 177 | AudioStream *input = makeMODStream(_file); |
| 178 | mixer->playInputStream(Audio::Mixer::kMusicSoundType, handle, input); |
| 179 | } |
| 180 | |
| 181 | DigitalTrackInfo *getMODTrack(int track) { |
| 182 | char track_name[32]; |
| 183 | File *file = new File(); |
| 184 | |
| 185 | // TODO: MikMod can handle a lot more MOD file types than this |
| 186 | sprintf(track_name, "track%d.mod", track); |
| 187 | file->open(track_name); |
| 188 | |
| 189 | if (file->isOpen()) { |
| 190 | MODTrackInfo *trackInfo = new MODTrackInfo(file); |
| 191 | file->decRef(); |
| 192 | if (!trackInfo->error()) |
| 193 | return trackInfo; |
| 194 | delete trackInfo; |
| 195 | } |
| 196 | delete file; |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | #pragma mark - |
| 201 | #pragma mark --- MOD stream --- |
| 202 | #pragma mark - |
| 203 | |
| 204 | |
| 205 | class MODInputStream : public AudioStream { |
| 206 | private: |
| 207 | int16 _buffer[4096]; |
| 208 | const int16 *_bufferEnd; |
| 209 | const int16 *_pos; |
| 210 | MODULE *_module; |
| 211 | |
| 212 | void refill(); |
| 213 | inline bool eosIntern() const; |
| 214 | |
| 215 | public: |
| 216 | MODInputStream(File *file); |
| 217 | ~MODInputStream(); |
| 218 | |
| 219 | int readBuffer(int16 *buffer, const int numSamples); |
| 220 | |
| 221 | bool endOfData() const { return eosIntern(); } |
| 222 | bool isStereo() const { return true; } |
| 223 | |
| 224 | int getRate() const { return md_mixfreq; } |
| 225 | }; |
| 226 | |
| 227 | MODInputStream::MODInputStream(File *file) { |
| 228 | _module = loadModuleFromFile(file); |
| 229 | |
| 230 | // Read in initial data |
| 231 | refill(); |
| 232 | } |
| 233 | |
| 234 | MODInputStream::~MODInputStream() { |
| 235 | if (_module) { |
| 236 | Player_Free(_module); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | inline bool MODInputStream::eosIntern() const { |
| 241 | return _pos >= _bufferEnd; |
| 242 | } |
| 243 | |
| 244 | int MODInputStream::readBuffer(int16 *buffer, const int numSamples) { |
| 245 | int samples = 0; |
| 246 | while (samples < numSamples && !eosIntern()) { |
| 247 | const int len = MIN(numSamples - samples, (int)(_bufferEnd - _pos)); |
| 248 | memcpy(buffer, _pos, len * 2); |
| 249 | buffer += len; |
| 250 | _pos += len; |
| 251 | samples += len; |
| 252 | if (_pos >= _bufferEnd) { |
| 253 | refill(); |
| 254 | } |
| 255 | } |
| 256 | return samples; |
| 257 | } |
| 258 | |
| 259 | void MODInputStream::refill() { |
| 260 | // Read the samples |
| 261 | uint len_left = sizeof(_buffer); |
| 262 | char *read_pos = (char *)_buffer; |
| 263 | |
| 264 | Player_Start(_module); |
| 265 | |
| 266 | int len = VC_WriteBytes((SBYTE *)_buffer, len_left); |
| 267 | |
| 268 | len_left -= len; |
| 269 | read_pos += len; |
| 270 | |
| 271 | _pos = _buffer; |
| 272 | _bufferEnd = (int16 *)read_pos; |
| 273 | } |
| 274 | |
| 275 | AudioStream *makeMODStream(File *file) { |
| 276 | return new MODInputStream(file); |
| 277 | } |
| 278 | |
| 279 | #endif |