Library API Reference¶
The audiomancer.library module provides sample library management functionality.
Overview¶
library
¶
Sample library management module.
Provides tools for managing sample packs from Google Drive (or other sources) with local caching, symlink management, and integration with pattern generation.
__all__ = ['LibraryStore', 'SampleLookup', 'LibraryManager', 'LibraryError', 'PackNotFoundError', 'SourceNotAvailableError', 'scan_source_packs', 'scan_pack_files', 'group_files_into_samples', 'detect_category', 'detect_bpm', 'detect_is_loop', 'abbreviate_pack_name', 'generate_sample_id', 'PackInfo', 'PackStatus', 'SampleInfo', 'CopyStats', 'EnableResult']
module-attribute
¶
CopyStats
¶
Bases: TypedDict
Statistics from a copy operation.
EnableResult
¶
Bases: TypedDict
Result from enabling a pack.
LibraryError
¶
Bases: AudiomancerError
Sample library management errors.
Base class for errors during library operations (enable/disable packs).
Example
raise LibraryError( ... "Failed to copy pack", ... details={"pack": "808 Drum Kit", "error": "disk full"} ... )
LibraryManager
¶
Manages sample library from source (Google Drive) to local project.
Handles: - Scanning source for available packs - Copying packs to local cache (samples/) - Creating symlinks for enabled packs (library/) - Querying enabled samples for pattern generation
__init__(source_dir: Path, samples_dir: Path, library_dir: Path, ignore_patterns: Optional[set[str]] = None)
¶
Initialize library manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dir
|
Path
|
Path to source samples (e.g., Google Drive) |
required |
samples_dir
|
Path
|
Path to local cache directory |
required |
library_dir
|
Path
|
Path to enabled samples directory (symlinks) |
required |
ignore_patterns
|
Optional[set[str]]
|
Set of pack names to ignore |
None
|
disable_pack(pack_name: str) -> int
¶
Disable a pack (remove symlinks, keep cache).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Name of pack to disable |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of samples disabled |
enable_pack(pack_name: str, max_size_mb: int = 10, workers: int = 16) -> EnableResult
¶
Enable a pack (sync wrapper for async operation).
enable_pack_async(pack_name: str, max_size_mb: int = 10, workers: int = 16) -> EnableResult
async
¶
Enable a pack (copy from source and create symlinks).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Name of pack to enable |
required |
max_size_mb
|
int
|
Skip files larger than this (0 = no limit) |
10
|
workers
|
int
|
Number of parallel copy workers |
16
|
Returns:
| Type | Description |
|---|---|
EnableResult
|
EnableResult with copy stats and enabled sample IDs |
Raises:
| Type | Description |
|---|---|
PackNotFoundError
|
If pack doesn't exist |
get_pack_status(pack_name: str) -> PackStatus
¶
Get detailed status of a pack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Name of pack folder |
required |
Returns:
| Type | Description |
|---|---|
PackStatus
|
PackStatus with enabled/cached/remote status for each sample |
Raises:
| Type | Description |
|---|---|
PackNotFoundError
|
If pack doesn't exist in source |
get_samples_by_type(instrument_type: str, bpm: Optional[float] = None, is_loop: Optional[bool] = None, limit: int = 10) -> list[str]
¶
Get sample IDs matching criteria (implements SampleLookup protocol).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument_type
|
str
|
Category to match (bd, sn, hh, etc.) |
required |
bpm
|
Optional[float]
|
Optional BPM to filter by |
None
|
is_loop
|
Optional[bool]
|
Optional filter for loops vs one-shots |
None
|
limit
|
int
|
Maximum results |
10
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of sample IDs |
list_enabled_samples() -> list[SampleInfo]
¶
List all enabled sample IDs with their info.
Returns:
| Type | Description |
|---|---|
list[SampleInfo]
|
List of SampleInfo for all enabled samples |
list_packs() -> list[PackInfo]
¶
List all available packs from source.
Returns:
| Type | Description |
|---|---|
list[PackInfo]
|
List of PackInfo for each pack in source directory |
purge_pack(pack_name: str) -> bool
¶
Remove pack from local cache entirely.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Name of pack to purge |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if any files were removed |
LibraryStore
¶
Bases: Protocol
Interface for library management operations.
disable_pack(pack_name: str) -> int
¶
Disable a pack (remove symlinks, keep cache). Returns count disabled.
enable_pack(pack_name: str, max_size_mb: int = 10, workers: int = 16) -> EnableResult
¶
Enable a pack (copy from source and create symlinks).
get_pack_status(pack_name: str) -> PackStatus
¶
Get detailed status of a pack.
list_enabled_samples() -> list[SampleInfo]
¶
List all enabled sample IDs with their info.
list_packs() -> list[PackInfo]
¶
List all available packs from source.
purge_pack(pack_name: str) -> bool
¶
Remove pack from cache entirely.
search_packs(pattern: str) -> list[PackInfo]
¶
Search packs by name pattern.
PackInfo
¶
Bases: TypedDict
Information about a sample pack from the source.
PackNotFoundError
¶
Bases: LibraryError
Sample pack does not exist in source directory.
Example
raise PackNotFoundError("Nonexistent Pack", Path("/source"))
PackStatus
¶
Bases: TypedDict
Detailed status of a sample pack.
SampleInfo
¶
Bases: TypedDict
Information about a single sample folder in the library.
SampleLookup
¶
Bases: Protocol
Interface for querying available samples by type.
Used by pattern generation to find real sample IDs.
get_samples_by_type(instrument_type: str, bpm: Optional[float] = None, is_loop: Optional[bool] = None, limit: int = 10) -> list[str]
¶
Return sample IDs matching the criteria.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument_type
|
str
|
Category to match (bd, sn, hh, oh, perc, bass, etc.) |
required |
bpm
|
Optional[float]
|
Optional BPM to filter by (matches samples with detected BPM) |
None
|
is_loop
|
Optional[bool]
|
Optional filter for loops vs one-shots |
None
|
limit
|
int
|
Maximum number of results |
10
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of sample IDs (e.g., ["808dk_bd", "absttex_bd_125"]) |
SourceNotAvailableError
¶
Bases: LibraryError
Source directory (e.g., Google Drive) is not accessible.
Example
raise SourceNotAvailableError(Path("/mnt/google-drive/samples"))
abbreviate_pack_name(pack_name: str) -> str
¶
Generate short abbreviation for pack name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Original pack folder name |
required |
Returns:
| Type | Description |
|---|---|
str
|
Short abbreviation (max 8 chars) for use in sample IDs |
detect_bpm(text: str) -> int | None
¶
Detect BPM from text (filename/path).
Returns:
| Type | Description |
|---|---|
int | None
|
BPM value if detected and in valid range (60-200), None otherwise |
detect_category(text: str) -> tuple[str, str]
¶
Detect sample category from text (filename/path).
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
Tuple of (category_id, category_type) |
detect_is_loop(text: str) -> bool
¶
Detect if sample is a loop vs one-shot.
Returns:
| Type | Description |
|---|---|
bool
|
True if detected as loop, False otherwise |
generate_sample_id(pack_abbr: str, category: str, bpm: int | None, is_loop: bool) -> str
¶
Generate a sample ID from components.
Format: {pack}[lp]{category}[_{bpm}]
Examples:
- "808dk_bd" (808 Drum Kit, kick)
- "absttex_lp_hh_125" (Abstract Techno, loop, hi-hat, 125 BPM)
group_files_into_samples(pack_name: str, files: list[dict[str, Any]]) -> dict[str, SampleInfo]
¶
Group files into sample folders by category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Pack folder name |
required |
files
|
list[dict[str, Any]]
|
List of file info dicts from scan_pack_files |
required |
Returns:
| Type | Description |
|---|---|
dict[str, SampleInfo]
|
Dict mapping sample_id to SampleInfo |
scan_pack_files(source_dir: Path, pack_name: str) -> list[dict[str, Any]]
¶
Scan a pack and categorize all audio files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dir
|
Path
|
Path to source directory |
required |
pack_name
|
str
|
Name of pack folder to scan |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of file info dicts with path, category, bpm, is_loop, size |
scan_source_packs(source_dir: Path) -> list[str]
¶
Get list of top-level pack folders from source directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dir
|
Path
|
Path to source directory (e.g., Google Drive samples folder) |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted list of pack folder names |
Library Manager¶
manager
¶
Library management for sample packs.
Provides LibraryManager class for enabling/disabling sample packs from Google Drive (or other source) with local caching and symlink management.
LibraryManager
¶
Manages sample library from source (Google Drive) to local project.
Handles: - Scanning source for available packs - Copying packs to local cache (samples/) - Creating symlinks for enabled packs (library/) - Querying enabled samples for pattern generation
Source code in src/audiomancer/library/manager.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | |
__init__(source_dir: Path, samples_dir: Path, library_dir: Path, ignore_patterns: Optional[set[str]] = None)
¶
Initialize library manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dir
|
Path
|
Path to source samples (e.g., Google Drive) |
required |
samples_dir
|
Path
|
Path to local cache directory |
required |
library_dir
|
Path
|
Path to enabled samples directory (symlinks) |
required |
ignore_patterns
|
Optional[set[str]]
|
Set of pack names to ignore |
None
|
Source code in src/audiomancer/library/manager.py
disable_pack(pack_name: str) -> int
¶
Disable a pack (remove symlinks, keep cache).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Name of pack to disable |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of samples disabled |
Source code in src/audiomancer/library/manager.py
enable_pack(pack_name: str, max_size_mb: int = 10, workers: int = 16) -> EnableResult
¶
Enable a pack (sync wrapper for async operation).
Source code in src/audiomancer/library/manager.py
enable_pack_async(pack_name: str, max_size_mb: int = 10, workers: int = 16) -> EnableResult
async
¶
Enable a pack (copy from source and create symlinks).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Name of pack to enable |
required |
max_size_mb
|
int
|
Skip files larger than this (0 = no limit) |
10
|
workers
|
int
|
Number of parallel copy workers |
16
|
Returns:
| Type | Description |
|---|---|
EnableResult
|
EnableResult with copy stats and enabled sample IDs |
Raises:
| Type | Description |
|---|---|
PackNotFoundError
|
If pack doesn't exist |
Source code in src/audiomancer/library/manager.py
get_pack_status(pack_name: str) -> PackStatus
¶
Get detailed status of a pack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Name of pack folder |
required |
Returns:
| Type | Description |
|---|---|
PackStatus
|
PackStatus with enabled/cached/remote status for each sample |
Raises:
| Type | Description |
|---|---|
PackNotFoundError
|
If pack doesn't exist in source |
Source code in src/audiomancer/library/manager.py
get_samples_by_type(instrument_type: str, bpm: Optional[float] = None, is_loop: Optional[bool] = None, limit: int = 10) -> list[str]
¶
Get sample IDs matching criteria (implements SampleLookup protocol).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument_type
|
str
|
Category to match (bd, sn, hh, etc.) |
required |
bpm
|
Optional[float]
|
Optional BPM to filter by |
None
|
is_loop
|
Optional[bool]
|
Optional filter for loops vs one-shots |
None
|
limit
|
int
|
Maximum results |
10
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of sample IDs |
Source code in src/audiomancer/library/manager.py
list_enabled_samples() -> list[SampleInfo]
¶
List all enabled sample IDs with their info.
Returns:
| Type | Description |
|---|---|
list[SampleInfo]
|
List of SampleInfo for all enabled samples |
Source code in src/audiomancer/library/manager.py
list_packs() -> list[PackInfo]
¶
List all available packs from source.
Returns:
| Type | Description |
|---|---|
list[PackInfo]
|
List of PackInfo for each pack in source directory |
Source code in src/audiomancer/library/manager.py
purge_pack(pack_name: str) -> bool
¶
Remove pack from local cache entirely.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Name of pack to purge |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if any files were removed |
Source code in src/audiomancer/library/manager.py
search_packs(pattern: str) -> list[PackInfo]
¶
Search packs by name pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Regex pattern to match against pack names |
required |
Returns:
| Type | Description |
|---|---|
list[PackInfo]
|
List of matching PackInfo |
Source code in src/audiomancer/library/manager.py
Scanner¶
scanner
¶
Sample pack scanning and categorization.
Ported from library_manager.py with improvements for audiomancer integration.
abbreviate_pack_name(pack_name: str) -> str
¶
Generate short abbreviation for pack name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Original pack folder name |
required |
Returns:
| Type | Description |
|---|---|
str
|
Short abbreviation (max 8 chars) for use in sample IDs |
Source code in src/audiomancer/library/scanner.py
detect_bpm(text: str) -> int | None
¶
Detect BPM from text (filename/path).
Returns:
| Type | Description |
|---|---|
int | None
|
BPM value if detected and in valid range (60-200), None otherwise |
Source code in src/audiomancer/library/scanner.py
detect_category(text: str) -> tuple[str, str]
¶
Detect sample category from text (filename/path).
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
Tuple of (category_id, category_type) |
Source code in src/audiomancer/library/scanner.py
detect_is_loop(text: str) -> bool
¶
Detect if sample is a loop vs one-shot.
Returns:
| Type | Description |
|---|---|
bool
|
True if detected as loop, False otherwise |
Source code in src/audiomancer/library/scanner.py
generate_sample_id(pack_abbr: str, category: str, bpm: int | None, is_loop: bool) -> str
¶
Generate a sample ID from components.
Format: {pack}[lp]{category}[_{bpm}]
Examples:
- "808dk_bd" (808 Drum Kit, kick)
- "absttex_lp_hh_125" (Abstract Techno, loop, hi-hat, 125 BPM)
Source code in src/audiomancer/library/scanner.py
group_files_into_samples(pack_name: str, files: list[dict[str, Any]]) -> dict[str, SampleInfo]
¶
Group files into sample folders by category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack_name
|
str
|
Pack folder name |
required |
files
|
list[dict[str, Any]]
|
List of file info dicts from scan_pack_files |
required |
Returns:
| Type | Description |
|---|---|
dict[str, SampleInfo]
|
Dict mapping sample_id to SampleInfo |
Source code in src/audiomancer/library/scanner.py
scan_pack_files(source_dir: Path, pack_name: str) -> list[dict[str, Any]]
¶
Scan a pack and categorize all audio files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dir
|
Path
|
Path to source directory |
required |
pack_name
|
str
|
Name of pack folder to scan |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of file info dicts with path, category, bpm, is_loop, size |
Source code in src/audiomancer/library/scanner.py
scan_source_packs(source_dir: Path) -> list[str]
¶
Get list of top-level pack folders from source directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dir
|
Path
|
Path to source directory (e.g., Google Drive samples folder) |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted list of pack folder names |