SoundInstaller.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <cstdio>
  16. #include <fstream>
  17. #include <iostream>
  18. #include <unordered_map>
  19. #include <boost/filesystem.hpp>
  20. #include <tinyxml.h>
  21. #include "installer/extractors/SoundInstaller.h"
  22. #include "installer/extractors/SfxDump.h"
  23. #include "installer/tables/SoundNames.h"
  24. const int SoundInstaller::TOTAL_SOUNDS = 750;
  25. SoundInstaller::SoundInstaller(
  26. DiskImage& disk_image, const std::string& data_dir, const bool keep_originals,
  27. MediaConverter& converter
  28. ):
  29. disk_image_(disk_image), data_dir_(data_dir), keep_originals_(keep_originals),
  30. converter_(converter)
  31. {}
  32. int SoundInstaller::InstallSoundsInit(){
  33. std::unique_ptr<std::string> fmt_path = disk_image_.filePath("data/sound/audio.fmt");
  34. std::unique_ptr<std::string> data_path = disk_image_.filePath("data/sound/audio.dat");
  35. std::string sound_dir =
  36. (boost::filesystem::path(data_dir_) / "audio" / "sounds").string();
  37. if (!fmt_path){
  38. std::cerr << "MediaDataInstaller: could not find data/sound/audio.fmt in the selected game data."
  39. << std::endl;
  40. }
  41. else if (!data_path){
  42. std::cerr << "MediaDataInstaller: could not find data/sound/audio.dat in the selected game data."
  43. << std::endl;
  44. }
  45. else if (SfxDumpConvert(fmt_path->c_str(), data_path->c_str(), sound_dir.c_str()) != 0){
  46. std::cerr << "MediaDataInstaller: failed to convert FFVII sound effects."
  47. << std::endl;
  48. }
  49. processed_sounds_ = 0;
  50. return TOTAL_SOUNDS;
  51. }
  52. bool SoundInstaller::InstallSounds(){
  53. if (converter_.ToolsReady(false)){
  54. const std::string wav = data_dir_ + "audio/sounds/" + std::to_string(processed_sounds_) + ".wav";
  55. const std::string ogg = data_dir_ + "audio/sounds/" + std::to_string(processed_sounds_) + ".ogg";
  56. std::ifstream file(wav);
  57. if (file.is_open()){
  58. file.close();
  59. // Converted in the background. The wav file is removed once it has been converted.
  60. const std::string ffmpeg = converter_.GetFfmpegPath();
  61. const bool keep = keep_originals_;
  62. converter_.Run(
  63. "sound " + std::to_string(processed_sounds_),
  64. [ffmpeg, wav, ogg](std::string* error){return ExternalTools::ConvertToOgg(ffmpeg, wav, ogg, error);},
  65. [wav, keep](){if (!keep) std::remove(wav.c_str());}
  66. );
  67. sounds_.push_back("audio/sounds/" + std::to_string(processed_sounds_) + ".ogg");
  68. }
  69. else{
  70. sounds_.push_back("audio/sounds/INVALID.ogg");
  71. }
  72. }
  73. processed_sounds_ ++;
  74. if (processed_sounds_ >= TOTAL_SOUNDS) return true;
  75. else return false;
  76. }
  77. void SoundInstaller::WriteSoundIndex(){
  78. std::unordered_map<int, const SoundName*> names;
  79. for (const SoundName& name : SOUND_NAMES) names[name.id] = &name;
  80. TiXmlDocument xml;
  81. std::unique_ptr<TiXmlElement> container(new TiXmlElement("sounds"));
  82. for (int id = 0; id < sounds_.size(); id ++){
  83. std::string path = sounds_[id];
  84. // A sound that could not be converted has no file, and listing it would make the game look for it.
  85. if (path != "audio/sounds/INVALID.ogg" && !boost::filesystem::exists(data_dir_ + path)) continue;
  86. std::unique_ptr<TiXmlElement> xml_sound(new TiXmlElement("sound"));
  87. xml_sound->SetAttribute("file_name", path);
  88. xml_sound->SetAttribute("name", id);
  89. container->LinkEndChild(xml_sound.release());
  90. // If there is a friendly name for this sound, add another entry
  91. const auto found = names.find(id);
  92. if (found != names.end() && found->second->name[0] != 0){
  93. std::unique_ptr<TiXmlElement> xml_sound_name(new TiXmlElement("sound"));
  94. xml_sound_name->SetAttribute("file_name", path);
  95. xml_sound_name->SetAttribute("name", found->second->name);
  96. xml_sound_name->SetAttribute("description", found->second->description);
  97. container->LinkEndChild(xml_sound_name.release());
  98. }
  99. }
  100. xml.LinkEndChild(container.release());
  101. xml.SaveFile(data_dir_ + "audio/sounds/_sounds.xml");
  102. }