@nafr/echo-ui
Version:
A UI library born for WAA
37 lines (36 loc) • 1.87 kB
TypeScript
export interface UseFetchAudioProps {
url: string;
requestOptions?: RequestInit;
onSuccess?: () => void;
onError?: () => void;
}
/**
* useFetchAudio is a custom React hook for fetching and decoding audio data from a specified URL.
* It handles the entire process from making an HTTP request to processing the audio data.
*
* @param {UseFetchAudioProps} props - The configuration properties for fetching the audio.
* @param {string} props.url - The URL of the audio file to be fetched.
* @param {RequestInit} [props.requestOptions] - Optional configuration for the fetch request.
* @param {Function} [props.onSuccess] - Optional callback to be executed when the audio is successfully fetched and decoded.
* @param {Function} [props.onError] - Optional callback to be executed in case of an error during fetching or decoding.
*
* @returns {object} An object containing several states and a function to initiate the audio fetching:
* - pending: A boolean indicating whether the request is in progress.
* - fetched: A boolean indicating whether the audio has been successfully fetched and decoded.
* - error: A boolean indicating if an error has occurred.
* - errorMessage: A string containing the error message if an error has occurred.
* - response: The response object from the fetch request.
* - audioBuffer: The decoded audio data as an AudioBuffer.
* - fetchAudio: A function that initiates the fetching and decoding process.
*
* This hook is useful in scenarios where you need to retrieve audio files from a server and process them for web audio applications.
*/
export declare const useFetchAudio: (props: UseFetchAudioProps) => {
pending: boolean;
fetched: boolean;
error: boolean;
errorMessage: string;
response: Response | null;
audioBuffer: AudioBuffer | null;
fetchAudio: () => Promise<void>;
};