API Reference¶
The API reference is generated automatically from the source docstrings.
Parser and data classes¶
WAVParser ¶
Pure Python WAV file parser.
Source code in src/riffy/wav.py
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 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 | |
__init__ ¶
__init__(file_path)
Initialize parser with file path and automatically parse the file.
Source code in src/riffy/wav.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
parse ¶
parse()
Parse the WAV file and return comprehensive information.
Re-parsing re-reads the file from disk and discards any in-memory
modifications previously made via add_chunk, replace_chunk,
or set_chunk.
Source code in src/riffy/wav.py
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 | |
get_info ¶
get_info()
Get comprehensive information about the WAV file.
Source code in src/riffy/wav.py
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 | |
get_chunks ¶
get_chunks(chunk_id)
Return every chunk with the given ID, in file order (empty if none).
Source code in src/riffy/wav.py
319 320 321 | |
get_chunk ¶
get_chunk(chunk_id)
Return the first chunk with the given ID, or None if absent.
Convenience accessor for the common single-occurrence case, so callers
do not have to index into the per-ID list returned by chunks.
Source code in src/riffy/wav.py
323 324 325 326 327 328 329 330 | |
get_chunk_bytes ¶
get_chunk_bytes(chunk_id)
Return the raw payload of the first chunk with the given ID, or None.
This is the raw-bytes accessor the metadata layer decodes from.
Source code in src/riffy/wav.py
332 333 334 335 336 337 338 | |
export_chunk ¶
export_chunk(chunk_id, output_path)
Export a specific chunk's data to a binary file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_id
|
str
|
The ID of the chunk to export (e.g., 'fmt ', 'data') |
required |
output_path
|
str | Path
|
Path where the chunk data will be written |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of bytes written |
Raises:
| Type | Description |
|---|---|
WAVError
|
If file hasn't been parsed yet or file write errors occur |
MissingChunkError
|
If the specified chunk doesn't exist |
Example
parser = WAVParser("audio.wav") parser.export_chunk('data', 'audio_data.bin') 176400
Source code in src/riffy/wav.py
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 | |
export_audio_data ¶
export_audio_data(output_path)
Export raw audio data to a binary file (convenience method).
This is equivalent to export_chunk('data', output_path) but provides a more intuitive interface for the common use case of extracting audio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
str | Path
|
Path where the audio data will be written |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of bytes written |
Raises:
| Type | Description |
|---|---|
WAVError
|
If file hasn't been parsed yet or no audio data exists |
Example
parser = WAVParser("audio.wav") parser.export_audio_data('raw_audio.bin') 176400
Source code in src/riffy/wav.py
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 | |
list_chunks ¶
list_chunks()
List all chunks in the WAV file with their sizes and offsets.
Because a WAV file may contain multiple chunks with the same ID, each ID
maps to a list of {"size", "offset"} entries in file order.
Returns:
| Type | Description |
|---|---|
dict[str, list[dict[str, int]]]
|
Dictionary mapping chunk IDs to a list of their occurrences' metadata |
Raises:
| Type | Description |
|---|---|
WAVError
|
If file hasn't been parsed yet |
Example
parser = WAVParser("audio.wav") chunks = parser.list_chunks() print(chunks) {'fmt ': [{'size': 16, 'offset': 12}], 'data': [{'size': 176400, 'offset': 36}]}
Source code in src/riffy/wav.py
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 | |
replace_chunk ¶
replace_chunk(chunk_id, new_data)
Replace an existing chunk's data with new data.
When a file contains multiple chunks with the same ID, this replaces the first occurrence and leaves the others untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_id
|
str
|
The ID of the chunk to replace (e.g., 'fmt ', 'data') |
required |
new_data
|
bytes
|
The new chunk data (raw bytes) |
required |
Raises:
| Type | Description |
|---|---|
WAVError
|
If file hasn't been parsed yet |
MissingChunkError
|
If the specified chunk doesn't exist |
Example
parser = WAVParser("audio.wav") with open("new_data.bin", "rb") as f: ... parser.replace_chunk('data', f.read()) parser.write_wav("modified.wav")
Source code in src/riffy/wav.py
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 | |
add_chunk ¶
add_chunk(chunk_id, chunk_data)
Add a chunk to the WAV file, appending it after any existing chunks with the same ID.
Unlike v0.2.x, adding a chunk whose ID already exists is allowed and
appends a new occurrence (the chunk store now keeps every occurrence).
Use replace_chunk to overwrite an existing occurrence instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_id
|
str
|
The ID of the new chunk (must be exactly 4 ASCII characters) |
required |
chunk_data
|
bytes
|
The chunk data (raw bytes) |
required |
Raises:
| Type | Description |
|---|---|
WAVError
|
If file hasn't been parsed yet |
InvalidChunkError
|
If chunk_id is not exactly 4 ASCII characters |
Example
parser = WAVParser("audio.wav") parser.add_chunk('INFO', b'Artist: Example') parser.write_wav("modified.wav")
Source code in src/riffy/wav.py
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 | |
set_chunk ¶
set_chunk(chunk_id, chunk_data)
Set a chunk's data, replacing it if it exists or adding it if it doesn't.
This is a convenience method that combines add_chunk and replace_chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_id
|
str
|
The ID of the chunk (must be exactly 4 ASCII characters) |
required |
chunk_data
|
bytes
|
The chunk data (raw bytes) |
required |
Raises:
| Type | Description |
|---|---|
WAVError
|
If file hasn't been parsed yet |
InvalidChunkError
|
If chunk_id is not exactly 4 ASCII characters |
Example
parser = WAVParser("audio.wav") parser.set_chunk('INFO', b'Artist: Example') parser.write_wav("modified.wav")
Source code in src/riffy/wav.py
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 | |
copy_chunk_from_parser ¶
copy_chunk_from_parser(chunk_id, source_parser)
Copy a chunk from another WAVParser instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_id
|
str
|
The ID of the chunk to copy |
required |
source_parser
|
WAVParser
|
The source WAVParser instance to copy from |
required |
Raises:
| Type | Description |
|---|---|
WAVError
|
If either file hasn't been parsed yet |
MissingChunkError
|
If the chunk doesn't exist in the source parser |
Example
parser1 = WAVParser("audio1.wav") parser2 = WAVParser("audio2.wav") parser2.copy_chunk_from_parser('data', parser1) parser2.write_wav("modified.wav")
Source code in src/riffy/wav.py
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | |
remove_chunk ¶
remove_chunk(chunk_id, index=None)
Remove a chunk from the in-memory chunk store.
When the file carries multiple chunks with the same ID, index selects
which occurrence to remove (in file order); with the default
index=None every occurrence of chunk_id is removed. Call
write_wav(...) afterwards to persist the change to disk.
Removing the fmt or data chunk is allowed but leaves the file
un-writable until it is restored, since write_wav requires both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_id
|
str
|
The ID of the chunk to remove (e.g., 'guan', 'LIST') |
required |
index
|
int | None
|
The occurrence to remove, or |
None
|
Raises:
| Type | Description |
|---|---|
WAVError
|
If file hasn't been parsed yet |
MissingChunkError
|
If the chunk ID (or the given index) is absent |
Example
parser = WAVParser("audio.wav") parser.remove_chunk('guan') parser.write_wav("stripped.wav")
Source code in src/riffy/wav.py
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | |
write_wav ¶
write_wav(output_path, overwrite=False, force_rf64=False)
Write the WAV file with all modifications to disk.
This method reconstructs the entire WAV file, properly updating all chunk offsets and the RIFF file size.
Classic 32-bit WAV output is used whenever the file fits, byte-for-byte
identical to prior versions. The RF64/BW64 large-file form (with a
ds64 chunk carrying 64-bit sizes) is emitted only when a size crosses
the 4 GB 32-bit limit, or when force_rf64 is set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
str | Path
|
Path where the WAV file will be written |
required |
overwrite
|
bool
|
If True, allow overwriting the source file. Default is False for safety. |
False
|
force_rf64
|
bool
|
If True, always emit the RF64/BW64 form even when the sizes would fit in classic WAV. Useful for workflows that require BW64 output. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
Number of bytes written |
Raises:
| Type | Description |
|---|---|
WAVError
|
If file hasn't been parsed yet |
FileExistsError
|
If output_path is the same as input and overwrite=False |
MissingChunkError
|
If required chunks are missing |
Example
parser = WAVParser("audio.wav") parser.replace_chunk('data', new_audio_data) parser.write_wav("modified.wav") 176444
Source code in src/riffy/wav.py
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 | |
WAVFormat
dataclass
¶
WAV format information.
Source code in src/riffy/wav.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
WAVChunk
dataclass
¶
Represents a RIFF chunk in the WAV file.
Source code in src/riffy/wav.py
34 35 36 37 38 39 40 41 | |
Recorder metadata¶
The unified entry point and per-standard classes. See the Recorder Metadata guide for worked examples.
Unified metadata view: surface whichever standards a file contains.
:class:RecordingMetadata inspects a WAV file and exposes each recorder-metadata
standard it finds (GUANO, RIFF INFO, Broadcast Wave bext, AudioMoth) side by
side, each kept close to its own raw parsed form.
Deliberately, this view does not perform cross-standard reconciliation: it does not pick a "best-available" timestamp, location, or device by precedence across standards. That merging is policy, and it belongs downstream (in bioamla), which can decide precedence for its own workflows. Keeping riffy close to the raw standards keeps its behavior predictable and avoids baking one consumer's opinions into the library.
(The AudioMoth view is a decoded read of the same INFO block that info
exposes raw, so a file may legitimately report both info and audiomoth —
that is within-standard convenience, not cross-standard merging.)
RecordingMetadata
dataclass
¶
The recorder-metadata standards found in one WAV file, side by side.
Each attribute is the parsed view for that standard, or None if the file
does not contain it.
Source code in src/riffy/metadata/recording.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
from_parser
classmethod
¶
from_parser(parser)
Detect and parse every supported standard from a parsed WAV file.
Source code in src/riffy/metadata/recording.py
45 46 47 48 49 50 51 52 53 54 | |
read_metadata ¶
read_metadata(path)
Parse a WAV file and return the recorder metadata it contains.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to a WAV file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
RecordingMetadata
|
class: |
Example
meta = read_metadata("recording.wav") meta.sources ('guano',) meta.guano.make 'Wildlife Acoustics, Inc.'
Source code in src/riffy/metadata/recording.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
dump_metadata ¶
dump_metadata(path)
Parse a WAV file and return its recorder metadata as a plain, JSON-serializable dict.
A convenience for inspection and tooling (it backs python -m riffy). The
returned dict has file, riff_form, format, sources, and one
entry per standard (guano, info, bext, audiomoth, wamd,
ixml), each None when absent. All values are JSON-safe: datetimes
become ISO strings and binary fields (e.g. bext umid) become hex strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to a WAV file. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A JSON-serializable dict describing the file's metadata. |
Source code in src/riffy/metadata/recording.py
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 | |
GUANO metadata: read and write the guan chunk.
GUANO (Grand Unified Acoustic Notation Ontology) is the closest thing the
bioacoustics field has to a vendor-neutral metadata standard. It is embedded in
a WAV sub-chunk with ID guan holding UTF-8 text, one [Namespace|]Key:
Value field per line, with GUANO|Version required as the first field.
Design highlights (see the v0.3.0 plan §5.1):
- Attribute-first. Well-known fields are exposed as typed attributes
(
timestampis adatetimewith its offset preserved,loc_positionis a(lat, lon)tuple,species_manual_idis a list, ...). Vendor and arbitrary fields remain reachable through :meth:~GuanoMetadata.get/ :meth:~GuanoMetadata.set/ :attr:~GuanoMetadata.fields. - Round-trip safety. Unknown fields and vendor namespaces are preserved verbatim and in order — a hard requirement of the GUANO spec, not a nicety.
- Fail soft. Non-UTF-8 payloads fall back to latin-1 with a warning; a single malformed line is skipped with a warning rather than abandoning the whole chunk.
GuanoMetadata ¶
Typed, round-trip-safe view of a guan chunk's GUANO metadata.
Construct from an existing file via :meth:from_parser / :meth:from_bytes,
or build a fresh one with GuanoMetadata(version="1.0"). Well-known fields
are typed attributes; everything else is reachable via :meth:get /
:meth:set and :attr:fields.
Source code in src/riffy/metadata/guano.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 | |
loc_position
property
writable
¶
loc_position
Loc Position — (latitude, longitude) WGS84 float tuple.
from_bytes
classmethod
¶
from_bytes(data)
Parse GUANO metadata from a raw guan chunk payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
The raw bytes of the |
required |
Returns:
| Type | Description |
|---|---|
GuanoMetadata
|
A populated :class: |
Source code in src/riffy/metadata/guano.py
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 | |
from_parser
classmethod
¶
from_parser(parser)
Parse GUANO metadata from a parser's guan chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
WAVParser
|
A parsed :class: |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
GuanoMetadata | None
|
class: |
GuanoMetadata | None
|
chunk. |
Source code in src/riffy/metadata/guano.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
get ¶
get(namespace, key, default=None)
Return the raw string value for (namespace, key), or default.
Use the empty string for the base (un-prefixed) namespace.
Source code in src/riffy/metadata/guano.py
127 128 129 130 131 132 | |
set ¶
set(namespace, key, value)
Set the raw string value for (namespace, key).
Newlines in value are allowed and re-escaped on write.
Source code in src/riffy/metadata/guano.py
134 135 136 137 138 139 | |
remove ¶
remove(namespace, key)
Delete (namespace, key) if present (no error if absent).
Source code in src/riffy/metadata/guano.py
141 142 143 | |
get_binary ¶
get_binary(namespace, key)
Return a Base64-decoded (RFC 4648) value, or None if absent.
Source code in src/riffy/metadata/guano.py
156 157 158 159 160 161 | |
set_binary ¶
set_binary(namespace, key, value)
Store value as a Base64-encoded (RFC 4648) string.
Source code in src/riffy/metadata/guano.py
163 164 165 | |
to_chunk_bytes ¶
to_chunk_bytes()
Serialize to a guan chunk payload (UTF-8, Version-first, even).
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/riffy/metadata/guano.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
write_to_parser ¶
write_to_parser(parser)
Write this metadata into parser as the guan chunk.
Replaces any existing guan chunk. Call parser.write_wav(...)
afterwards to persist to disk.
Source code in src/riffy/metadata/guano.py
473 474 475 476 477 478 479 | |
RIFF INFO metadata: read and write the LIST/INFO chunk.
A RIFF INFO block is a LIST chunk whose list-type is INFO, containing
NUL-terminated (ZSTR) subchunks each keyed by a 4-character FOURCC (INAM for
title, IART for artist, ICMT for comment, ...). This module decodes that
payload into friendly typed attributes while keeping raw FOURCC access, and
rebuilds it on write.
Practical details handled here (see the v0.3.0 plan §5.2):
- Each value is a NUL-terminated string, but real-world producers are sloppy — the reader tolerates both terminated and non-terminated values.
- Each subchunk is padded to an even length; padding is honored on read and emitted on write.
- A file may carry several
LISTchunks (e.g. anadtllist alongside theINFOone); this module targets theINFOlist specifically and leaves the others untouched.
Values are treated as latin-1, which losslessly round-trips any byte and matches the ASCII/codepage heritage of RIFF INFO. (UTF-8 metadata belongs in GUANO.)
This module is also the carrier the AudioMoth comment decoder reads from.
InfoMetadata ¶
Typed, round-trip-safe view of a LIST/INFO block.
Construct from a file via :meth:from_parser / :meth:from_bytes, or build
a fresh one with InfoMetadata(). Well-known tags are exposed as friendly
str | None attributes (title, artist, comment, ...); every
tag, known or not, is reachable by FOURCC via :meth:get / :meth:set and
:attr:tags.
Source code in src/riffy/metadata/info.py
55 56 57 58 59 60 61 62 63 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 | |
from_bytes
classmethod
¶
from_bytes(data)
Parse an INFO block from a raw LIST chunk payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
The full |
required |
Returns:
| Type | Description |
|---|---|
InfoMetadata
|
A populated :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the payload is not an |
Source code in src/riffy/metadata/info.py
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 | |
from_parser
classmethod
¶
from_parser(parser)
Parse the INFO block from a parser's LIST chunks.
Scans every LIST chunk and decodes the first one whose list-type is
INFO.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
WAVParser
|
A parsed :class: |
required |
Returns:
| Name | Type | Description |
|---|---|---|
An |
InfoMetadata | None
|
class: |
Source code in src/riffy/metadata/info.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
get ¶
get(fourcc, default=None)
Return the value for a FOURCC tag, or default if absent.
Source code in src/riffy/metadata/info.py
133 134 135 | |
set ¶
set(fourcc, value)
Set (or, when value is None, remove) a FOURCC tag.
Raises:
| Type | Description |
|---|---|
InvalidChunkError
|
If |
Source code in src/riffy/metadata/info.py
137 138 139 140 141 142 143 144 145 146 147 | |
remove ¶
remove(fourcc)
Delete a FOURCC tag if present (no error if absent).
Source code in src/riffy/metadata/info.py
149 150 151 | |
to_chunk_bytes ¶
to_chunk_bytes()
Serialize to a LIST chunk payload (INFO type + subchunks).
Each value is written NUL-terminated and padded to even length. FOURCCs
and values are encoded as latin-1, byte-for-byte, so anything read back
(including non-ASCII tags produced by sloppy encoders) round-trips
without loss. New tags added via :meth:set are already ASCII-validated
on the way in.
Source code in src/riffy/metadata/info.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
write_to_parser ¶
write_to_parser(parser)
Write this INFO block into parser as a LIST chunk.
Replaces the existing INFO LIST chunk if present (leaving any
other LIST chunks, such as adtl, untouched), otherwise appends a
new one. Call parser.write_wav(...) afterwards to persist to disk.
Source code in src/riffy/metadata/info.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
Broadcast Wave bext metadata: read and write the fixed binary layout.
Unlike GUANO and RIFF INFO, the bext chunk (EBU Tech 3285) is a fixed
C-struct binary layout, not key-value text, so it is parsed with :mod:struct.
The layout grows across versions — v0 has the base fields, v1 adds the 64-byte
UMID, v2 adds five loudness fields — but the fixed portion is always 602
bytes (a Reserved region absorbs the difference), with CodingHistory
filling the remainder of the chunk.
This module reads and writes all three versions. The writer is table-driven off
the version field and gates the version-specific regions accordingly; the v2
loudness fields are preserved verbatim on write but never computed (per the
v0.3.0 plan §5.3).
BextMetadata
dataclass
¶
Typed view of a bext (Broadcast Wave) chunk.
Version-specific fields are None when the chunk's version does not
include them: umid requires v1+, and the five loudness fields require
v2+.
Source code in src/riffy/metadata/bext.py
59 60 61 62 63 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 | |
from_bytes
classmethod
¶
from_bytes(data)
Parse a bext chunk payload.
Tolerates a truncated fixed header (a short chunk is zero-padded to the 602-byte layout before unpacking), so malformed producers degrade rather than raise.
Source code in src/riffy/metadata/bext.py
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 | |
from_parser
classmethod
¶
from_parser(parser)
Parse the bext chunk from a parser, or None if absent.
Source code in src/riffy/metadata/bext.py
132 133 134 135 136 137 138 | |
to_chunk_bytes ¶
to_chunk_bytes()
Serialize to a bext chunk payload, gated by version.
Version-specific regions are emitted only when version includes
them; otherwise they are zero-filled. Loudness values are written
verbatim (never computed).
Source code in src/riffy/metadata/bext.py
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 | |
write_to_parser ¶
write_to_parser(parser)
Write this metadata into parser as the bext chunk.
Replaces an existing bext chunk or appends one. Call
parser.write_wav(...) afterwards to persist to disk.
Source code in src/riffy/metadata/bext.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
WAMD metadata: read and write the Wildlife Acoustics wamd chunk.
WAMD (Wildlife Acoustics Metadata) is the vendor format Song Meter recorders embed alongside — or, on older firmware, instead of — GUANO. Unlike GUANO's line-oriented text, WAMD is a packed binary stream of length-prefixed entries::
uint16 LE id # which field (see WAMD_IDS)
uint32 LE length # value length in bytes
length value # the field's raw bytes
Entries sit back-to-back with no inter-entry padding; odd-length values are
followed by an explicit 0xFFFF alignment entry rather than an implicit pad
byte. Most values are UTF-8 text; version and time_expansion are 16-bit
integers, and a few (voice notes, program image, run-state) are opaque blobs.
Design highlights (mirroring the GUANO/bext modules):
- Round-trip safety. Entries are kept as an ordered
(id, raw-bytes)list, so every untouched field — including alignment padding, duplicate ids, and blobs riffy does not interpret — re-serializes byte-for-byte. Only the field you edit changes. - Typed accessors. Well-known fields are exposed by name;
loc_positiondecodes the GPS waypoint to a(lat, lon)tuple. Everything else stays reachable through :meth:get_raw/ :meth:set_raw. - Fail soft. A length that overruns the chunk is clamped with a warning; non-UTF-8 text degrades to latin-1 rather than raising.
The junk variant: older firmware wrote the same binary stream under a
junk chunk id. :meth:from_parser treats a junk chunk as WAMD only when
it carries the unmistakable WAMD signature (a leading 2-byte version entry
that parses cleanly to the end), so ordinary junk padding is never mistaken
for metadata.
WamdMetadata ¶
Typed, round-trip-safe view of a wamd chunk's WAMD metadata.
Construct from a file via :meth:from_parser / :meth:from_bytes, or build a
fresh one with WamdMetadata(). Well-known fields are exposed as
attributes; every entry, known or not, is reachable via :meth:get_raw /
:meth:set_raw and :attr:entries.
Source code in src/riffy/metadata/wamd.py
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 | |
timestamp
property
writable
¶
timestamp
The recording timestamp as the recorder wrote it (raw text).
time_expansion
property
¶
time_expansion
The time-expansion factor (a 16-bit integer), or None if absent.
loc_position
property
writable
¶
loc_position
(latitude, longitude) WGS84 tuple decoded from the GPS waypoint.
Handles both Wildlife Acoustics dialects: the Song Meter
datum, lat, N|S, lon, E|W form and the EMTouch signed
datum, lat, lon form. Altitude, if present, is preserved on write
but not surfaced here.
from_bytes
classmethod
¶
from_bytes(data, chunk_id=CHUNK_ID)
Parse WAMD metadata from a raw chunk payload.
A length field that overruns the buffer is clamped (with a warning) so a truncated or malformed chunk degrades rather than raising.
Source code in src/riffy/metadata/wamd.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
from_parser
classmethod
¶
from_parser(parser)
Parse WAMD metadata from a parser's wamd (or junk) chunk.
Prefers a real wamd chunk; failing that, a junk chunk is used
only when it carries the WAMD signature. Returns None if neither is
present.
Source code in src/riffy/metadata/wamd.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
get_raw ¶
get_raw(tag)
Return the raw value bytes for the first entry with tag, or None.
Source code in src/riffy/metadata/wamd.py
196 197 198 199 200 201 | |
set_raw ¶
set_raw(tag, value)
Set the raw value for tag, replacing the first entry in place or appending.
Source code in src/riffy/metadata/wamd.py
203 204 205 206 207 208 209 | |
remove ¶
remove(tag)
Delete every entry with tag (no error if absent).
Source code in src/riffy/metadata/wamd.py
211 212 213 | |
get_text ¶
get_text(tag)
Return the UTF-8 (fail-soft latin-1) decoded value for tag, or None.
Source code in src/riffy/metadata/wamd.py
227 228 229 230 231 232 | |
set_text ¶
set_text(tag, value)
Set tag to the UTF-8 encoding of value.
Source code in src/riffy/metadata/wamd.py
234 235 236 | |
to_chunk_bytes ¶
to_chunk_bytes()
Serialize to a wamd chunk payload (entries back-to-back, no padding).
The chunk-level even-byte padding is left to the writer, matching bext
and guan; the pad byte lives outside the declared chunk size, so a
re-read never mistakes it for a truncated entry.
Source code in src/riffy/metadata/wamd.py
362 363 364 365 366 367 368 369 370 371 372 373 | |
write_to_parser ¶
write_to_parser(parser)
Write this metadata back into parser under its source chunk id.
Replaces the chunk it was read from (the wamd chunk, or the specific
WAMD-bearing junk chunk) or appends a new wamd chunk. Call
parser.write_wav(...) afterwards to persist to disk.
Source code in src/riffy/metadata/wamd.py
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
to_dict ¶
to_dict()
Return a JSON-safe view: named fields decoded, blobs/unknowns as hex.
Alignment padding is omitted. loc_position is added as a [lat, lon]
pair when the GPS waypoint parses.
Source code in src/riffy/metadata/wamd.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
AudioMoth comment-string decoder.
AudioMoth recorders pack their metadata into a single free-text comment stored
in the RIFF INFO ICMT field (with the device string in IART), in an
undocumented format that drifts between firmware versions. A typical string::
Recorded at 19:30:00 12/11/2021 (UTC) by AudioMoth 248D9B045EC9EE79 at
medium gain while battery was 4.1V and temperature was 14.0C.
Rather than switch on firmware version (the clause order and wording differ between the metamoth reference parser and the AudioMoth firmware source, and change repeatedly across versions), this decoder extracts each field with its own tolerant, independently-anchored regex. That makes it partial: if one field (say the timestamp) fails to match, every other field that did match is still returned, rather than discarding the whole string — a concrete pain point reported against existing parsers.
Format knowledge here is derived from the metamoth library
(github.com/mbsantiago/metamoth, src/metamoth/parsing.py) and the AudioMoth
firmware source (github.com/OpenAcousticDevices/AudioMoth-Firmware-Basic,
setHeaderComment in src/main.c). Parsing is best-effort and
firmware-dependent by nature.
Known limitation: the firmware 1.8+ Frequency trigger (...) clause is not
decoded into a dedicated field (no real sample was available to validate it
against). It is left in the raw comment string for downstream consumers, and
it is deliberately not mistaken for a frequency filter.
AudioMothMetadata
dataclass
¶
Structured, best-effort view of an AudioMoth comment string.
Every field is optional: an attribute is left unset when the corresponding clause is absent or unparseable, so a partially-recognized comment still yields whatever could be extracted.
Source code in src/riffy/metadata/audiomoth.py
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 | |
from_comment
classmethod
¶
from_comment(comment, artist=None)
Decode an AudioMoth comment string (and optional artist string).
Extraction is partial: each field is parsed independently, so a failure in one clause never prevents the others from being returned.
Source code in src/riffy/metadata/audiomoth.py
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 | |
from_info
classmethod
¶
from_info(info)
Decode from a parsed :class:~riffy.metadata.InfoMetadata.
Reads the comment from ICMT and the device string from IART.
Returns None if the INFO block shows no sign of being AudioMoth's.
Source code in src/riffy/metadata/audiomoth.py
153 154 155 156 157 158 159 160 161 162 163 164 | |
from_parser
classmethod
¶
from_parser(parser)
Decode from a parser's INFO block, or None if not AudioMoth.
Source code in src/riffy/metadata/audiomoth.py
166 167 168 169 170 171 172 | |
to_guano ¶
to_guano()
Map the extracted fields onto GUANO-equivalent keys.
A within-standard convenience (not cross-standard reconciliation): it
normalizes an AudioMoth recording into the same shape as a GUANO-native
one, so downstream code can treat them uniformly. Device-specific fields
with no GUANO well-known equivalent go under the AudioMoth namespace.
Source code in src/riffy/metadata/audiomoth.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
iXML metadata: read the iXML chunk (stretch goal, read-only).
iXML is a UTF-8 XML document embedded in an iXML chunk, used by production
recorders (Sound Devices, Zoom, ...) to carry structured take/scene/track
metadata. This module parses it into a nested dict / element tree for
inspection. Authoring iXML is out of scope for v0.3.0.
Security note: parsing uses the standard library xml.etree.ElementTree,
which does not fetch external entities but is not hardened against maliciously
crafted XML (e.g. entity-expansion attacks). iXML from field recorders is
effectively trusted input; do not point this at hostile XML without your own
hardening (riffy keeps the zero-dependency promise, so it does not bundle
defusedxml).
IXmlMetadata ¶
Read-only view of an iXML chunk's XML document.
Source code in src/riffy/metadata/ixml.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
from_bytes
classmethod
¶
from_bytes(data)
Parse iXML from a raw iXML chunk payload.
Raises:
| Type | Description |
|---|---|
ParseError
|
If the payload is not valid XML. |
Source code in src/riffy/metadata/ixml.py
33 34 35 36 37 38 39 40 41 42 43 | |
from_parser
classmethod
¶
from_parser(parser)
Parse the iXML chunk from a parser.
Returns None if the file has no iXML chunk, or if the chunk is
present but does not contain parseable XML (a warning is emitted).
Source code in src/riffy/metadata/ixml.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
find ¶
find(path)
Return the text of the first element matching an ElementTree path.
Source code in src/riffy/metadata/ixml.py
61 62 63 | |
to_dict ¶
to_dict()
Return the document as a nested dict keyed by the root tag.
Leaf elements become their stripped text; repeated child tags become a list of values.
Source code in src/riffy/metadata/ixml.py
65 66 67 68 69 70 71 | |
Diffing¶
Compare two WAV files for verification and validation.
riffy.diff(a, b) reports the differences between two files at two levels:
- Chunks — which RIFF chunks were added, removed, or changed (by raw bytes), or left unchanged. Multi-occurrence IDs are compared occurrence by occurrence.
- Metadata fields — a decoded, per-standard diff (GUANO, RIFF INFO, Broadcast
Wave
bext) so you can confirm exactly which fields changed — e.g. that a batch edit touched onlyLoc Positionand left everything else intact.
Both files are read fully into memory (as the parser already does), so diffing a pair of very large recordings uses memory for both at once.
WavDiff
dataclass
¶
The structured difference between two WAV files.
Source code in src/riffy/diff.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
ChunkDelta
dataclass
¶
One chunk occurrence's difference between file A and file B.
Source code in src/riffy/diff.py
32 33 34 35 36 37 38 39 40 | |
FieldDelta
dataclass
¶
One decoded metadata field's difference between file A and file B.
Source code in src/riffy/diff.py
43 44 45 46 47 48 49 50 51 | |
diff ¶
diff(a, b, *, include_unchanged=False)
Compare two WAV files (paths or parsers) and return a :class:WavDiff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
str | Path | WAVParser
|
The first file — a path or an already-parsed :class: |
required |
b
|
str | Path | WAVParser
|
The second file. |
required |
include_unchanged
|
bool
|
If True, include |
False
|
Source code in src/riffy/diff.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
Exceptions¶
Custom exceptions for the riffy RIFF parser library.
RiffyError ¶
Bases: Exception
Base exception for all riffy errors.
Source code in src/riffy/exceptions.py
4 5 6 7 | |
WAVError ¶
Bases: RiffyError
Base exception for WAV-related errors.
Source code in src/riffy/exceptions.py
10 11 12 13 | |
InvalidWAVFormatError ¶
Bases: WAVError
Raised when a WAV file has an invalid format.
Source code in src/riffy/exceptions.py
16 17 18 19 | |
CorruptedFileError ¶
Bases: WAVError
Raised when a file is corrupted or incomplete.
Source code in src/riffy/exceptions.py
22 23 24 25 | |
UnsupportedFormatError ¶
Bases: WAVError
Raised when a WAV format is not supported.
Source code in src/riffy/exceptions.py
28 29 30 31 | |
ChunkError ¶
Bases: RiffyError
Base exception for RIFF chunk-related errors.
Source code in src/riffy/exceptions.py
34 35 36 37 | |
InvalidChunkError ¶
Bases: ChunkError
Raised when a RIFF chunk is invalid.
Source code in src/riffy/exceptions.py
40 41 42 43 | |
MissingChunkError ¶
Bases: ChunkError
Raised when a required RIFF chunk is missing.
Source code in src/riffy/exceptions.py
46 47 48 49 | |