API Reference¶
Complete API reference for coremusic. The package provides both functional and object-oriented APIs.
Note
The object-oriented API is recommended for new applications due to automatic resource management and Pythonic interfaces.
Object-Oriented API¶
High-level Pythonic wrappers with automatic resource management.
AudioFile Class¶
coremusic.audio.AudioFile
¶
Bases: CoreAudioObject
High-level audio file operations with automatic resource management
Source code in src/coremusic/audio/core.py
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 | |
_path = str(path)
instance-attribute
¶
_format = None
instance-attribute
¶
_is_open = False
instance-attribute
¶
_writable = writable
instance-attribute
¶
format
property
¶
Get the audio format of the file
metadata
property
¶
Read the info dictionary metadata from the audio file.
Returns a dict with string keys and string/number/bytes values, or None if the file format does not support metadata.
duration
property
¶
Duration in seconds
__init__(path, *, writable=False)
¶
open()
¶
Open the audio file
Source code in src/coremusic/audio/core.py
close()
¶
Close the audio file
Source code in src/coremusic/audio/core.py
__enter__()
¶
__exit__(exc_type, exc_val, exc_tb)
¶
read_packets(start_packet, packet_count)
¶
Read audio packets from the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_packet
|
int
|
Starting packet index (must be non-negative) |
required |
packet_count
|
int
|
Number of packets to read (must be positive) |
required |
Returns:
| Type | Description |
|---|---|
tuple[bytes, int]
|
Tuple of (audio_data_bytes, packets_read) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If start_packet < 0 or packet_count <= 0 |
AudioFileError
|
If reading fails |
Example::
from coremusic.audio import AudioFile
# Read audio data in chunks
with AudioFile("audio.wav") as audio:
chunk_size = 4096
offset = 0
while True:
data, packets_read = audio.read_packets(offset, chunk_size)
if packets_read == 0:
break
# Process data...
offset += packets_read
Source code in src/coremusic/audio/core.py
read_as_numpy(start_packet=0, packet_count=None)
¶
Read audio data from the file as a NumPy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_packet
|
int
|
Starting packet index (default: 0) |
0
|
packet_count
|
int | None
|
Number of packets to read (default: all remaining packets) |
None
|
Returns:
| Type | Description |
|---|---|
'NDArray[Any]'
|
NumPy array with shape (frames, channels) for multi-channel audio, |
'NDArray[Any]'
|
or (frames,) for mono audio. The dtype is determined by the audio format. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If NumPy is not available |
AudioFileError
|
If reading fails |
Example:
with AudioFile("audio.wav") as audio:
data = audio.read_as_numpy()
print(f"Shape: {data.shape}, dtype: {data.dtype}")
# output: Shape: (44100, 2), dtype: int16
Source code in src/coremusic/audio/core.py
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 | |
get_property(property_id)
¶
Get a property from the audio file
Source code in src/coremusic/audio/core.py
set_property(property_id, data)
¶
Set a property on the audio file.
The file must have been opened with writable=True.
Source code in src/coremusic/audio/core.py
set_metadata(tags)
¶
Write metadata tags to the audio file.
The file must have been opened with writable=True. Keys should be strings. Values can be str, int, or float.
Common keys: 'title', 'artist', 'album', 'genre', 'year', 'track number', 'comments', 'approximate duration in seconds'.
Source code in src/coremusic/audio/core.py
__repr__()
¶
dispose()
¶
Dispose of the audio file
Source code in src/coremusic/audio/core.py
AudioFormat Class¶
coremusic.audio.AudioFormat
¶
Pythonic representation of AudioStreamBasicDescription
Source code in src/coremusic/audio/core.py
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 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 | |
sample_rate = sample_rate
instance-attribute
¶
format_id = format_id
instance-attribute
¶
format_flags = format_flags
instance-attribute
¶
bytes_per_packet = bytes_per_packet
instance-attribute
¶
frames_per_packet = frames_per_packet
instance-attribute
¶
bytes_per_frame = bytes_per_frame
instance-attribute
¶
channels_per_frame = channels_per_frame
instance-attribute
¶
bits_per_channel = bits_per_channel
instance-attribute
¶
is_pcm
property
¶
Check if this is a PCM format
is_stereo
property
¶
Check if this is stereo (2 channels)
is_mono
property
¶
Check if this is mono (1 channel)
__init__(sample_rate, format_id, format_flags=0, bytes_per_packet=0, frames_per_packet=0, bytes_per_frame=0, channels_per_frame=2, bits_per_channel=16)
¶
Source code in src/coremusic/audio/core.py
from_asbd_bytes(data)
classmethod
¶
Parse an AudioStreamBasicDescription (40 bytes) into an AudioFormat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Raw ASBD bytes (at least 40 bytes) |
required |
Returns:
| Type | Description |
|---|---|
'AudioFormat'
|
AudioFormat with parsed fields |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is too short |
Source code in src/coremusic/audio/core.py
pcm(sample_rate=44100.0, channels=2, bits=16, is_float=False)
classmethod
¶
Create a PCM AudioFormat with correctly computed derived fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_rate
|
float
|
Sample rate in Hz (default: 44100.0) |
44100.0
|
channels
|
int
|
Number of channels (default: 2) |
2
|
bits
|
int
|
Bits per sample (default: 16) |
16
|
is_float
|
bool
|
If True, create float format; otherwise signed integer |
False
|
Returns:
| Type | Description |
|---|---|
'AudioFormat'
|
AudioFormat with all ASBD fields correctly computed |
Source code in src/coremusic/audio/core.py
to_dict()
¶
Convert to dictionary format for functional API
Source code in src/coremusic/audio/core.py
to_numpy_dtype()
¶
Convert audio format to NumPy dtype for audio data arrays.
Returns:
| Type | Description |
|---|---|
'np.dtype[Any]'
|
NumPy dtype object suitable for audio data representation |
Raises:
| Type | Description |
|---|---|
ImportError
|
If NumPy is not available |
ValueError
|
If format cannot be converted to NumPy dtype |
Source code in src/coremusic/audio/core.py
AudioUnit Class¶
coremusic.audio.AudioUnit
¶
Bases: CoreAudioObject
Audio unit for real-time audio processing
Source code in src/coremusic/audio/units.py
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 | |
_description = description
instance-attribute
¶
_is_initialized = False
instance-attribute
¶
sample_rate
property
writable
¶
Get the sample rate (kAudioUnitProperty_SampleRate on global scope)
latency
property
¶
Get the latency in seconds (kAudioUnitProperty_Latency)
cpu_load
property
¶
Get the CPU load as a fraction (0.0 to 1.0) (kAudioUnitProperty_CPULoad)
max_frames_per_slice
property
writable
¶
Get the maximum frames per slice (kAudioUnitProperty_MaximumFramesPerSlice)
is_initialized
property
¶
__init__(description)
¶
default_output()
classmethod
¶
Create a default output AudioUnit
Source code in src/coremusic/audio/units.py
initialize()
¶
Initialize the AudioUnit
Source code in src/coremusic/audio/units.py
uninitialize()
¶
Uninitialize the AudioUnit
Source code in src/coremusic/audio/units.py
start()
¶
Start the AudioUnit output
Source code in src/coremusic/audio/units.py
stop()
¶
get_property(property_id, scope, element)
¶
Get a property from the AudioUnit
Source code in src/coremusic/audio/units.py
set_property(property_id, scope, element, data)
¶
Set a property on the AudioUnit
Source code in src/coremusic/audio/units.py
get_stream_format(scope='output', element=0)
¶
Get the stream format for a specific scope and element
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
str
|
'input', 'output', or 'global' (default: 'output') |
'output'
|
element
|
int
|
Element index (default: 0) |
0
|
Returns:
| Type | Description |
|---|---|
AudioFormat
|
AudioFormat object with the current stream format |
Raises:
| Type | Description |
|---|---|
AudioUnitError
|
If scope is invalid or getting format fails |
Example::
# Get the output format of an AudioUnit
with AudioUnit(component) as unit:
format = unit.get_stream_format('output', 0)
print(f"Sample rate: {format.sample_rate}")
print(f"Channels: {format.channels_per_frame}")
print(f"Bits: {format.bits_per_channel}")
Source code in src/coremusic/audio/units.py
set_stream_format(format, scope='output', element=0)
¶
Set the stream format for a specific scope and element
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
AudioFormat
|
AudioFormat object with desired format |
required |
scope
|
str
|
'input', 'output', or 'global' (default: 'output') |
'output'
|
element
|
int
|
Element index (must be non-negative, default: 0) |
0
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If format is not an AudioFormat |
ValueError
|
If scope is invalid or element is negative |
AudioUnitError
|
If setting format fails |
Example::
from coremusic.audio import AudioFormat, AudioUnit
# Create a stereo 44.1kHz 16-bit PCM format
format = AudioFormat(
sample_rate=44100.0,
format_id='lpcm',
channels_per_frame=2,
bits_per_channel=16
)
# Set the input format on an effect unit
with AudioUnit(effect_component) as effect:
effect.set_stream_format(format, 'input', 0)
Source code in src/coremusic/audio/units.py
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 | |
get_parameter_list(scope='global')
¶
Get list of available parameter IDs (kAudioUnitProperty_ParameterList)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
str
|
'input', 'output', or 'global' (default: 'global') |
'global'
|
Returns:
| Type | Description |
|---|---|
list[int]
|
List of parameter IDs |
Source code in src/coremusic/audio/units.py
render(num_frames, timestamp=None)
¶
Render audio frames (for offline processing)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_frames
|
int
|
Number of frames to render |
required |
timestamp
|
int | None
|
Optional timestamp (default: None uses current time) |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
Rendered audio data as bytes |
Note: This is a simplified render method for offline processing. For real-time audio, use render callbacks with the audio player infrastructure.
Source code in src/coremusic/audio/units.py
__repr__()
¶
Source code in src/coremusic/audio/units.py
__enter__()
¶
__exit__(exc_type, exc_val, exc_tb)
¶
dispose()
¶
Dispose of the AudioUnit
Source code in src/coremusic/audio/units.py
AudioQueue Class¶
coremusic.audio.AudioQueue
¶
Bases: CoreAudioObject
Audio queue for buffered playback and recording
Source code in src/coremusic/audio/core.py
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 | |
_format = audio_format
instance-attribute
¶
_buffers = []
instance-attribute
¶
__init__(audio_format)
¶
new_output(audio_format)
classmethod
¶
Create a new output audio queue
Source code in src/coremusic/audio/core.py
allocate_buffer(buffer_size)
¶
Allocate an audio buffer
Source code in src/coremusic/audio/core.py
enqueue_buffer(buffer)
¶
Enqueue an audio buffer
Source code in src/coremusic/audio/core.py
start()
¶
stop(immediate=True)
¶
Stop the audio queue
Source code in src/coremusic/audio/core.py
__repr__()
¶
dispose(immediate=True)
¶
Dispose of the audio queue
Source code in src/coremusic/audio/core.py
AudioConverter Class¶
coremusic.audio.AudioConverter
¶
Bases: CoreAudioObject
Audio format converter for sample rate and format conversion
Provides high-level interface for converting between audio formats, sample rates, bit depths, and channel configurations.
Source code in src/coremusic/audio/core.py
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 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 | |
_source_format = source_format
instance-attribute
¶
_dest_format = dest_format
instance-attribute
¶
source_format
property
¶
Get source audio format
dest_format
property
¶
Get destination audio format
__init__(source_format, dest_format)
¶
Create an AudioConverter
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_format
|
AudioFormat
|
Source audio format |
required |
dest_format
|
AudioFormat
|
Destination audio format |
required |
Raises:
| Type | Description |
|---|---|
AudioConverterError
|
If converter creation fails |
Source code in src/coremusic/audio/core.py
convert(audio_data)
¶
Convert audio data from source to destination format
This method uses the simple buffer-based API (AudioConverterConvertBuffer) which only supports conversions where the input/output sizes are predictable. For complex conversions (sample rate, bit depth), use convert_with_callback().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_data
|
bytes
|
Input audio data in source format (raw PCM samples) |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Converted audio data in destination format |
Raises:
| Type | Description |
|---|---|
AudioConverterError
|
If conversion fails |
Example:
# Convert stereo to mono (simple format conversion)
source = AudioFormat(44100.0, 'lpcm', channels_per_frame=2, bits_per_channel=16)
dest = AudioFormat(44100.0, 'lpcm', channels_per_frame=1, bits_per_channel=16)
with AudioConverter(source, dest) as converter:
stereo_data = b'\x00\x00\xff\xff' * 1024 # Raw 16-bit stereo samples
mono_data = converter.convert(stereo_data)
Source code in src/coremusic/audio/core.py
convert_with_callback(input_data, input_packet_count, output_packet_count=None)
¶
Convert audio using callback-based API for complex conversions
This method supports all types of conversions including: - Sample rate changes (e.g., 44.1kHz -> 48kHz) - Bit depth changes (e.g., 16-bit -> 24-bit) - Channel count changes (stereo <-> mono) - Combinations of the above
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_data
|
bytes
|
Input audio data as bytes |
required |
input_packet_count
|
int
|
Number of packets in input data |
required |
output_packet_count
|
int | None
|
Expected output packets (auto-calculated if None) |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
Converted audio data as bytes |
Raises:
| Type | Description |
|---|---|
AudioConverterError
|
If conversion fails |
Example:
# Convert 44.1kHz to 48kHz
source_format = AudioFormat(44100.0, 'lpcm', channels_per_frame=2, bits_per_channel=16)
dest_format = AudioFormat(48000.0, 'lpcm', channels_per_frame=2, bits_per_channel=16)
with AudioConverter(source_format, dest_format) as converter:
# Read input data
with AudioFile("input_44100.wav") as af:
input_data, packet_count = af.read_packets(0, 999999999)
# Convert
output_data = converter.convert_with_callback(input_data, packet_count)
# Write output
with ExtendedAudioFile.create("output_48000.wav", 'WAVE', dest_format) as out:
num_frames = len(output_data) // dest_format.bytes_per_frame
out.write(num_frames, output_data)
Source code in src/coremusic/audio/core.py
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 | |
get_property(property_id)
¶
Get a property from the converter
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
property_id
|
int
|
Property ID |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Property data as bytes |
Raises:
| Type | Description |
|---|---|
AudioConverterError
|
If getting property fails |
Source code in src/coremusic/audio/core.py
set_property(property_id, data)
¶
Set a property on the converter
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
property_id
|
int
|
Property ID (from capi.get_audio_converter_property_*()) |
required |
data
|
bytes
|
Property data as bytes (use struct.pack for binary encoding) |
required |
Raises:
| Type | Description |
|---|---|
AudioConverterError
|
If setting property fails |
Example:
import struct
import coremusic as cm
converter = AudioConverter(source_fmt, dest_fmt)
# Set bitrate to 128 kbps (requires UInt32)
bitrate_prop = cm.capi.get_audio_converter_property_bit_rate()
converter.set_property(bitrate_prop, struct.pack('<I', 128000))
# Set quality (requires UInt32, 0=lowest, 127=highest)
quality_prop = cm.capi.get_audio_converter_property_quality()
converter.set_property(quality_prop, struct.pack('<I', 127))
Source code in src/coremusic/audio/core.py
reset()
¶
Reset the converter to its initial state
Source code in src/coremusic/audio/core.py
dispose()
¶
Dispose of the audio converter
__enter__()
¶
__exit__(exc_type, exc_val, exc_tb)
¶
MIDIClient Class¶
coremusic.midi.MIDIClient
¶
Bases: CoreAudioObject
MIDI client for managing MIDI operations
Source code in src/coremusic/midi/core.py
_name = name
instance-attribute
¶
_ports = []
instance-attribute
¶
name
property
¶
__init__(name)
¶
Source code in src/coremusic/midi/core.py
__repr__()
¶
create_input_port(name)
¶
Create a MIDI input port
Source code in src/coremusic/midi/core.py
create_output_port(name)
¶
Create a MIDI output port
Source code in src/coremusic/midi/core.py
dispose()
¶
Dispose of the MIDI client and all its ports
Source code in src/coremusic/midi/core.py
AudioClock Class¶
coremusic.audio.AudioClock
¶
Bases: CoreAudioObject
High-level CoreAudioClock for audio/MIDI synchronization and timing
AudioClock provides synchronization services for audio and MIDI applications, supporting multiple time formats and playback control.
Supported time formats:
- Host time (mach_absolute_time)
- Audio samples
- Musical beats
- Seconds
- SMPTE timecode
Example::
# Create and control a clock
with AudioClock() as clock:
clock.play_rate = 1.0
clock.start()
# Get current time in different formats
seconds = clock.get_time_seconds()
beats = clock.get_time_beats()
print(f"Position: {seconds:.2f}s ({beats:.2f} beats)")
clock.stop()
Example with tempo and speed control::
clock = AudioClock()
clock.play_rate = 0.5 # Half speed
clock.start()
# ... use clock for synchronization
clock.stop()
clock.dispose()
Source code in src/coremusic/audio/clock.py
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 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 | |
_is_created = False
instance-attribute
¶
_is_running = False
instance-attribute
¶
is_running
property
¶
Check if the clock is currently running
play_rate
property
writable
¶
Get or set the playback rate (1.0 = normal speed)
__init__()
¶
create()
¶
Create the underlying clock object
Returns:
| Type | Description |
|---|---|
'AudioClock'
|
Self for method chaining |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If clock creation fails |
Source code in src/coremusic/audio/clock.py
start()
¶
Start the clock advancing on its timeline
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If clock is not created or start fails |
Source code in src/coremusic/audio/clock.py
stop()
¶
Stop the clock
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If stop fails |
Source code in src/coremusic/audio/clock.py
get_current_time(time_format)
¶
Get current time in specified format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_format
|
int
|
Time format constant from ClockTimeFormat |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with 'format' and 'value' keys |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If getting time fails |
Source code in src/coremusic/audio/clock.py
get_time_seconds()
¶
Get current time in seconds
Returns:
| Type | Description |
|---|---|
float
|
Current time in seconds |
get_time_beats()
¶
Get current time in musical beats
Returns:
| Type | Description |
|---|---|
float
|
Current time in beats |
get_time_samples()
¶
Get current time in audio samples
Returns:
| Type | Description |
|---|---|
float
|
Current time in samples |
get_time_host()
¶
Get current time as host time
Returns:
| Type | Description |
|---|---|
int
|
Current host time (mach_absolute_time) |
get_smpte_time()
¶
Get current time as SMPTE timecode
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Dictionary with SMPTE time components: |
dict[str, int]
|
|
dict[str, int]
|
|
dict[str, int]
|
|
Source code in src/coremusic/audio/clock.py
dispose()
¶
Dispose the clock and free resources
Source code in src/coremusic/audio/clock.py
__enter__()
¶
__exit__(exc_type, exc_val, exc_tb)
¶
__repr__()
¶
Source code in src/coremusic/audio/clock.py
ClockTimeFormat¶
coremusic.audio.ClockTimeFormat
¶
Time format constants for CoreAudioClock
Source code in src/coremusic/audio/clock.py
HOST_TIME = capi.get_ca_clock_time_format_host_time()
class-attribute
instance-attribute
¶
SAMPLES = capi.get_ca_clock_time_format_samples()
class-attribute
instance-attribute
¶
BEATS = capi.get_ca_clock_time_format_beats()
class-attribute
instance-attribute
¶
SECONDS = capi.get_ca_clock_time_format_seconds()
class-attribute
instance-attribute
¶
SMPTE_TIME = capi.get_ca_clock_time_format_smpte_time()
class-attribute
instance-attribute
¶
AudioEffectsChain Class¶
coremusic.audio.AudioEffectsChain
¶
High-level audio effects chain using AUGraph.
Provides a simplified API for creating and managing chains of AudioUnit effects for real-time audio processing.
Example
from coremusic.audio.utilities import AudioEffectsChain
# Create an effects chain
chain = AudioEffectsChain()
# Add effects (example effect types)
reverb_node = chain.add_effect('aumu', 'rvb2', 'appl') # Reverb
eq_node = chain.add_effect('aufx', 'eqal', 'appl') # EQ
# Connect to output
output_node = chain.add_output()
chain.connect(reverb_node, eq_node)
chain.connect(eq_node, output_node)
# Initialize and start
chain.initialize()
chain.start()
# ... process audio ...
# Cleanup
chain.stop()
chain.dispose()
Source code in src/coremusic/audio/utilities.py
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 | |
_graph = AUGraph()
instance-attribute
¶
_nodes = {}
instance-attribute
¶
graph
property
¶
Get the underlying AUGraph
is_open
property
¶
Check if the graph is open
is_initialized
property
¶
Check if the graph is initialized
is_running
property
¶
Check if the graph is running
node_count
property
¶
Get the number of nodes in the chain
__init__()
¶
add_effect(effect_type, effect_subtype, manufacturer='appl', flags=0)
¶
Add an audio effect to the chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
effect_type
|
str
|
AudioUnit type (4-char code or string) |
required |
effect_subtype
|
str
|
AudioUnit subtype (4-char code or string) |
required |
manufacturer
|
str
|
Manufacturer code (default: 'appl' for Apple) |
'appl'
|
flags
|
int
|
Component flags (default: 0) |
0
|
Returns:
| Type | Description |
|---|---|
int
|
Node ID for this effect |
Example
Source code in src/coremusic/audio/utilities.py
add_effect_by_name(name)
¶
Add an audio effect to the chain by name.
This searches for an AudioUnit matching the given name and adds it to the chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
AudioUnit name (e.g., 'AUDelay', 'Reverb', 'AUGraphicEQ') |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
Node ID for this effect, or None if not found |
Example
Source code in src/coremusic/audio/utilities.py
add_output(output_type='auou', output_subtype='def ')
¶
Add an output node to the chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_type
|
str
|
Output type (default: 'auou' for AudioUnit Output) |
'auou'
|
output_subtype
|
str
|
Output subtype (default: 'def ' for default output) |
'def '
|
Returns:
| Type | Description |
|---|---|
int
|
Node ID for the output |
Example
Source code in src/coremusic/audio/utilities.py
connect(source_node, dest_node, source_bus=0, dest_bus=0)
¶
Connect two nodes in the effects chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_node
|
int
|
Source node ID |
required |
dest_node
|
int
|
Destination node ID |
required |
source_bus
|
int
|
Source output bus (default: 0) |
0
|
dest_bus
|
int
|
Destination input bus (default: 0) |
0
|
Example
Source code in src/coremusic/audio/utilities.py
disconnect(dest_node, dest_bus=0)
¶
Disconnect a node input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dest_node
|
int
|
Destination node ID |
required |
dest_bus
|
int
|
Destination input bus (default: 0) |
0
|
Source code in src/coremusic/audio/utilities.py
remove_node(node_id)
¶
Remove a node from the chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
int
|
Node ID to remove |
required |
open()
¶
Open the graph (opens all AudioUnits).
Returns:
| Type | Description |
|---|---|
'AudioEffectsChain'
|
Self for method chaining |
initialize()
¶
Initialize the graph (prepares for rendering).
Returns:
| Type | Description |
|---|---|
'AudioEffectsChain'
|
Self for method chaining |
start()
¶
stop()
¶
dispose()
¶
__enter__()
¶
Functional API¶
Low-level C-style functions are available through the coremusic.capi module
for advanced use cases requiring direct access to CoreAudio frameworks.
Note
The object-oriented API is recommended for most use cases. The functional
API in coremusic.capi provides low-level access when needed.
For direct access to low-level functions:
import coremusic.capi as capi
# Low-level audio file operations
file_id = capi.audio_file_open_url("audio.wav")
# ... operations ...
capi.audio_file_close(file_id)
# Low-level clock operations
clock_id = capi.ca_clock_new()
capi.ca_clock_start(clock_id)
# ... operations ...
capi.ca_clock_dispose(clock_id)
Error Handling¶
coremusic provides exception classes for different CoreAudio subsystems:
coremusic.exceptions.CoreAudioError
¶
Bases: Exception
Base exception for CoreAudio errors
Source code in src/coremusic/exceptions.py
status_code = status_code
instance-attribute
¶
__init__(message, status_code=0)
¶
from_os_status(status, operation='')
classmethod
¶
Create exception from OSStatus code with human-readable error message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
int
|
OSStatus error code |
required |
operation
|
str
|
Description of failed operation (e.g., "open audio file") |
''
|
Returns:
| Type | Description |
|---|---|
'CoreAudioError'
|
CoreAudioError with formatted message including error name and suggestion |
Source code in src/coremusic/exceptions.py
coremusic.exceptions.AudioFileError
¶
coremusic.exceptions.AudioUnitError
¶
coremusic.exceptions.AudioQueueError
¶
Bases: CoreAudioError
Exception for AudioQueue operations
Source code in src/coremusic/exceptions.py
coremusic.exceptions.AudioConverterError
¶
Bases: CoreAudioError
Exception for AudioConverter operations
Source code in src/coremusic/exceptions.py
coremusic.exceptions.MIDIError
¶
coremusic.exceptions.MusicPlayerError
¶
Bases: CoreAudioError
Exception for MusicPlayer operations
Source code in src/coremusic/exceptions.py
coremusic.exceptions.AudioDeviceError
¶
Bases: CoreAudioError
Exception for AudioDevice operations
Source code in src/coremusic/exceptions.py
coremusic.exceptions.AUGraphError
¶
Utility Functions¶
Utility functions are available through coremusic.capi for FourCC conversion
and other low-level operations: