๐ฆ Resource Management - Gestiรณn de Assets Externos¶
๐ Descripciรณn¶
El Resource Management System gestiona todos los assets externos referenciados por presets (samples, wavetables, impulse responses, lookup tables, etc.). Proporciona resoluciรณn inteligente con fallbacks, caching eficiente, y preloading estratรฉgico.
Criticidad: โญโญโญโญโญ (Crรญtico - presets sin recursos no funcionan)
๐ฏ Objetivos¶
- Resource Resolution: Resolver referencias a recursos con mรบltiples estrategias
- Intelligent Caching: Cache LRU con lรญmites de memoria configurables
- Fallback System: Buscar recursos alternativos cuando faltan originales
- Preloading: Cargar recursos crรญticos anticipadamente
- Similarity Matching: Encontrar recursos similares automรกticamente
- Cloud Integration: Descargar recursos desde CDN cuando sea necesario
๐๏ธ Arquitectura¶
Componentes Principales¶
Resource Management System
โ
โโโ ResourceRef (from PresetSchema)
โ โโโ LocationType (Embedded, Relative, Absolute, Cloud)
โ โโโ Path (filesystem or URL)
โ โโโ Checksum (SHA256 for verification)
โ โโโ Metadata (size, type, fallback)
โ
โโโ ResourceResolver
โ โโโ resolve() - Main resolution entry point
โ โโโ resolveEmbedded()
โ โโโ resolveRelative()
โ โโโ resolveAbsolute()
โ โโโ resolveCloud()
โ โโโ findSimilar() - Similarity matching
โ
โโโ ResourceCache
โ โโโ LRU eviction policy
โ โโโ Memory limit (default 2GB)
โ โโโ get() / put()
โ โโโ preload()
โ โโโ Statistics tracking
โ
โโโ ResourceScanner
โ โโโ scanDirectory() - Index resources
โ โโโ buildDatabase() - Create search index
โ โโโ findByChecksum() - Fast lookup
โ
โโโ ResourceManager (orchestrator)
โโโ loadResource()
โโโ cacheResource()
โโโ preloadResources()
โโโ findSimilarResource()
Resource Location Types¶
1. Embedded
{
"type": "sample",
"location_type": "embedded",
"path": "data:audio/wav;base64,UklGRi4AAABXQVZFZm10...",
"size_bytes": 44100
}
2. Relative
{
"type": "wavetable",
"location_type": "relative",
"path": "./wavetables/saw_rich.wav",
"checksum": "sha256:abc123...",
"fallback_path": "./wavetables/saw_basic.wav"
}
3. Absolute
{
"type": "impulse_response",
"location_type": "absolute",
"path": "C:/AudioLab/Resources/IR/plate_reverb.wav",
"checksum": "sha256:def456..."
}
4. Cloud
{
"type": "sample",
"location_type": "cloud",
"path": "https://cdn.audiolab.com/samples/kick_808.wav",
"checksum": "sha256:ghi789...",
"size_bytes": 22050
}
๐ Resolution Pipeline¶
Resolution Flow¶
1. Check Cache
โโ Hit โ Return cached resource
โโ Miss โ Continue to resolution
2. Determine Location Type
โโ Embedded โ Extract from preset
โโ Relative โ Search relative to preset
โโ Absolute โ Load from absolute path
โโ Cloud โ Download from URL
3. Verify Checksum
โโ Match โ Success
โโ Mismatch โ Try fallback
4. Fallback Strategy (if resource not found)
โโ Try fallback_path (if specified)
โโ Search by checksum in database
โโ Find similar resource (by name/type)
โโ Use default placeholder
5. Cache Result
โโ Store in cache for future use
Code Example¶
// Load resource
auto resource_ref = preset.getResourceRef("sample_kick");
auto resource = resource_manager.loadResource(resource_ref);
// Resolution happens automatically
if (resource.isValid()) {
// Use resource
audio_engine.loadSample(resource.getData());
} else {
// Resource not found, fallback applied
std::cerr << "Resource not found, using fallback\n";
}
๐พ Cache System¶
LRU Cache¶
Features: - Least Recently Used (LRU) eviction policy - Configurable memory limit (default: 2GB) - Per-resource type limits - Preload hints for frequently used resources
API:
ResourceCache cache(2_GB); // 2GB limit
// Get resource (may load if not cached)
auto resource = cache.get(resource_ref);
// Preload resources
cache.preload({
"factory_presets/*",
"user_favorites/*"
});
// Statistics
auto stats = cache.getStatistics();
std::cout << "Hit rate: " << stats.hit_rate << "%\n";
std::cout << "Memory used: " << stats.memory_used / 1_MB << " MB\n";
Preloading Strategy¶
Factory Presets: - Load all factory preset resources on startup - Typically <500MB total - Ensures instant preset loading
User Favorites: - Track most-used presets - Preload their resources - Adaptive based on usage patterns
Recent History: - Keep last 10 presets' resources cached - Quick switching between recent presets
๐ Similarity Matching¶
Finding Similar Resources¶
When a resource is missing, find similar alternatives:
Matching Criteria: 1. Checksum Match: Exact same file (renamed/moved) 2. Filename Match: Similar name (edit distance) 3. Type Match: Same resource type (sample, wavetable, IR) 4. Size Match: Similar file size (ยฑ10%) 5. Acoustic Similarity: Audio feature matching (advanced)
Example:
// Original resource not found: "kick_808_punchy.wav"
auto similar = resource_manager.findSimilarResource(
resource_ref,
SimilarityOptions{
.type_match = true,
.name_match = true,
.max_results = 5
}
);
// Results:
// 1. kick_808.wav (90% match)
// 2. kick_909_punchy.wav (75% match)
// 3. kick_acoustic.wav (60% match)
๐ Resource Database¶
Indexing System¶
Build searchable index of all resources:
ResourceScanner scanner;
// Scan directories
scanner.scanDirectory("C:/AudioLab/Resources");
scanner.scanDirectory("C:/Users/Me/Samples");
// Build database
auto database = scanner.buildDatabase();
// Query
auto results = database.findByType("sample");
auto kick_samples = database.findByName("*kick*");
auto by_checksum = database.findByChecksum("sha256:abc123...");
Database Fields: - Path (absolute) - Checksum (SHA256) - Type (sample, wavetable, IR, LUT) - Size (bytes) - Sample rate (for audio) - Duration (for audio) - Metadata (tags, description)
โก Performance Targets¶
| Operation | Target | Typical |
|---|---|---|
| Cache lookup | <1 ฮผs | 0.5 ฮผs |
| Resource load (cached) | <10 ฮผs | 5 ฮผs |
| Resource load (disk) | <10 ms | 5 ms |
| Resource load (cloud) | <500 ms | 200 ms |
| Checksum verification | <5 ms | 2 ms |
| Similarity search | <50 ms | 20 ms |
| Cache eviction | <1 ms | 0.5 ms |
๐งช Usage Examples¶
Example 1: Load Resource¶
#include "resource_manager.hpp"
// Get resource ref from preset
auto ref = preset.getResourceRef("kick_sample");
// Load resource
ResourceManager manager;
auto resource = manager.loadResource(ref);
if (resource.isValid()) {
auto data = resource.getData();
auto size = resource.getSize();
// Use resource
processAudio(data, size);
}
Example 2: Preload Resources¶
ResourceManager manager;
// Preload factory presets
manager.preloadPattern("factory/*");
// Preload user favorites
std::vector<std::string> favorites = getUserFavorites();
manager.preloadResources(favorites);
Example 3: Handle Missing Resource¶
auto resource = manager.loadResource(ref);
if (!resource.isValid()) {
// Try fallback
if (ref.has_fallback()) {
resource = manager.loadResource(ref.getFallback());
}
// Still not found? Find similar
if (!resource.isValid()) {
auto similar = manager.findSimilarResource(ref);
if (!similar.empty()) {
resource = manager.loadResource(similar[0]);
}
}
// Last resort: use default
if (!resource.isValid()) {
resource = manager.getDefaultResource(ref.getType());
}
}
Example 4: Cache Statistics¶
auto& cache = manager.getCache();
// Get statistics
auto stats = cache.getStatistics();
std::cout << "Cache Statistics:\n";
std::cout << " Entries: " << stats.entry_count << "\n";
std::cout << " Memory: " << stats.memory_used / 1_MB << " MB\n";
std::cout << " Hit rate: " << stats.hit_rate << "%\n";
std::cout << " Evictions: " << stats.eviction_count << "\n";
๐ง Configuration¶
Cache Configuration¶
ResourceCache::Config config;
config.max_memory_bytes = 2_GB;
config.max_entries = 10000;
config.eviction_policy = EvictionPolicy::LRU;
config.preload_factory = true;
config.preload_favorites = true;
ResourceCache cache(config);
Resolver Configuration¶
ResourceResolver::Config config;
config.search_paths = {
"C:/AudioLab/Resources",
"C:/Users/Me/Samples",
"%APPDATA%/AudioLab/Resources"
};
config.enable_cloud = true;
config.cloud_timeout_ms = 5000;
config.verify_checksums = true;
config.enable_similarity = true;
ResourceResolver resolver(config);
๐ Resource Types¶
Supported Types¶
- Sample (
.wav,.aiff,.flac) - Audio samples
- One-shot sounds
-
Loops
-
Wavetable (
.wav,.wt) - Single-cycle waveforms
- Wavetable banks
-
Typically 2048 samples
-
Impulse Response (
.wav,.ir) - Reverb IRs
- Cabinet IRs
-
Effect IRs
-
Lookup Table (
.lut,.csv) - Waveshaping curves
- Transfer functions
-
Coefficient tables
-
MIDI File (
.mid) - Sequences
- Patterns
-
Arpeggios
-
Image (
.png,.jpg) - Custom UI backgrounds
- Visualizations
- Logos
๐ Security¶
Checksum Verification¶
All resources verified with SHA256:
bool ResourceManager::verifyChecksum(
const std::vector<uint8_t>& data,
const std::string& expected_checksum) {
auto calculated = calculateSHA256(data);
return calculated == expected_checksum;
}
Safe Cloud Downloads¶
// Only download from trusted domains
const std::vector<std::string> TRUSTED_DOMAINS = {
"cdn.audiolab.com",
"resources.audiolab.com"
};
bool isTrustedUrl(const std::string& url) {
for (const auto& domain : TRUSTED_DOMAINS) {
if (url.find(domain) != std::string::npos) {
return true;
}
}
return false;
}
๐ Related Systems¶
- Preset Schemas (TAREA 1): ResourceRef definition
- Serialization Engine (TAREA 2): Embed resources in presets
- Version Management (TAREA 3): Migrate resource references
- Cloud Integration (TAREA 10): Download resources from CDN
๐ Metrics & Monitoring¶
Key Metrics¶
- Cache hit rate: >90% (factory presets), >70% (user presets)
- Average load time: <10ms (cached), <50ms (disk)
- Memory usage: <2GB under normal use
- Resource availability: >95% (with fallbacks)
Monitoring¶
// Log cache statistics
manager.setStatisticsCallback([](const auto& stats) {
if (stats.hit_rate < 0.7) {
LOG_WARNING("Low cache hit rate: " << stats.hit_rate);
}
if (stats.memory_used > 1.8_GB) {
LOG_WARNING("Cache memory usage high: "
<< stats.memory_used / 1_GB << " GB");
}
});
Status: ๐ In Development Last Updated: 2025-01-15 Version: 1.0.0