| 450 | } |
| 451 | |
| 452 | void CFamiTrackerModule::RemoveUnusedDSamples() { |
| 453 | bool AssignUsed[MAX_INSTRUMENTS][NOTE_COUNT] = { }; |
| 454 | |
| 455 | auto &Manager = *GetDSampleManager(); |
| 456 | auto &InstManager = *GetInstrumentManager(); |
| 457 | |
| 458 | for (int i = 0; i < MAX_DSAMPLES; ++i) { |
| 459 | if (Manager.IsSampleUsed(i)) { |
| 460 | bool Used = false; |
| 461 | VisitSongs([&] (const CSongData &song) { |
| 462 | for (unsigned int Frame = 0; Frame < song.GetFrameCount(); ++Frame) { |
| 463 | unsigned int Pattern = song.GetFramePattern(Frame, apu_subindex_t::dpcm); |
| 464 | for (unsigned int Row = 0; Row < song.GetPatternLength(); ++Row) { |
| 465 | const auto &Note = song.GetPatternData(apu_subindex_t::dpcm, Pattern, Row); // // // |
| 466 | int Index = Note.Instrument; |
| 467 | if (!is_note(Note.Note) || Index == MAX_INSTRUMENTS) |
| 468 | continue; // // // |
| 469 | if (InstManager.GetInstrumentType(Index) != INST_2A03) |
| 470 | continue; |
| 471 | AssignUsed[Index][Note.ToMidiNote()] = true; |
| 472 | auto pInst = std::static_pointer_cast<CInstrument2A03>(InstManager.GetInstrument(Index)); |
| 473 | if (pInst->GetSampleIndex(Note.ToMidiNote()) == i) |
| 474 | Used = true; |
| 475 | } |
| 476 | } |
| 477 | }); |
| 478 | if (!Used) |
| 479 | Manager.RemoveDSample(i); |
| 480 | } |
| 481 | } |
| 482 | // also remove unused assignments |
| 483 | InstManager.VisitInstruments([&] (CInstrument &inst, std::size_t i) { |
| 484 | if (auto pInst = dynamic_cast<CInstrument2A03 *>(&inst)) |
| 485 | for (int n = 0; n < NOTE_COUNT; ++n) |
| 486 | if (!AssignUsed[i][n]) |
| 487 | pInst->SetSampleIndex(n, CInstrument2A03::NO_DPCM); |
| 488 | }); |
| 489 | } |
no test coverage detected