Audio Recorder¶
recorder ¶
Classes¶
AudioFormat ¶
Bases: str, Enum
Supported audio output formats.
AudioConfig ¶
Bases: NamedTuple
Audio recording configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
format | int | Audio format (pyaudio constant). |
channels | int | Number of audio channels (1=mono, 2=stereo). |
rate | int | Sample rate in Hz (e.g., 16000, 44100, 48000). |
chunk | int | Number of frames per buffer. |
input_device_index | int | None | Index of input device, None for default. |
AudioFrame ¶
Single audio frame with timestamp.
Uses slots to minimize memory footprint.
Attributes:
| Name | Type | Description |
|---|---|---|
timestamp | When this frame was captured. | |
data | Raw audio bytes. |
Source code in audex/lib/recorder.py
AudioSegment ¶
Bases: NamedTuple
Represents a recorded audio segment.
Attributes:
| Name | Type | Description |
|---|---|---|
key | str | Storage key where the audio is saved. |
duration_ms | int | Duration of the segment in milliseconds. |
started_at | datetime | Timestamp when recording started. |
ended_at | datetime | Timestamp when recording ended. |
frames | bytes | Raw audio frames (bytes). |
AudioRecorder ¶
AudioRecorder(store: Store, config: AudioConfig | None = None)
Bases: LoggingMixin, AsyncContextMixin
High-performance audio recorder using PyAudio with real-time streaming.
This recorder captures audio from a microphone and can start/stop recording multiple times, creating separate audio segments for each recording session. Audio data is automatically uploaded to the configured Store.
Features: - Real-time audio streaming with async generators - Multiple output format support (PCM, WAV, MP3, OPUS) - Efficient numpy-based audio processing - Non-blocking streaming while recording - Time-based segment extraction - Dynamic dtype handling based on AudioConfig
Attributes:
| Name | Type | Description |
|---|---|---|
store | Storage backend for uploading audio files. | |
config | Audio recording configuration. |
Example
recorder = AudioRecorder(
store=local_store,
config=AudioConfig(
format=pyaudio.paInt16,
channels=1,
rate=16000,
chunk=1024,
),
)
await recorder.init()
await recorder.start("session-123", "segment")
# Stream audio chunks in real-time
async for chunk in recorder.stream(
chunk_size=16000, # 1 second chunks
format=AudioFormat.MP3,
):
await send_to_api(chunk)
segment = await recorder.stop()
await recorder.close()
Source code in audex/lib/recorder.py
Attributes¶
current_segment_key property ¶
Get the key of the current recording segment.
Functions¶
init async ¶
Initialize the audio system.
Creates the PyAudio instance and validates the audio configuration.
Raises:
| Type | Description |
|---|---|
Exception | If audio initialization fails. |
Source code in audex/lib/recorder.py
close async ¶
Close the audio system and release resources.
Stops any active recording and cleans up PyAudio resources.
Source code in audex/lib/recorder.py
start async ¶
Start a new recording segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*prefixes | str | Prefix parts for the storage key. | () |
Returns:
| Type | Description |
|---|---|
str | The full storage key for this segment. |
Raises:
| Type | Description |
|---|---|
RuntimeError | If already recording or audio system not initialized. |
Source code in audex/lib/recorder.py
stream async ¶
stream(chunk_size: int | None = None, format: AudioFormat = PCM, channels: int | None = None, rate: int | None = None) -> AsyncGenerator[bytes, None]
Stream audio chunks in real-time while recording.
This does NOT affect the recording buffer. You can stream and record simultaneously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_size | int | None | Number of samples per chunk. None = config.chunk. | None |
format | AudioFormat | Output audio format. | PCM |
channels | int | None | Target channels. None = config.channels. | None |
rate | int | None | Target sample rate. None = config.rate. | None |
Yields:
| Type | Description |
|---|---|
AsyncGenerator[bytes, None] | Audio chunks in specified format. |
Example
Source code in audex/lib/recorder.py
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 | |
segment async ¶
segment(started_at: datetime, ended_at: datetime, *, channels: int | None = None, rate: int | None = None, format: AudioFormat = PCM) -> bytes
Extract audio segment between two timestamps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
started_at | datetime | Start timestamp. | required |
ended_at | datetime | End timestamp. | required |
channels | int | None | Target channels. None = config.channels. | None |
rate | int | None | Target sample rate. None = config.rate. | None |
format | AudioFormat | Output format (PCM, WAV, MP3, OPUS). | PCM |
Returns:
| Type | Description |
|---|---|
bytes | Audio segment in specified format. |
Raises:
| Type | Description |
|---|---|
RuntimeError | If audio system not initialized. |
ValueError | If invalid time range or no frames. |
Source code in audex/lib/recorder.py
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 | |
stop async ¶
stop() -> AudioSegment
Stop recording and save to storage.
Returns:
| Type | Description |
|---|---|
AudioSegment | AudioSegment containing recording information. |
Raises:
| Type | Description |
|---|---|
RuntimeError | If not currently recording. |
Source code in audex/lib/recorder.py
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 | |
clear_frames ¶
Clear all recorded frames from memory.
list_input_devices ¶
List available audio input devices.
Source code in audex/lib/recorder.py
options: show_root_heading: true show_source: true heading_level: 2 members_order: source show_signature_annotations: true separate_signature: true