SoundAssetBuilder.cpp 4.0 KB

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