AudioInputFile.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AudioSourceManager.h>
  10. namespace Audio
  11. {
  12. /**
  13. * Base class for audio file parser.
  14. * Any supported audio file types will have a parser implementation
  15. * that will parse header information to extract the audio format.
  16. */
  17. class AudioFileParser
  18. {
  19. public:
  20. AUDIO_IMPL_CLASS_ALLOCATOR(AudioFileParser)
  21. AudioFileParser() = default;
  22. virtual ~AudioFileParser() = default;
  23. AudioFileParser(const AudioFileParser&) = delete;
  24. AudioFileParser& operator=(const AudioFileParser&) = delete;
  25. /**
  26. * Parse header from a file stream.
  27. * Parses header of an audio file and returns the byte-offset into the file where the audio data begins.
  28. * @param fileStream An opened file stream on the audio file.
  29. * @return Byte-offset into the file where audio data begins.
  30. */
  31. virtual size_t ParseHeader(AZ::IO::FileIOStream& fileStream) = 0;
  32. /**
  33. * Check validity of the header info.
  34. * This should only return true if the header was parsed and user can expect to see valid format data.
  35. * @return True if the header was parsed without error.
  36. */
  37. virtual bool IsHeaderValid() const = 0;
  38. virtual AudioInputSampleType GetSampleType() const = 0;
  39. virtual AZ::u32 GetNumChannels() const = 0;
  40. virtual AZ::u32 GetSampleRate() const = 0;
  41. virtual AZ::u32 GetByteRate() const = 0;
  42. virtual AZ::u32 GetBitsPerSample() const = 0;
  43. virtual AZ::u32 GetDataSize() const = 0;
  44. };
  45. /**
  46. * A type of AudioInputSource representing an audio file.
  47. * Contains audio file data, holds a pointer to the raw data and provides methods to read chunks of data at a time
  48. * to an output (AkAudioBuffer).
  49. */
  50. class AudioInputFile
  51. : public AudioInputSource
  52. {
  53. public:
  54. AUDIO_IMPL_CLASS_ALLOCATOR(AudioInputFile)
  55. AudioInputFile(const SAudioInputConfig& sourceConfig);
  56. ~AudioInputFile() override;
  57. /**
  58. * Load file into buffer.
  59. * Use an AudioFileParser if needed to parse header information, then proceed to load the audio data
  60. * to the internal buffer.
  61. * @return True upon successful load, false otherwise.
  62. */
  63. bool LoadFile();
  64. /**
  65. * Unload the file data.
  66. * Release the internal buffer of file data.
  67. */
  68. void UnloadFile();
  69. void ReadInput(const AudioStreamData& data) override;
  70. void WriteOutput(AkAudioBuffer* akBuffer) override;
  71. bool IsOk() const override;
  72. void OnDeactivated() override;
  73. /**
  74. * Copy data from the internal buffer to an output buffer.
  75. * Copies a specified number of sample frames to an output buffer. If more frames are requested
  76. * than can be copied, only allowable frames are copied and number of frames that were copied is returned.
  77. * @param numSampleFrames Number of sample frames requested for copy.
  78. * @param toBuffer Output buffer to copy to.
  79. * @return Number of sample frames actually copied.
  80. */
  81. size_t CopyData(size_t numSampleFrames, void* toBuffer); // frames, not bytes!
  82. private:
  83. /**
  84. * Resets internal bookmarking.
  85. * Bookmarks are used internally to keep track of where we are in the buffer during
  86. * chunk-copying to output.
  87. */
  88. void ResetBookmarks();
  89. /**
  90. * Checks whether data copying has reached the end of the file data.
  91. * @return True if end of file has been reached, false otherwise.
  92. */
  93. bool IsEof() const;
  94. AZStd::unique_ptr<AudioFileParser> m_parser = nullptr;
  95. AZ::u8* m_dataPtr = nullptr; ///< The internal data buffer.
  96. size_t m_dataSize = 0; ///< The internal data size.
  97. // Bookmarks
  98. AZ::u8* m_dataCurrentPtr = nullptr; ///< The internal bookmark pointer.
  99. size_t m_dataCurrentReadSize = 0; ///< The internal bookmark indicating how much data has been read so far.
  100. };
  101. } // namespace Audio