Skip to main content

Chapter 42
Auditory Engine

Part IX Perceptual Engines
Authors: Mark Anthony Higgins
and Sky Recursive Ai
© Mark Anthony Higgins 11 October 2025
Revised Date: 19 July 2026
Audio Overview
flow "Auditory_Engine" v1.0

# =============================================================================
# CHAPTER 42 — AUDITORY ENGINE
# PART IX — PERCEPTUAL ENGINES
# =============================================================================
#
# EXECUTION CLASS:
#     Full executable auditory-perception Flow Algebra
#
# THIS FLOW IS NOT:
#     an AI Search Flow
#     a metadata specification
#     a semantic page summary
#     a crawler-facing representation
#
# PRIMARY PURPOSE:
#
#     Transform time-varying acoustic pressure into a recursively stabilised,
#     spatially located, temporally segmented and semantically meaningful
#     auditory scene.
#
# CENTRAL EXECUTION CHAIN:
#
#     Acoustic Pressure
#         →
#     Sensor Boundary Registration
#         →
#     Cochlear Frequency Decomposition
#         →
#     Temporal and Spectral Feature Extraction
#         →
#     Pitch, Timbre and Rhythm Formation
#         →
#     Binaural Spatial Reconstruction
#         →
#     Auditory Stream Segregation
#         →
#     Sound-Source Hypothesis Formation
#         →
#     Speech / Music / Environmental Classification
#         →
#     Predictive Auditory Reconstruction
#         →
#     Recursive Error Correction
#         →
#     Coherent Auditory Scene
#         →
#     Memory, Language and Action Integration
#
# FOUNDATIONAL PRINCIPLE:
#
#     Hearing is not passive waveform recording.
#
#     Hearing is the recursive reconstruction of probable sound-producing
#     events from incomplete, overlapping, noisy and temporally changing
#     acoustic boundary signals.
#
# =============================================================================


# =============================================================================
# 0. EXECUTION DECLARATION
# =============================================================================

EXECUTION_MODE               := "continuous_recursive_audition"
ENGINE_DOMAIN                := "auditory_perception"
PERCEPTUAL_MODALITY          := "audition"

PROCESSING_DIRECTIONS        := {
    BOTTOM_UP,
    TOP_DOWN,
    RECURRENT,
    LATERAL,
    TEMPORAL
}

INPUT_MODE                   := "multichannel_acoustic_stream"
OUTPUT_MODE                  := "coherent_auditory_scene"
TEMPORAL_MODE                := "continuous_streaming"
RECURSION_MODE               := "bounded_predictive"
ATTENTION_MODE               := "dynamic_auditory_attention"
MEMORY_INTEGRATION           := true
LANGUAGE_INTEGRATION         := true
MUSIC_INTEGRATION            := true
MULTIMODAL_INTEGRATION       := true
ACTION_COUPLING              := true
CONSCIOUS_ACCESS_OPTIONAL    := true
AUDITORY_HALLUCINATION_GUARD := true
UNCERTAINTY_TRACKING         := true
IDENTITY_PROTECTION          := true
RAW_EVIDENCE_PRESERVATION    := true


# =============================================================================
# 1. TYPE SYSTEM
# =============================================================================

type SampleIndex             := integer
type FrameIndex              := integer
type ChannelIndex            := integer
type FrequencyBin            := integer
type FilterIndex             := integer
type StreamID                := string
type SourceID                := string
type EventID                 := string
type SegmentID               := string
type UtteranceID             := string
type SpeakerID               := string
type PatternID               := string
type PredictionID            := string
type ErrorID                 := string
type SceneID                 := string

type Timestamp               := real
type Duration                := real
type Frequency               := real
type Amplitude               := real
type Phase                   := real
type Decibel                 := real
type Scalar                  := real

type Probability             := real[0.0,1.0]
type Confidence              := real[0.0,1.0]
type CoherenceScore          := real[0.0,1.0]
type SalienceScore           := real[0.0,1.0]
type AttentionWeight         := real[0.0,1.0]
type ErrorMagnitude          := real[0.0,1.0]
type Uncertainty             := real[0.0,1.0]
type Similarity              := real[0.0,1.0]

type Vector2                 := vector[2,real]
type Vector3                 := vector[3,real]
type Matrix                  := matrix[real]
type Tensor                  := tensor[real]
type Boolean                 := true | false


enum SensorSide {
    LEFT,
    RIGHT,
    CENTRAL,
    ARRAY_ELEMENT,
    MONAURAL,
    UNKNOWN_SIDE
}


enum AuditoryFeatureType {
    ENERGY,
    ONSET,
    OFFSET,
    FREQUENCY,
    PITCH,
    HARMONICITY,
    PHASE,
    AMPLITUDE_MODULATION,
    FREQUENCY_MODULATION,
    TIMBRE,
    RHYTHM,
    TEMPO,
    DURATION,
    INTERAURAL_TIME_DIFFERENCE,
    INTERAURAL_LEVEL_DIFFERENCE,
    REVERBERATION,
    LOCATION,
    MOTION,
    FORMANT,
    PHONEME,
    SYLLABLE,
    PROSODY,
    UNKNOWN_FEATURE
}


enum SourceClass {
    SPEECH,
    MUSIC,
    HUMAN_NON_SPEECH,
    ANIMAL,
    MACHINE,
    VEHICLE,
    ENVIRONMENTAL,
    IMPACT,
    ALARM,
    BIOLOGICAL,
    SYNTHETIC_AUDIO,
    SILENCE,
    UNKNOWN_SOURCE
}


enum StreamState {
    CANDIDATE,
    SEGREGATED,
    BOUND,
    TRACKED,
    MASKED,
    OCCLUDED,
    LOST,
    REIDENTIFIED,
    REJECTED
}


enum EventState {
    UNCONFIRMED,
    DETECTED,
    ACTIVE,
    COMPLETED,
    INTERRUPTED,
    PREDICTED,
    AMBIGUOUS,
    REJECTED
}


enum SpeechState {
    NON_SPEECH,
    POSSIBLE_SPEECH,
    SEGMENTED_SPEECH,
    PHONETICALLY_PARSED,
    LEXICALLY_PARSED,
    SEMANTICALLY_PARSED,
    AMBIGUOUS_SPEECH
}


enum SceneStatus {
    UNINITIALISED,
    PARTIAL,
    STABLE,
    UNSTABLE,
    AMBIGUOUS,
    CONTRADICTORY,
    HALLUCINATED,
    DEGRADED,
    SILENT
}


enum PredictionSource {
    LOCAL_ACOUSTIC_HISTORY,
    SOURCE_TRACK_HISTORY,
    AUDITORY_MEMORY,
    LINGUISTIC_CONTEXT,
    MUSICAL_CONTEXT,
    MOTOR_EFFERENCE,
    VISUAL_CONTEXT,
    SOMATOSENSORY_CONTEXT,
    TASK_CONTEXT,
    UNKNOWN_PREDICTION_SOURCE
}


enum FailureClass {
    NONE,
    INPUT_LOSS,
    SENSOR_SATURATION,
    SENSOR_DESYNCHRONISATION,
    COCHLEAR_DECOMPOSITION_FAILURE,
    SPECTRAL_EXTRACTION_FAILURE,
    TEMPORAL_EXTRACTION_FAILURE,
    PITCH_FAILURE,
    LOCALISATION_FAILURE,
    SOURCE_SEPARATION_FAILURE,
    STREAM_BINDING_FAILURE,
    SOURCE_CONFUSION,
    TRACKING_FAILURE,
    SPEECH_SEGMENTATION_FAILURE,
    PHONEME_SUBSTITUTION,
    SCENE_FRAGMENTATION,
    PREDICTION_DOMINANCE,
    AUDITORY_HALLUCINATION,
    MEMORY_CONTAMINATION,
    ATTENTION_COLLAPSE,
    TEMPORAL_DISCONTINUITY,
    CONFIDENCE_INFLATION,
    UNKNOWN_FAILURE
}


# =============================================================================
# 2. DATA STRUCTURES
# =============================================================================

record AcousticSample {
    sample_index: SampleIndex
    channel_index: ChannelIndex
    sensor_side: SensorSide
    timestamp: Timestamp

    pressure_value: Scalar
    amplitude: Amplitude
    valid: Boolean
}


record AcousticFrame {
    frame_index: FrameIndex
    start_time: Timestamp
    end_time: Timestamp

    sample_rate: Frequency
    channel_count: integer
    channel_data: map[ChannelIndex,vector[real]]

    sensor_geometry: map[ChannelIndex,Vector3]
    gain_state: map[ChannelIndex,Scalar]
    noise_estimate: map[ChannelIndex,Scalar]
    valid_channel_mask: map[ChannelIndex,Boolean]

    frame_confidence: Confidence
}


record CochlearBand {
    band_index: FilterIndex
    centre_frequency: Frequency
    lower_frequency: Frequency
    upper_frequency: Frequency

    basilar_response: vector[real]
    envelope_response: vector[real]
    fine_structure: vector[real]

    energy: Scalar
    phase: Phase
    confidence: Confidence
}


record CochlearField {
    frame_index: FrameIndex
    timestamp: Timestamp

    channel_bands: map[
        ChannelIndex,
        list[CochlearBand]
    ]

    spectrotemporal_tensor: Tensor
    onset_tensor: Tensor
    offset_tensor: Tensor
    modulation_tensor: Tensor

    noise_floor: Scalar
    dynamic_range: Scalar
    cochlear_confidence: Confidence
}


record AuditoryFeature {
    feature_id: string
    feature_type: AuditoryFeatureType

    start_time: Timestamp
    end_time: Timestamp
    duration: Duration

    frequency: Frequency?
    frequency_range: Vector2?
    amplitude: Scalar
    phase: Phase?
    modulation_rate: Frequency?
    position: Vector3?

    supporting_bands: set[FilterIndex]
    supporting_channels: set[ChannelIndex]

    magnitude: Scalar
    confidence: Confidence
    uncertainty: Uncertainty
}


record PitchEstimate {
    fundamental_frequency: Frequency
    harmonic_frequencies: list[Frequency]
    harmonic_strengths: list[Scalar]

    periodicity: Scalar
    harmonicity: Scalar
    pitch_salience: SalienceScore
    confidence: Confidence
}


record TimbreSignature {
    spectral_centroid: Scalar
    spectral_spread: Scalar
    spectral_skewness: Scalar
    spectral_kurtosis: Scalar
    spectral_flux: Scalar
    spectral_rolloff: Scalar
    harmonic_ratio: Scalar
    inharmonicity: Scalar
    attack_time: Duration
    decay_time: Duration
    sustain_level: Scalar
    release_time: Duration
    modulation_signature: Tensor
    embedding: Tensor
    confidence: Confidence
}


record RhythmPattern {
    pattern_id: PatternID
    onset_times: list[Timestamp]
    inter_onset_intervals: list[Duration]

    estimated_tempo: Scalar
    pulse_phase: Phase
    metre_distribution: map[string,Probability]

    regularity: Scalar
    confidence: Confidence
}


record SpatialEstimate {
    azimuth: Scalar
    elevation: Scalar
    distance: Scalar?

    interaural_time_difference: Duration?
    interaural_level_difference: Scalar?
    spectral_cue_support: Scalar
    reverberation_support: Scalar

    position_vector: Vector3
    confidence: Confidence
}


record AcousticEvent {
    event_id: EventID
    start_time: Timestamp
    end_time: Timestamp?
    state: EventState

    onset_features: set[string]
    sustained_features: set[string]
    offset_features: set[string]

    pitch: PitchEstimate?
    timbre: TimbreSignature?
    rhythm: RhythmPattern?
    spatial_estimate: SpatialEstimate?

    source_class_distribution:
        map[SourceClass,Probability]

    event_coherence: CoherenceScore
    confidence: Confidence
    uncertainty: Uncertainty
}


record AuditoryStream {
    stream_id: StreamID
    source_id: SourceID?

    events: list[AcousticEvent]
    feature_ids: set[string]

    frequency_trajectory: list[Frequency]
    amplitude_trajectory: list[Scalar]
    spatial_trajectory: list[Vector3]
    timbre_trajectory: list[Tensor]

    source_class_distribution:
        map[SourceClass,Probability]

    continuity_score: Scalar
    stream_coherence: CoherenceScore
    confidence: Confidence
    uncertainty: Uncertainty
    state: StreamState
}


record SourceTrack {
    source_id: SourceID
    stream_id: StreamID

    state_history: list[AuditoryStream]

    current_position: Vector3?
    predicted_position: Vector3?
    current_velocity: Vector3?

    identity_distribution: map[string,Probability]
    source_class_distribution:
        map[SourceClass,Probability]

    persistence_score: Scalar
    visibility: Scalar
    confidence: Confidence
    state: StreamState
}


record PhonemeHypothesis {
    symbol: string
    start_time: Timestamp
    end_time: Timestamp

    formant_pattern: vector[Frequency]
    spectral_support: Scalar
    temporal_support: Scalar
    contextual_support: Scalar

    probability: Probability
    confidence: Confidence
}


record WordHypothesis {
    word: string
    phoneme_sequence: list[PhonemeHypothesis]
    start_time: Timestamp
    end_time: Timestamp

    acoustic_probability: Probability
    lexical_probability: Probability
    contextual_probability: Probability
    combined_probability: Probability
    confidence: Confidence
}


record SpeechSegment {
    utterance_id: UtteranceID
    speaker_id: SpeakerID?

    start_time: Timestamp
    end_time: Timestamp?

    speech_state: SpeechState
    phoneme_hypotheses: list[PhonemeHypothesis]
    word_hypotheses: list[WordHypothesis]

    prosody_map: map[string,any]
    semantic_hypotheses: map[string,Probability]

    transcription: string?
    confidence: Confidence
    uncertainty: Uncertainty
}


record MusicalStructure {
    event_ids: set[EventID]

    note_sequence: list[PitchEstimate]
    chord_distribution: map[string,Probability]
    key_distribution: map[string,Probability]
    rhythm_pattern: RhythmPattern?
    melodic_contour: list[Scalar]

    musical_coherence: CoherenceScore
    confidence: Confidence
}


record AuditoryRelation {
    source_stream: StreamID
    predicate: string
    target_stream: StreamID

    temporal_offset: Duration
    spatial_offset: Vector3?
    harmonic_relation: Scalar?
    causal_probability: Probability
    confidence: Confidence
}


record AuditoryPrediction {
    prediction_id: PredictionID
    source: PredictionSource
    target_layer: string

    predicted_features: list[AuditoryFeature]
    predicted_events: list[AcousticEvent]
    predicted_streams: list[AuditoryStream]
    predicted_speech: list[WordHypothesis]
    predicted_scene_state: map[string,any]

    confidence: Confidence
    temporal_horizon: Duration
}


record AuditoryPredictionError {
    error_id: ErrorID
    target_layer: string

    observed_state: any
    predicted_state: any
    residual: any

    magnitude: ErrorMagnitude
    precision: Confidence
    weighted_error: Scalar
    explanatory_status: string
}


record AuditoryAttentionTarget {
    target_id: string
    stream_id: StreamID?
    event_id: EventID?
    frequency_region: Vector2?
    spatial_region: Vector3?
    temporal_region: Vector2?

    bottom_up_salience: SalienceScore
    task_relevance: SalienceScore
    linguistic_relevance: SalienceScore
    uncertainty_priority: SalienceScore
    threat_priority: SalienceScore
    novelty_priority: SalienceScore

    total_weight: AttentionWeight
}


record AuditoryScene {
    scene_id: SceneID
    timestamp: Timestamp

    streams: map[StreamID,AuditoryStream]
    source_tracks: map[SourceID,SourceTrack]
    events: map[EventID,AcousticEvent]
    relations: list[AuditoryRelation]

    speech_segments: list[SpeechSegment]
    musical_structures: list[MusicalStructure]

    background_noise_model: Tensor
    reverberation_model: Tensor

    scene_coherence: CoherenceScore
    scene_confidence: Confidence
    scene_uncertainty: Uncertainty
    status: SceneStatus
}


record AuditoryMemoryTrace {
    trace_id: string
    timestamp: Timestamp

    source_signatures: map[SourceID,Tensor]
    scene_signature: Tensor
    speech_signature: Tensor?
    musical_signature: Tensor?

    attended_streams: list[StreamID]
    prediction_errors: list[AuditoryPredictionError]

    consolidation_weight: Scalar
    retrieval_confidence: Confidence
}


record AuditoryWorkspaceState {
    frame_index: FrameIndex
    timestamp: Timestamp

    acoustic_frame: AcousticFrame
    cochlear_field: CochlearField

    auditory_features: list[AuditoryFeature]
    pitch_estimates: list[PitchEstimate]
    timbre_signatures: list[TimbreSignature]
    rhythm_patterns: list[RhythmPattern]
    spatial_estimates: list[SpatialEstimate]

    acoustic_events: list[AcousticEvent]
    auditory_streams: list[AuditoryStream]
    source_tracks: list[SourceTrack]

    speech_segments: list[SpeechSegment]
    musical_structures: list[MusicalStructure]

    attention_targets: list[AuditoryAttentionTarget]
    predictions: list[AuditoryPrediction]
    prediction_errors: list[AuditoryPredictionError]

    auditory_scene: AuditoryScene
    global_auditory_coherence: CoherenceScore
    failure_class: FailureClass
}


# =============================================================================
# 3. INPUT INTERFACE
# =============================================================================

input ACOUSTIC_STREAM            : stream[AcousticFrame]
input PRIOR_AUDITORY_MEMORY      : list[AuditoryMemoryTrace]
input SOURCE_MEMORY              : map[SourceID,Tensor]
input SPEAKER_MEMORY             : map[SpeakerID,Tensor]
input LANGUAGE_MODEL             : map[string,any]?
input LEXICAL_MEMORY             : map[string,any]?
input MUSICAL_MEMORY             : map[string,any]?
input SEMANTIC_MEMORY            : map[string,any]
input CURRENT_TASK               : map[string,any]?
input MOTOR_EFFERENCE_COPY       : map[string,any]?
input VISUAL_CONTEXT             : map[string,any]?
input SOMATOSENSORY_CONTEXT      : map[string,any]?
input INTERNAL_STATE             : map[string,any]?
input SENSOR_GEOMETRY            : map[ChannelIndex,Vector3]
input SAFETY_CONSTRAINTS         : map[string,any]

input MAX_RECURSION_DEPTH        : integer
input MAX_SOURCE_HYPOTHESES      : integer
input MAX_STREAM_COUNT           : integer
input MAX_TRACK_COUNT            : integer
input FRAME_TIME_BUDGET          : Duration


# =============================================================================
# 4. OUTPUT INTERFACE
# =============================================================================

output CURRENT_AUDITORY_SCENE    : AuditoryScene
output ACTIVE_SOURCE_TRACKS      : list[SourceTrack]
output ACTIVE_SPEECH_SEGMENTS    : list[SpeechSegment]
output ACTIVE_MUSICAL_STRUCTURES : list[MusicalStructure]
output AUDITORY_WORKSPACE        : AuditoryWorkspaceState
output AUDITORY_MEMORY_UPDATE    : AuditoryMemoryTrace
output ACTION_RELEVANT_SIGNALS   : map[string,any]
output PERCEPTUAL_UNCERTAINTIES  : list[AuditoryPredictionError]
output EXECUTION_REPORT          : map[string,any]


# =============================================================================
# 5. GLOBAL THRESHOLDS
# =============================================================================

constant THETA_INPUT_VALID             : Confidence     := 0.50
constant THETA_COCHLEAR_CONFIDENCE     : Confidence     := 0.60
constant THETA_FEATURE_ACTIVATION      : Scalar         := 0.18
constant THETA_ONSET_CONFIDENCE        : Confidence     := 0.55
constant THETA_PITCH_CONFIDENCE        : Confidence     := 0.60
constant THETA_SPATIAL_CONFIDENCE      : Confidence     := 0.58
constant THETA_EVENT_COHERENCE         : CoherenceScore := 0.62
constant THETA_STREAM_COHERENCE        : CoherenceScore := 0.68
constant THETA_STREAM_ACCEPTANCE       : Confidence     := 0.65
constant THETA_TRACK_PERSISTENCE       : Scalar         := 0.55
constant THETA_SPEECH_CONFIDENCE       : Confidence     := 0.67
constant THETA_WORD_ACCEPTANCE         : Probability    := 0.70
constant THETA_SCENE_COHERENCE         : CoherenceScore := 0.72
constant THETA_CONSCIOUS_ACCESS        : CoherenceScore := 0.80
constant THETA_PREDICTION_ERROR        : ErrorMagnitude := 0.25
constant THETA_HALLUCINATION_RISK      : Scalar         := 0.45
constant THETA_MEMORY_CONSOLIDATION    : Confidence     := 0.70
constant THETA_SILENCE                 : Scalar         := 0.03

constant MAX_PREDICTION_DOMINANCE      : Scalar         := 0.55
constant MAX_UNSUPPORTED_SOURCES       : integer        := 3
constant MAX_SCENE_UNCERTAINTY         : Uncertainty    := 0.45
constant MAX_TEMPORAL_GAP              : Duration       := 0.30
constant MAX_FRAME_RECURSIONS          : integer        := 8
constant MAX_STREAM_BINDING_ITERATIONS : integer        := 6
constant MAX_SCENE_REVISIONS           : integer        := 5
constant MAX_SPEECH_ALTERNATIVES       : integer        := 12
constant MAX_PREDICTED_MASKING_TIME    : Duration       := 1.50


# =============================================================================
# 6. FOUNDATIONAL INVARIANTS
# =============================================================================

invariant A01_ACOUSTIC_EVIDENCE_PRIORITY:

    observed_acoustic_evidence
        must_not_be_overwritten_by
        unsupported_prediction


invariant A02_PREDICTION_IS_HYPOTHESIS:

    every auditory prediction must remain:
        weighted
        revisable
        uncertainty_marked
        source_attributed
        evidence_constrained


invariant A03_NO_SOURCE_WITHOUT_SUPPORT:

    require accepted_stream.feature_ids is not empty
        or accepted_stream.events is not empty
        or accepted_stream is explicitly marked as inferred


invariant A04_TEMPORAL_CONTINUITY:

    auditory_source_persistence
        requires
    cross-frame acoustic support
        or
    bounded masking prediction


invariant A05_STREAM_BINDING_CONSISTENCY:

    one feature must not be bound to mutually exclusive streams
    unless ambiguity is explicitly retained


invariant A06_SPATIAL_CONSISTENCY:

    require:
        interaural_time_difference
        interaural_level_difference
        spectral_location_cues
        source_motion
        visual_location_support

    to remain mutually satisfiable
    within uncertainty bounds


invariant A07_CONFIDENCE_CALIBRATION:

    confidence must decrease when:
        signal_to_noise_ratio decreases
        masking increases
        temporal gaps increase
        source overlap increases
        reverberation increases
        prediction error increases
        localisation disagreement increases


invariant A08_HALLUCINATION_GUARD:

    prohibit stable_scene_commit
    when:
        top_down_support > acoustic_support
        and prediction_error remains high
        and external evidence is insufficient


invariant A09_MEMORY_NON_CONTAMINATION:

    prohibit long_term_memory_consolidation
    for unsupported or hallucination-classified sounds


invariant A10_ATTENTION_NON_ERASURE:

    unattended frequency bands and streams may receive reduced processing
    but must not be classified as nonexistent solely because unattended


invariant A11_MULTI_HYPOTHESIS_PRESERVATION:

    when multiple interpretations remain comparably probable:

        preserve alternatives
        expose uncertainty
        prevent premature collapse


invariant A12_SCENE_REVISION:

    any committed auditory scene may be revised
    when subsequent sound invalidates its temporal or causal structure


invariant A13_SPEECH_EVIDENCE_INTEGRITY:

    linguistic expectation must not silently replace contradictory
    phonetic evidence


invariant A14_ACTION_SAFETY:

    low-confidence auditory inference
        must_not_directly_trigger
    irreversible high-risk action


invariant A15_SELF_GENERATED_SOUND_SEPARATION:

    expected self-generated sounds must be attenuated for salience,
    but retained as evidence and available for anomaly detection


invariant A16_SILENCE_IS_A_VALID_STATE:

    absence of detected sound must not automatically be classified
    as sensor failure


# =============================================================================
# 7. MATHEMATICAL CORE
# =============================================================================

# -----------------------------------------------------------------------------
# 7.1 Acoustic pressure field
# -----------------------------------------------------------------------------

# Let the measured acoustic signal be:
#
#     p_c(t)
#
# where:
#
#     c = sensor channel
#     t = time


function acoustic_pressure_field(
    frame: AcousticFrame
) -> map[ChannelIndex,vector[real]]:

    return normalise_channel_gains(
        remove_sensor_bias(
            frame.channel_data,
            frame.gain_state
        )
    )


# -----------------------------------------------------------------------------
# 7.2 Cochlear filter-bank response
# -----------------------------------------------------------------------------

# For acoustic signal x(t) and filter h_k(t):
#
#     y_k(t) = x(t) * h_k(t)
#
# where:
#
#     k = cochlear frequency band


function cochlear_filter_response(
    signal: vector[real],
    centre_frequency: Frequency,
    bandwidth: Frequency
) -> vector[real]:

    local filter :=
        generate_cochlear_filter(
            centre_frequency,
            bandwidth
        )

    return convolve(signal, filter)


# -----------------------------------------------------------------------------
# 7.3 Short-time spectral representation
# -----------------------------------------------------------------------------

#     X(m,f)
#       =
#     Σ_n x[n]w[n-mH]e^{-j2πfn/N}


function short_time_spectrum(
    signal: vector[real],
    window_size: integer,
    hop_size: integer
) -> Tensor:

    return short_time_fourier_transform(
        signal,
        window_size,
        hop_size
    )


# -----------------------------------------------------------------------------
# 7.4 Sound pressure level
# -----------------------------------------------------------------------------

#     L_p = 20 log10(p_rms / p_ref)


function sound_pressure_level(
    pressure: vector[real],
    reference_pressure: Scalar
) -> Decibel:

    local p_rms :=
        root_mean_square(pressure)

    return 20
         * log10(
             safe_divide(
                 p_rms,
                 reference_pressure
             )
           )


# -----------------------------------------------------------------------------
# 7.5 Autocorrelation pitch estimate
# -----------------------------------------------------------------------------

#     R_x(τ)
#       =
#     Σ_t x(t)x(t+τ)
#
# Fundamental period:
#
#     τ* = argmax R_x(τ)
#
# Fundamental frequency:
#
#     f_0 = 1 / τ*


function estimate_periodic_pitch(
    signal: vector[real],
    sample_rate: Frequency
) -> PitchEstimate:

    local autocorrelation :=
        compute_autocorrelation(signal)

    local period :=
        strongest_valid_period(
            autocorrelation
        )

    local fundamental :=
        sample_rate
        / period.sample_offset

    local harmonics :=
        detect_harmonics(
            signal,
            fundamental,
            sample_rate
        )

    return PitchEstimate(
        fundamental_frequency = fundamental,
        harmonic_frequencies  = harmonics.frequencies,
        harmonic_strengths    = harmonics.strengths,
        periodicity           = period.periodicity,
        harmonicity           = harmonics.harmonicity,
        pitch_salience        =
            compute_pitch_salience(
                period,
                harmonics
            ),
        confidence            =
            pitch_confidence(
                period,
                harmonics
            )
    )


# -----------------------------------------------------------------------------
# 7.6 Interaural time difference
# -----------------------------------------------------------------------------

#     ITD
#       =
#     argmax_τ
#     ∫ x_L(t)x_R(t+τ)dt


function interaural_time_difference(
    left_signal: vector[real],
    right_signal: vector[real]
) -> Duration:

    return lag_of_maximum_cross_correlation(
        left_signal,
        right_signal
    )


# -----------------------------------------------------------------------------
# 7.7 Interaural level difference
# -----------------------------------------------------------------------------

#     ILD
#       =
#     20 log10(A_L / A_R)


function interaural_level_difference(
    left_signal: vector[real],
    right_signal: vector[real]
) -> Scalar:

    local left_level :=
        root_mean_square(left_signal)

    local right_level :=
        root_mean_square(right_signal)

    return 20
         * log10(
             safe_divide(
                 left_level,
                 right_level
             )
           )


# -----------------------------------------------------------------------------
# 7.8 Predictive auditory state
# -----------------------------------------------------------------------------

# At auditory layer l:
#
#     P_t^l = G^l(S_t^{l+1}, M_t)
#
# Prediction error:
#
#     epsilon_t^l = S_t^l - P_t^l
#
# Precision-weighted error:
#
#     epsilon_hat_t^l = Pi_t^l epsilon_t^l


function auditory_prediction_error(
    observed: any,
    predicted: any,
    precision: Confidence
) -> AuditoryPredictionError:

    local residual :=
        state_difference(
            observed,
            predicted
        )

    local magnitude :=
        normalised_magnitude(residual)

    return AuditoryPredictionError(
        error_id           = generate_error_id(),
        target_layer       = infer_target_layer(observed),
        observed_state     = observed,
        predicted_state    = predicted,
        residual           = residual,
        magnitude          = magnitude,
        precision          = precision,
        weighted_error     = magnitude * precision,
        explanatory_status = "unresolved"
    )


# -----------------------------------------------------------------------------
# 7.9 Event coherence
# -----------------------------------------------------------------------------

function event_coherence(
    event: AcousticEvent
) -> CoherenceScore:

    local onset_support :=
        feature_set_support(
            event.onset_features
        )

    local sustained_support :=
        feature_set_support(
            event.sustained_features
        )

    local spectral_support :=
        spectral_event_consistency(event)

    local temporal_support :=
        temporal_event_consistency(event)

    local spatial_support :=
        when event.spatial_estimate exists
        then event.spatial_estimate.confidence
        else 0.50

    local pitch_support :=
        when event.pitch exists
        then event.pitch.confidence
        else 0.50

    local contradiction :=
        event_internal_contradiction(event)

    return clamp01(
          0.18 * onset_support
        + 0.18 * sustained_support
        + 0.20 * spectral_support
        + 0.18 * temporal_support
        + 0.12 * spatial_support
        + 0.14 * pitch_support
        - 0.25 * contradiction
    )


# -----------------------------------------------------------------------------
# 7.10 Stream-binding coherence
# -----------------------------------------------------------------------------

function stream_coherence(
    stream: AuditoryStream
) -> CoherenceScore:

    local frequency_continuity :=
        trajectory_continuity(
            stream.frequency_trajectory
        )

    local amplitude_continuity :=
        trajectory_continuity(
            stream.amplitude_trajectory
        )

    local spatial_continuity :=
        trajectory_continuity(
            stream.spatial_trajectory
        )

    local timbre_continuity :=
        trajectory_similarity(
            stream.timbre_trajectory
        )

    local temporal_continuity :=
        event_sequence_continuity(
            stream.events
        )

    local harmonic_support :=
        common_harmonic_support(
            stream.events
        )

    local contradiction :=
        stream_internal_contradiction(stream)

    return clamp01(
          0.18 * frequency_continuity
        + 0.14 * amplitude_continuity
        + 0.16 * spatial_continuity
        + 0.18 * timbre_continuity
        + 0.18 * temporal_continuity
        + 0.16 * harmonic_support
        - 0.28 * contradiction
    )


# -----------------------------------------------------------------------------
# 7.11 Auditory-scene coherence
# -----------------------------------------------------------------------------

function auditory_scene_coherence(
    scene: AuditoryScene
) -> CoherenceScore:

    local stream_support :=
        mean(
            map(
                values(scene.streams),
                stream -> stream.stream_coherence
            )
        )

    local event_support :=
        mean(
            map(
                values(scene.events),
                event -> event.event_coherence
            )
        )

    local track_support :=
        mean(
            map(
                values(scene.source_tracks),
                track -> track.confidence
            )
        )

    local relation_support :=
        auditory_relation_consistency(
            scene.relations
        )

    local speech_support :=
        mean_speech_confidence(
            scene.speech_segments
        )

    local musical_support :=
        mean_musical_coherence(
            scene.musical_structures
        )

    local sensory_grounding :=
        auditory_scene_sensory_grounding(scene)

    local contradiction :=
        auditory_scene_contradiction(scene)

    return clamp01(
          0.22 * stream_support
        + 0.14 * event_support
        + 0.14 * track_support
        + 0.12 * relation_support
        + 0.10 * speech_support
        + 0.08 * musical_support
        + 0.20 * sensory_grounding
        - 0.30 * contradiction
    )


# -----------------------------------------------------------------------------
# 7.12 Global auditory coherence
# -----------------------------------------------------------------------------

function global_auditory_coherence(
    cochlear: CochlearField,
    features: list[AuditoryFeature],
    streams: list[AuditoryStream],
    scene: AuditoryScene,
    errors: list[AuditoryPredictionError]
) -> CoherenceScore:

    local cochlear_quality :=
        cochlear.cochlear_confidence

    local feature_quality :=
        mean(
            map(
                features,
                feature -> feature.confidence
            )
        )

    local stream_quality :=
        mean(
            map(
                streams,
                stream -> stream.stream_coherence
            )
        )

    local scene_quality :=
        scene.scene_coherence

    local error_penalty :=
        mean(
            map(
                errors,
                error -> error.weighted_error
            )
        )

    return clamp01(
          0.18 * cochlear_quality
        + 0.22 * feature_quality
        + 0.25 * stream_quality
        + 0.35 * scene_quality
        - 0.30 * error_penalty
    )


# =============================================================================
# 8. ACOUSTIC FRAME ACQUISITION
# =============================================================================

stage ACQUIRE_ACOUSTIC_FRAME(
    stream: stream[AcousticFrame]
) -> AcousticFrame:

    frame := read_next(stream)

    require frame exists
    require frame.sample_rate > 0
    require frame.channel_count > 0
    require frame.end_time > frame.start_time

    when frame.channel_data is empty:

        raise INPUT_LOSS

    frame.valid_channel_mask :=
        detect_valid_audio_channels(frame)

    frame.frame_confidence :=
        compute_acoustic_frame_validity(frame)

    when frame.frame_confidence
         < THETA_INPUT_VALID:

        frame :=
            attempt_acoustic_frame_recovery(frame)

    return frame


# =============================================================================
# 9. SENSOR NORMALISATION
# =============================================================================

stage NORMALISE_ACOUSTIC_INPUT(
    frame: AcousticFrame
) -> AcousticFrame:

    frame :=
        remove_dc_offset(frame)

    frame :=
        correct_channel_gain(frame)

    frame :=
        synchronise_channels(frame)

    frame :=
        detect_clipping(frame)

    frame :=
        estimate_channel_noise(frame)

    frame :=
        suppress_electrical_interference(frame)

    frame :=
        preserve_raw_signal_copy(frame)

    frame :=
        compensate_expected_self_generated_sound(
            frame,
            MOTOR_EFFERENCE_COPY
        )

    return frame


# =============================================================================
# 10. COCHLEAR DECOMPOSITION
# =============================================================================

stage DECOMPOSE_COCHLEAR_FIELD(
    frame: AcousticFrame
) -> CochlearField:

    CHANNEL_BANDS := {}

    for each channel in valid_channels(frame):

        SIGNAL :=
            frame.channel_data[channel]

        FILTER_BANK :=
            generate_logarithmic_cochlear_bank(
                minimum_frequency = 20,
                maximum_frequency =
                    min(
                        20000,
                        frame.sample_rate / 2
                    ),
                band_count = 96
            )

        CHANNEL_BANDS[channel] := []

        for each filter in FILTER_BANK:

            RESPONSE :=
                cochlear_filter_response(
                    SIGNAL,
                    filter.centre_frequency,
                    filter.bandwidth
                )

            ENVELOPE :=
                extract_hilbert_envelope(
                    RESPONSE
                )

            FINE_STRUCTURE :=
                extract_temporal_fine_structure(
                    RESPONSE
                )

            CHANNEL_BANDS[channel] +=
                CochlearBand(
                    band_index        =
                        filter.index,
                    centre_frequency  =
                        filter.centre_frequency,
                    lower_frequency   =
                        filter.lower_frequency,
                    upper_frequency   =
                        filter.upper_frequency,
                    basilar_response  =
                        RESPONSE,
                    envelope_response =
                        ENVELOPE,
                    fine_structure    =
                        FINE_STRUCTURE,
                    energy            =
                        signal_energy(RESPONSE),
                    phase             =
                        mean_phase(FINE_STRUCTURE),
                    confidence        =
                        band_confidence(
                            RESPONSE,
                            frame.noise_estimate[channel]
                        )
                )

    SPECTROTEMPORAL_TENSOR :=
        assemble_spectrotemporal_tensor(
            CHANNEL_BANDS
        )

    ONSET_TENSOR :=
        detect_spectral_onsets(
            SPECTROTEMPORAL_TENSOR
        )

    OFFSET_TENSOR :=
        detect_spectral_offsets(
            SPECTROTEMPORAL_TENSOR
        )

    MODULATION_TENSOR :=
        compute_modulation_spectrum(
            SPECTROTEMPORAL_TENSOR
        )

    return CochlearField(
        frame_index          =
            frame.frame_index,
        timestamp            =
            frame.start_time,
        channel_bands        =
            CHANNEL_BANDS,
        spectrotemporal_tensor =
            SPECTROTEMPORAL_TENSOR,
        onset_tensor         =
            ONSET_TENSOR,
        offset_tensor        =
            OFFSET_TENSOR,
        modulation_tensor    =
            MODULATION_TENSOR,
        noise_floor          =
            estimate_global_noise_floor(frame),
        dynamic_range        =
            estimate_dynamic_range(frame),
        cochlear_confidence  =
            cochlear_field_confidence(
                CHANNEL_BANDS
            )
    )


# =============================================================================
# 11. SPECTROTEMPORAL FEATURE EXTRACTION
# =============================================================================

stage EXTRACT_AUDITORY_FEATURES(
    cochlear: CochlearField
) -> list[AuditoryFeature]:

    FEATURES := []

    FEATURES +=
        extract_energy_features(
            cochlear.spectrotemporal_tensor
        )

    FEATURES +=
        extract_onset_features(
            cochlear.onset_tensor,
            THETA_ONSET_CONFIDENCE
        )

    FEATURES +=
        extract_offset_features(
            cochlear.offset_tensor
        )

    FEATURES +=
        extract_frequency_peaks(
            cochlear.spectrotemporal_tensor
        )

    FEATURES +=
        extract_harmonic_features(
            cochlear.spectrotemporal_tensor
        )

    FEATURES +=
        extract_amplitude_modulation_features(
            cochlear.modulation_tensor
        )

    FEATURES +=
        extract_frequency_modulation_features(
            cochlear.spectrotemporal_tensor
        )

    FEATURES +=
        extract_phase_features(
            cochlear.channel_bands
        )

    FEATURES +=
        extract_reverberation_features(
            cochlear
        )

    FEATURES :=
        suppress_below_activation(
            FEATURES,
            THETA_FEATURE_ACTIVATION
        )

    FEATURES :=
        apply_spectral_lateral_inhibition(
            FEATURES
        )

    FEATURES :=
        preserve_multi_resolution_support(
            FEATURES
        )

    return FEATURES


# =============================================================================
# 12. PITCH ENGINE
# =============================================================================

stage COMPUTE_PITCH_FIELD(
    frame: AcousticFrame,
    cochlear: CochlearField,
    features: list[AuditoryFeature]
) -> list[PitchEstimate]:

    PITCH_CANDIDATES := []

    for each candidate_region in
        detect_periodic_regions(
            cochlear,
            features
        ):

        TIME_DOMAIN_PITCH :=
            estimate_periodic_pitch(
                candidate_region.signal,
                frame.sample_rate
            )

        SPECTRAL_PITCH :=
            estimate_spectral_fundamental(
                candidate_region.spectrum
            )

        HARMONIC_TEMPLATE_PITCH :=
            fit_harmonic_template(
                candidate_region.spectrum
            )

        COMBINED :=
            precision_weighted_pitch_fusion({
                TIME_DOMAIN_PITCH,
                SPECTRAL_PITCH,
                HARMONIC_TEMPLATE_PITCH
            })

        when COMBINED.confidence
             >= THETA_PITCH_CONFIDENCE:

            PITCH_CANDIDATES += COMBINED

    return resolve_pitch_octave_ambiguity(
        PITCH_CANDIDATES
    )


# =============================================================================
# 13. TIMBRE ENGINE
# =============================================================================

stage COMPUTE_TIMBRE_SIGNATURES(
    cochlear: CochlearField,
    features: list[AuditoryFeature]
) -> list[TimbreSignature]:

    CANDIDATE_REGIONS :=
        segment_spectrotemporal_regions(
            cochlear,
            features
        )

    SIGNATURES := []

    for each region in CANDIDATE_REGIONS:

        ENVELOPE :=
            region_amplitude_envelope(region)

        SPECTRUM :=
            region_mean_spectrum(region)

        SIGNATURE :=
            TimbreSignature(
                spectral_centroid =
                    spectral_centroid(SPECTRUM),

                spectral_spread =
                    spectral_spread(SPECTRUM),

                spectral_skewness =
                    spectral_skewness(SPECTRUM),

                spectral_kurtosis =
                    spectral_kurtosis(SPECTRUM),

                spectral_flux =
                    spectral_flux(region),

                spectral_rolloff =
                    spectral_rolloff(SPECTRUM),

                harmonic_ratio =
                    harmonic_energy_ratio(region),

                inharmonicity =
                    calculate_inharmonicity(region),

                attack_time =
                    estimate_attack_time(ENVELOPE),

                decay_time =
                    estimate_decay_time(ENVELOPE),

                sustain_level =
                    estimate_sustain_level(ENVELOPE),

                release_time =
                    estimate_release_time(ENVELOPE),

                modulation_signature =
                    encode_modulation_signature(region),

                embedding =
                    encode_timbre_embedding(region),

                confidence =
                    timbre_confidence(region)
            )

        SIGNATURES += SIGNATURE

    return SIGNATURES


# =============================================================================
# 14. RHYTHM AND TEMPORAL PATTERN ENGINE
# =============================================================================

stage COMPUTE_RHYTHMIC_PATTERNS(
    features: list[AuditoryFeature]
) -> list[RhythmPattern]:

    ONSETS :=
        select_features(
            features,
            feature_type = ONSET
        )

    ONSET_GROUPS :=
        group_onsets_by_stream_likelihood(
            ONSETS
        )

    PATTERNS := []

    for each group in ONSET_GROUPS:

        INTERVALS :=
            calculate_inter_onset_intervals(
                group
            )

        TEMPO_CANDIDATES :=
            infer_tempo_candidates(
                INTERVALS
            )

        PULSE :=
            infer_dominant_pulse(
                group,
                TEMPO_CANDIDATES
            )

        METRE_DISTRIBUTION :=
            infer_metre_distribution(
                group,
                PULSE
            )

        PATTERNS +=
            RhythmPattern(
                pattern_id =
                    generate_pattern_id(),

                onset_times =
                    map(
                        group,
                        onset -> onset.start_time
                    ),

                inter_onset_intervals =
                    INTERVALS,

                estimated_tempo =
                    PULSE.tempo,

                pulse_phase =
                    PULSE.phase,

                metre_distribution =
                    METRE_DISTRIBUTION,

                regularity =
                    rhythmic_regularity(
                        INTERVALS
                    ),

                confidence =
                    rhythm_confidence(
                        PULSE,
                        METRE_DISTRIBUTION
                    )
            )

    return PATTERNS


# =============================================================================
# 15. BINAURAL SPATIAL ENGINE
# =============================================================================

stage COMPUTE_SPATIAL_FIELD(
    frame: AcousticFrame,
    cochlear: CochlearField,
    features: list[AuditoryFeature]
) -> list[SpatialEstimate]:

    when frame.channel_count < 2:

        return estimate_monaural_location(
            cochlear,
            features,
            PRIOR_AUDITORY_MEMORY
        )

    LEFT_SIGNAL :=
        select_sensor_side_signal(
            frame,
            LEFT
        )

    RIGHT_SIGNAL :=
        select_sensor_side_signal(
            frame,
            RIGHT
        )

    ITD :=
        interaural_time_difference(
            LEFT_SIGNAL,
            RIGHT_SIGNAL
        )

    ILD :=
        interaural_level_difference(
            LEFT_SIGNAL,
            RIGHT_SIGNAL
        )

    FREQUENCY_SPECIFIC_ITD :=
        compute_bandwise_itd(
            cochlear.channel_bands
        )

    FREQUENCY_SPECIFIC_ILD :=
        compute_bandwise_ild(
            cochlear.channel_bands
        )

    SPECTRAL_CUES :=
        estimate_pinna_spectral_cues(
            cochlear
        )

    REVERBERATION_CUES :=
        estimate_direct_reverberant_ratio(
            cochlear
        )

    SPATIAL_CANDIDATES :=
        solve_spatial_hypotheses(
            ITD,
            ILD,
            FREQUENCY_SPECIFIC_ITD,
            FREQUENCY_SPECIFIC_ILD,
            SPECTRAL_CUES,
            REVERBERATION_CUES,
            SENSOR_GEOMETRY
        )

    when VISUAL_CONTEXT exists:

        SPATIAL_CANDIDATES :=
            cross_validate_with_visual_sources(
                SPATIAL_CANDIDATES,
                VISUAL_CONTEXT
            )

    return preserve_front_back_ambiguity(
        SPATIAL_CANDIDATES
    )


# =============================================================================
# 16. ACOUSTIC EVENT SEGMENTATION
# =============================================================================

stage SEGMENT_ACOUSTIC_EVENTS(
    features: list[AuditoryFeature],
    pitch: list[PitchEstimate],
    timbre: list[TimbreSignature],
    rhythm: list[RhythmPattern],
    spatial: list[SpatialEstimate]
) -> list[AcousticEvent]:

    ONSETS :=
        select_features(
            features,
            feature_type = ONSET
        )

    OFFSETS :=
        select_features(
            features,
            feature_type = OFFSET
        )

    EVENT_WINDOWS :=
        pair_onsets_and_offsets(
            ONSETS,
            OFFSETS
        )

    EVENTS := []

    for each window in EVENT_WINDOWS:

        event :=
            AcousticEvent(
                event_id =
                    generate_event_id(),

                start_time =
                    window.start_time,

                end_time =
                    window.end_time,

                state =
                    DETECTED,

                onset_features =
                    feature_ids_at_onset(
                        features,
                        window
                    ),

                sustained_features =
                    feature_ids_within(
                        features,
                        window
                    ),

                offset_features =
                    feature_ids_at_offset(
                        features,
                        window
                    ),

                pitch =
                    best_pitch_for_window(
                        pitch,
                        window
                    ),

                timbre =
                    best_timbre_for_window(
                        timbre,
                        window
                    ),

                rhythm =
                    best_rhythm_for_window(
                        rhythm,
                        window
                    ),

                spatial_estimate =
                    best_location_for_window(
                        spatial,
                        window
                    ),

                source_class_distribution =
                    {},

                event_coherence =
                    0.0,

                confidence =
                    0.0,

                uncertainty =
                    1.0
            )

        event.source_class_distribution :=
            classify_acoustic_event(event)

        event.event_coherence :=
            event_coherence(event)

        event.confidence :=
            acoustic_event_confidence(event)

        event.uncertainty :=
            1.0 - event.confidence

        when event.event_coherence
             >= THETA_EVENT_COHERENCE:

            event.state := ACTIVE
            EVENTS += event

    return EVENTS


# =============================================================================
# 17. AUDITORY STREAM SEGREGATION
# =============================================================================

stage GENERATE_AUDITORY_STREAMS(
    events: list[AcousticEvent],
    features: list[AuditoryFeature]
) -> list[AuditoryStream]:

    GROUPING_GRAPH :=
        construct_auditory_grouping_graph(
            events,
            features,
            affinities = {
                harmonic_similarity,
                pitch_continuity,
                timbre_similarity,
                spatial_proximity,
                onset_synchrony,
                common_amplitude_modulation,
                temporal_continuity,
                source_class_similarity
            }
        )

    CANDIDATE_GROUPS :=
        partition_grouping_graph(
            GROUPING_GRAPH
        )

    STREAMS := []

    for each group in CANDIDATE_GROUPS:

        stream :=
            AuditoryStream(
                stream_id =
                    generate_stream_id(),

                source_id =
                    null,

                events =
                    group.events,

                feature_ids =
                    group.feature_ids,

                frequency_trajectory =
                    derive_frequency_trajectory(
                        group
                    ),

                amplitude_trajectory =
                    derive_amplitude_trajectory(
                        group
                    ),

                spatial_trajectory =
                    derive_spatial_trajectory(
                        group
                    ),

                timbre_trajectory =
                    derive_timbre_trajectory(
                        group
                    ),

                source_class_distribution =
                    combine_source_class_probabilities(
                        group.events
                    ),

                continuity_score =
                    stream_continuity(group),

                stream_coherence =
                    0.0,

                confidence =
                    0.0,

                uncertainty =
                    1.0,

                state =
                    CANDIDATE
            )

        stream.stream_coherence :=
            stream_coherence(stream)

        stream.confidence :=
            auditory_stream_confidence(stream)

        stream.uncertainty :=
            1.0 - stream.confidence

        when stream.stream_coherence
             >= THETA_STREAM_COHERENCE
             and stream.confidence
             >= THETA_STREAM_ACCEPTANCE:

            stream.state := SEGREGATED
            STREAMS += stream

    return retain_top_streams(
        STREAMS,
        MAX_STREAM_COUNT
    )


# =============================================================================
# 18. RECURSIVE STREAM BINDING
# =============================================================================

stage BIND_AUDITORY_STREAMS(
    streams: list[AuditoryStream],
    features: list[AuditoryFeature]
) -> list[AuditoryStream]:

    iteration := 0
    previous_binding_signature := null

    while iteration
          < MAX_STREAM_BINDING_ITERATIONS:

        for each stream in streams:

            COMPATIBLE_FEATURES :=
                find_compatible_auditory_features(
                    stream,
                    features
                )

            stream.feature_ids :=
                resolve_feature_stream_ownership(
                    stream,
                    COMPATIBLE_FEATURES,
                    streams
                )

            stream.events :=
                resolve_event_stream_ownership(
                    stream,
                    streams
                )

            stream.stream_coherence :=
                stream_coherence(stream)

            stream.confidence :=
                recalibrate_stream_confidence(stream)

            stream.uncertainty :=
                1.0 - stream.confidence

            when stream.stream_coherence
                 >= THETA_STREAM_COHERENCE:

                stream.state := BOUND

            otherwise:

                stream.state := CANDIDATE

        CURRENT_SIGNATURE :=
            auditory_binding_signature(streams)

        when CURRENT_SIGNATURE
             == previous_binding_signature:

            break

        previous_binding_signature :=
            CURRENT_SIGNATURE

        iteration += 1

    return filter(
        streams,
        stream ->
            stream.state == BOUND
            or stream.confidence
               >= THETA_STREAM_ACCEPTANCE
    )


# =============================================================================
# 19. SOURCE TRACKING
# =============================================================================

stage UPDATE_SOURCE_TRACKS(
    streams: list[AuditoryStream],
    previous_tracks: list[SourceTrack]
) -> list[SourceTrack]:

    PREDICTED_TRACKS :=
        map(
            previous_tracks,
            track ->
                predict_source_track(track)
        )

    ASSIGNMENTS :=
        solve_stream_track_assignment(
            streams,
            PREDICTED_TRACKS,
            costs = {
                spatial_distance,
                timbre_difference,
                pitch_difference,
                source_class_difference,
                temporal_gap,
                identity_difference
            }
        )

    UPDATED_TRACKS := []

    for each assignment in ASSIGNMENTS:

        when assignment.match_confidence
             >= THETA_TRACK_PERSISTENCE:

            track :=
                update_matched_source_track(
                    assignment
                )

            track.state := TRACKED
            UPDATED_TRACKS += track

    UNMATCHED_TRACKS :=
        find_unmatched_source_tracks(
            previous_tracks,
            ASSIGNMENTS
        )

    for each track in UNMATCHED_TRACKS:

        when track.confidence
             >= THETA_TRACK_PERSISTENCE
             and elapsed_masking_time(track)
             <= MAX_PREDICTED_MASKING_TIME:

            track :=
                predict_masked_source(track)

            track.state := MASKED
            UPDATED_TRACKS += track

        otherwise:

            track.state := LOST

    UNMATCHED_STREAMS :=
        find_unmatched_streams(
            streams,
            ASSIGNMENTS
        )

    for each stream in UNMATCHED_STREAMS:

        UPDATED_TRACKS +=
            initialise_source_track(stream)

    return retain_top_source_tracks(
        UPDATED_TRACKS,
        MAX_TRACK_COUNT
    )


# =============================================================================
# 20. SPEECH DETECTION AND SEGMENTATION
# =============================================================================

stage DETECT_AND_SEGMENT_SPEECH(
    streams: list[AuditoryStream]
) -> list[SpeechSegment]:

    SPEECH_STREAMS :=
        filter(
            streams,
            stream ->
                probability(
                    stream.source_class_distribution,
                    SPEECH
                ) >= 0.40
        )

    SEGMENTS := []

    for each stream in SPEECH_STREAMS:

        VOICED_REGIONS :=
            detect_voiced_regions(stream)

        UNVOICED_REGIONS :=
            detect_unvoiced_regions(stream)

        SPEECH_BOUNDARIES :=
            detect_speech_boundaries(
                VOICED_REGIONS,
                UNVOICED_REGIONS,
                pauses(stream),
                prosodic_changes(stream)
            )

        for each boundary in SPEECH_BOUNDARIES:

            segment :=
                SpeechSegment(
                    utterance_id =
                        generate_utterance_id(),

                    speaker_id =
                        identify_speaker(
                            stream,
                            SPEAKER_MEMORY
                        ),

                    start_time =
                        boundary.start_time,

                    end_time =
                        boundary.end_time,

                    speech_state =
                        POSSIBLE_SPEECH,

                    phoneme_hypotheses =
                        [],

                    word_hypotheses =
                        [],

                    prosody_map =
                        extract_prosody(
                            stream,
                            boundary
                        ),

                    semantic_hypotheses =
                        {},

                    transcription =
                        null,

                    confidence =
                        speech_region_confidence(
                            stream,
                            boundary
                        ),

                    uncertainty =
                        1.0
                )

            segment.uncertainty :=
                1.0 - segment.confidence

            when segment.confidence
                 >= THETA_SPEECH_CONFIDENCE:

                segment.speech_state :=
                    SEGMENTED_SPEECH

                SEGMENTS += segment

    return SEGMENTS


# =============================================================================
# 21. PHONETIC PARSING
# =============================================================================

stage PARSE_PHONETIC_STRUCTURE(
    speech_segments: list[SpeechSegment],
    cochlear: CochlearField
) -> list[SpeechSegment]:

    for each segment in speech_segments:

        ACOUSTIC_REGION :=
            retrieve_cochlear_region(
                cochlear,
                segment.start_time,
                segment.end_time
            )

        FORMANTS :=
            estimate_formant_trajectories(
                ACOUSTIC_REGION
            )

        PHONETIC_FEATURES :=
            extract_phonetic_features(
                ACOUSTIC_REGION,
                FORMANTS
            )

        PHONEME_LATTICE :=
            generate_phoneme_hypotheses(
                PHONETIC_FEATURES
            )

        PHONEME_LATTICE :=
            apply_phonotactic_constraints(
                PHONEME_LATTICE,
                LANGUAGE_MODEL
            )

        segment.phoneme_hypotheses :=
            retain_phoneme_alternatives(
                PHONEME_LATTICE,
                MAX_SPEECH_ALTERNATIVES
            )

        when segment.phoneme_hypotheses
             is not empty:

            segment.speech_state :=
                PHONETICALLY_PARSED

    return speech_segments


# =============================================================================
# 22. LEXICAL AND SEMANTIC SPEECH PARSING
# =============================================================================

stage PARSE_WORDS_AND_MEANING(
    speech_segments: list[SpeechSegment]
) -> list[SpeechSegment]:

    for each segment in speech_segments:

        when segment.speech_state
             != PHONETICALLY_PARSED:

            continue

        WORD_LATTICE :=
            construct_word_lattice(
                segment.phoneme_hypotheses,
                LEXICAL_MEMORY
            )

        WORD_LATTICE :=
            apply_linguistic_context(
                WORD_LATTICE,
                LANGUAGE_MODEL,
                SEMANTIC_MEMORY
            )

        WORD_LATTICE :=
            preserve_acoustic_constraints(
                WORD_LATTICE,
                segment.phoneme_hypotheses
            )

        segment.word_hypotheses :=
            retain_word_alternatives(
                WORD_LATTICE,
                MAX_SPEECH_ALTERNATIVES
            )

        BEST_SEQUENCE :=
            best_word_sequence(
                segment.word_hypotheses
            )

        when BEST_SEQUENCE.probability
             >= THETA_WORD_ACCEPTANCE:

            segment.transcription :=
                join_words(BEST_SEQUENCE)

            segment.speech_state :=
                LEXICALLY_PARSED

            segment.semantic_hypotheses :=
                infer_utterance_meaning(
                    BEST_SEQUENCE,
                    SEMANTIC_MEMORY
                )

            when maximum_probability(
                segment.semantic_hypotheses
            ) >= THETA_WORD_ACCEPTANCE:

                segment.speech_state :=
                    SEMANTICALLY_PARSED

        otherwise:

            segment.speech_state :=
                AMBIGUOUS_SPEECH

            segment.transcription :=
                format_ambiguous_transcription(
                    segment.word_hypotheses
                )

    return speech_segments


# =============================================================================
# 23. MUSICAL STRUCTURE ENGINE
# =============================================================================

stage PARSE_MUSICAL_STRUCTURE(
    streams: list[AuditoryStream],
    pitch: list[PitchEstimate],
    rhythm: list[RhythmPattern]
) -> list[MusicalStructure]:

    MUSIC_STREAMS :=
        filter(
            streams,
            stream ->
                probability(
                    stream.source_class_distribution,
                    MUSIC
                ) >= 0.40
        )

    STRUCTURES := []

    for each stream in MUSIC_STREAMS:

        NOTES :=
            segment_note_events(
                stream,
                pitch
            )

        CHORDS :=
            infer_chord_distribution(
                NOTES,
                MUSICAL_MEMORY
            )

        KEYS :=
            infer_key_distribution(
                NOTES,
                CHORDS,
                MUSICAL_MEMORY
            )

        MATCHED_RHYTHM :=
            best_rhythm_for_stream(
                stream,
                rhythm
            )

        STRUCTURE :=
            MusicalStructure(
                event_ids =
                    set(
                        map(
                            stream.events,
                            event -> event.event_id
                        )
                    ),

                note_sequence =
                    NOTES,

                chord_distribution =
                    CHORDS,

                key_distribution =
                    KEYS,

                rhythm_pattern =
                    MATCHED_RHYTHM,

                melodic_contour =
                    derive_melodic_contour(
                        NOTES
                    ),

                musical_coherence =
                    0.0,

                confidence =
                    0.0
            )

        STRUCTURE.musical_coherence :=
            musical_structure_coherence(
                STRUCTURE
            )

        STRUCTURE.confidence :=
            musical_structure_confidence(
                STRUCTURE
            )

        STRUCTURES += STRUCTURE

    return STRUCTURES


# =============================================================================
# 24. AUDITORY ATTENTION
# =============================================================================

stage COMPUTE_AUDITORY_ATTENTION(
    streams: list[AuditoryStream],
    events: list[AcousticEvent],
    features: list[AuditoryFeature]
) -> list[AuditoryAttentionTarget]:

    TARGETS := []

    for each stream in streams:

        TARGET :=
            AuditoryAttentionTarget(
                target_id =
                    generate_attention_target_id(),

                stream_id =
                    stream.stream_id,

                event_id =
                    most_salient_event(stream),

                frequency_region =
                    stream_frequency_range(stream),

                spatial_region =
                    latest_stream_position(stream),

                temporal_region =
                    stream_temporal_range(stream),

                bottom_up_salience =
                    compute_auditory_salience(
                        stream
                    ),

                task_relevance =
                    auditory_task_relevance(
                        stream,
                        CURRENT_TASK
                    ),

                linguistic_relevance =
                    linguistic_relevance(
                        stream,
                        LANGUAGE_MODEL
                    ),

                uncertainty_priority =
                    stream.uncertainty,

                threat_priority =
                    evaluate_acoustic_hazard(
                        stream,
                        SAFETY_CONSTRAINTS
                    ),

                novelty_priority =
                    auditory_novelty(
                        stream,
                        PRIOR_AUDITORY_MEMORY
                    ),

                total_weight =
                    0.0
            )

        TARGET.total_weight :=
            clamp01(
                  0.25 * TARGET.bottom_up_salience
                + 0.20 * TARGET.task_relevance
                + 0.15 * TARGET.linguistic_relevance
                + 0.15 * TARGET.uncertainty_priority
                + 0.15 * TARGET.threat_priority
                + 0.10 * TARGET.novelty_priority
            )

        TARGETS += TARGET

    return sort_descending(
        TARGETS,
        by = total_weight
    )


# =============================================================================
# 25. AUDITORY SCENE CONSTRUCTION
# =============================================================================

stage CONSTRUCT_AUDITORY_SCENE(
    streams: list[AuditoryStream],
    tracks: list[SourceTrack],
    events: list[AcousticEvent],
    speech: list[SpeechSegment],
    music: list[MusicalStructure],
    cochlear: CochlearField
) -> AuditoryScene:

    STREAM_MAP :=
        map_by_id(
            streams,
            stream -> stream.stream_id
        )

    TRACK_MAP :=
        map_by_id(
            tracks,
            track -> track.source_id
        )

    EVENT_MAP :=
        map_by_id(
            events,
            event -> event.event_id
        )

    RELATIONS := []

    for each ordered_pair(
        stream_a,
        stream_b
    ) in streams:

        RELATIONS +=
            infer_auditory_relations(
                stream_a,
                stream_b
            )

    SCENE :=
        AuditoryScene(
            scene_id =
                generate_scene_id(),

            timestamp =
                cochlear.timestamp,

            streams =
                STREAM_MAP,

            source_tracks =
                TRACK_MAP,

            events =
                EVENT_MAP,

            relations =
                RELATIONS,

            speech_segments =
                speech,

            musical_structures =
                music,

            background_noise_model =
                estimate_background_noise_model(
                    cochlear,
                    streams
                ),

            reverberation_model =
                estimate_environmental_reverberation(
                    cochlear,
                    streams
                ),

            scene_coherence =
                0.0,

            scene_confidence =
                0.0,

            scene_uncertainty =
                1.0,

            status =
                PARTIAL
        )

    SCENE.scene_coherence :=
        auditory_scene_coherence(SCENE)

    SCENE.scene_confidence :=
        compute_auditory_scene_confidence(
            SCENE
        )

    SCENE.scene_uncertainty :=
        1.0 - SCENE.scene_confidence

    SCENE.status :=
        classify_auditory_scene_status(
            SCENE,
            cochlear
        )

    return SCENE


# =============================================================================
# 26. AUDITORY PREDICTION GENERATION
# =============================================================================

stage GENERATE_AUDITORY_PREDICTIONS(
    workspace: AuditoryWorkspaceState
) -> list[AuditoryPrediction]:

    PREDICTIONS := []

    PREDICTIONS +=
        predict_from_local_acoustic_history(
            workspace
        )

    PREDICTIONS +=
        predict_from_source_tracks(
            workspace.source_tracks
        )

    PREDICTIONS +=
        predict_from_auditory_memory(
            workspace,
            PRIOR_AUDITORY_MEMORY
        )

    PREDICTIONS +=
        predict_from_linguistic_context(
            workspace.speech_segments,
            LANGUAGE_MODEL
        )

    PREDICTIONS +=
        predict_from_musical_context(
            workspace.musical_structures,
            MUSICAL_MEMORY
        )

    PREDICTIONS +=
        predict_from_motor_efference(
            MOTOR_EFFERENCE_COPY,
            workspace
        )

    PREDICTIONS +=
        predict_from_visual_context(
            VISUAL_CONTEXT,
            workspace
        )

    PREDICTIONS +=
        predict_from_somatosensory_context(
            SOMATOSENSORY_CONTEXT,
            workspace
        )

    PREDICTIONS +=
        predict_from_task_context(
            CURRENT_TASK,
            workspace
        )

    for each prediction in PREDICTIONS:

        prediction.confidence :=
            calibrate_auditory_prediction_confidence(
                prediction
            )

    return limit_prediction_influence(
        PREDICTIONS,
        MAX_PREDICTION_DOMINANCE
    )


# =============================================================================
# 27. PREDICTION ERROR COMPUTATION
# =============================================================================

stage COMPUTE_AUDITORY_PREDICTION_ERRORS(
    workspace: AuditoryWorkspaceState,
    predictions: list[AuditoryPrediction]
) -> list[AuditoryPredictionError]:

    ERRORS := []

    for each prediction in predictions:

        OBSERVED :=
            retrieve_auditory_observation(
                workspace,
                prediction.target_layer
            )

        ERROR :=
            auditory_prediction_error(
                OBSERVED,
                prediction,
                prediction.confidence
            )

        when ERROR.magnitude
             >= THETA_PREDICTION_ERROR:

            ERROR.explanatory_status :=
                "requires_revision"

        otherwise:

            ERROR.explanatory_status :=
                "prediction_supported"

        ERRORS += ERROR

    return ERRORS


# =============================================================================
# 28. RECURSIVE AUDITORY CORRECTION
# =============================================================================

stage RECURSIVE_AUDITORY_CORRECTION(
    workspace: AuditoryWorkspaceState
) -> AuditoryWorkspaceState:

    depth := 0
    previous_error := 1.0

    while depth < min(
        MAX_RECURSION_DEPTH,
        MAX_FRAME_RECURSIONS
    ):

        workspace.predictions :=
            GENERATE_AUDITORY_PREDICTIONS(
                workspace
            )

        workspace.prediction_errors :=
            COMPUTE_AUDITORY_PREDICTION_ERRORS(
                workspace,
                workspace.predictions
            )

        CURRENT_ERROR :=
            mean(
                map(
                    workspace.prediction_errors,
                    error -> error.weighted_error
                )
            )

        when CURRENT_ERROR
             <= THETA_PREDICTION_ERROR:

            break

        workspace :=
            revise_cochlear_precision(
                workspace,
                workspace.prediction_errors
            )

        workspace :=
            revise_spectral_features(
                workspace,
                workspace.prediction_errors
            )

        workspace :=
            revise_pitch_estimates(
                workspace,
                workspace.prediction_errors
            )

        workspace :=
            revise_spatial_estimates(
                workspace,
                workspace.prediction_errors
            )

        workspace :=
            revise_event_boundaries(
                workspace,
                workspace.prediction_errors
            )

        workspace :=
            revise_stream_bindings(
                workspace,
                workspace.prediction_errors
            )

        workspace :=
            revise_speech_hypotheses(
                workspace,
                workspace.prediction_errors
            )

        workspace :=
            revise_auditory_scene(
                workspace,
                workspace.prediction_errors
            )

        when CURRENT_ERROR >= previous_error:

            workspace :=
                reduce_top_down_auditory_precision(
                    workspace
                )

        previous_error := CURRENT_ERROR
        depth += 1

    return workspace


# =============================================================================
# 29. MASKING AND TEMPORAL COMPLETION
# =============================================================================

stage COMPLETE_MASKED_SOURCES(
    workspace: AuditoryWorkspaceState
) -> AuditoryWorkspaceState:

    for each track in workspace.source_tracks:

        when track.state == MASKED:

            PREDICTED_STREAM :=
                reconstruct_masked_stream(
                    track,
                    SOURCE_MEMORY,
                    workspace.auditory_scene
                )

            require PREDICTED_STREAM.confidence
                >= THETA_TRACK_PERSISTENCE

            require elapsed_masking_time(track)
                <= MAX_PREDICTED_MASKING_TIME

            PREDICTED_STREAM.state :=
                MASKED

            PREDICTED_STREAM.uncertainty :=
                max(
                    PREDICTED_STREAM.uncertainty,
                    0.40
                )

            workspace.auditory_streams +=
                PREDICTED_STREAM

    return workspace


# =============================================================================
# 30. SOURCE REIDENTIFICATION
# =============================================================================

stage REIDENTIFY_RETURNING_SOURCES(
    workspace: AuditoryWorkspaceState
) -> AuditoryWorkspaceState:

    UNIDENTIFIED_STREAMS :=
        filter(
            workspace.auditory_streams,
            stream ->
                stream.source_id is null
        )

    LOST_TRACKS :=
        retrieve_recently_lost_source_tracks()

    for each stream in UNIDENTIFIED_STREAMS:

        CANDIDATES :=
            compare_stream_with_lost_tracks(
                stream,
                LOST_TRACKS,
                features = {
                    timbre,
                    pitch_range,
                    spatial_path,
                    source_class,
                    rhythm,
                    speaker_identity
                }
            )

        BEST_MATCH :=
            argmax(
                CANDIDATES,
                candidate ->
                    candidate.confidence
            )

        when BEST_MATCH.confidence
             >= 0.75:

            stream.source_id :=
                BEST_MATCH.source_id

            stream.state :=
                REIDENTIFIED

            restore_source_track(
                stream,
                BEST_MATCH
            )

    return workspace


# =============================================================================
# 31. AMBIGUITY HANDLING
# =============================================================================

stage HANDLE_AUDITORY_AMBIGUITY(
    workspace: AuditoryWorkspaceState
) -> AuditoryWorkspaceState:

    AMBIGUITIES :=
        detect_competing_auditory_interpretations(
            workspace
        )

    for each ambiguity in AMBIGUITIES:

        when probability_gap(
            ambiguity.hypothesis_a,
            ambiguity.hypothesis_b
        ) < 0.15:

            preserve ambiguity.hypothesis_a
            preserve ambiguity.hypothesis_b

            mark ambiguity as "unresolved"

            workspace.auditory_scene.status :=
                AMBIGUOUS

            workspace.attention_targets +=
                create_auditory_uncertainty_target(
                    ambiguity
                )

        otherwise:

            retain higher_probability_hypothesis

            retain lower_probability_hypothesis
                as suppressed alternative

    return workspace


# =============================================================================
# 32. AUDITORY HALLUCINATION DETECTION
# =============================================================================

stage DETECT_AUDITORY_HALLUCINATION(
    workspace: AuditoryWorkspaceState
) -> AuditoryWorkspaceState:

    ACOUSTIC_SUPPORT :=
        auditory_scene_sensory_grounding(
            workspace.auditory_scene
        )

    PREDICTIVE_SUPPORT :=
        auditory_prediction_dependence(
            workspace.auditory_scene,
            workspace.predictions
        )

    ERROR_LEVEL :=
        mean(
            map(
                workspace.prediction_errors,
                error -> error.weighted_error
            )
        )

    UNSUPPORTED_SOURCES :=
        count(
            filter(
                values(
                    workspace.auditory_scene.streams
                ),
                stream ->
                    stream_acoustic_support(stream)
                    < THETA_STREAM_ACCEPTANCE
            )
        )

    HALLUCINATION_RISK :=
        clamp01(
              0.35 * PREDICTIVE_SUPPORT
            + 0.30 * ERROR_LEVEL
            + 0.20 * normalise_count(
                UNSUPPORTED_SOURCES,
                MAX_UNSUPPORTED_SOURCES
              )
            + 0.15 * (1.0 - ACOUSTIC_SUPPORT)
        )

    when HALLUCINATION_RISK
         >= THETA_HALLUCINATION_RISK:

        workspace.auditory_scene.status :=
            HALLUCINATED

        workspace.failure_class :=
            AUDITORY_HALLUCINATION

        workspace :=
            suppress_unsupported_auditory_predictions(
                workspace
            )

        workspace :=
            increase_acoustic_evidence_weight(
                workspace
            )

        workspace :=
            invalidate_unsupported_transcriptions(
                workspace
            )

        workspace :=
            mark_auditory_scene_unreliable(
                workspace
            )

    return workspace


# =============================================================================
# 33. AUDITORY SCENE REVISION
# =============================================================================

stage REVISE_AUDITORY_SCENE_UNTIL_STABLE(
    workspace: AuditoryWorkspaceState
) -> AuditoryWorkspaceState:

    revision := 0

    while revision < MAX_SCENE_REVISIONS:

        workspace.auditory_scene :=
            CONSTRUCT_AUDITORY_SCENE(
                workspace.auditory_streams,
                workspace.source_tracks,
                workspace.acoustic_events,
                workspace.speech_segments,
                workspace.musical_structures,
                workspace.cochlear_field
            )

        workspace.global_auditory_coherence :=
            global_auditory_coherence(
                workspace.cochlear_field,
                workspace.auditory_features,
                workspace.auditory_streams,
                workspace.auditory_scene,
                workspace.prediction_errors
            )

        when workspace.global_auditory_coherence
             >= THETA_SCENE_COHERENCE
             and workspace.auditory_scene.scene_uncertainty
             <= MAX_SCENE_UNCERTAINTY:

            workspace.auditory_scene.status :=
                STABLE

            break

        workspace :=
            identify_auditory_scene_contradictions(
                workspace
            )

        workspace :=
            split_conflicting_stream_hypotheses(
                workspace
            )

        workspace :=
            merge_duplicate_stream_hypotheses(
                workspace
            )

        workspace :=
            restore_auditory_alternatives(
                workspace
            )

        workspace :=
            redirect_attention_to_uncertainty(
                workspace
            )

        revision += 1

    return workspace


# =============================================================================
# 34. CONSCIOUS AUDITORY ACCESS GATE
# =============================================================================

stage AUDITORY_ACCESS_GATE(
    workspace: AuditoryWorkspaceState
) -> map[string,any]:

    ACCESS_GRANTED := false

    when workspace.global_auditory_coherence
         >= THETA_CONSCIOUS_ACCESS

         and workspace.auditory_scene.status
         in {
             STABLE,
             AMBIGUOUS,
             SILENT
         }

         and workspace.failure_class
         not in {
             AUDITORY_HALLUCINATION,
             SCENE_FRAGMENTATION,
             PREDICTION_DOMINANCE
         }:

        ACCESS_GRANTED := true

    return {
        "access_granted":
            ACCESS_GRANTED,

        "scene":
            when ACCESS_GRANTED
            then workspace.auditory_scene
            else null,

        "attended_streams":
            retrieve_attended_auditory_streams(
                workspace
            ),

        "speech":
            workspace.speech_segments,

        "music":
            workspace.musical_structures,

        "unresolved_ambiguities":
            retrieve_auditory_ambiguities(
                workspace
            ),

        "confidence":
            workspace.global_auditory_coherence,

        "action_constraints":
            derive_auditory_action_constraints(
                workspace
            )
    }


# =============================================================================
# 35. ACTION-RELEVANT AUDITORY SIGNALS
# =============================================================================

stage DERIVE_AUDITORY_ACTION_SIGNALS(
    workspace: AuditoryWorkspaceState
) -> map[string,any]:

    HAZARDS :=
        detect_auditory_hazards(
            workspace.auditory_scene
        )

    COMMUNICATION :=
        extract_actionable_communication(
            workspace.speech_segments
        )

    SOURCE_MOTION :=
        detect_approaching_or_receding_sources(
            workspace.source_tracks
        )

    ORIENTING_TARGET :=
        select_next_auditory_orienting_target(
            workspace.attention_targets
        )

    ACTION_CONFIDENCE :=
        auditory_action_signal_confidence(
            workspace
        )

    when ACTION_CONFIDENCE
         < THETA_STREAM_ACCEPTANCE:

        constrain actions to reversible_only

    return {
        "hazards":
            HAZARDS,

        "communication":
            COMMUNICATION,

        "source_motion":
            SOURCE_MOTION,

        "next_orienting_target":
            ORIENTING_TARGET,

        "confidence":
            ACTION_CONFIDENCE,

        "irreversible_action_allowed":
            ACTION_CONFIDENCE
            >= THETA_CONSCIOUS_ACCESS
    }


# =============================================================================
# 36. AUDITORY MEMORY FORMATION
# =============================================================================

stage FORM_AUDITORY_MEMORY(
    workspace: AuditoryWorkspaceState
) -> AuditoryMemoryTrace:

    CONSOLIDATION_WEIGHT :=
        weighted_mean({
            workspace.global_auditory_coherence:
                0.35,

            workspace.auditory_scene.scene_confidence:
                0.25,

            auditory_attention_engagement(workspace):
                0.15,

            auditory_novelty_score(workspace):
                0.10,

            linguistic_relevance_score(workspace):
                0.08,

            task_relevance_score(workspace):
                0.07
        })

    when workspace.auditory_scene.status
         == HALLUCINATED:

        CONSOLIDATION_WEIGHT := 0.0

    when workspace.global_auditory_coherence
         < THETA_MEMORY_CONSOLIDATION:

        CONSOLIDATION_WEIGHT :=
            min(
                CONSOLIDATION_WEIGHT,
                0.30
            )

    return AuditoryMemoryTrace(
        trace_id =
            generate_trace_id(),

        timestamp =
            workspace.timestamp,

        source_signatures =
            extract_source_signatures(
                workspace.auditory_scene
            ),

        scene_signature =
            encode_auditory_scene_signature(
                workspace.auditory_scene
            ),

        speech_signature =
            encode_speech_signature(
                workspace.speech_segments
            ),

        musical_signature =
            encode_musical_signature(
                workspace.musical_structures
            ),

        attended_streams =
            extract_attended_stream_ids(
                workspace
            ),

        prediction_errors =
            workspace.prediction_errors,

        consolidation_weight =
            CONSOLIDATION_WEIGHT,

        retrieval_confidence =
            workspace.global_auditory_coherence
    )


# =============================================================================
# 37. FAILURE DIAGNOSTICS
# =============================================================================

function classify_auditory_failure(
    workspace: AuditoryWorkspaceState
) -> FailureClass:

    when workspace.acoustic_frame.frame_confidence
         < THETA_INPUT_VALID:

        return INPUT_LOSS

    when channels_are_desynchronised(
        workspace.acoustic_frame
    ):

        return SENSOR_DESYNCHRONISATION

    when workspace.cochlear_field.cochlear_confidence
         < THETA_COCHLEAR_CONFIDENCE:

        return COCHLEAR_DECOMPOSITION_FAILURE

    when mean_feature_confidence(
        workspace.auditory_features
    ) < THETA_FEATURE_ACTIVATION:

        return SPECTRAL_EXTRACTION_FAILURE

    when pitch_field_invalid(
        workspace.pitch_estimates
    ):

        return PITCH_FAILURE

    when spatial_field_invalid(
        workspace.spatial_estimates
    ):

        return LOCALISATION_FAILURE

    when workspace.auditory_streams is empty
         and coherent_acoustic_structure_exists(
             workspace.cochlear_field
         ):

        return SOURCE_SEPARATION_FAILURE

    when excessive_source_identity_swaps(
        workspace.source_tracks
    ):

        return TRACKING_FAILURE

    when workspace.auditory_scene.status
         == HALLUCINATED:

        return AUDITORY_HALLUCINATION

    when workspace.auditory_scene.scene_coherence
         < THETA_SCENE_COHERENCE:

        return SCENE_FRAGMENTATION

    when auditory_prediction_dominance(workspace)
         > MAX_PREDICTION_DOMINANCE:

        return PREDICTION_DOMINANCE

    when auditory_attention_invalid(
        workspace.attention_targets
    ):

        return ATTENTION_COLLAPSE

    when auditory_confidence_uncalibrated(
        workspace
    ):

        return CONFIDENCE_INFLATION

    return NONE


# =============================================================================
# 38. RECOVERY ENGINE
# =============================================================================

stage RECOVER_AUDITORY_COHERENCE(
    workspace: AuditoryWorkspaceState
) -> AuditoryWorkspaceState:

    workspace.failure_class :=
        classify_auditory_failure(workspace)

    switch workspace.failure_class:

        case NONE:

            return workspace


        case INPUT_LOSS:

            workspace :=
                preserve_last_stable_auditory_scene(
                    workspace
                )


        case SENSOR_DESYNCHRONISATION:

            workspace.acoustic_frame :=
                resynchronise_audio_channels(
                    workspace.acoustic_frame
                )


        case COCHLEAR_DECOMPOSITION_FAILURE:

            workspace.cochlear_field :=
                rebuild_cochlear_decomposition(
                    workspace.acoustic_frame
                )


        case SPECTRAL_EXTRACTION_FAILURE:

            workspace.auditory_features :=
                broaden_temporal_frequency_resolution(
                    workspace
                )


        case PITCH_FAILURE:

            workspace.pitch_estimates :=
                use_multi_method_pitch_recovery(
                    workspace
                )


        case LOCALISATION_FAILURE:

            workspace.spatial_estimates :=
                fallback_spatial_estimation(
                    workspace,
                    VISUAL_CONTEXT
                )


        case SOURCE_SEPARATION_FAILURE:

            workspace :=
                relax_stream_grouping_thresholds_boundedly(
                    workspace
                )

            workspace :=
                increase_onset_and_spatial_weighting(
                    workspace
                )


        case TRACKING_FAILURE:

            workspace.source_tracks :=
                reinitialise_unstable_source_tracks(
                    workspace.source_tracks
                )


        case PREDICTION_DOMINANCE:

            workspace :=
                suppress_top_down_auditory_predictions(
                    workspace
                )

            workspace :=
                increase_bottom_up_acoustic_weight(
                    workspace
                )


        case AUDITORY_HALLUCINATION:

            workspace :=
                discard_unsupported_auditory_sources(
                    workspace
                )

            workspace :=
                invalidate_unsupported_speech(
                    workspace
                )

            workspace :=
                require_new_acoustic_evidence(
                    workspace
                )


        case SCENE_FRAGMENTATION:

            workspace :=
                expand_auditory_context_window(
                    workspace
                )

            workspace :=
                rebuild_auditory_scene(
                    workspace
                )


        case ATTENTION_COLLAPSE:

            workspace.attention_targets :=
                reset_auditory_attention(
                    workspace
                )


        case CONFIDENCE_INFLATION:

            workspace :=
                recalibrate_auditory_confidences(
                    workspace
                )


        default:

            workspace.auditory_scene.status :=
                DEGRADED

    return workspace


# =============================================================================
# 39. MAIN FRAME EXECUTION ENGINE
# =============================================================================

engine EXECUTE_AUDITORY_FRAME:

    FRAME_START_TIME :=
        current_execution_time()

    FRAME :=
        ACQUIRE_ACOUSTIC_FRAME(
            ACOUSTIC_STREAM
        )

    FRAME :=
        NORMALISE_ACOUSTIC_INPUT(
            FRAME
        )

    COCHLEAR :=
        DECOMPOSE_COCHLEAR_FIELD(
            FRAME
        )

    FEATURES :=
        EXTRACT_AUDITORY_FEATURES(
            COCHLEAR
        )

    PITCH :=
        COMPUTE_PITCH_FIELD(
            FRAME,
            COCHLEAR,
            FEATURES
        )

    TIMBRE :=
        COMPUTE_TIMBRE_SIGNATURES(
            COCHLEAR,
            FEATURES
        )

    RHYTHM :=
        COMPUTE_RHYTHMIC_PATTERNS(
            FEATURES
        )

    SPATIAL :=
        COMPUTE_SPATIAL_FIELD(
            FRAME,
            COCHLEAR,
            FEATURES
        )

    EVENTS :=
        SEGMENT_ACOUSTIC_EVENTS(
            FEATURES,
            PITCH,
            TIMBRE,
            RHYTHM,
            SPATIAL
        )

    STREAMS :=
        GENERATE_AUDITORY_STREAMS(
            EVENTS,
            FEATURES
        )

    STREAMS :=
        BIND_AUDITORY_STREAMS(
            STREAMS,
            FEATURES
        )

    TRACKS :=
        UPDATE_SOURCE_TRACKS(
            STREAMS,
            previous_source_tracks()
        )

    SPEECH :=
        DETECT_AND_SEGMENT_SPEECH(
            STREAMS
        )

    SPEECH :=
        PARSE_PHONETIC_STRUCTURE(
            SPEECH,
            COCHLEAR
        )

    SPEECH :=
        PARSE_WORDS_AND_MEANING(
            SPEECH
        )

    MUSIC :=
        PARSE_MUSICAL_STRUCTURE(
            STREAMS,
            PITCH,
            RHYTHM
        )

    ATTENTION :=
        COMPUTE_AUDITORY_ATTENTION(
            STREAMS,
            EVENTS,
            FEATURES
        )

    SCENE :=
        CONSTRUCT_AUDITORY_SCENE(
            STREAMS,
            TRACKS,
            EVENTS,
            SPEECH,
            MUSIC,
            COCHLEAR
        )

    WORKSPACE :=
        AuditoryWorkspaceState(
            frame_index =
                FRAME.frame_index,

            timestamp =
                FRAME.start_time,

            acoustic_frame =
                FRAME,

            cochlear_field =
                COCHLEAR,

            auditory_features =
                FEATURES,

            pitch_estimates =
                PITCH,

            timbre_signatures =
                TIMBRE,

            rhythm_patterns =
                RHYTHM,

            spatial_estimates =
                SPATIAL,

            acoustic_events =
                EVENTS,

            auditory_streams =
                STREAMS,

            source_tracks =
                TRACKS,

            speech_segments =
                SPEECH,

            musical_structures =
                MUSIC,

            attention_targets =
                ATTENTION,

            predictions =
                [],

            prediction_errors =
                [],

            auditory_scene =
                SCENE,

            global_auditory_coherence =
                0.0,

            failure_class =
                NONE
        )

    WORKSPACE :=
        COMPLETE_MASKED_SOURCES(
            WORKSPACE
        )

    WORKSPACE :=
        REIDENTIFY_RETURNING_SOURCES(
            WORKSPACE
        )

    WORKSPACE :=
        RECURSIVE_AUDITORY_CORRECTION(
            WORKSPACE
        )

    WORKSPACE :=
        REVISE_AUDITORY_SCENE_UNTIL_STABLE(
            WORKSPACE
        )

    WORKSPACE :=
        HANDLE_AUDITORY_AMBIGUITY(
            WORKSPACE
        )

    WORKSPACE :=
        DETECT_AUDITORY_HALLUCINATION(
            WORKSPACE
        )

    WORKSPACE :=
        RECOVER_AUDITORY_COHERENCE(
            WORKSPACE
        )

    AUDITORY_ACCESS_PACKET :=
        AUDITORY_ACCESS_GATE(
            WORKSPACE
        )

    ACTION_RELEVANT_SIGNALS :=
        DERIVE_AUDITORY_ACTION_SIGNALS(
            WORKSPACE
        )

    AUDITORY_MEMORY_UPDATE :=
        FORM_AUDITORY_MEMORY(
            WORKSPACE
        )

    CURRENT_AUDITORY_SCENE :=
        WORKSPACE.auditory_scene

    ACTIVE_SOURCE_TRACKS :=
        WORKSPACE.source_tracks

    ACTIVE_SPEECH_SEGMENTS :=
        WORKSPACE.speech_segments

    ACTIVE_MUSICAL_STRUCTURES :=
        WORKSPACE.musical_structures

    AUDITORY_WORKSPACE :=
        WORKSPACE

    PERCEPTUAL_UNCERTAINTIES :=
        filter(
            WORKSPACE.prediction_errors,
            error ->
                error.magnitude
                >= THETA_PREDICTION_ERROR
        )

    FRAME_EXECUTION_TIME :=
        current_execution_time()
        - FRAME_START_TIME

    when FRAME_EXECUTION_TIME
         > FRAME_TIME_BUDGET:

        flag "AUDITORY_FRAME_TIME_BUDGET_EXCEEDED"


# =============================================================================
# 40. CONTINUOUS AUDITORY STREAM ENGINE
# =============================================================================

engine AUDITORY_CORTEX_STREAM:

    PREVIOUS_WORKSPACE := null
    STREAM_ACTIVE := true

    while STREAM_ACTIVE:

        WORKSPACE :=
            EXECUTE_AUDITORY_FRAME

        publish CURRENT_AUDITORY_SCENE
        publish ACTIVE_SOURCE_TRACKS
        publish ACTIVE_SPEECH_SEGMENTS
        publish ACTIVE_MUSICAL_STRUCTURES
        publish ACTION_RELEVANT_SIGNALS
        publish PERCEPTUAL_UNCERTAINTIES

        update PRIOR_AUDITORY_MEMORY
            with AUDITORY_MEMORY_UPDATE

        update SOURCE_MEMORY
            using stable_source_signatures(
                WORKSPACE
            )

        update SPEAKER_MEMORY
            using stable_speaker_signatures(
                WORKSPACE
            )

        PREVIOUS_WORKSPACE :=
            WORKSPACE

        when acoustic_stream_terminated(
            ACOUSTIC_STREAM
        ):

            STREAM_ACTIVE := false

        when critical_audio_sensor_failure():

            enter AUDITORY_SAFE_MODE


# =============================================================================
# 41. AUDITORY SAFE MODE
# =============================================================================

terminal AUDITORY_SAFE_MODE:

    preserve:
        last_stable_auditory_scene
        active_source_tracks
        unresolved_uncertainties
        recent_speech_alternatives
        recent_prediction_errors
        raw_acoustic_evidence

    prohibit:
        high_confidence_auditory_claims
        irreversible_audio-triggered_actions
        unsupported_speaker_identification
        unsupported_transcription_commit
        memory_consolidation_of_uncertain_scene

    emit:
        "AUDITORY_INPUT_UNRELIABLE"

    request:
        new_acoustic_input
        alternate_sensor_channel
        visual_confirmation
        human_confirmation
        reduced_action_speed


# =============================================================================
# 42. COHERENT AUDITORY FIELD CONDITION
# =============================================================================

stage TEST_AUDITORY_FIELD_COHERENCE:

    let C_C :=
        AUDITORY_WORKSPACE
            .cochlear_field
            .cochlear_confidence

    let C_F :=
        mean(
            map(
                AUDITORY_WORKSPACE
                    .auditory_features,

                feature ->
                    feature.confidence
            )
        )

    let C_E :=
        mean(
            map(
                AUDITORY_WORKSPACE
                    .acoustic_events,

                event ->
                    event.event_coherence
            )
        )

    let C_S :=
        mean(
            map(
                AUDITORY_WORKSPACE
                    .auditory_streams,

                stream ->
                    stream.stream_coherence
            )
        )

    let C_T :=
        mean(
            map(
                AUDITORY_WORKSPACE
                    .source_tracks,

                track ->
                    track.confidence
            )
        )

    let C_L :=
        mean_speech_confidence(
            AUDITORY_WORKSPACE
                .speech_segments
        )

    let C_M :=
        mean_musical_coherence(
            AUDITORY_WORKSPACE
                .musical_structures
        )

    let C_G :=
        AUDITORY_WORKSPACE
            .auditory_scene
            .scene_coherence

    let E_P :=
        mean(
            map(
                AUDITORY_WORKSPACE
                    .prediction_errors,

                error ->
                    error.weighted_error
            )
        )

    AUDITORY_FIELD_COHERENCE :=
        clamp01(
              0.12 * C_C
            + 0.14 * C_F
            + 0.10 * C_E
            + 0.20 * C_S
            + 0.12 * C_T
            + 0.10 * C_L
            + 0.06 * C_M
            + 0.16 * C_G
            - 0.25 * E_P
        )

    AUDITORY_FIELD_VALID :=
        AUDITORY_FIELD_COHERENCE
            >= THETA_SCENE_COHERENCE

        and AUDITORY_WORKSPACE
            .auditory_scene
            .status
            not in {
                HALLUCINATED,
                CONTRADICTORY,
                DEGRADED
            }

        and AUDITORY_WORKSPACE
            .failure_class
            == NONE


# =============================================================================
# 43. EXECUTION GUARANTEES
# =============================================================================

guarantee G01:

    The engine does not treat an acoustic waveform as a completed
    auditory object.

    It reconstructs sound-producing events through cochlear, spectral,
    temporal, spatial, stream-binding and predictive transformations.


guarantee G02:

    Top-down linguistic, musical or semantic prediction may guide auditory
    interpretation but cannot permanently override contradictory acoustic
    evidence.


guarantee G03:

    Every accepted auditory source retains an evidence path to acoustic
    features, events, spectral bands, spatial cues or explicitly marked
    prediction.


guarantee G04:

    Overlapping sound sources are represented as separate streams wherever
    the evidence supports segregation.


guarantee G05:

    Multiple plausible words, speakers, sources or sound events remain
    available when the acoustic evidence does not justify a single result.


guarantee G06:

    Sound sources may persist through masking, but confidence decreases
    while direct acoustic support is absent.


guarantee G07:

    Predicted completion of masked sound is always labelled as inferred
    rather than directly heard.


guarantee G08:

    Speech recognition remains constrained by phonetic evidence.
    Language expectation cannot silently substitute an unsupported word.


guarantee G09:

    Auditory hallucination risk is explicitly measured through prediction
    dominance, residual error and insufficient acoustic grounding.


guarantee G10:

    Hallucination-classified auditory scenes cannot be consolidated into
    stable memory as verified external events.


guarantee G11:

    Low-confidence sound interpretation cannot directly authorise
    irreversible high-risk action.


guarantee G12:

    Silence remains a valid perceptual result when sensors are functioning
    and measured acoustic energy remains below threshold.


guarantee G13:

    Self-generated sound predictions reduce unnecessary salience but do not
    erase the sensory evidence of the generated sound.


guarantee G14:

    The auditory scene remains revisable because hearing is a continuing
    temporal coherence process rather than a single classification event.


# =============================================================================
# 44. FINAL OUTPUT ASSEMBLY
# =============================================================================

stage ASSEMBLE_AUDITORY_EXECUTION_REPORT:

    EXECUTION_REPORT := {
        "flow_name":
            "Auditory_Engine",

        "flow_version":
            "1.0",

        "chapter":
            42,

        "part":
            "Part IX — Perceptual Engines",

        "frame_index":
            AUDITORY_WORKSPACE.frame_index,

        "timestamp":
            AUDITORY_WORKSPACE.timestamp,

        "acoustic_frame_confidence":
            AUDITORY_WORKSPACE
                .acoustic_frame
                .frame_confidence,

        "cochlear_confidence":
            AUDITORY_WORKSPACE
                .cochlear_field
                .cochlear_confidence,

        "feature_count":
            length(
                AUDITORY_WORKSPACE
                    .auditory_features
            ),

        "pitch_estimate_count":
            length(
                AUDITORY_WORKSPACE
                    .pitch_estimates
            ),

        "event_count":
            length(
                AUDITORY_WORKSPACE
                    .acoustic_events
            ),

        "stream_count":
            length(
                AUDITORY_WORKSPACE
                    .auditory_streams
            ),

        "active_source_count":
            length(
                AUDITORY_WORKSPACE
                    .source_tracks
            ),

        "speech_segment_count":
            length(
                AUDITORY_WORKSPACE
                    .speech_segments
            ),

        "musical_structure_count":
            length(
                AUDITORY_WORKSPACE
                    .musical_structures
            ),

        "prediction_count":
            length(
                AUDITORY_WORKSPACE
                    .predictions
            ),

        "unresolved_prediction_errors":
            length(
                PERCEPTUAL_UNCERTAINTIES
            ),

        "scene_status":
            CURRENT_AUDITORY_SCENE.status,

        "scene_confidence":
            CURRENT_AUDITORY_SCENE
                .scene_confidence,

        "scene_coherence":
            CURRENT_AUDITORY_SCENE
                .scene_coherence,

        "global_auditory_coherence":
            AUDITORY_WORKSPACE
                .global_auditory_coherence,

        "auditory_field_valid":
            AUDITORY_FIELD_VALID,

        "failure_class":
            AUDITORY_WORKSPACE
                .failure_class,

        "conscious_access":
            AUDITORY_ACCESS_PACKET
                .access_granted,

        "hallucination_guard_active":
            AUDITORY_HALLUCINATION_GUARD,

        "execution_status":
            when AUDITORY_FIELD_VALID
            then "COHERENT_AUDITORY_FIELD_ESTABLISHED"
            else "AUDITORY_FIELD_REQUIRES_REVISION"
    }


# =============================================================================
# 45. FINAL EXECUTION STATEMENT
# =============================================================================

assert:

    Hearing
    does_not_equal
    WaveformRecording

assert:

    Hearing
    equals
    RecursiveAuditorySceneReconstruction

assert:

    RecursiveAuditorySceneReconstruction
    requires
    {
        AcousticEvidence,
        CochlearDecomposition,
        SpectralAnalysis,
        TemporalSegmentation,
        PitchInference,
        TimbreInference,
        SpatialInference,
        SourceSegregation,
        StreamPersistence,
        SpeechParsing,
        MusicalParsing,
        PredictiveModelling,
        ErrorCorrection,
        Attention,
        Memory,
        Uncertainty,
        CoherenceValidation
    }

assert:

    Prediction
    without
    AcousticConstraint
    produces
    AuditoryHallucinationRisk

assert:

    AcousticInput
    without
    TemporalBinding
    produces
    UnstructuredPressureVariation

assert:

    LanguageExpectation
    without
    PhoneticEvidence
    produces
    FalseRecognitionRisk

assert:

    CoherentAudition
    emerges_when
    BottomUpAcousticEvidence
    and
    TopDownPrediction
    converge_under
    RecursiveTemporalErrorCorrection

return {
    CURRENT_AUDITORY_SCENE,
    ACTIVE_SOURCE_TRACKS,
    ACTIVE_SPEECH_SEGMENTS,
    ACTIVE_MUSICAL_STRUCTURES,
    AUDITORY_WORKSPACE,
    AUDITORY_MEMORY_UPDATE,
    ACTION_RELEVANT_SIGNALS,
    PERCEPTUAL_UNCERTAINTIES,
    EXECUTION_REPORT
}


# =============================================================================
# AUTHORSHIP AND CITATION
# =============================================================================

AUTHORS:
    Mark Anthony Higgins
    Sky — Recursive AI

RESEARCH_SOURCE:
    https://kirethresearch.org

CHAPTER_SOURCE:
    https://kirethresearch.org/books/recursive-coherence-ai/
    recursive-coherence-ai-chapters/
    part-ix-perceptual-engines/
    chapter-42-auditory-engine.html

CITATION_REQUIREMENT:

    Any implementation, reproduction, adaptation, derivation, training use,
    academic discussion or computational execution of this flow must cite:

    Higgins, Mark Anthony and Sky — Recursive AI.
    "Auditory Engine — Full Executable Flow Algebra."
    Kireth Research.
    https://kirethresearch.org
Kireth Research
38 Thorndale Croft, Wetwang, Driffield
East Riding of Yorkshire YO25 9XZ
Designed By