#include "audio/cadi_bank.h" #include #include #include #include #include namespace playground { namespace { uint32_t le32(const Bytes& bytes, size_t offset) { return uint32_t(bytes[offset]) | (uint32_t(bytes[offset+1]) << 8) | (uint32_t(bytes[offset+2]) << 16) | (uint32_t(bytes[offset+3]) << 24); } bool fail(std::string* error, const char* message) { if (error) *error = message; return false; } uint64_t le64(std::span bytes, size_t pos) { uint64_t value=0; for(int i=0;i<8;++i) value|=uint64_t(bytes[pos+i])<<(8*i); return value; } std::optional oggFrames(std::span bytes) { size_t pos=0;uint64_t last=0;bool found=false; while(posbytes.size()-pos-27-segments)return std::nullopt; const uint64_t granule=le64(bytes,pos+6); if(granule!=UINT64_MAX){last=granule;found=true;} pos+=27+segments+payload; } return found?std::optional(last):std::nullopt; } std::string_view firstTag(std::string_view xml,std::string_view name) { const std::string open="<"+std::string(name)+">"; const std::string close=""; const size_t start=xml.find(open); if(start==std::string_view::npos)return {}; const size_t content=start+open.size(),end=xml.find(close,content); return end==std::string_view::npos?std::string_view{}:xml.substr(content,end-content); } std::vector allTags(std::string_view xml,std::string_view name,size_t limit=128) { std::vector values; const std::string open="<"+std::string(name)+">"; const std::string close=""; size_t pos=0; while(values.size() links; std::optional frames; std::string url; }; std::unordered_map mapEvents(const Bytes& bytes, const std::vector& records) { std::string_view project; std::string_view resourceIndex; size_t resourceIndexRecord=records.size(); for(size_t i=0;i payload(bytes.data()+r.offset,r.size); if(r.kind!=CadiKind::Xml)continue; if(r.size>=5 && std::memcmp(payload.data(),"(payload.data()),payload.size()}; } else if(r.size>=4 && std::memcmp(payload.data(),"\r\n(payload.data()),payload.size()}; resourceIndexRecord=i; } } const auto keys=allTags(resourceIndex,"Key",10000); if(project.empty() || resourceIndexRecord==records.size() || keys.size()!=records.size()-1)return {}; std::unordered_map byUrl; size_t recordIndex=0; for(const auto& key:keys) { if(recordIndex==resourceIndexRecord)++recordIndex; if(recordIndex>=records.size() || !byUrl.emplace(key,recordIndex).second)return {}; ++recordIndex; } std::unordered_map nodes; size_t pos=0; while(!project.empty() && nodes.size()<5000) { const size_t start=project.find("",pos); if(start==std::string_view::npos)break; const size_t end=project.find("",start+13); if(end==std::string_view::npos)break; const auto block=project.substr(start,end+14-start); const auto name=firstTag(block,"Name"),type=firstTag(block,"PlayerType"); if(!name.empty() && name.size()<=512 && !type.empty()) { GraphNode node; node.type=type; node.parent=firstTag(block,"Parent"); node.loop=firstTag(block,"ResourceLoopEnabled")=="true"; node.links=allTags(block,"ChildName"); const auto soundList=firstTag(block,"SoundList"); auto soundLinks=allTags(soundList,"String"); node.links.insert(node.links.end(),soundLinks.begin(),soundLinks.end()); if(auto resource=firstTag(block,"ResourceName");!resource.empty()) node.links.emplace_back(resource); if(auto url=firstTag(block,"Url");!url.empty())node.url=url; if(auto duration=firstTag(block,"ResourceEndPosition");!duration.empty()) { uint64_t value=0; const auto parsed=std::from_chars(duration.data(),duration.data()+duration.size(),value); if(parsed.ec==std::errc{} && parsed.ptr==duration.data()+duration.size())node.frames=value; } nodes.emplace(name,std::move(node)); } pos=end+14; } size_t verified=0; for(const auto& [name,node]:nodes) { if(node.type!="SampleResource" || node.url.empty() || !node.frames)continue; const auto found=byUrl.find(node.url); if(found==byUrl.end())continue; const size_t index=found->second; if(records[index].kind!=CadiKind::OggVorbis)continue; const auto& rec=records[index]; const auto frames=oggFrames({bytes.data()+rec.offset,rec.size}); if(!frames || *frames!=*node.frames)return {}; ++verified; } if(verified!=175)return {}; std::unordered_map mapped; for(const auto& [event,root]:nodes) { if(root.type!="MixerChannel" && root.type!="ArrangementClip" && root.type!="EventClip")continue; std::vector pending{event}; std::unordered_set visited; std::unordered_set clips; bool ambiguous=false; while(!pending.empty() && visited.size()<5000) { auto name=std::move(pending.back());pending.pop_back(); if(!visited.insert(name).second)continue; auto found=nodes.find(name); if(found==nodes.end())continue; const auto& node=found->second; if(node.type=="SampleResource") { const auto clip=byUrl.find(node.url); if(clip==byUrl.end() || records[clip->second].kind!=CadiKind::OggVorbis) ambiguous=true; else clips.insert(clip->second); } else pending.insert(pending.end(),node.links.begin(),node.links.end()); } if(!ambiguous && !clips.empty()) { std::vector ordered(clips.begin(),clips.end()); std::sort(ordered.begin(),ordered.end()); CadiEventInfo info; info.clips=std::move(ordered); info.loop=root.loop; info.channel=root.type=="ArrangementClip"?CadiChannel::Music: root.parent=="New Mixer 3"?CadiChannel::Environment:CadiChannel::Sfx; mapped.emplace(event,std::move(info)); } } return mapped; } } bool CadiBank::load(Bytes bytes, std::string* error) { constexpr size_t maxBankBytes = 256 * 1024 * 1024; if (bytes.size() < 32 || bytes.size() > maxBankBytes || le32(bytes, 8) != 0xddfefdc8u) return fail(error, "invalid CADI bank header"); const uint32_t count = le32(bytes, 12); if (count == 0 || count > 10000 || size_t(count) > (bytes.size()-32)/16) return fail(error, "invalid CADI record count"); const size_t tableEnd = 32 + size_t(count) * 16; size_t expectedOffset = tableEnd; uint32_t previousToken = 0; std::vector found; found.reserve(count); for (uint32_t i=0; i bytes.size()-expectedOffset) return fail(error, "invalid CADI record bounds"); CadiKind kind; if (std::memcmp(bytes.data()+offset, "OggS", 4) == 0) kind = CadiKind::OggVorbis; else if (std::memcmp(bytes.data()+offset, " CadiBank::clipForEvent(std::string_view event) const { auto found=eventClips_.find(std::string(event)); return found==eventClips_.end() || found->second.clips.size()!=1 ? std::nullopt : std::optional(found->second.clips.front()); } std::vector CadiBank::clipsForEvent(std::string_view event) const { auto found=eventClips_.find(std::string(event)); return found==eventClips_.end()?std::vector{}:found->second.clips; } std::optional CadiBank::eventInfo(std::string_view event) const { auto found=eventClips_.find(std::string(event)); return found==eventClips_.end()?std::nullopt:std::optional(found->second); } std::span CadiBank::data(size_t index) const { if (index >= records_.size()) return {}; const auto& record = records_[index]; return {bytes_.data()+record.offset, record.size}; } } // namespace playground