SoundAssetBuilder.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <AssetBuilderSDK/AssetBuilderSDK.h>
  9. #include <AssetBuilderSDK/SerializationDependencies.h>
  10. #include <AzCore/Asset/AssetDataStream.h>
  11. #include <AzCore/IO/FileIO.h>
  12. #include <AzCore/IO/IOUtils.h>
  13. #include <AzCore/std/smart_ptr/make_shared.h>
  14. #include <AzFramework/StringFunc/StringFunc.h>
  15. #include <MiniAudio/SoundAsset.h>
  16. #include <Tools/SoundAssetBuilder.h>
  17. namespace MiniAudio
  18. {
  19. void SoundAssetBuilder::CreateJobs(
  20. const AssetBuilderSDK::CreateJobsRequest& request,
  21. AssetBuilderSDK::CreateJobsResponse& response) const
  22. {
  23. for (const AssetBuilderSDK::PlatformInfo& platformInfo : request.m_enabledPlatforms)
  24. {
  25. AssetBuilderSDK::JobDescriptor jobDescriptor;
  26. jobDescriptor.m_critical = true;
  27. jobDescriptor.m_jobKey = "MiniSound Asset";
  28. jobDescriptor.SetPlatformIdentifier(platformInfo.m_identifier.c_str());
  29. response.m_createJobOutputs.push_back(jobDescriptor);
  30. }
  31. response.m_result = AssetBuilderSDK::CreateJobsResultCode::Success;
  32. }
  33. void SoundAssetBuilder::ProcessJob(
  34. [[maybe_unused]] const AssetBuilderSDK::ProcessJobRequest& request,
  35. AssetBuilderSDK::ProcessJobResponse& response) const
  36. {
  37. const AZStd::string& fromFile = request.m_fullPath;
  38. AZ::Data::Asset<SoundAsset> soundAsset;
  39. soundAsset.Create(AZ::Data::AssetId(AZ::Uuid::CreateRandom()));
  40. auto assetDataStream = AZStd::make_shared<AZ::Data::AssetDataStream>();
  41. // Read in the data from a file to a buffer, then hand ownership of the buffer over to the assetDataStream
  42. {
  43. AZ::IO::FileIOStream stream(fromFile.c_str(), AZ::IO::OpenMode::ModeRead);
  44. if (!AZ::IO::RetryOpenStream(stream))
  45. {
  46. AZ_Error("SoundAssetBuilder", false, "Source file '%s' could not be opened.", fromFile.c_str());
  47. return;
  48. }
  49. AZStd::vector<AZ::u8> fileBuffer(stream.GetLength());
  50. const size_t bytesRead = stream.Read(fileBuffer.size(), fileBuffer.data());
  51. if (bytesRead != stream.GetLength())
  52. {
  53. AZ_Error("SoundAssetBuilder", false, "Source file '%s' could not be read.", fromFile.c_str());
  54. return;
  55. }
  56. soundAsset->m_data.swap(fileBuffer);
  57. }
  58. AZStd::string filename;
  59. AzFramework::StringFunc::Path::GetFileName(request.m_sourceFile.c_str(), filename);
  60. AZStd::string currentExtension;
  61. AzFramework::StringFunc::Path::GetExtension(request.m_sourceFile.c_str(), currentExtension);
  62. AZStd::string outputExtension = currentExtension + "." + SoundAsset::FileExtension;
  63. AzFramework::StringFunc::Path::ReplaceExtension(filename, outputExtension.c_str());
  64. AZStd::string outputPath;
  65. AzFramework::StringFunc::Path::ConstructFull(request.m_tempDirPath.c_str(), filename.c_str(), outputPath, true);
  66. if (!AZ::Utils::SaveObjectToFile(outputPath, AZ::DataStream::ST_BINARY, soundAsset.Get()))
  67. {
  68. AZ_Error(__FUNCTION__, false, "Failed to save material type to file '%s'!", outputPath.c_str());
  69. return;
  70. }
  71. AssetBuilderSDK::JobProduct soundJobProduct;
  72. if (!AssetBuilderSDK::OutputObject(
  73. soundAsset.Get(),
  74. outputPath,
  75. azrtti_typeid<SoundAsset>(),
  76. SoundAsset::AssetSubId,
  77. soundJobProduct))
  78. {
  79. AZ_Error("SoundAssetBuilder", false, "Failed to output product dependencies.");
  80. response.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  81. }
  82. else
  83. {
  84. response.m_outputProducts.push_back(AZStd::move(soundJobProduct));
  85. response.m_resultCode = AssetBuilderSDK::ProcessJobResult_Success;
  86. }
  87. }
  88. } // namespace MiniAudio