1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2012 GarageGames, LLC
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 //-----------------------------------------------------------------------------
23 #ifndef _SFXFILESTREAM_H_
24 #define _SFXFILESTREAM_H_
27 # include "sfx/sfxStream.h"
30 # include "core/util/tVector.h"
32 #ifndef _TORQUE_STRING_H_
33 # include "core/util/str.h"
41 typedef SFXFileStream
* ( *SFXFILESTREAM_CREATE_FN
)( Stream
*stream
);
43 /// An SFXStream that streams from a file.
44 class SFXFileStream
: public SFXStream
47 typedef Vector
< String
> ExtensionsVector
;
48 typedef Vector
< SFXFILESTREAM_CREATE_FN
> CreateFnsVector
;
50 static ExtensionsVector smExtensions
;
51 static CreateFnsVector smCreateFns
;
53 /// The file stream we're reading from.
56 /// If true then we're responsible for closing the stream.
59 /// The format of the data in the stream.
62 /// The number of samples in the data stream.
65 /// Constructs the stream in an uninitilized state.
69 SFXFileStream( const SFXFileStream
& cloneFrom
);
71 /// Overloaded in the derived classes to read
72 /// the file header. It should initialize
73 /// mFormat and mSamples.
74 virtual bool _readHeader() = 0;
76 /// Overloaded for cleanup of file format
77 /// specific structures.
78 virtual void _close() = 0;
83 static void registerExtension( String ext
, SFXFILESTREAM_CREATE_FN create_fn
);
86 static void unregisterExtension( String ext
);
88 /// This is a helper function used to create an appropriate SFXStream
89 /// for the requested sound file.
91 /// @param filename The sound file path with or without extension.
93 static SFXFileStream
* create( String filename
);
96 static bool exists( String filename
);
99 virtual ~SFXFileStream();
101 /// Opens and optionally takes ownership of the stream.
102 bool open( Stream
*stream
, bool ownStream
= false );
104 /// Closes the stream.
108 const SFXFormat
& getFormat() const { return mFormat
; }
109 U32
getSampleCount() const { return mSamples
; }
110 U32
getDataLength() const { return mSamples
* mFormat
.getBytesPerSample(); }
111 U32
getDuration() const { return mFormat
.getDuration( mSamples
); }
115 #endif // _SFXFILESTREAM_H_