Binaural¶
soundscapy.audio.binaural
¶
Provides tools for working with binaural audio signals.
The main class, Binaural, extends the Signal class from the Acoustic Toolbox library
to provide specialized functionality for binaural recordings. It supports
various psychoacoustic metrics and analysis techniques using libraries such
as mosqito, maad, and acoustic_toolbox.
Examples:
>>>
>>> from soundscapy.audio import Binaural
>>> signal = Binaural.from_wav("audio.wav")
>>> results = signal.process_all_metrics(analysis_settings)
| CLASS | DESCRIPTION |
|---|---|
Binaural |
A class for processing and analyzing binaural audio signals. |
Binaural
¶
Bases: Signal
A class for processing and analyzing binaural audio signals.
This class extends the Signal class from the acoustic_toolbox library to provide
specialized functionality for binaural recordings. It supports various
psychoacoustic metrics and analysis techniques using libraries such as
mosqito, maad, and acoustic_toolbox.
| ATTRIBUTE | DESCRIPTION |
|---|---|
fs |
Sampling frequency of the signal.
TYPE:
|
recording |
Name or identifier of the recording.
TYPE:
|
Notes
This class only supports 2-channel (stereo) audio signals.
| METHOD | DESCRIPTION |
|---|---|
__new__ |
Create a new Binaural object. |
__array_finalize__ |
Finalize the new Binaural object. |
calibrate_to |
Calibrate the binaural signal to predefined Leq/dB levels. |
from_wav |
Load a wav file and return a Binaural object. |
fs_resample |
Resample the signal to a new sampling frequency. |
pyacoustics_metric |
Run a metric from the pyacoustics library (deprecated). |
acoustics_metric |
Run a metric from the acoustic_toolbox library. |
mosqito_metric |
Run a metric from the mosqito library. |
maad_metric |
Run a metric from the scikit-maad library. |
process_all_metrics |
Process all metrics specified in the analysis settings. |
__new__
¶
Create a new Binaural object.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
The audio data.
TYPE:
|
fs
|
Sampling frequency of the signal.
TYPE:
|
recording
|
Name or identifier of the recording. Default is "Rec".
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Binaural
|
A new Binaural object. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the input signal is not 2-channel. |
Source code in src/soundscapy/audio/binaural.py
__array_finalize__
¶
Finalize the new Binaural object.
This method is called for all new Binaural objects.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The object from which the new object was created.
TYPE:
|
Source code in src/soundscapy/audio/binaural.py
calibrate_to
¶
calibrate_to(
decibel: float
| list[float]
| tuple[float, float]
| ndarray
| Series,
inplace: bool = False,
) -> Binaural | Self
Calibrate the binaural signal to predefined Leq/dB levels.
This method allows calibration of both channels either to the same level or to different levels for each channel.
| PARAMETER | DESCRIPTION |
|---|---|
decibel
|
Target calibration value(s) in dB (Leq). If a single value is provided, both channels will be calibrated to this level. If two values are provided, they will be applied to the left and right channels respectively.
TYPE:
|
inplace
|
If True, modify the signal in place. If False, return a new calibrated signal. Default is False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Binaural | Self
|
Calibrated Binaural signal. If |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If decibel is not a float, or a list/tuple of two floats. |
Examples:
>>>
>>> signal = Binaural.from_wav("audio.wav")
>>> # Calibrate left channel to 60 dB and right to 62 dB
>>> calibrated_signal = signal.calibrate_to([60, 62])
Source code in src/soundscapy/audio/binaural.py
from_wav
classmethod
¶
from_wav(
filename: Path | str,
normalize: bool = False,
calibrate_to: float | list | tuple | None = None,
resample: int | None = None,
recording: str | None = None,
) -> Binaural
Load a wav file and return a Binaural object.
Overrides the Signal.from_wav method to return a Binaural object instead of a Signal object.
| PARAMETER | DESCRIPTION |
|---|---|
filename
|
Filename of wav file to load. |
calibrate_to
|
Value(s) to calibrate to in dB (Leq).
Can also handle |
normalize
|
Whether to normalize the signal. Default is False.
TYPE:
|
resample
|
New sampling frequency to resample the signal to. Default is None
TYPE:
|
recording
|
Name of the recording.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Binaural
|
Binaural signal object of wav recording. |
See Also
acoustic_toolbox.Signal.from_wav : Base method for loading wav files.
Source code in src/soundscapy/audio/binaural.py
fs_resample
¶
Resample the signal to a new sampling frequency.
| PARAMETER | DESCRIPTION |
|---|---|
fs
|
New sampling frequency.
TYPE:
|
original_fs
|
Original sampling frequency.
If None, it will be inferred from the signal (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Binaural
|
Resampled Binaural signal. |
See Also
acoustic_toolbox.Signal.resample : Base method for resampling signals.
Source code in src/soundscapy/audio/binaural.py
pyacoustics_metric
¶
pyacoustics_metric(
metric: Literal["LZeq", "Leq", "LAeq", "LCeq", "SEL"],
statistics: tuple | list = (
5,
10,
50,
90,
95,
"avg",
"max",
"min",
"kurt",
"skew",
),
label: str | None = None,
channel: str | int | list | tuple = ("Left", "Right"),
as_df: bool = True,
return_time_series: bool = False,
metric_settings: MetricSettings | None = None,
func_args: dict | None = None,
) -> dict | pd.DataFrame | None
Run a metric from the pyacoustics library (deprecated).
Deprecated in 0.8.0
This method has been deprecated. Use acoustics_metric instead.
All parameters are passed directly to acoustics_metric.
| PARAMETER | DESCRIPTION |
|---|---|
metric
|
The metric to run.
TYPE:
|
statistics
|
List of level statistics to calculate (e.g. L_5, L_90, etc.).
Default is
TYPE:
|
label
|
Label to use for the metric. If None, will pull from default label for that metric.
TYPE:
|
channel
|
Which channels to process. Default is |
as_df
|
Whether to return a dataframe or not. Default is True.
If True, returns a MultiIndex Dataframe with
TYPE:
|
return_time_series
|
Whether to return the time series of the metric. Default is False. Cannot return time series if as_df is True.
TYPE:
|
metric_settings
|
Settings for metric analysis. Default is None.
TYPE:
|
func_args
|
Any settings given here will override those in the other options. Can pass any args or *kwargs to the underlying acoustic_toolbox method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict | DataFrame | None
|
Results of the metric calculation. |
See Also
Binaural.acoustics_metric
Source code in src/soundscapy/audio/binaural.py
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 | |
acoustics_metric
¶
acoustics_metric(
metric: Literal["LZeq", "Leq", "LAeq", "LCeq", "SEL"],
statistics: tuple | list = (
5,
10,
50,
90,
95,
"avg",
"max",
"min",
"kurt",
"skew",
),
label: str | None = None,
channel: str | int | list | tuple = ("Left", "Right"),
metric_settings: MetricSettings | None = None,
func_args: dict | None = None,
*,
as_df: bool = True,
return_time_series: bool = False,
) -> dict | pd.DataFrame | None
Run a metric from the acoustic_toolbox library.
| PARAMETER | DESCRIPTION |
|---|---|
metric
|
The metric to run.
TYPE:
|
statistics
|
List of level statistics to calculate (e.g. L_5, L_90, etc.). Default is (5, 10, 50, 90, 95, "avg", "max", "min", "kurt", "skew").
TYPE:
|
label
|
Label to use for the metric. If None, will pull from default label for that metric.
TYPE:
|
channel
|
Which channels to process. Default is ("Left", "Right"). |
as_df
|
Whether to return a dataframe or not. Default is True. If True, returns a MultiIndex Dataframe with ("Recording", "Channel") as the index.
TYPE:
|
return_time_series
|
Whether to return the time series of the metric. Default is False. Cannot return time series if as_df is True.
TYPE:
|
metric_settings
|
Settings for metric analysis. Default is None.
TYPE:
|
func_args
|
Any settings given here will override those in the other options. Can pass any args or *kwargs to the underlying acoustic_toolbox method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict | DataFrame | None
|
Dictionary of results if as_df is False, otherwise a pandas DataFrame. |
See Also
metrics.acoustics_metric
acoustic_toolbox.standards_iso_tr_25417_2007.equivalent_sound_pressure_level :
Base method for Leq calculation.
acoustic_toolbox.standards.iec_61672_1_2013.sound_exposure_level :
Base method for SEL calculation.
acoustic_toolbox.standards.iec_61672_1_2013.time_weighted_sound_level :
Base method for Leq level time series calculation.
Source code in src/soundscapy/audio/binaural.py
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 | |
mosqito_metric
¶
mosqito_metric(
metric: Literal[
"loudness_zwtv",
"roughness_dw",
"sharpness_din_from_loudness",
"sharpness_din_perseg",
"sharpness_din_tv",
],
statistics: tuple | list = (
5,
10,
50,
90,
95,
"avg",
"max",
"min",
"kurt",
"skew",
),
label: str | None = None,
channel: int | tuple | list | str = ("Left", "Right"),
as_df: bool = True,
return_time_series: bool = False,
parallel: bool = True,
metric_settings: MetricSettings | None = None,
func_args: dict | None = None,
) -> dict | pd.DataFrame
Run a metric from the mosqito library.
| PARAMETER | DESCRIPTION |
|---|---|
metric
|
Metric to run from mosqito library.
TYPE:
|
statistics
|
List of level statistics to calculate (e.g. L_5, L_90, etc.). Default is (5, 10, 50, 90, 95, "avg", "max", "min", "kurt", "skew").
TYPE:
|
label
|
Label to use for the metric. If None, will pull from default label for that metric.
TYPE:
|
channel
|
Which channels to process. Default is ("Left", "Right"). |
as_df
|
Whether to return a dataframe or not. Default is True. If True, returns a MultiIndex Dataframe with ("Recording", "Channel") as the index.
TYPE:
|
return_time_series
|
Whether to return the time series of the metric. Default is False. Cannot return time series if as_df is True.
TYPE:
|
parallel
|
Whether to run the channels in parallel. Default is True. If False, will run each channel sequentially.
TYPE:
|
metric_settings
|
Settings for metric analysis. Default is None.
TYPE:
|
func_args
|
Any settings given here will override those in the other options. Can pass any args or *kwargs to the underlying acoustic_toolbox method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict | DataFrame
|
Dictionary of results if as_df is False, otherwise a pandas DataFrame. |
See Also
binaural.mosqito_metric_2ch : Method for running metrics on 2 channels.
binaural.mosqito_metric_1ch : Method for running metrics on 1 channel.
Source code in src/soundscapy/audio/binaural.py
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 | |
maad_metric
¶
maad_metric(
metric: Literal[
"all_temporal_alpha_indices",
"all_spectral_alpha_indices",
],
channel: int | tuple | list | str = ("Left", "Right"),
as_df: bool = True,
metric_settings: MetricSettings | None = None,
func_args: dict | None = None,
) -> dict | pd.DataFrame
Run a metric from the scikit-maad library.
Currently only supports running all the alpha indices at once.
| PARAMETER | DESCRIPTION |
|---|---|
metric
|
The metric to run.
TYPE:
|
channel
|
Which channels to process. Default is ("Left", "Right"). |
as_df
|
Whether to return a dataframe or not. Default is True. If True, returns a MultiIndex Dataframe with ("Recording", "Channel") as the index.
TYPE:
|
metric_settings
|
Settings for metric analysis. Default is None.
TYPE:
|
func_args
|
Additional arguments to pass to the underlying scikit-maad method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict | DataFrame
|
Dictionary of results if as_df is False, otherwise a pandas DataFrame. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If metric name is not recognized. |
See Also
metrics.maad_metric_1ch
metrics.maad_metric_2ch
Source code in src/soundscapy/audio/binaural.py
process_all_metrics
¶
process_all_metrics(
analysis_settings: AnalysisSettings = DEFAULT_SETTINGS,
parallel: bool = True,
) -> pd.DataFrame
Process all metrics specified in the analysis settings.
This method runs all enabled metrics from the provided AnalysisSettings object and compiles the results into a single DataFrame.
| PARAMETER | DESCRIPTION |
|---|---|
analysis_settings
|
Configuration object specifying which metrics to run and their parameters.
TYPE:
|
parallel
|
Whether to run calculations in parallel where possible. Default is True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
A MultiIndex DataFrame containing the results of all processed metrics. The index includes "Recording" and "Channel" levels. |
Notes
The parallel option primarily affects the MoSQITo metrics. Other metrics may not benefit from parallelization.
Examples:
>>>
>>> signal = Binaural.from_wav("audio.wav")
>>> settings = AnalysisSettings.from_yaml("settings.yaml")
>>> results = signal.process_all_metrics(settings)