- got rid of std::auto_ptr, courtesy of Blzut3's patch.

This commit is contained in:
Christoph Oelckers 2015-04-25 17:50:57 +02:00
parent d880783784
commit 6bb79be85c
22 changed files with 181 additions and 167 deletions

View file

@ -26,7 +26,6 @@
#include <io.h> #include <io.h>
#endif #endif
#include <fcntl.h> #include <fcntl.h>
#include <memory>
#include "i_system.h" #include "i_system.h"
#include "i_sound.h" #include "i_sound.h"
@ -2434,7 +2433,7 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force)
musicname += 7; musicname += 7;
} }
std::auto_ptr<FileReader> reader; FileReader *reader = NULL;
if (!FileExists (musicname)) if (!FileExists (musicname))
{ {
if ((lumpnum = Wads.CheckNumForFullName (musicname, true, ns_music)) == -1) if ((lumpnum = Wads.CheckNumForFullName (musicname, true, ns_music)) == -1)
@ -2472,7 +2471,7 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force)
musiccache.Resize(length); musiccache.Resize(length);
Wads.ReadLump(lumpnum, &musiccache[0]); Wads.ReadLump(lumpnum, &musiccache[0]);
reader.reset(new MemoryReader((const char*)&musiccache[0], musiccache.Size())); reader = new MemoryReader((const char*)&musiccache[0], musiccache.Size());
} }
else else
{ {
@ -2480,14 +2479,14 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force)
{ {
return false; return false;
} }
reader.reset(Wads.ReopenLumpNum(lumpnum)); reader = Wads.ReopenLumpNum(lumpnum);
} }
} }
} }
else else
{ {
// Load an external file. // Load an external file.
reader.reset(new FileReader(musicname)); reader = new FileReader(musicname);
} }
// shutdown old music // shutdown old music
@ -2500,6 +2499,7 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force)
mus_playing.name = musicname; mus_playing.name = musicname;
mus_playing.baseorder = order; mus_playing.baseorder = order;
LastSong = musicname; LastSong = musicname;
delete reader;
return true; return true;
} }
@ -2507,6 +2507,7 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force)
if (handle != NULL) if (handle != NULL)
{ {
mus_playing.handle = handle; mus_playing.handle = handle;
delete reader;
} }
else else
{ {

View file

@ -304,12 +304,12 @@ class FMODStreamCapsule : public SoundStream
public: public:
FMODStreamCapsule(FMOD::Sound *stream, FMODSoundRenderer *owner, const char *url) FMODStreamCapsule(FMOD::Sound *stream, FMODSoundRenderer *owner, const char *url)
: Owner(owner), Stream(NULL), Channel(NULL), : Owner(owner), Stream(NULL), Channel(NULL),
UserData(NULL), Callback(NULL), URL(url), Ended(false) UserData(NULL), Callback(NULL), URL(url), Reader(NULL), Ended(false)
{ {
SetStream(stream); SetStream(stream);
} }
FMODStreamCapsule(FMOD::Sound *stream, FMODSoundRenderer *owner, std::auto_ptr<FileReader> reader) FMODStreamCapsule(FMOD::Sound *stream, FMODSoundRenderer *owner, FileReader *reader)
: Owner(owner), Stream(NULL), Channel(NULL), : Owner(owner), Stream(NULL), Channel(NULL),
UserData(NULL), Callback(NULL), Reader(reader), Ended(false) UserData(NULL), Callback(NULL), Reader(reader), Ended(false)
{ {
@ -318,7 +318,7 @@ public:
FMODStreamCapsule(void *udata, SoundStreamCallback callback, FMODSoundRenderer *owner) FMODStreamCapsule(void *udata, SoundStreamCallback callback, FMODSoundRenderer *owner)
: Owner(owner), Stream(NULL), Channel(NULL), : Owner(owner), Stream(NULL), Channel(NULL),
UserData(udata), Callback(callback), Ended(false) UserData(udata), Callback(callback), Reader(NULL), Ended(false)
{} {}
~FMODStreamCapsule() ~FMODStreamCapsule()
@ -331,6 +331,7 @@ public:
{ {
Stream->release(); Stream->release();
} }
delete Reader;
} }
void SetStream(FMOD::Sound *stream) void SetStream(FMOD::Sound *stream)
@ -600,7 +601,7 @@ private:
FMOD::Channel *Channel; FMOD::Channel *Channel;
void *UserData; void *UserData;
SoundStreamCallback Callback; SoundStreamCallback Callback;
std::auto_ptr<FileReader> Reader; FileReader *Reader;
FString URL; FString URL;
bool Ended; bool Ended;
bool JustStarted; bool JustStarted;
@ -1645,7 +1646,7 @@ static FMOD_RESULT F_CALLBACK seek_reader_callback(void *handle, unsigned int po
// //
//========================================================================== //==========================================================================
SoundStream *FMODSoundRenderer::OpenStream(std::auto_ptr<FileReader> reader, int flags) SoundStream *FMODSoundRenderer::OpenStream(FileReader *reader, int flags)
{ {
FMOD_MODE mode; FMOD_MODE mode;
FMOD_CREATESOUNDEXINFO exinfo; FMOD_CREATESOUNDEXINFO exinfo;
@ -1681,7 +1682,7 @@ SoundStream *FMODSoundRenderer::OpenStream(std::auto_ptr<FileReader> reader, int
exinfo.dlsname = patches; exinfo.dlsname = patches;
} }
name.Format("_FileReader_%p", reader.get()); name.Format("_FileReader_%p", reader);
result = Sys->createSound(name, mode, &exinfo, &stream); result = Sys->createSound(name, mode, &exinfo, &stream);
if(result == FMOD_ERR_FORMAT && exinfo.dlsname != NULL) if(result == FMOD_ERR_FORMAT && exinfo.dlsname != NULL)
{ {

View file

@ -24,7 +24,7 @@ public:
// Streaming sounds. // Streaming sounds.
SoundStream *CreateStream (SoundStreamCallback callback, int buffsamples, int flags, int samplerate, void *userdata); SoundStream *CreateStream (SoundStreamCallback callback, int buffsamples, int flags, int samplerate, void *userdata);
SoundStream *OpenStream (std::auto_ptr<FileReader> reader, int flags); SoundStream *OpenStream (FileReader *reader, int flags);
SoundStream *OpenStream (const char *url, int flags); SoundStream *OpenStream (const char *url, int flags);
// Starts a sound. // Starts a sound.

View file

@ -301,7 +301,7 @@ MusInfo *MusInfo::GetWaveDumper(const char *filename, int rate)
// //
//========================================================================== //==========================================================================
static MIDIStreamer *CreateMIDIStreamer(std::auto_ptr<FileReader> &reader, EMidiDevice devtype, EMIDIType miditype) static MIDIStreamer *CreateMIDIStreamer(FileReader &reader, EMidiDevice devtype, EMIDIType miditype)
{ {
switch (miditype) switch (miditype)
{ {
@ -377,22 +377,23 @@ static EMIDIType IdentifyMIDIType(DWORD *id, int size)
// //
//========================================================================== //==========================================================================
MusInfo *I_RegisterSong (std::auto_ptr<FileReader> reader, int device) MusInfo *I_RegisterSong (FileReader *reader, int device)
{ {
MusInfo *info = NULL; MusInfo *info = NULL;
const char *fmt; const char *fmt;
DWORD id[32/4]; DWORD id[32/4];
int i;
if (nomusic) if (nomusic)
{ {
delete reader;
return 0; return 0;
} }
if(reader->Read(id, 32) != 32 || reader->Seek(-32, SEEK_CUR) != 0) if(reader->Read(id, 32) != 32 || reader->Seek(-32, SEEK_CUR) != 0)
{ {
return 0; delete reader;
} return 0;
}
#ifndef _WIN32 #ifndef _WIN32
// non-Windows platforms don't support MDEV_MMAPI so map to MDEV_SNDSYS // non-Windows platforms don't support MDEV_MMAPI so map to MDEV_SNDSYS
@ -403,31 +404,35 @@ MusInfo *I_RegisterSong (std::auto_ptr<FileReader> reader, int device)
// Check for gzip compression. Some formats are expected to have players // Check for gzip compression. Some formats are expected to have players
// that can handle it, so it simplifies things if we make all songs // that can handle it, so it simplifies things if we make all songs
// gzippable. // gzippable.
if ((id[0] & MAKE_ID(255,255,255,0)) == GZIP_ID) if ((id[0] & MAKE_ID(255, 255, 255, 0)) == GZIP_ID)
{ {
int len = reader->GetLength(); int len = reader->GetLength();
BYTE *gzipped = new BYTE[len]; BYTE *gzipped = new BYTE[len];
if (reader->Read(gzipped, len) != len) if (reader->Read(gzipped, len) != len)
{ {
delete[] gzipped; delete[] gzipped;
return NULL; delete reader;
} return NULL;
}
delete reader;
std::auto_ptr<MemoryArrayReader> reader2(new MemoryArrayReader(NULL, 0)); MemoryArrayReader *memreader = new MemoryArrayReader(NULL, 0);
if(!ungzip(gzipped, len, reader2->GetArray())) if (!ungzip(gzipped, len, memreader->GetArray()))
{ {
delete[] gzipped; delete[] gzipped;
return 0; delete memreader;
} return 0;
delete[] gzipped; }
reader2->UpdateLength(); delete[] gzipped;
memreader->UpdateLength();
reader.reset(reader2.release()); if (memreader->Read(id, 32) != 32 || memreader->Seek(-32, SEEK_CUR) != 0)
if(reader->Read(id, 32) != 32 || reader->Seek(-32, SEEK_CUR) != 0) {
{ delete memreader;
return 0; return 0;
} }
} reader = memreader;
}
EMIDIType miditype = IdentifyMIDIType(id, sizeof(id)); EMIDIType miditype = IdentifyMIDIType(id, sizeof(id));
if (miditype != MIDI_NOTMIDI) if (miditype != MIDI_NOTMIDI)
@ -435,7 +440,7 @@ MusInfo *I_RegisterSong (std::auto_ptr<FileReader> reader, int device)
EMidiDevice devtype = (EMidiDevice)device; EMidiDevice devtype = (EMidiDevice)device;
retry_as_sndsys: retry_as_sndsys:
info = CreateMIDIStreamer(reader, devtype, miditype); info = CreateMIDIStreamer(*reader, devtype, miditype);
if (info != NULL && !info->IsValid()) if (info != NULL && !info->IsValid())
{ {
delete info; delete info;
@ -449,7 +454,7 @@ retry_as_sndsys:
#ifdef _WIN32 #ifdef _WIN32
if (info == NULL && devtype != MDEV_MMAPI && snd_mididevice >= 0) if (info == NULL && devtype != MDEV_MMAPI && snd_mididevice >= 0)
{ {
info = CreateMIDIStreamer(reader, MDEV_MMAPI, miditype); info = CreateMIDIStreamer(*reader, MDEV_MMAPI, miditype);
} }
#endif #endif
} }
@ -460,17 +465,17 @@ retry_as_sndsys:
(id[0] == MAKE_ID('D','B','R','A') && id[1] == MAKE_ID('W','O','P','L')) || // DosBox Raw OPL (id[0] == MAKE_ID('D','B','R','A') && id[1] == MAKE_ID('W','O','P','L')) || // DosBox Raw OPL
(id[0] == MAKE_ID('A','D','L','I') && *((BYTE *)id + 4) == 'B')) // Martin Fernandez's modified IMF (id[0] == MAKE_ID('A','D','L','I') && *((BYTE *)id + 4) == 'B')) // Martin Fernandez's modified IMF
{ {
info = new OPLMUSSong (reader); info = new OPLMUSSong (*reader);
} }
// Check for game music // Check for game music
else if ((fmt = GME_CheckFormat(id[0])) != NULL && fmt[0] != '\0') else if ((fmt = GME_CheckFormat(id[0])) != NULL && fmt[0] != '\0')
{ {
info = GME_OpenSong(reader, fmt); info = GME_OpenSong(*reader, fmt);
} }
// Check for module formats // Check for module formats
else else
{ {
info = MOD_OpenSong(reader); info = MOD_OpenSong(*reader);
} }
if (info == NULL) if (info == NULL)
@ -483,6 +488,7 @@ retry_as_sndsys:
reader->Seek(8, SEEK_CUR); reader->Seek(8, SEEK_CUR);
if (reader->Read (&subid, 4) != 4) if (reader->Read (&subid, 4) != 4)
{ {
delete reader;
return 0; return 0;
} }
reader->Seek(-12, SEEK_CUR); reader->Seek(-12, SEEK_CUR);
@ -490,7 +496,7 @@ retry_as_sndsys:
if (subid == (('C')|(('D')<<8)|(('D')<<16)|(('A')<<24))) if (subid == (('C')|(('D')<<8)|(('D')<<16)|(('A')<<24)))
{ {
// This is a CDDA file // This is a CDDA file
info = new CDDAFile (reader); info = new CDDAFile (*reader);
} }
} }
@ -503,9 +509,13 @@ retry_as_sndsys:
{ {
// Let the sound system figure out what it is. // Let the sound system figure out what it is.
info = new StreamSong (reader); info = new StreamSong (reader);
// Assumed ownership
reader = NULL;
} }
} }
if (reader != NULL) delete reader;
if (info && !info->IsValid ()) if (info && !info->IsValid ())
{ {
delete info; delete info;

View file

@ -34,8 +34,6 @@
#ifndef __I_MUSIC_H__ #ifndef __I_MUSIC_H__
#define __I_MUSIC_H__ #define __I_MUSIC_H__
#include <memory>
#include "doomdef.h" #include "doomdef.h"
class FileReader; class FileReader;
@ -54,7 +52,7 @@ void I_SetMusicVolume (float volume);
// Registers a song handle to song data. // Registers a song handle to song data.
class MusInfo; class MusInfo;
MusInfo *I_RegisterSong (std::auto_ptr<FileReader> reader, int device); MusInfo *I_RegisterSong (FileReader *reader, int device);
MusInfo *I_RegisterCDSong (int track, int cdid = 0); MusInfo *I_RegisterCDSong (int track, int cdid = 0);
MusInfo *I_RegisterURLSong (const char *url); MusInfo *I_RegisterURLSong (const char *url);

View file

@ -501,7 +501,7 @@ protected:
class MUSSong2 : public MIDIStreamer class MUSSong2 : public MIDIStreamer
{ {
public: public:
MUSSong2(std::auto_ptr<FileReader> reader, EMidiDevice type); MUSSong2(FileReader &reader, EMidiDevice type);
~MUSSong2(); ~MUSSong2();
MusInfo *GetOPLDumper(const char *filename); MusInfo *GetOPLDumper(const char *filename);
@ -527,7 +527,7 @@ protected:
class MIDISong2 : public MIDIStreamer class MIDISong2 : public MIDIStreamer
{ {
public: public:
MIDISong2(std::auto_ptr<FileReader> reader, EMidiDevice type); MIDISong2(FileReader &reader, EMidiDevice type);
~MIDISong2(); ~MIDISong2();
MusInfo *GetOPLDumper(const char *filename); MusInfo *GetOPLDumper(const char *filename);
@ -584,7 +584,7 @@ protected:
class HMISong : public MIDIStreamer class HMISong : public MIDIStreamer
{ {
public: public:
HMISong(std::auto_ptr<FileReader> reader, EMidiDevice type); HMISong(FileReader &reader, EMidiDevice type);
~HMISong(); ~HMISong();
MusInfo *GetOPLDumper(const char *filename); MusInfo *GetOPLDumper(const char *filename);
@ -627,7 +627,7 @@ protected:
class XMISong : public MIDIStreamer class XMISong : public MIDIStreamer
{ {
public: public:
XMISong(std::auto_ptr<FileReader> reader, EMidiDevice type); XMISong(FileReader &reader, EMidiDevice type);
~XMISong(); ~XMISong();
MusInfo *GetOPLDumper(const char *filename); MusInfo *GetOPLDumper(const char *filename);
@ -666,7 +666,7 @@ protected:
class StreamSong : public MusInfo class StreamSong : public MusInfo
{ {
public: public:
StreamSong (std::auto_ptr<FileReader> reader); StreamSong (FileReader *reader);
StreamSong (const char *url); StreamSong (const char *url);
~StreamSong (); ~StreamSong ();
void Play (bool looping, int subsong); void Play (bool looping, int subsong);
@ -690,7 +690,7 @@ protected:
class OPLMUSSong : public StreamSong class OPLMUSSong : public StreamSong
{ {
public: public:
OPLMUSSong (std::auto_ptr<FileReader> reader); OPLMUSSong (FileReader &reader);
~OPLMUSSong (); ~OPLMUSSong ();
void Play (bool looping, int subsong); void Play (bool looping, int subsong);
bool IsPlaying (); bool IsPlaying ();
@ -739,17 +739,17 @@ protected:
class CDDAFile : public CDSong class CDDAFile : public CDSong
{ {
public: public:
CDDAFile (std::auto_ptr<FileReader> reader); CDDAFile (FileReader &reader);
}; };
// Module played via foo_dumb ----------------------------------------------- // Module played via foo_dumb -----------------------------------------------
MusInfo *MOD_OpenSong(std::auto_ptr<FileReader> &reader); MusInfo *MOD_OpenSong(FileReader &reader);
// Music played via Game Music Emu ------------------------------------------ // Music played via Game Music Emu ------------------------------------------
const char *GME_CheckFormat(uint32 header); const char *GME_CheckFormat(uint32 header);
MusInfo *GME_OpenSong(std::auto_ptr<FileReader> &reader, const char *fmt); MusInfo *GME_OpenSong(FileReader &reader, const char *fmt);
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View file

@ -48,7 +48,6 @@ extern HINSTANCE g_hInst;
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <memory>
#include "doomtype.h" #include "doomtype.h"
#include <math.h> #include <math.h>
@ -173,8 +172,9 @@ public:
{ {
return NULL; return NULL;
} }
SoundStream *OpenStream (std::auto_ptr<FileReader> reader, int flags) SoundStream *OpenStream (FileReader *reader, int flags)
{ {
delete reader;
return NULL; return NULL;
} }
@ -372,17 +372,19 @@ short *SoundRenderer::DecodeSample(int outlen, const void *coded, int sizebytes,
SampleType type; SampleType type;
int srate; int srate;
std::auto_ptr<SoundDecoder> decoder(CreateDecoder(&reader)); SoundDecoder *decoder = CreateDecoder(&reader);
if(!decoder.get()) return samples; if(!decoder) return samples;
decoder->getInfo(&srate, &chans, &type); decoder->getInfo(&srate, &chans, &type);
if(chans != ChannelConfig_Mono || type != SampleType_Int16) if(chans != ChannelConfig_Mono || type != SampleType_Int16)
{ {
DPrintf("Sample is not 16-bit mono\n"); DPrintf("Sample is not 16-bit mono\n");
delete decoder;
return samples; return samples;
} }
decoder->read((char*)samples, outlen); decoder->read((char*)samples, outlen);
delete decoder;
return samples; return samples;
} }

View file

@ -35,8 +35,6 @@
#ifndef __I_SOUND__ #ifndef __I_SOUND__
#define __I_SOUND__ #define __I_SOUND__
#include <memory>
#include "doomtype.h" #include "doomtype.h"
#include "i_soundinternal.h" #include "i_soundinternal.h"
@ -107,7 +105,7 @@ public:
// Streaming sounds. // Streaming sounds.
virtual SoundStream *CreateStream (SoundStreamCallback callback, int buffbytes, int flags, int samplerate, void *userdata) = 0; virtual SoundStream *CreateStream (SoundStreamCallback callback, int buffbytes, int flags, int samplerate, void *userdata) = 0;
virtual SoundStream *OpenStream (std::auto_ptr<FileReader> reader, int flags) = 0; virtual SoundStream *OpenStream (FileReader *reader, int flags) = 0;
virtual SoundStream *OpenStream (const char *url, int flags); virtual SoundStream *OpenStream (const char *url, int flags);
// Starts a sound. // Starts a sound.

View file

@ -1,7 +1,6 @@
#ifndef __SNDINT_H #ifndef __SNDINT_H
#define __SNDINT_H #define __SNDINT_H
#include <vector>
#include <stdio.h> #include <stdio.h>
#include "basictypes.h" #include "basictypes.h"

View file

@ -79,31 +79,31 @@ bool CDSong::IsPlaying ()
return m_Status != STATE_Stopped; return m_Status != STATE_Stopped;
} }
CDDAFile::CDDAFile (std::auto_ptr<FileReader> reader) CDDAFile::CDDAFile (FileReader &reader)
: CDSong () : CDSong ()
{ {
DWORD chunk; DWORD chunk;
WORD track; WORD track;
DWORD discid; DWORD discid;
long endpos = reader->Tell() + reader->GetLength() - 8; long endpos = reader.Tell() + reader.GetLength() - 8;
// I_RegisterSong already identified this as a CDDA file, so we // I_RegisterSong already identified this as a CDDA file, so we
// just need to check the contents we're interested in. // just need to check the contents we're interested in.
reader->Seek(12, SEEK_CUR); reader.Seek(12, SEEK_CUR);
while (reader->Tell() < endpos) while (reader.Tell() < endpos)
{ {
reader->Read(&chunk, 4); reader.Read(&chunk, 4);
if (chunk != (('f')|(('m')<<8)|(('t')<<16)|((' ')<<24))) if (chunk != (('f')|(('m')<<8)|(('t')<<16)|((' ')<<24)))
{ {
reader->Read(&chunk, 4); reader.Read(&chunk, 4);
reader->Seek(chunk, SEEK_CUR); reader.Seek(chunk, SEEK_CUR);
} }
else else
{ {
reader->Seek(6, SEEK_CUR); reader.Seek(6, SEEK_CUR);
reader->Read(&track, 2); reader.Read(&track, 2);
reader->Read(&discid, 4); reader.Read(&discid, 4);
if (CD_InitID (discid) && CD_CheckTrack (track)) if (CD_InitID (discid) && CD_CheckTrack (track))
{ {

View file

@ -532,7 +532,7 @@ static DUMBFILE_SYSTEM mem_dfs = {
// //
//========================================================================== //==========================================================================
DUMBFILE *dumb_read_allfile(dumbfile_mem_status *filestate, BYTE *start, std::auto_ptr<FileReader> &reader, int lenhave, int lenfull) DUMBFILE *dumb_read_allfile(dumbfile_mem_status *filestate, BYTE *start, FileReader &reader, int lenhave, int lenfull)
{ {
filestate->size = lenfull; filestate->size = lenfull;
filestate->offset = 0; filestate->offset = 0;
@ -542,7 +542,7 @@ DUMBFILE *dumb_read_allfile(dumbfile_mem_status *filestate, BYTE *start, std::au
{ {
BYTE *mem = new BYTE[lenfull]; BYTE *mem = new BYTE[lenfull];
memcpy(mem, start, lenhave); memcpy(mem, start, lenhave);
if (reader->Read(mem + lenhave, lenfull - lenhave) != (lenfull - lenhave)) if (reader.Read(mem + lenhave, lenfull - lenhave) != (lenfull - lenhave))
{ {
delete[] mem; delete[] mem;
return NULL; return NULL;
@ -747,7 +747,7 @@ static void MOD_SetAutoChip(DUH *duh)
// //
//========================================================================== //==========================================================================
MusInfo *MOD_OpenSong(std::auto_ptr<FileReader> &reader) MusInfo *MOD_OpenSong(FileReader &reader)
{ {
DUH *duh = 0; DUH *duh = 0;
int headsize; int headsize;
@ -771,14 +771,14 @@ MusInfo *MOD_OpenSong(std::auto_ptr<FileReader> &reader)
atterm(dumb_exit); atterm(dumb_exit);
int size = reader->GetLength(); int size = reader.GetLength();
fpos = reader->Tell(); fpos = reader.Tell();
filestate.ptr = start; filestate.ptr = start;
filestate.offset = 0; filestate.offset = 0;
headsize = MIN((int)sizeof(start), size); headsize = MIN((int)sizeof(start), size);
if (headsize != reader->Read(start, headsize)) if (headsize != reader.Read(start, headsize))
{ {
return NULL; return NULL;
} }
@ -890,7 +890,7 @@ MusInfo *MOD_OpenSong(std::auto_ptr<FileReader> &reader)
{ {
if (!(f = dumb_read_allfile(&filestate, start, reader, headsize, size))) if (!(f = dumb_read_allfile(&filestate, start, reader, headsize, size)))
{ {
reader->Seek(fpos, SEEK_SET); reader.Seek(fpos, SEEK_SET);
return NULL; return NULL;
} }
} }
@ -931,13 +931,12 @@ MusInfo *MOD_OpenSong(std::auto_ptr<FileReader> &reader)
else else
{ {
// Reposition file pointer for other codecs to do their checks. // Reposition file pointer for other codecs to do their checks.
reader->Seek(fpos, SEEK_SET); reader.Seek(fpos, SEEK_SET);
} }
if (filestate.ptr != (BYTE *)start) if (filestate.ptr != (BYTE *)start)
{ {
delete[] const_cast<BYTE *>(filestate.ptr); delete[] const_cast<BYTE *>(filestate.ptr);
} }
if(state) reader.reset();
return state; return state;
} }

View file

@ -105,7 +105,7 @@ const char *GME_CheckFormat(uint32 id)
// //
//========================================================================== //==========================================================================
MusInfo *GME_OpenSong(std::auto_ptr<FileReader> &reader, const char *fmt) MusInfo *GME_OpenSong(FileReader &reader, const char *fmt)
{ {
gme_type_t type; gme_type_t type;
gme_err_t err; gme_err_t err;
@ -125,14 +125,14 @@ MusInfo *GME_OpenSong(std::auto_ptr<FileReader> &reader, const char *fmt)
return NULL; return NULL;
} }
int fpos = reader->Tell(); int fpos = reader.Tell();
int len = reader->GetLength(); int len = reader.GetLength();
song = new BYTE[len]; song = new BYTE[len];
if (reader->Read(song, len) != len) if (reader.Read(song, len) != len)
{ {
delete[] song; delete[] song;
gme_delete(emu); gme_delete(emu);
reader->Seek(fpos, SEEK_SET); reader.Seek(fpos, SEEK_SET);
return NULL; return NULL;
} }
@ -143,10 +143,9 @@ MusInfo *GME_OpenSong(std::auto_ptr<FileReader> &reader, const char *fmt)
{ {
Printf("Failed loading song: %s\n", err); Printf("Failed loading song: %s\n", err);
gme_delete(emu); gme_delete(emu);
reader->Seek(fpos, SEEK_SET); reader.Seek(fpos, SEEK_SET);
return NULL; return NULL;
} }
reader.reset();
return new GMESong(emu, sample_rate); return new GMESong(emu, sample_rate);
} }

View file

@ -128,7 +128,7 @@ extern char MIDI_CommonLengths[15];
// //
//========================================================================== //==========================================================================
HMISong::HMISong (std::auto_ptr<FileReader> reader, EMidiDevice type) HMISong::HMISong (FileReader &reader, EMidiDevice type)
: MIDIStreamer(type), MusHeader(0), Tracks(0) : MIDIStreamer(type), MusHeader(0), Tracks(0)
{ {
#ifdef _WIN32 #ifdef _WIN32
@ -137,7 +137,7 @@ HMISong::HMISong (std::auto_ptr<FileReader> reader, EMidiDevice type)
return; return;
} }
#endif #endif
int len = reader->GetLength(); int len = reader.GetLength();
if (len < 0x100) if (len < 0x100)
{ // Way too small to be HMI. { // Way too small to be HMI.
return; return;
@ -145,7 +145,7 @@ HMISong::HMISong (std::auto_ptr<FileReader> reader, EMidiDevice type)
MusHeader = new BYTE[len]; MusHeader = new BYTE[len];
SongLen = len; SongLen = len;
NumTracks = 0; NumTracks = 0;
if (reader->Read(MusHeader, len) != len) if (reader.Read(MusHeader, len) != len)
return; return;
// Do some validation of the MIDI file // Do some validation of the MIDI file

View file

@ -92,7 +92,7 @@ static const BYTE CtrlTranslate[15] =
// //
//========================================================================== //==========================================================================
MUSSong2::MUSSong2 (std::auto_ptr<FileReader> reader, EMidiDevice type) MUSSong2::MUSSong2 (FileReader &reader, EMidiDevice type)
: MIDIStreamer(type), MusHeader(0), MusBuffer(0) : MIDIStreamer(type), MusHeader(0), MusBuffer(0)
{ {
#ifdef _WIN32 #ifdef _WIN32
@ -105,7 +105,7 @@ MUSSong2::MUSSong2 (std::auto_ptr<FileReader> reader, EMidiDevice type)
BYTE front[32]; BYTE front[32];
int start; int start;
if (reader->Read(front, sizeof(front)) != sizeof(front)) if (reader.Read(front, sizeof(front)) != sizeof(front))
{ {
return; return;
} }
@ -121,14 +121,14 @@ MUSSong2::MUSSong2 (std::auto_ptr<FileReader> reader, EMidiDevice type)
} }
// Read the remainder of the song. // Read the remainder of the song.
int len = int(reader->GetLength() - start); int len = int(reader.GetLength() - start);
if (len < (int)sizeof(MusHeader)) if (len < (int)sizeof(MusHeader))
{ // It's too short. { // It's too short.
return; return;
} }
MusHeader = (MUSHeader *)new BYTE[len]; MusHeader = (MUSHeader *)new BYTE[len];
memcpy(MusHeader, front + start, sizeof(front) - start); memcpy(MusHeader, front + start, sizeof(front) - start);
if (reader->Read((BYTE *)MusHeader + sizeof(front) - start, len - (sizeof(front) - start)) != (len - (32 - start))) if (reader.Read((BYTE *)MusHeader + sizeof(front) - start, len - (sizeof(front) - start)) != (len - (32 - start)))
{ {
return; return;
} }

View file

@ -22,11 +22,11 @@ CUSTOM_CVAR (Int, opl_numchips, 2, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
CVAR(Int, opl_core, 0, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) CVAR(Int, opl_core, 0, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
OPLMUSSong::OPLMUSSong (std::auto_ptr<FileReader> reader) OPLMUSSong::OPLMUSSong (FileReader &reader)
{ {
int samples = int(OPL_SAMPLE_RATE / 14); int samples = int(OPL_SAMPLE_RATE / 14);
Music = new OPLmusicFile (reader.get()); Music = new OPLmusicFile (&reader);
m_Stream = GSnd->CreateStream (FillStream, samples*4, m_Stream = GSnd->CreateStream (FillStream, samples*4,
(opl_core == 0 ? SoundStream::Mono : 0) | SoundStream::Float, int(OPL_SAMPLE_RATE), this); (opl_core == 0 ? SoundStream::Mono : 0) | SoundStream::Float, int(OPL_SAMPLE_RATE), this);

View file

@ -254,12 +254,11 @@ int SndSysMIDIDevice::Open(void (*callback)(unsigned int, void *, DWORD, DWORD),
bool SndSysMIDIDevice::Preprocess(MIDIStreamer *song, bool looping) bool SndSysMIDIDevice::Preprocess(MIDIStreamer *song, bool looping)
{ {
std::auto_ptr<MemoryArrayReader> reader(new MemoryArrayReader(NULL, 0)); MemoryArrayReader *reader = new MemoryArrayReader(NULL, 0);
song->CreateSMF(reader->GetArray(), looping ? 0 : 1); song->CreateSMF(reader->GetArray(), looping ? 0 : 1);
reader->UpdateLength(); reader->UpdateLength();
bLooping = looping; bLooping = looping;
Stream = GSnd->OpenStream(std::auto_ptr<FileReader>(reader.release()), Stream = GSnd->OpenStream(reader, looping ? SoundStream::Loop : 0);
looping ? SoundStream::Loop : 0);
return false; return false;
} }

View file

@ -102,7 +102,7 @@ char MIDI_CommonLengths[15] = { 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// //
//========================================================================== //==========================================================================
MIDISong2::MIDISong2 (std::auto_ptr<FileReader> reader, EMidiDevice type) MIDISong2::MIDISong2 (FileReader &reader, EMidiDevice type)
: MIDIStreamer(type), MusHeader(0), Tracks(0) : MIDIStreamer(type), MusHeader(0), Tracks(0)
{ {
int p; int p;
@ -114,9 +114,9 @@ MIDISong2::MIDISong2 (std::auto_ptr<FileReader> reader, EMidiDevice type)
return; return;
} }
#endif #endif
SongLen = reader->GetLength(); SongLen = reader.GetLength();
MusHeader = new BYTE[SongLen]; MusHeader = new BYTE[SongLen];
if (reader->Read(MusHeader, SongLen) != SongLen) if (reader.Read(MusHeader, SongLen) != SongLen)
return; return;
// Do some validation of the MIDI file // Do some validation of the MIDI file

View file

@ -52,7 +52,7 @@ StreamSong::~StreamSong ()
} }
} }
StreamSong::StreamSong (std::auto_ptr<FileReader> reader) StreamSong::StreamSong (FileReader *reader)
{ {
m_Stream = GSnd->OpenStream (reader, SoundStream::Loop); m_Stream = GSnd->OpenStream (reader, SoundStream::Loop);
} }

View file

@ -108,7 +108,7 @@ extern char MIDI_CommonLengths[15];
// //
//========================================================================== //==========================================================================
XMISong::XMISong (std::auto_ptr<FileReader> reader, EMidiDevice type) XMISong::XMISong (FileReader &reader, EMidiDevice type)
: MIDIStreamer(type), MusHeader(0), Songs(0) : MIDIStreamer(type), MusHeader(0), Songs(0)
{ {
#ifdef _WIN32 #ifdef _WIN32
@ -117,9 +117,9 @@ XMISong::XMISong (std::auto_ptr<FileReader> reader, EMidiDevice type)
return; return;
} }
#endif #endif
SongLen = reader->GetLength(); SongLen = reader.GetLength();
MusHeader = new BYTE[SongLen]; MusHeader = new BYTE[SongLen];
if (reader->Read(MusHeader, SongLen) != SongLen) if (reader.Read(MusHeader, SongLen) != SongLen)
return; return;
// Find all the songs in this file. // Find all the songs in this file.

View file

@ -82,19 +82,14 @@ void I_BuildALDeviceList(FOptionValues *opt)
names += strlen(names) + 1; names += strlen(names) + 1;
} }
} }
__except (CheckException(GetExceptionCode())) __except (CheckException(GetExceptionCode()))
{ {
} }
#endif #endif
} }
#ifndef NO_OPENAL #ifndef NO_OPENAL
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
EXTERN_CVAR (Int, snd_channels) EXTERN_CVAR (Int, snd_channels)
EXTERN_CVAR (Int, snd_samplerate) EXTERN_CVAR (Int, snd_samplerate)
@ -173,8 +168,8 @@ class OpenALSoundStream : public SoundStream
ALfloat Volume; ALfloat Volume;
std::auto_ptr<FileReader> Reader; FileReader *Reader;
std::auto_ptr<SoundDecoder> Decoder; SoundDecoder *Decoder;
static bool DecoderCallback(SoundStream *_sstream, void *ptr, int length, void *user) static bool DecoderCallback(SoundStream *_sstream, void *ptr, int length, void *user)
{ {
OpenALSoundStream *self = static_cast<OpenALSoundStream*>(_sstream); OpenALSoundStream *self = static_cast<OpenALSoundStream*>(_sstream);
@ -230,7 +225,7 @@ class OpenALSoundStream : public SoundStream
public: public:
OpenALSoundStream(OpenALSoundRenderer *renderer) OpenALSoundStream(OpenALSoundRenderer *renderer)
: Renderer(renderer), Source(0), Playing(false), Looping(false), Volume(1.0f) : Renderer(renderer), Source(0), Playing(false), Looping(false), Volume(1.0f), Reader(NULL), Decoder(NULL)
{ {
Renderer->Streams.Push(this); Renderer->Streams.Push(this);
memset(Buffers, 0, sizeof(Buffers)); memset(Buffers, 0, sizeof(Buffers));
@ -256,6 +251,9 @@ public:
Renderer->Streams.Delete(Renderer->Streams.Find(this)); Renderer->Streams.Delete(Renderer->Streams.Find(this));
Renderer = NULL; Renderer = NULL;
delete Decoder;
delete Reader;
} }
@ -516,14 +514,19 @@ public:
return true; return true;
} }
bool Init(std::auto_ptr<FileReader> reader, bool loop) bool Init(FileReader *reader, bool loop)
{ {
if(!SetupSource()) if(!SetupSource())
{
delete reader;
return false; return false;
}
if(Decoder) delete Decoder;
if(Reader) delete Reader;
Reader = reader; Reader = reader;
Decoder.reset(Renderer->CreateDecoder(Reader.get())); Decoder = Renderer->CreateDecoder(Reader);
if(!Decoder.get()) return false; if(!Decoder) return false;
Callback = DecoderCallback; Callback = DecoderCallback;
UserData = NULL; UserData = NULL;
@ -615,12 +618,12 @@ ALCdevice *OpenALSoundRenderer::InitDevice()
Printf(TEXTCOLOR_RED" Could not open audio device\n"); Printf(TEXTCOLOR_RED" Could not open audio device\n");
} }
} }
} }
__except(CheckException(GetExceptionCode())) __except(CheckException(GetExceptionCode()))
{ {
Printf(TEXTCOLOR_ORANGE"Failed to load openal32.dll\n"); Printf(TEXTCOLOR_ORANGE"Failed to load openal32.dll\n");
} }
return device; return device;
} }
@ -652,20 +655,20 @@ OpenALSoundRenderer::OpenALSoundRenderer()
DPrintf(" ALC Version: "TEXTCOLOR_BLUE"%d.%d\n", major, minor); DPrintf(" ALC Version: "TEXTCOLOR_BLUE"%d.%d\n", major, minor);
DPrintf(" ALC Extensions: "TEXTCOLOR_ORANGE"%s\n", alcGetString(Device, ALC_EXTENSIONS)); DPrintf(" ALC Extensions: "TEXTCOLOR_ORANGE"%s\n", alcGetString(Device, ALC_EXTENSIONS));
std::vector<ALCint> attribs; TArray<ALCint> attribs;
if(*snd_samplerate > 0) if(*snd_samplerate > 0)
{ {
attribs.push_back(ALC_FREQUENCY); attribs.Push(ALC_FREQUENCY);
attribs.push_back(*snd_samplerate); attribs.Push(*snd_samplerate);
} }
// Make sure one source is capable of stereo output with the rest doing // Make sure one source is capable of stereo output with the rest doing
// mono, without running out of voices // mono, without running out of voices
attribs.push_back(ALC_MONO_SOURCES); attribs.Push(ALC_MONO_SOURCES);
attribs.push_back(std::max<ALCint>(*snd_channels, 2) - 1); attribs.Push(MAX<ALCint>(*snd_channels, 2) - 1);
attribs.push_back(ALC_STEREO_SOURCES); attribs.Push(ALC_STEREO_SOURCES);
attribs.push_back(1); attribs.Push(1);
// Other attribs..? // Other attribs..?
attribs.push_back(0); attribs.Push(0);
Context = alcCreateContext(Device, &attribs[0]); Context = alcCreateContext(Device, &attribs[0]);
if(!Context || alcMakeContextCurrent(Context) == ALC_FALSE) if(!Context || alcMakeContextCurrent(Context) == ALC_FALSE)
@ -678,7 +681,7 @@ OpenALSoundRenderer::OpenALSoundRenderer()
Device = NULL; Device = NULL;
return; return;
} }
attribs.clear(); attribs.Clear();
DPrintf(" Vendor: "TEXTCOLOR_ORANGE"%s\n", alGetString(AL_VENDOR)); DPrintf(" Vendor: "TEXTCOLOR_ORANGE"%s\n", alGetString(AL_VENDOR));
DPrintf(" Renderer: "TEXTCOLOR_ORANGE"%s\n", alGetString(AL_RENDERER)); DPrintf(" Renderer: "TEXTCOLOR_ORANGE"%s\n", alGetString(AL_RENDERER));
@ -723,7 +726,7 @@ OpenALSoundRenderer::OpenALSoundRenderer()
alcGetIntegerv(Device, ALC_MONO_SOURCES, 1, &numMono); alcGetIntegerv(Device, ALC_MONO_SOURCES, 1, &numMono);
alcGetIntegerv(Device, ALC_STEREO_SOURCES, 1, &numStereo); alcGetIntegerv(Device, ALC_STEREO_SOURCES, 1, &numStereo);
Sources.Resize(std::min<int>(std::max<int>(*snd_channels, 2), numMono+numStereo)); Sources.Resize(MIN<int>(MAX<int>(*snd_channels, 2), numMono+numStereo));
for(size_t i = 0;i < Sources.Size();i++) for(size_t i = 0;i < Sources.Size();i++)
{ {
alGenSources(1, &Sources[i]); alGenSources(1, &Sources[i]);
@ -1024,8 +1027,8 @@ SoundHandle OpenALSoundRenderer::LoadSound(BYTE *sfxdata, int length)
SampleType type; SampleType type;
int srate; int srate;
std::auto_ptr<SoundDecoder> decoder(CreateDecoder(&reader)); SoundDecoder *decoder = CreateDecoder(&reader);
if(!decoder.get()) return retval; if(!decoder) return retval;
decoder->getInfo(&srate, &chans, &type); decoder->getInfo(&srate, &chans, &type);
if(chans == ChannelConfig_Mono) if(chans == ChannelConfig_Mono)
@ -1043,6 +1046,7 @@ SoundHandle OpenALSoundRenderer::LoadSound(BYTE *sfxdata, int length)
{ {
Printf("Unsupported audio format: %s, %s\n", GetChannelConfigName(chans), Printf("Unsupported audio format: %s, %s\n", GetChannelConfigName(chans),
GetSampleTypeName(type)); GetSampleTypeName(type));
delete decoder;
return retval; return retval;
} }
@ -1058,10 +1062,12 @@ SoundHandle OpenALSoundRenderer::LoadSound(BYTE *sfxdata, int length)
Printf("Failed to buffer data: %s\n", alGetString(err)); Printf("Failed to buffer data: %s\n", alGetString(err));
alDeleteBuffers(1, &buffer); alDeleteBuffers(1, &buffer);
getALError(); getALError();
delete decoder;
return retval; return retval;
} }
retval.data = MAKE_PTRID(buffer); retval.data = MAKE_PTRID(buffer);
delete decoder;
return retval; return retval;
} }
@ -1096,18 +1102,24 @@ void OpenALSoundRenderer::UnloadSound(SoundHandle sfx)
SoundStream *OpenALSoundRenderer::CreateStream(SoundStreamCallback callback, int buffbytes, int flags, int samplerate, void *userdata) SoundStream *OpenALSoundRenderer::CreateStream(SoundStreamCallback callback, int buffbytes, int flags, int samplerate, void *userdata)
{ {
std::auto_ptr<OpenALSoundStream> stream(new OpenALSoundStream(this)); OpenALSoundStream *stream = new OpenALSoundStream(this);
if(!stream->Init(callback, buffbytes, flags, samplerate, userdata)) if (!stream->Init(callback, buffbytes, flags, samplerate, userdata))
return NULL; {
return stream.release(); delete stream;
return NULL;
}
return stream;
} }
SoundStream *OpenALSoundRenderer::OpenStream(std::auto_ptr<FileReader> reader, int flags) SoundStream *OpenALSoundRenderer::OpenStream(FileReader *reader, int flags)
{ {
std::auto_ptr<OpenALSoundStream> stream(new OpenALSoundStream(this)); OpenALSoundStream *stream = new OpenALSoundStream(this);
if (!stream->Init(reader, !!(flags&SoundStream::Loop)))
if (!stream->Init(reader, !!(flags&SoundStream::Loop))) return NULL; {
return stream.release(); delete stream;
return NULL;
}
return stream;
} }
FISoundChannel *OpenALSoundRenderer::StartSound(SoundHandle sfx, float vol, int pitch, int chanflags, FISoundChannel *reuse_chan) FISoundChannel *OpenALSoundRenderer::StartSound(SoundHandle sfx, float vol, int pitch, int chanflags, FISoundChannel *reuse_chan)

View file

@ -1,10 +1,6 @@
#ifndef OALSOUND_H #ifndef OALSOUND_H
#define OALSOUND_H #define OALSOUND_H
#include <vector>
#include <map>
#include "i_sound.h" #include "i_sound.h"
#include "s_sound.h" #include "s_sound.h"
#include "menu/menu.h" #include "menu/menu.h"
@ -82,7 +78,7 @@ public:
// Streaming sounds. // Streaming sounds.
virtual SoundStream *CreateStream(SoundStreamCallback callback, int buffbytes, int flags, int samplerate, void *userdata); virtual SoundStream *CreateStream(SoundStreamCallback callback, int buffbytes, int flags, int samplerate, void *userdata);
virtual SoundStream *OpenStream(std::auto_ptr<FileReader> reader, int flags); virtual SoundStream *OpenStream(FileReader *reader, int flags);
// Starts a sound. // Starts a sound.
virtual FISoundChannel *StartSound(SoundHandle sfx, float vol, int pitch, int chanflags, FISoundChannel *reuse_chan); virtual FISoundChannel *StartSound(SoundHandle sfx, float vol, int pitch, int chanflags, FISoundChannel *reuse_chan);

View file

@ -6,12 +6,12 @@
#include "except.h" #include "except.h"
#include "sndfile_decoder.h" #include "sndfile_decoder.h"
#include "templates.h"
#include "files.h" #include "files.h"
#include "xs_Float.h"
#ifdef HAVE_SNDFILE #ifdef HAVE_SNDFILE
#include <algorithm>
sf_count_t SndFileDecoder::file_get_filelen(void *user_data) sf_count_t SndFileDecoder::file_get_filelen(void *user_data)
{ {
FileReader *reader = reinterpret_cast<SndFileDecoder*>(user_data)->Reader; FileReader *reader = reinterpret_cast<SndFileDecoder*>(user_data)->Reader;
@ -69,10 +69,10 @@ bool SndFileDecoder::open(FileReader *reader)
SndFile = 0; SndFile = 0;
} }
} }
__except (CheckException(GetExceptionCode())) __except (CheckException(GetExceptionCode()))
{ {
// this means that the delay loaded decoder DLL was not found. // this means that the delay loaded decoder DLL was not found.
} }
return false; return false;
} }
@ -103,14 +103,14 @@ size_t SndFileDecoder::read(char *buffer, size_t bytes)
// could be more. // could be more.
while(total < frames) while(total < frames)
{ {
size_t todo = std::min<size_t>(frames-total, 64/SndInfo.channels); size_t todo = MIN<size_t>(frames-total, 64/SndInfo.channels);
float tmp[64]; float tmp[64];
size_t got = (size_t)sf_readf_float(SndFile, tmp, todo); size_t got = (size_t)sf_readf_float(SndFile, tmp, todo);
if(got < todo) frames = total + got; if(got < todo) frames = total + got;
for(size_t i = 0;i < got*SndInfo.channels;i++) for(size_t i = 0;i < got*SndInfo.channels;i++)
*out++ = (short)((std::min)((std::max)(tmp[i] * 32767.f, -32768.f), 32767.f)); *out++ = (short)xs_CRoundToInt(clamp(tmp[i] * 32767.f, -32768.f, 32767.f));
total += got; total += got;
} }
return total * SndInfo.channels * 2; return total * SndInfo.channels * 2;