sfxResource.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  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. //-----------------------------------------------------------------------------
  22. #ifndef _SFXRESOURCE_H_
  23. #define _SFXRESOURCE_H_
  24. #ifndef _SFXCOMMON_H_
  25. #include "sfx/sfxCommon.h"
  26. #endif
  27. #ifndef __RESOURCE_H__
  28. #include "core/resource.h"
  29. #endif
  30. #ifndef _SFXSTREAM_H_
  31. #include "sfx/sfxStream.h"
  32. #endif
  33. /// This is the base class for all sound file resources including
  34. /// streamed sound files. It acts much like an always in-core
  35. /// header to the actual sound data which is read through an SFXStream.
  36. ///
  37. /// The first step occurs at ResourceManager::load() time at which
  38. /// only the header information, the format, size frequency, and
  39. /// looping flag, are loaded from the sound file. This provides
  40. /// just the nessasary information to simulate sound playback for
  41. /// sounds playing just out of the users hearing range.
  42. ///
  43. /// The second step loads the actual sound data or begins filling
  44. /// the stream buffer. This is triggered by a call to openStream().
  45. /// SFXProfile, for example, does this when mPreload is enabled.
  46. ///
  47. class SFXResource
  48. {
  49. public:
  50. typedef void Parent;
  51. protected:
  52. /// The constructor is protected.
  53. /// @see SFXResource::load()
  54. SFXResource();
  55. /// Path to the sound file.
  56. String mFileName;
  57. /// The format of the sample data.
  58. SFXFormat mFormat;
  59. /// The length of the sample in milliseconds.
  60. U32 mDuration;
  61. ThreadSafeRef<SFXStream> mStream;
  62. /// Construct a resource instance for the given file. Format and duration
  63. /// are read from the given stream.
  64. SFXResource( String fileName, const ThreadSafeRef<SFXStream>& stream);
  65. public:
  66. /// The destructor.
  67. virtual ~SFXResource() {}
  68. /// This is a helper function used by SFXProfile for load
  69. /// a sound resource. It takes care of trying different
  70. /// types for extension-less filenames.
  71. ///
  72. /// @param filename The sound file path with or without extension.
  73. ///
  74. static Resource< SFXResource > load( String filename );
  75. /// A helper function which returns true if the
  76. /// sound resource exists.
  77. ///
  78. /// @param filename The sound file path with or without extension.
  79. ///
  80. static bool exists( String filename );
  81. /// Return the path to the sound file.
  82. const String& getFileName() { return mFileName; }
  83. /// Returns the total playback time milliseconds.
  84. U32 getDuration() const { return mDuration; }
  85. /// The format of the data in the resource.
  86. const SFXFormat& getFormat() const { return mFormat; }
  87. /// Open a stream for reading the resource's sample data.
  88. ThreadSafeRef<SFXStream> openStream();
  89. // Internal.
  90. struct _NewHelper;
  91. friend struct _NewHelper;
  92. };
  93. #endif // _SFXRESOURCE_H_