Implements an audio fader for an HTML media element instance. This fader supports two concepts:

  • a master volume, that emulates a set, overall audio level
  • independent fading operations, which internally control the actually set audio level at the media element.

Remarks

Currently only supports a linear audio fade, with a constant gradient, only determined by the predefined durations for a full-scale fade. Video content, in case of an HTMLVideoElement, is not changed in any way.

Implements

Constructors

  • Parameters

    • audio: HTMLMediaElement

      The HTML media element to act upon

    • fadeInDuration: number = 1000

      The fade-in duration in [seconds]. Default is 1000 (1 second)

    • fadeOutDuration: number = 500

      The fade-out duration in [seconds]. Default is 500 (500 milliseconds)

    • addFadeInPreRoll: boolean = true

      Whether to apply the seek offset before fade-in operations, to compensate the fading duration. (Default: true)

    • masterVolume: number = 1

      The overall volume of the output. Can be used to control the output volume in addition to fadings. (Default: 1, representing full scale)

    Returns default

Properties

_anySoloed: boolean = false

The any soloed state

_muted: boolean = false

The muted state

_soloed: boolean = false

The soloed state

addFadeInPreRoll: boolean = true

Whether to apply a seek offset before fade-in operations, to compensate the fading duration.

audio: HTMLAudioElement

The audio element instance to act upon

fadeInDuration: number

The fade-in duration in [milliseconds] (zero means no fading)

fadeOutDuration: number

The fade-out duration in [milliseconds] (zero means no fading)

isFadingEnabled: boolean = true

Remarks

Fading is generally enabled by default, but may get disabled permanently or on some events

masterVolume: number = 1

The master volume level

Remarks

The master volume emulates an expected volume that is output from the fader, without any mute/solo/fading taken into account.

onFadingChanged: SubEventImmediate<FadingMode> = ...

Emits a changed fading state.

Param: fading

kind of fading operation that is currently ongoing

onMutedChanged: SubEventImmediate<boolean> = ...

Emits a changed muted state.

Param: muted

the changed muted state

onSoloedChanged: SubEventImmediate<boolean> = ...

Emits a changed soloed state.

Param: soloed

the changed soloed state

onVolumeChanged: SubEventImmediate<number> = ...

Emits a changed master volume state.

Param: volume

the changed master volume

operationToken: string = AudioFader.cancelOperationToken

The token for the currently running fade operation.

Remarks

Allows an ongoing fading operation to determine whether it has been superseded by a subsequent operation. This allows the first operation to reject the promise and abandon the fade operation in favor of the later one.

audioVolumeMax: number = 1

The maximum audio volume level

audioVolumeMin: number = 0.00003162

The minimum audio volume level

Remarks

-90dbFS Amplitude

cancelOperationToken: string = 'CANCEL'
stepDuration: number = 16

The fading step duration, in [milliseconds]. This is set to a fixed value, as a tradeoff between call frequency and smoothness

Devdoc

This should be set to a value that produces small volume changes, that are barely audible

Accessors

  • get anySoloed(): boolean
  • Gets the overall solo state in a multitrack context.

    Returns boolean

    Remarks

    When any is soloed, but not this track, this is effectively muted.

  • set anySoloed(value): void
  • Sets the overall solo state in a multitrack context.

    Parameters

    • value: boolean

    Returns void

    Remarks

    When any is soloed, but not this track, this is effectively muted.

  • get audioVolume(): number
  • Internally gets the media element volume, with the level limited to the allowed audio volume range.

    Returns number

  • set audioVolume(volume): void
  • Internally sets the media element volume, with the level limited to the allowed audio volume range.

    Parameters

    • volume: number

    Returns void

  • get effectiveFadeInDuration(): number
  • Gets the effective fade-in duration, in [milliseconds], taking into account whether fade-in is enabled.

    Returns number

  • get effectiveFadeOutDuration(): number
  • Gets the effective fade-out duration, in [milliseconds], taking into account whether fade-out is enabled.

    Returns number

  • get fading(): boolean
  • Gets the fading state, which is true for any kind of fading.

    Returns boolean

Methods

  • Applies the pre-roll:

    • an general offset/pre-roll according to the setting
    • an offset/pre-roll to compensate for fade-in durations, if appliccable

    Parameters

    • fadeInDuration: number

      The fade-in duration.

    Returns void

    Remarks

    At the beginning of a resource, the offset is cut off at zero.

  • Resets the token for the currently running fade operation.

    Returns void

    Remarks

    Allows operations to cancel themselves in favor of a subsequent operation.

  • Returns a linear fade promise for the currently playing track

    Parameters

    • from: number
    • to: number
    • duration: number

      {number} - A non-zero duration for the fading operation

    Returns Promise<void>

    Devdoc

    This currently only supports linear fade operations

  • Returns a fade-in promise for the currently playing track

    Parameters

    • Optional immediate: boolean

      When set to true, the fade operation is done with duration zero.

    Returns Promise<void>

    Remarks

    The sound is faded to the master volume audio level. A pre-fade offset is applied, when configured An actual fade operation is only started when

    • the set duration is non-zero and
    • no previous fade operation is ongoing
    • the immediate parameter is not set to true otherwise
    • a fade with duration zero is started and the promise is immediately resolved.
  • Returns a fade-out promise for the currently playing track

    Parameters

    • Optional immediate: boolean

      When set to true, the fade operation is done with duration zero.

    Returns Promise<void>

    Remarks

    The sound is faded to the minimum audio level. An actual fade operation is only started when

    • the set duration is non-zero and
    • no previous fade operation is ongoing
    • the immediate parameter is not set to true otherwise
    • a fade with duration zero is started and the promise is immediately resolved.
  • Gets the master audio volume, with the possible muted and soloed state (but not a possibly ongoing fade-in/fade-out) observed

    Returns number

    A value between 0 (zero) and 1 (representing full scale), while observing the muted state.

    Remarks

    A muted state returns the min volume. A non-soloed state, when any is soloed, returns the min volume.

  • If there is a currently running fade operation, reset the token.

    Returns boolean

    Whether an operation was ongoing

  • Returns a limited volume value, that satisfies the allowed audio volume range

    Parameters

    • volume: number

    Returns number

  • Sets the volume to the initial value.

    Returns void

    Remarks

    This method is to be used at creation time or for a hard reset. When a non-zero fading duration is set, this is the minimum value (prepare for the fade-in). When a zero fading duration is set, this is the maximum value (effectively remove any fade). Does not affect the muted state.

  • Sets up emission of intentional volume changes on the HTML media element.

    Returns void

    Remarks

    This allows a user to control the volume direcly from the HTML audio element controls, which are usually coupled with external input devices like Bluetooth headsets. NOTE: Only changes that are not caused by internal automation are considered.

  • Updates the current settings.

    Parameters

    • fadeInDuration: number = 1000

      The fade-in duration. Default is 1000 (1 second)

    • fadeOutDuration: number = 500

      The fade-out duration. Default is 500 (500 milliseconds)

    • addFadeInPreRoll: boolean = true

      Whether to apply an additional seek offset before fade-in operations, to compensate the fading duration. (Default: true)

    Returns void

    Remarks

    The settings will be used for the next fade. However, when the new duration is zero (no fade), the cancel operation is immediately called, resetting the volume to the initial value for this case.

  • Decreases the master audio volume level by rougly 3dB

    Returns number

    The new, possibly limited, master audio volume

    Remarks

    Applies some limitation on the upper and lower end of the range

  • Increases the master audio volume level by rougly 3dB

    Returns number

    The new, possibly limited, master audio volume

    Remarks

    Applies some limitation on the upper and lower end of the range