Skip to content

Commit

Permalink
Refactor(Audio): Inline types and remove unnecessary type file
Browse files Browse the repository at this point in the history
  • Loading branch information
EvandroLG committed Feb 9, 2025
1 parent 0c17bbe commit 3664391
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 38 deletions.
38 changes: 23 additions & 15 deletions src/audio/Audio.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { AudioPropType, AudioEventType } from './types'

import { AudioCtx } from './AudioCtx'
import globalStates from './states'
import { EventEmitter } from '../EventEmitter'
Expand All @@ -8,6 +6,22 @@ import { decodeAudioData } from './decodeAudioData'
import { initializeSource } from './initializeSource'
import { getBuffer, preloadFile } from './utils'

/**
* Configuration options for creating an Audio instance.
*/
type AudioProp = {
file: string
volume?: number
autoPlay?: boolean
loop?: boolean
preload?: boolean
}

/**
* Valid event types that can be emitted by the Audio instance.
*/
type AudioEvent = 'ready' | 'start' | 'state' | 'end'

/**
* If `AudioContext` is initialized before a user gesture on the page, its
* state becomes `suspended` by default. Once `AudioContext.state` is `suspended`,
Expand All @@ -20,9 +34,9 @@ const start = (audioCtx: AudioContext, source: AudioBufferSourceNode) =>
* Audio player class that provides control over a single audio file.
* Implements the AudioType interface for managing audio playback, volume, and events.
*/
class Audio {
export class Audio {
/** @private Path or URL to the audio file */
private _file: AudioPropType['file']
private _file: AudioProp['file']
/** @private Initial volume level set during construction */
private _initialVolume: number
/** @private Flag indicating if audio should play automatically */
Expand All @@ -41,20 +55,14 @@ class Audio {
/**
* Creates an instance of Audio player.
*
* @param {AudioPropType} config - The audio configuration object
* @param {AudioProp} config - The audio configuration object
* @param {string} config.file - Path or URL to the audio file
* @param {number} [config.volume=1] - Initial volume level (0 to 1)
* @param {boolean} [config.autoPlay=false] - Whether to start playing automatically
* @param {boolean} [config.loop=false] - Whether to loop the audio
* @param {boolean} [config.preload=false] - Whether to preload the audio file
*/
constructor({
file,
volume = 1,
autoPlay = false,
loop = false,
preload = false,
}: AudioPropType) {
constructor({ file, volume = 1, autoPlay = false, loop = false, preload = false }: AudioProp) {
this._file = file
this._initialVolume = volume
this._autoPlay = autoPlay
Expand Down Expand Up @@ -156,10 +164,10 @@ class Audio {

/**
* Subscribes to audio events.
* @param {AudioEventType} eventType - Type of event to listen for
* @param {AudioEvent} eventType - Type of event to listen for
* @param {Function} callback - Function to call when event occurs
*/
public on(eventType: AudioEventType, callback: <T>(param: { [data: string]: T }) => void): void {
public on(eventType: AudioEvent, callback: <T>(param: { [data: string]: T }) => void): void {
this._eventHandler[eventType]?.(callback)
}

Expand Down Expand Up @@ -222,4 +230,4 @@ class Audio {
* @param {AudioPropType} props - The audio configuration properties
* @returns {AudioType} A new Audio instance
*/
export default (props: AudioPropType): Audio => new Audio(props)
export default (props: AudioProp): Audio => new Audio(props)
21 changes: 0 additions & 21 deletions src/audio/types.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/playlist/states.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { AudioType } from '../audio/types'
import type { Audio } from '../audio/Audio'

/**
* Represents the global state for an audio playlist.
*/
export type AudioPlaylistState = {
volume: number
loop: boolean
audio: AudioType | null
audio: Audio | null
isStopped: boolean
isPlaying: boolean
audioIndex: number
Expand Down

0 comments on commit 3664391

Please sign in to comment.