diff options
author | Paweł Kołodziejski | 2002-09-02 07:53:43 +0000 |
---|---|---|
committer | Paweł Kołodziejski | 2002-09-02 07:53:43 +0000 |
commit | ca03c9b5fc58b6beb5dbc6c1307464ef9e2c9f45 (patch) | |
tree | 539f84d8edc7002ccb01278aa06a174ab53bda3d | |
parent | 97ef7c2a34c4077d6d046f0aabce7e1dde04e092 (diff) | |
download | scummvm-rg350-ca03c9b5fc58b6beb5dbc6c1307464ef9e2c9f45.tar.gz scummvm-rg350-ca03c9b5fc58b6beb5dbc6c1307464ef9e2c9f45.tar.bz2 scummvm-rg350-ca03c9b5fc58b6beb5dbc6c1307464ef9e2c9f45.zip |
changed file io in sounds to class File
svn-id: r4896
-rw-r--r-- | common/file.cpp | 15 | ||||
-rw-r--r-- | common/file.h | 2 | ||||
-rw-r--r-- | scumm/sound.cpp | 92 | ||||
-rw-r--r-- | scumm/sound.h | 9 | ||||
-rw-r--r-- | sound/mixer.cpp | 10 | ||||
-rw-r--r-- | sound/mixer.h | 8 |
6 files changed, 64 insertions, 72 deletions
diff --git a/common/file.cpp b/common/file.cpp index faa09dde1e..809c843ecf 100644 --- a/common/file.cpp +++ b/common/file.cpp @@ -114,7 +114,7 @@ void File::seek(uint32 offs, int whence) { clearerr(_handle); } -void File::read(void *ptr, uint32 size) { +uint32 File::read(void *ptr, uint32 size) { byte *ptr2 = (byte *)ptr; if (_handle == NULL) { @@ -125,14 +125,19 @@ void File::read(void *ptr, uint32 size) { if (size == 0) return; - if ((uint32)fread(ptr2, size, 1, _handle) != 1) { + if ((uint32)fread(ptr2, 1, size, _handle) != size) { clearerr(_handle); _readFailed = true; } - do { - *ptr2++ ^= _encbyte; - } while (--size); + if (_encbyte != 0) { + uint32 t_size = size; + do { + *ptr2++ ^= _encbyte; + } while (--t_size); + } + + return size; } byte File::readByte() { diff --git a/common/file.h b/common/file.h index a3cb1988cc..6820e77255 100644 --- a/common/file.h +++ b/common/file.h @@ -46,7 +46,7 @@ public: bool eof(); uint32 pos(); void seek(uint32 offs, int whence); - void read(void *ptr, uint32 size); + uint32 read(void *ptr, uint32 size); byte readByte(); uint16 readWordLE(); uint32 readDwordLE(); diff --git a/scumm/sound.cpp b/scumm/sound.cpp index 67eedc7240..9028ae5feb 100644 --- a/scumm/sound.cpp +++ b/scumm/sound.cpp @@ -403,7 +403,7 @@ int Sound::startTalkSound(uint32 offset, uint32 b, int mode) { byte file_byte, file_byte_2; int size; - if (!_sfxFile) { + if (_sfxFile->isOpen() == false) { warning("startTalkSound: SFX file is not open"); return -1; } @@ -437,11 +437,11 @@ int Sound::startTalkSound(uint32 offset, uint32 b, int mode) { size = -1; } - _scumm->fileSeek((FILE *) _sfxFile, offset, SEEK_SET); + _sfxFile->seek(offset, SEEK_SET); i = 0; while (num > 0) { - _scumm->fileRead((FILE *) _sfxFile, &file_byte, sizeof(file_byte)); - _scumm->fileRead((FILE *) _sfxFile, &file_byte_2, sizeof(file_byte_2)); + _sfxFile->read(&file_byte, sizeof(file_byte)); + _sfxFile->read(&file_byte_2, sizeof(file_byte_2)); _mouthSyncTimes[i++] = file_byte | (file_byte_2 << 8); num--; } @@ -620,7 +620,7 @@ void Sound::pauseSounds(bool pause) { _scumm->_mixer->pause(pause); } -int Sound::startSfxSound(void *file, int file_size) { +int Sound::startSfxSound(File *file, int file_size) { char ident[8]; int block_type; byte work[8]; @@ -639,7 +639,7 @@ int Sound::startSfxSound(void *file, int file_size) { if (file_size > 0) { data = (byte *)calloc(file_size + MAD_BUFFER_GUARD, 1); - if (fread(data, file_size, 1, (FILE *) file) != 1) { + if (file->read(data, file_size) != file_size) { /* no need to free the memory since error will shut down */ error("startSfxSound: cannot read %d bytes", size); return -1; @@ -647,30 +647,30 @@ int Sound::startSfxSound(void *file, int file_size) { return playSfxSound_MP3(data, file_size); } #endif - if (fread(ident, 8, 1, (FILE *) file) != 1) + if (file->read(ident, 8) != 8) goto invalid; if (!memcmp(ident, "VTLK", 4)) { - fseek((FILE *) file, SOUND_HEADER_BIG_SIZE - 8, SEEK_CUR); + file->seek(SOUND_HEADER_BIG_SIZE - 8, SEEK_CUR); } else if (!memcmp(ident, "Creative", 8)) { - fseek((FILE *) file, SOUND_HEADER_SIZE - 8, SEEK_CUR); + file->seek(SOUND_HEADER_SIZE - 8, SEEK_CUR); } else { invalid:; warning("startSfxSound: invalid header"); return -1; } - block_type = getc((FILE *) file); + block_type = file->readByte(); if (block_type != 1) { warning("startSfxSound: Expecting block_type == 1, got %d", block_type); return -1; } - fread(work, 3, 1, (FILE *) file); + file->read(work, 3); size = (work[0] | (work[1] << 8) | (work[2] << 16)) - 2; - rate = getc((FILE *) file); - comp = getc((FILE *) file); + rate = file->readByte(); + comp = file->readByte(); if (comp != 0) { warning("startSfxSound: Unsupported compression type %d", comp); @@ -683,7 +683,7 @@ int Sound::startSfxSound(void *file, int file_size) { return -1; } - if (fread(data, size, 1, (FILE *) file) != 1) { + if (file->read(data, size) != size) { /* no need to free the memory since error will shut down */ error("startSfxSound: cannot read %d bytes", size); return -1; @@ -692,25 +692,9 @@ int Sound::startSfxSound(void *file, int file_size) { return playSfxSound(data, size, 1000000 / (256 - rate), true); } - -#ifdef COMPRESSED_SOUND_FILE -static int get_int(FILE * f) { - int ret = 0; - for (int size = 0; size < 4; size++) { - int c = fgetc(f); - if (c == EOF) { - error("Unexpected end of file !!!"); - } - ret <<= 8; - ret |= c; - } - return ret; -} -#endif - -void * Sound::openSfxFile() { +File * Sound::openSfxFile() { char buf[256]; - FILE *file = NULL; + File * file = new File(); /* Try opening the file <_exe_name>.sou first, eg tentacle.sou. * That way, you can keep .sou files for multiple games in the @@ -719,12 +703,11 @@ void * Sound::openSfxFile() { offset_table = NULL; sprintf(buf, "%s%s.so3", _scumm->getGameDataPath(), _scumm->_exe_name); - file = fopen(buf, "rb"); - if (!file) { + if (!file->open(buf)) { sprintf(buf, "%smonster.so3", _scumm->getGameDataPath()); - file = fopen(buf, "rb"); + file->open(buf); } - if (file != NULL) { + if (file->isOpen() == true) { /* Now load the 'offset' index in memory to be able to find the MP3 data The format of the .SO3 file is easy : @@ -742,17 +725,17 @@ void * Sound::openSfxFile() { int size, compressed_offset; MP3OffsetTable *cur; - compressed_offset = get_int(file); + compressed_offset = file->readDwordBE(); offset_table = (MP3OffsetTable *) malloc(compressed_offset); num_sound_effects = compressed_offset / 16; size = compressed_offset; cur = offset_table; while (size > 0) { - cur[0].org_offset = get_int(file); - cur[0].new_offset = get_int(file) + compressed_offset + 4; /* The + 4 is to take into accound the 'size' field */ - cur[0].num_tags = get_int(file); - cur[0].compressed_size = get_int(file); + cur[0].org_offset = file->readDwordBE(); + cur[0].new_offset = file->readDwordBE() + compressed_offset + 4; /* The + 4 is to take into accound the 'size' field */ + cur[0].num_tags = file->readDwordBE(); + cur[0].compressed_size = file->readDwordBE(); size -= 4 * 4; cur++; } @@ -760,10 +743,9 @@ void * Sound::openSfxFile() { } #endif sprintf(buf, "%s%s.sou", _scumm->getGameDataPath(), _scumm->_exe_name); - file = fopen(buf, "rb"); - if (!file) { + if (!file->open(buf)) { sprintf(buf, "%smonster.sou", _scumm->getGameDataPath()); - file = fopen(buf, "rb"); + file->open(buf); } return file; } @@ -1042,7 +1024,7 @@ void Sound::updateCD() int Sound::getCachedTrack(int track) { int i; char track_name[1024]; - FILE* file; + File * file = new File(); int current_index; struct mad_stream stream; struct mad_frame frame; @@ -1051,7 +1033,7 @@ int Sound::getCachedTrack(int track) { int count = 0; // See if we find the track in the cache - for (i=0; i<CACHE_TRACKS; i++) + for (i = 0; i < CACHE_TRACKS; i++) if (_cached_tracks[i] == track) { if (_mp3_tracks[i]) return i; @@ -1063,18 +1045,20 @@ int Sound::getCachedTrack(int track) { // Not found, see if it exists sprintf(track_name, "%strack%d.mp3", _scumm->getGameDataPath(), track); - file = fopen(track_name, "rb"); + file->open(track_name); _cached_tracks[current_index] = track; /* First, close the previous file */ if (_mp3_tracks[current_index]) - fclose(_mp3_tracks[current_index]); + _mp3_tracks[current_index]->close(); + _mp3_tracks[current_index] = NULL; - if (!file) { + if (file->isOpen() == false) { // This warning is pretty pointless. debug(1, "Track %d not available in mp3 format", track); return -1; } + // Check the format and bitrate mad_stream_init(&stream); mad_frame_init(&frame); @@ -1083,7 +1067,7 @@ int Sound::getCachedTrack(int track) { if (buflen < sizeof(buffer)) { int bytes; - bytes = fread(buffer + buflen, 1, sizeof(buffer) - buflen, file); + bytes = file->read(buffer + buflen, sizeof(buffer) - buflen); if (bytes <= 0) { if (bytes == -1) { warning("Invalid format for track %d", track); @@ -1127,8 +1111,8 @@ int Sound::getCachedTrack(int track) { mad_frame_finish(&frame); mad_stream_finish(&stream); // Get file size - fseek(file, 0, SEEK_END); - _mp3_size[current_index] = ftell(file); + file->seek(0, SEEK_END); + _mp3_size[current_index] = file->pos(); _mp3_tracks[current_index] = file; return current_index; @@ -1136,7 +1120,7 @@ int Sound::getCachedTrack(int track) { error: mad_frame_finish(&frame); mad_stream_finish(&stream); - fclose(file); + delete file; return -1; } @@ -1170,7 +1154,7 @@ int Sound::playMP3CDTrack(int track, int num_loops, int start, int delay) { } // Go - fseek(_mp3_tracks[index], offset, SEEK_SET); + _mp3_tracks[index]->seek(offset, SEEK_SET); if (_mp3_cd_playing == true) _scumm->_mixer->stop(_mp3_index); diff --git a/scumm/sound.h b/scumm/sound.h index 1e650018cd..73dc23eeed 100644 --- a/scumm/sound.h +++ b/scumm/sound.h @@ -25,6 +25,7 @@ #include "sound/mixer.h" class Scumm; +class File; class Sound { @@ -52,7 +53,7 @@ enum { int _talkChannel; /* Mixer channel actor is talking on */ - void *_sfxFile; + File *_sfxFile; uint32 _talk_sound_a, _talk_sound_b; byte _talk_sound_mode; bool _mouthSyncMode; @@ -71,7 +72,7 @@ enum { int _cached_tracks[CACHE_TRACKS]; struct mad_header _mad_header[CACHE_TRACKS]; long _mp3_size[CACHE_TRACKS]; - FILE *_mp3_tracks[CACHE_TRACKS]; + File *_mp3_tracks[CACHE_TRACKS]; int _mp3_index; bool _mp3_cd_playing; #endif @@ -109,8 +110,8 @@ public: void talkSound(uint32 a, uint32 b, int mode); void setupSound(); void pauseSounds(bool pause); - int startSfxSound(void *file, int file_size); - void * openSfxFile(); + int startSfxSound(File *file, int file_size); + File * openSfxFile(); void stopSfxSound(); bool isSfxFinished(); uint32 decode12BitsSample(byte * src, byte ** dst, uint32 size); diff --git a/sound/mixer.cpp b/sound/mixer.cpp index 2158f949dd..150e5009e5 100644 --- a/sound/mixer.cpp +++ b/sound/mixer.cpp @@ -111,7 +111,7 @@ int SoundMixer::playMP3(PlayingSoundHandle * handle, void *sound, uint32 size, b warning("SoundMixer::out of mixer slots"); return -1; } -int SoundMixer::playMP3CDTrack(PlayingSoundHandle * handle, FILE * file, mad_timer_t duration) { +int SoundMixer::playMP3CDTrack(PlayingSoundHandle * handle, File * file, mad_timer_t duration) { /* Stop the previously playing CD track (if any) */ for (int i = 0; i != NUM_CHANNELS; i++) { if (_channels[i] == NULL) { @@ -551,7 +551,7 @@ void SoundMixer::ChannelRaw::mix(int16 * data, uint len) { if (s_org == NULL) error("ChannelRaw::mix out of memory"); - uint num_read = fread(s_org, 1, num, (FILE *) _ptr); + uint num_read = ((File *)_ptr)->read(s_org, num); if (num - num_read != 0) memset(s_org + num_read, 0x80, num - num_read); @@ -778,7 +778,7 @@ void SoundMixer::ChannelMP3::realDestroy() { #define MP3CD_BUFFERING_SIZE 131072 -SoundMixer::ChannelMP3CDMusic::ChannelMP3CDMusic(SoundMixer * mixer, FILE * file, +SoundMixer::ChannelMP3CDMusic::ChannelMP3CDMusic(SoundMixer * mixer, File * file, mad_timer_t duration){ _mixer = mixer; _file = file; @@ -811,7 +811,7 @@ void SoundMixer::ChannelMP3CDMusic::mix(int16 * data, uint len) { int skip_loop; // just skipped memset(_ptr, 0, _bufferSize); - _size = fread(_ptr, 1, _bufferSize, _file); + _size = _file->read(_ptr, _bufferSize); if (!_size) { realDestroy(); return; @@ -878,7 +878,7 @@ void SoundMixer::ChannelMP3CDMusic::mix(int16 * data, uint len) { } else { not_decoded = _stream.bufend - _stream.next_frame; memcpy(_ptr, _stream.next_frame, not_decoded); - _size = fread((unsigned char *)_ptr + not_decoded, 1, _bufferSize - not_decoded, _file); + _size = _file->read((unsigned char *)_ptr + not_decoded, _bufferSize - not_decoded); } _stream.error = (enum mad_error)0; // Restream diff --git a/sound/mixer.h b/sound/mixer.h index 19c0414fcd..64ab0c7070 100644 --- a/sound/mixer.h +++ b/sound/mixer.h @@ -31,6 +31,8 @@ typedef uint32 PlayingSoundHandle; +class File; + class SoundMixer { private: class Channel { @@ -112,11 +114,11 @@ private: uint32 _size; uint32 _bufferSize; mad_timer_t _duration; - FILE * _file; + File * _file; bool _initialized; public: - ChannelMP3CDMusic(SoundMixer * mixer, FILE * file, mad_timer_t duration); + ChannelMP3CDMusic(SoundMixer * mixer, File * file, mad_timer_t duration); void mix(int16 * data, uint len); void realDestroy(); bool soundFinished(); @@ -170,7 +172,7 @@ public: byte flags); #ifdef COMPRESSED_SOUND_FILE int playMP3(PlayingSoundHandle * handle, void * sound, uint32 size, byte flags); - int playMP3CDTrack(PlayingSoundHandle * handle, FILE * file, mad_timer_t duration); + int playMP3CDTrack(PlayingSoundHandle * handle, File * file, mad_timer_t duration); #endif /* Premix procedure, useful when using fmopl adlib */ |