You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
8.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. #ifndef __DROWAUDIO_STREAMANDFILEHANDLER_H__
  25. #define __DROWAUDIO_STREAMANDFILEHANDLER_H__
  26. #include "dRowAudio_MemoryInputSource.h"
  27. //==============================================================================
  28. /**
  29. Abstract class which just keeps track of what type of source was last assigned.
  30. Notes this doesn't take any ownership so make sure you delete the streams and call
  31. clear once you do to avoid any dangling pointers.
  32. @see AudioFilePlayer
  33. */
  34. class StreamAndFileHandler
  35. {
  36. public:
  37. //==============================================================================
  38. /** An enum to distinguish between the different input types. */
  39. enum InputType
  40. {
  41. file,
  42. memoryBlock,
  43. memoryInputStream,
  44. unknownStream,
  45. noInput
  46. };
  47. //==============================================================================
  48. /** Creates an empty StreamAndFileHandler. */
  49. StreamAndFileHandler()
  50. : inputType (noInput),
  51. inputStream (nullptr)
  52. {
  53. }
  54. /** Destructor. */
  55. virtual ~StreamAndFileHandler() {}
  56. //==============================================================================
  57. /** Clears all the internal references to any files or streams. */
  58. void clear()
  59. {
  60. inputType = noInput;
  61. currentFile = File();
  62. inputStream = nullptr;
  63. }
  64. /** Returns the type of input that was last used. */
  65. InputType getInputType() const noexcept { return inputType; }
  66. /** Sets the source to be any kind of InputStream.
  67. @returns true if the stream loaded correctly
  68. */
  69. bool setInputStream (InputStream* inputStream)
  70. {
  71. inputType = unknownStream;
  72. if (MemoryInputStream* mis = dynamic_cast<MemoryInputStream*> (inputStream))
  73. return setMemoryInputStream (mis);
  74. if (FileInputStream* fis = dynamic_cast<FileInputStream*> (inputStream))
  75. {
  76. const ScopedPointer<FileInputStream> deleter (fis);
  77. return setFile (fis->getFile());
  78. }
  79. return streamChanged (inputStream);
  80. }
  81. /** Returns a stream to the current source, you can find this out using getInputType().
  82. It is the caller's responsibility to delete this stream unless it has the
  83. type unknownStream which it can't make a copy of. You could use a
  84. dynamic_cast to do this yourself if you know the type.
  85. */
  86. InputStream* getInputStream()
  87. {
  88. switch (inputType)
  89. {
  90. case file:
  91. {
  92. return new FileInputStream (currentFile);
  93. }
  94. case memoryBlock:
  95. case memoryInputStream:
  96. {
  97. MemoryInputStream* memoryStream = dynamic_cast<MemoryInputStream*> (inputStream);
  98. if (memoryStream != nullptr)
  99. return new MemoryInputStream (memoryStream->getData(), memoryStream->getDataSize(), false);
  100. else
  101. return nullptr;
  102. }
  103. case unknownStream:
  104. {
  105. return inputStream;
  106. }
  107. default:
  108. {
  109. return nullptr;
  110. }
  111. }
  112. }
  113. /** Returns an InputSource to the current stream if it knows the type of stream.
  114. For example, if the input is a file this will return a FileInputStream etc.
  115. It is the callers responsibility to delete this source when finished.
  116. */
  117. InputSource* getInputSource()
  118. {
  119. switch (inputType)
  120. {
  121. case file:
  122. {
  123. return new FileInputSource (currentFile);
  124. }
  125. case memoryBlock:
  126. case memoryInputStream:
  127. {
  128. MemoryInputStream* memoryStream = dynamic_cast<MemoryInputStream*> (getInputStream());
  129. if (memoryStream != nullptr)
  130. return new MemoryInputSource (memoryStream);
  131. else
  132. return nullptr;
  133. }
  134. default:
  135. {
  136. return nullptr;
  137. }
  138. }
  139. }
  140. //==============================================================================
  141. /** Sets the source to a File.
  142. @returns true if the file loaded correctly
  143. */
  144. bool setFile (const File& newFile)
  145. {
  146. inputType = file;
  147. inputStream = nullptr;
  148. currentFile = newFile;
  149. return fileChanged (currentFile);
  150. }
  151. /** Sets the source to a MemoryInputStream.
  152. @returns true if the stream loaded correctly
  153. */
  154. bool setMemoryInputStream (MemoryInputStream* newMemoryInputStream)
  155. {
  156. inputType = memoryInputStream;
  157. currentFile = File();
  158. inputStream = newMemoryInputStream;
  159. return streamChanged (inputStream);
  160. }
  161. /** Sets the source to a memory block.
  162. @returns true if the block data loaded correctly
  163. */
  164. bool setMemoryBlock (MemoryBlock& inputBlock)
  165. {
  166. inputType = memoryBlock;
  167. currentFile = File();
  168. inputStream = new MemoryInputStream (inputBlock, false);
  169. return streamChanged (inputStream);
  170. }
  171. /** Returns the current file if it was set with a one.
  172. If a stream was used this will return File::nonexistant.
  173. */
  174. File getFile() const noexcept { return currentFile; }
  175. //==============================================================================
  176. /** Subclasses must override this to be informed of when a file changes.
  177. @returns true if the file was able to be loaded correctly
  178. */
  179. virtual bool fileChanged (const File& file) = 0;
  180. /** Subclasses must override this to be informed of when a stream changes.
  181. Note that this class doesn't retain any ownership of the stream so subclasses should
  182. delete them when no longer needed. This obviously means the getInputStream method is
  183. only valid for the duration that this stream is kept alive. Be sure to set this to
  184. nullptr if you delete the stream by any means other than in this class.
  185. @returns true if the stream was able to be loaded correctly
  186. */
  187. virtual bool streamChanged (InputStream* inputStream) = 0;
  188. private:
  189. //==============================================================================
  190. InputType inputType;
  191. File currentFile;
  192. InputStream* inputStream;
  193. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StreamAndFileHandler)
  194. };
  195. #endif // __DROWAUDIO_STREAMANDFILEHANDLER_H__