AudioInputFile.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #include <AudioInput/AudioInputFile.h>
  9. #include <AudioInput/WavParser.h>
  10. #include <Common_wwise.h>
  11. #include <AzCore/IO/FileIO.h>
  12. #include <AK/SoundEngine/Common/AkStreamMgrModule.h>
  13. namespace Audio
  14. {
  15. ///////////////////////////////////////////////////////////////////////////////////////////////
  16. // Audio Input File
  17. ///////////////////////////////////////////////////////////////////////////////////////////////
  18. ///////////////////////////////////////////////////////////////////////////////////////////////
  19. AudioInputFile::AudioInputFile(const SAudioInputConfig& sourceConfig)
  20. {
  21. m_config = sourceConfig;
  22. switch (sourceConfig.m_sourceType)
  23. {
  24. case AudioInputSourceType::WavFile:
  25. m_parser.reset(aznew WavFileParser());
  26. break;
  27. case AudioInputSourceType::PcmFile:
  28. break;
  29. default:
  30. return;
  31. }
  32. LoadFile();
  33. }
  34. ///////////////////////////////////////////////////////////////////////////////////////////////
  35. AudioInputFile::~AudioInputFile()
  36. {
  37. UnloadFile();
  38. }
  39. ///////////////////////////////////////////////////////////////////////////////////////////////
  40. bool AudioInputFile::LoadFile()
  41. {
  42. bool result = false;
  43. // Filename should be relative to the project assets root e.g.: 'sounds/files/my_sound.wav'
  44. AZ::IO::FileIOStream fileStream(m_config.m_sourceFilename.c_str(), AZ::IO::OpenMode::ModeRead | AZ::IO::OpenMode::ModeBinary);
  45. if (fileStream.IsOpen())
  46. {
  47. m_dataSize = fileStream.GetLength();
  48. if (m_dataSize > 0)
  49. {
  50. // Here if a parser is available, can pass the file stream forward
  51. // so it can parse header information.
  52. // It will return the number of header bytes read, that is an offset to
  53. // the beginning of the real signal data.
  54. if (m_parser)
  55. {
  56. size_t headerBytesRead = m_parser->ParseHeader(fileStream);
  57. if (headerBytesRead > 0 && m_parser->IsHeaderValid())
  58. {
  59. // Update the size...
  60. m_dataSize = m_parser->GetDataSize();
  61. // Set the format configuration obtained from the file...
  62. m_config.m_bitsPerSample = m_parser->GetBitsPerSample();
  63. m_config.m_numChannels = m_parser->GetNumChannels();
  64. m_config.m_sampleRate = m_parser->GetSampleRate();
  65. m_config.m_sampleType = m_parser->GetSampleType();
  66. }
  67. }
  68. if (IsOk())
  69. {
  70. // Allocate a new buffer to hold the data...
  71. m_dataPtr = new AZ::u8[m_dataSize];
  72. // Read file into internal buffer...
  73. size_t bytesRead = fileStream.Read(m_dataSize, m_dataPtr);
  74. ResetBookmarks();
  75. // Verify we read the full amount...
  76. result = (bytesRead == m_dataSize);
  77. }
  78. }
  79. fileStream.Close();
  80. }
  81. return result;
  82. }
  83. ///////////////////////////////////////////////////////////////////////////////////////////////
  84. void AudioInputFile::UnloadFile()
  85. {
  86. if (m_dataPtr)
  87. {
  88. delete [] m_dataPtr;
  89. m_dataPtr = nullptr;
  90. }
  91. m_dataSize = 0;
  92. m_dataCurrentPtr = nullptr;
  93. m_dataCurrentReadSize = 0;
  94. }
  95. ///////////////////////////////////////////////////////////////////////////////////////////////
  96. void AudioInputFile::ReadInput([[maybe_unused]] const AudioStreamData& data)
  97. {
  98. // Don't really need this for File-based sources, the whole file is read in the constructor.
  99. // However, we may need to implement this for asynchronous loading of the file (streaming).
  100. }
  101. ///////////////////////////////////////////////////////////////////////////////////////////////
  102. void AudioInputFile::WriteOutput(AkAudioBuffer* akBuffer)
  103. {
  104. AZ::u16 numSampleFramesRequested = (akBuffer->MaxFrames() - akBuffer->uValidFrames);
  105. if (m_config.m_sampleType == AudioInputSampleType::Int)
  106. {
  107. void* outBuffer = akBuffer->GetInterleavedData();
  108. AZ::u16 numSampleFramesCopied = static_cast<AZ::u16>(CopyData(numSampleFramesRequested, outBuffer));
  109. akBuffer->uValidFrames += numSampleFramesCopied;
  110. akBuffer->eState = (numSampleFramesCopied > 0) ? AK_DataReady
  111. : (IsEof() ? AK_NoMoreData : AK_NoDataReady);
  112. }
  113. else if (m_config.m_sampleType == AudioInputSampleType::Float)
  114. {
  115. // Not Implemented yet!
  116. akBuffer->eState = AK_NoMoreData;
  117. // Implementing this for files will likely involve de-interleaving the samples.
  118. }
  119. }
  120. ///////////////////////////////////////////////////////////////////////////////////////////////
  121. bool AudioInputFile::IsOk() const
  122. {
  123. bool ok = (m_dataSize > 0);
  124. ok &= IsFormatValid();
  125. if (m_parser)
  126. {
  127. ok &= m_parser->IsHeaderValid();
  128. ok &= (m_dataSize == m_parser->GetDataSize());
  129. }
  130. return ok;
  131. }
  132. ///////////////////////////////////////////////////////////////////////////////////////////////
  133. void AudioInputFile::OnDeactivated()
  134. {
  135. if (m_config.m_autoUnloadFile)
  136. {
  137. UnloadFile();
  138. }
  139. }
  140. ///////////////////////////////////////////////////////////////////////////////////////////////
  141. size_t AudioInputFile::CopyData(size_t numSampleFrames, void* toBuffer)
  142. {
  143. // Copies data to an output buffer.
  144. // Size requested is in sample frames, not bytes!
  145. // Number of frames actually copied is returned. This is useful if more
  146. // frames were requested than can be copied.
  147. if (!toBuffer || !numSampleFrames)
  148. {
  149. return 0;
  150. }
  151. const size_t frameBytes = (m_config.m_numChannels * m_config.m_bitsPerSample) >> 3; // bits --> bytes
  152. size_t copySize = numSampleFrames * frameBytes;
  153. // Check if request is larger than remaining, trim off excess.
  154. if (m_dataCurrentReadSize + copySize > m_dataSize)
  155. {
  156. size_t excess = (m_dataCurrentReadSize + copySize) - m_dataSize;
  157. copySize -= excess;
  158. numSampleFrames = (copySize / frameBytes);
  159. }
  160. if (copySize > 0)
  161. {
  162. ::memcpy(toBuffer, m_dataCurrentPtr, copySize);
  163. m_dataCurrentReadSize += copySize;
  164. m_dataCurrentPtr += copySize;
  165. }
  166. return numSampleFrames;
  167. }
  168. ///////////////////////////////////////////////////////////////////////////////////////////////
  169. void AudioInputFile::ResetBookmarks()
  170. {
  171. m_dataCurrentPtr = m_dataPtr;
  172. m_dataCurrentReadSize = 0;
  173. }
  174. ///////////////////////////////////////////////////////////////////////////////////////////////
  175. bool AudioInputFile::IsEof() const
  176. {
  177. return (m_dataCurrentReadSize == m_dataSize);
  178. }
  179. } // namespace Audio