Merge pull request #1874 from John3/readmeUpdate
[Torque-3d.git] / Engine / source / sfx / sfxFileStream.h
blob54c5780816e07f1fcc933e57bd087e7029c49db3
1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2012 GarageGames, LLC
3 //
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
20 // IN THE SOFTWARE.
21 //-----------------------------------------------------------------------------
23 #ifndef _SFXFILESTREAM_H_
24 #define _SFXFILESTREAM_H_
26 #ifndef _SFXSTREAM_H_
27 # include "sfx/sfxStream.h"
28 #endif
29 #ifndef _TVECTOR_H_
30 # include "core/util/tVector.h"
31 #endif
32 #ifndef _TORQUE_STRING_H_
33 # include "core/util/str.h"
34 #endif
37 class Stream;
38 class SFXFileStream;
40 ///
41 typedef SFXFileStream* ( *SFXFILESTREAM_CREATE_FN )( Stream *stream );
43 /// An SFXStream that streams from a file.
44 class SFXFileStream : public SFXStream
46 protected:
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.
54 Stream *mStream;
56 /// If true then we're responsible for closing the stream.
57 bool mOwnStream;
59 /// The format of the data in the stream.
60 SFXFormat mFormat;
62 /// The number of samples in the data stream.
63 U32 mSamples;
65 /// Constructs the stream in an uninitilized state.
66 SFXFileStream();
68 ///
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;
80 public:
82 ///
83 static void registerExtension( String ext, SFXFILESTREAM_CREATE_FN create_fn );
85 ///
86 static void unregisterExtension( String ext );
88 /// This is a helper function used to create an appropriate SFXStream
89 /// for the requested sound file.
90 ///
91 /// @param filename The sound file path with or without extension.
92 ///
93 static SFXFileStream* create( String filename );
95 ///
96 static bool exists( String filename );
98 /// Destructor.
99 virtual ~SFXFileStream();
101 /// Opens and optionally takes ownership of the stream.
102 bool open( Stream *stream, bool ownStream = false );
104 /// Closes the stream.
105 void close();
107 // SFXStream.
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 ); }
112 bool isEOS() const;
115 #endif // _SFXFILESTREAM_H_