| 1 | /* Mednafen - Multi-system Emulator |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | // AR_Open(), and AudioReader, will NOT take "ownership" of the Stream object(IE it won't ever delete it). Though it does assume it has exclusive access |
| 19 | // to it for as long as the AudioReader object exists. |
| 20 | |
| 21 | // Don't allow exceptions to propagate into the vorbis/musepack/etc. libraries, as it could easily leave the state of the library's decoder "object" in an |
| 22 | // inconsistent state, which would cause all sorts of unfun when we try to destroy it while handling the exception farther up. |
| 23 | |
| 24 | #include "../mednafen.h" |
| 25 | #include "audioreader.h" |
| 26 | |
| 27 | #include "../tremor/ivorbisfile.h" |
| 28 | |
| 29 | #ifdef HAVE_LIBSNDFILE |
| 30 | #include <sndfile.h> |
| 31 | #endif |
| 32 | |
| 33 | #ifdef HAVE_OPUSFILE |
| 34 | #include "audioreader_opus.h" |
| 35 | #endif |
| 36 | |
| 37 | #include <string.h> |
| 38 | #include <errno.h> |
| 39 | #include <time.h> |
| 40 | |
| 41 | #include "../general.h" |
| 42 | #include "../mednafen-endian.h" |
| 43 | |
| 44 | AudioReader::AudioReader() : LastReadPos(0) |
| 45 | { |
| 46 | |
| 47 | } |
| 48 | |
| 49 | AudioReader::~AudioReader() |
| 50 | { |
| 51 | |
| 52 | } |
| 53 | |
| 54 | int64 AudioReader::Read_(int16 *buffer, int64 frames) |
| 55 | { |
| 56 | abort(); |
| 57 | return(false); |
| 58 | } |
| 59 | |
| 60 | bool AudioReader::Seek_(int64 frame_offset) |
| 61 | { |
| 62 | abort(); |
| 63 | return(false); |
| 64 | } |
| 65 | |
| 66 | int64 AudioReader::FrameCount(void) |
| 67 | { |
| 68 | abort(); |
| 69 | return(0); |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | ** |
| 74 | ** |
| 75 | ** |
| 76 | ** |
| 77 | ** |
| 78 | ** |
| 79 | ** |
| 80 | ** |
| 81 | ** |
| 82 | */ |
| 83 | |
| 84 | class OggVorbisReader : public AudioReader |
| 85 | { |
| 86 | public: |
| 87 | OggVorbisReader(Stream *fp); |
| 88 | ~OggVorbisReader(); |
| 89 | |
| 90 | int64 Read_(int16 *buffer, int64 frames); |
| 91 | bool Seek_(int64 frame_offset); |
| 92 | int64 FrameCount(void); |
| 93 | |
| 94 | private: |
| 95 | OggVorbis_File ovfile; |
| 96 | Stream *fw; |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | static size_t iov_read_func(void *ptr, size_t size, size_t nmemb, void *user_data) |
| 101 | { |
| 102 | Stream *fw = (Stream*)user_data; |
| 103 | |
| 104 | if(!size) |
| 105 | return(0); |
| 106 | |
| 107 | try |
| 108 | { |
| 109 | return fw->read(ptr, size * nmemb, false) / size; |
| 110 | } |
| 111 | catch(...) |
| 112 | { |
| 113 | return(0); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static int iov_seek_func(void *user_data, ogg_int64_t offset, int whence) |
| 118 | { |
| 119 | Stream *fw = (Stream*)user_data; |
| 120 | |
| 121 | try |
| 122 | { |
| 123 | fw->seek(offset, whence); |
| 124 | return(0); |
| 125 | } |
| 126 | catch(...) |
| 127 | { |
| 128 | return(-1); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static int iov_close_func(void *user_data) |
| 133 | { |
| 134 | Stream *fw = (Stream*)user_data; |
| 135 | |
| 136 | try |
| 137 | { |
| 138 | fw->close(); |
| 139 | return(0); |
| 140 | } |
| 141 | catch(...) |
| 142 | { |
| 143 | return EOF; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static long iov_tell_func(void *user_data) |
| 148 | { |
| 149 | Stream *fw = (Stream*)user_data; |
| 150 | |
| 151 | try |
| 152 | { |
| 153 | return fw->tell(); |
| 154 | } |
| 155 | catch(...) |
| 156 | { |
| 157 | return(-1); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | OggVorbisReader::OggVorbisReader(Stream *fp) : fw(fp) |
| 162 | { |
| 163 | ov_callbacks cb; |
| 164 | |
| 165 | memset(&cb, 0, sizeof(cb)); |
| 166 | cb.read_func = iov_read_func; |
| 167 | cb.seek_func = iov_seek_func; |
| 168 | cb.close_func = iov_close_func; |
| 169 | cb.tell_func = iov_tell_func; |
| 170 | |
| 171 | fp->seek(0, SEEK_SET); |
| 172 | if(ov_open_callbacks(fp, &ovfile, NULL, 0, cb)) |
| 173 | throw(0); |
| 174 | } |
| 175 | |
| 176 | OggVorbisReader::~OggVorbisReader() |
| 177 | { |
| 178 | ov_clear(&ovfile); |
| 179 | } |
| 180 | |
| 181 | int64 OggVorbisReader::Read_(int16 *buffer, int64 frames) |
| 182 | { |
| 183 | uint8 *tw_buf = (uint8 *)buffer; |
| 184 | int cursection = 0; |
| 185 | long toread = frames * sizeof(int16) * 2; |
| 186 | |
| 187 | while(toread > 0) |
| 188 | { |
| 189 | long didread = ov_read(&ovfile, (char*)tw_buf, toread, &cursection); |
| 190 | |
| 191 | if(didread == 0) |
| 192 | break; |
| 193 | |
| 194 | tw_buf = (uint8 *)tw_buf + didread; |
| 195 | toread -= didread; |
| 196 | } |
| 197 | |
| 198 | return(frames - toread / sizeof(int16) / 2); |
| 199 | } |
| 200 | |
| 201 | bool OggVorbisReader::Seek_(int64 frame_offset) |
| 202 | { |
| 203 | ov_pcm_seek(&ovfile, frame_offset); |
| 204 | return(true); |
| 205 | } |
| 206 | |
| 207 | int64 OggVorbisReader::FrameCount(void) |
| 208 | { |
| 209 | return(ov_pcm_total(&ovfile, -1)); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | ** |
| 214 | ** |
| 215 | ** |
| 216 | ** |
| 217 | ** |
| 218 | ** |
| 219 | ** |
| 220 | ** |
| 221 | ** |
| 222 | */ |
| 223 | |
| 224 | #ifdef HAVE_LIBSNDFILE |
| 225 | class SFReader : public AudioReader |
| 226 | { |
| 227 | public: |
| 228 | |
| 229 | SFReader(Stream *fp); |
| 230 | ~SFReader(); |
| 231 | |
| 232 | int64 Read_(int16 *buffer, int64 frames); |
| 233 | bool Seek_(int64 frame_offset); |
| 234 | int64 FrameCount(void); |
| 235 | |
| 236 | private: |
| 237 | SNDFILE *sf; |
| 238 | SF_INFO sfinfo; |
| 239 | SF_VIRTUAL_IO sfvf; |
| 240 | |
| 241 | Stream *fw; |
| 242 | }; |
| 243 | |
| 244 | static sf_count_t isf_get_filelen(void *user_data) |
| 245 | { |
| 246 | Stream *fw = (Stream*)user_data; |
| 247 | |
| 248 | try |
| 249 | { |
| 250 | return fw->size(); |
| 251 | } |
| 252 | catch(...) |
| 253 | { |
| 254 | return(-1); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static sf_count_t isf_seek(sf_count_t offset, int whence, void *user_data) |
| 259 | { |
| 260 | Stream *fw = (Stream*)user_data; |
| 261 | |
| 262 | try |
| 263 | { |
| 264 | //printf("Seek: offset=%lld, whence=%lld\n", (long long)offset, (long long)whence); |
| 265 | |
| 266 | fw->seek(offset, whence); |
| 267 | return fw->tell(); |
| 268 | } |
| 269 | catch(...) |
| 270 | { |
| 271 | //printf(" SEEK FAILED\n"); |
| 272 | return(-1); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static sf_count_t isf_read(void *ptr, sf_count_t count, void *user_data) |
| 277 | { |
| 278 | Stream *fw = (Stream*)user_data; |
| 279 | |
| 280 | try |
| 281 | { |
| 282 | sf_count_t ret = fw->read(ptr, count, false); |
| 283 | |
| 284 | //printf("Read: count=%lld, ret=%lld\n", (long long)count, (long long)ret); |
| 285 | |
| 286 | return ret; |
| 287 | } |
| 288 | catch(...) |
| 289 | { |
| 290 | //printf(" READ FAILED\n"); |
| 291 | return(0); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | static sf_count_t isf_write(const void *ptr, sf_count_t count, void *user_data) |
| 296 | { |
| 297 | return(0); |
| 298 | } |
| 299 | |
| 300 | static sf_count_t isf_tell(void *user_data) |
| 301 | { |
| 302 | Stream *fw = (Stream*)user_data; |
| 303 | |
| 304 | try |
| 305 | { |
| 306 | return fw->tell(); |
| 307 | } |
| 308 | catch(...) |
| 309 | { |
| 310 | return(-1); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | SFReader::SFReader(Stream *fp) : fw(fp) |
| 315 | { |
| 316 | fp->seek(0, SEEK_SET); |
| 317 | |
| 318 | memset(&sfvf, 0, sizeof(sfvf)); |
| 319 | sfvf.get_filelen = isf_get_filelen; |
| 320 | sfvf.seek = isf_seek; |
| 321 | sfvf.read = isf_read; |
| 322 | sfvf.write = isf_write; |
| 323 | sfvf.tell = isf_tell; |
| 324 | |
| 325 | memset(&sfinfo, 0, sizeof(sfinfo)); |
| 326 | if(!(sf = sf_open_virtual(&sfvf, SFM_READ, &sfinfo, (void*)fp))) |
| 327 | throw(0); |
| 328 | } |
| 329 | |
| 330 | SFReader::~SFReader() |
| 331 | { |
| 332 | sf_close(sf); |
| 333 | } |
| 334 | |
| 335 | int64 SFReader::Read_(int16 *buffer, int64 frames) |
| 336 | { |
| 337 | return(sf_read_short(sf, (short*)buffer, frames * 2) / 2); |
| 338 | } |
| 339 | |
| 340 | bool SFReader::Seek_(int64 frame_offset) |
| 341 | { |
| 342 | // FIXME error condition |
| 343 | if(sf_seek(sf, frame_offset, SEEK_SET) != frame_offset) |
| 344 | return(false); |
| 345 | return(true); |
| 346 | } |
| 347 | |
| 348 | int64 SFReader::FrameCount(void) |
| 349 | { |
| 350 | return(sfinfo.frames); |
| 351 | } |
| 352 | |
| 353 | #endif |
| 354 | |
| 355 | |
| 356 | AudioReader *AR_Open(Stream *fp) |
| 357 | { |
| 358 | #ifdef HAVE_OPUSFILE |
| 359 | try |
| 360 | { |
| 361 | return new OpusReader(fp); |
| 362 | } |
| 363 | catch(int i) |
| 364 | { |
| 365 | } |
| 366 | #endif |
| 367 | |
| 368 | try |
| 369 | { |
| 370 | return new OggVorbisReader(fp); |
| 371 | } |
| 372 | catch(int i) |
| 373 | { |
| 374 | } |
| 375 | |
| 376 | #ifdef HAVE_LIBSNDFILE |
| 377 | try |
| 378 | { |
| 379 | return new SFReader(fp); |
| 380 | } |
| 381 | catch(int i) |
| 382 | { |
| 383 | } |
| 384 | #endif |
| 385 | |
| 386 | return(NULL); |
| 387 | } |
| 388 |