From cfa3e96d96b3a4f7f3b3e57721cb7d794a47dbef Mon Sep 17 00:00:00 2001 From: Michal Sek Date: Wed, 4 Dec 2024 17:56:16 +0100 Subject: [PATCH] feat: decode audio data source with promise --- .gitignore | 1 + .../src/examples/AudioFile/AudioFile.tsx | 33 ++++++----- apps/fabric-example/ios/Podfile.lock | 4 +- .../AudioAPIInstallerHostObject.cpp | 8 ++- .../AudioAPIInstallerHostObject.h | 6 +- .../cpp/HostObjects/AudioBufferHostObject.cpp | 6 ++ .../HostObjects/AudioContextHostObject.cpp | 4 +- .../cpp/HostObjects/AudioContextHostObject.h | 7 ++- .../BaseAudioContextHostObject.cpp | 41 +++++++------ .../HostObjects/BaseAudioContextHostObject.h | 4 +- .../common/cpp/utils/JsiPromise.cpp | 58 +++++++++++++++++++ .../common/cpp/utils/JsiPromise.h | 39 +++++++++++++ .../cpp/wrappers/BaseAudioContextWrapper.cpp | 1 + .../ios/AudioAPIModule.mm | 4 +- .../src/core/BaseAudioContext.ts | 10 +++- .../react-native-audio-api/src/interfaces.ts | 2 +- 16 files changed, 179 insertions(+), 49 deletions(-) create mode 100644 packages/react-native-audio-api/common/cpp/utils/JsiPromise.cpp create mode 100644 packages/react-native-audio-api/common/cpp/utils/JsiPromise.h diff --git a/.gitignore b/.gitignore index 6d08e317..94a48503 100644 --- a/.gitignore +++ b/.gitignore @@ -83,3 +83,4 @@ react-native-audio-api*.tgz # Android .kotlin +.env diff --git a/apps/common-app/src/examples/AudioFile/AudioFile.tsx b/apps/common-app/src/examples/AudioFile/AudioFile.tsx index 91b861ac..3a189a9a 100644 --- a/apps/common-app/src/examples/AudioFile/AudioFile.tsx +++ b/apps/common-app/src/examples/AudioFile/AudioFile.tsx @@ -1,12 +1,16 @@ -import React from 'react'; +import React, { useCallback } from 'react'; import { useState, useRef, useEffect, FC } from 'react'; import { Container, Button } from '../../components'; import { + AudioBuffer, AudioContext, AudioBufferSourceNode, - AudioBuffer, } from 'react-native-audio-api'; +import { ActivityIndicator } from 'react-native'; + +const assetUrl = + 'https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview18/v4/9c/db/54/9cdb54b3-5c52-3063-b1ad-abe42955edb5/mzaf_520282131402737225.plus.aac.p.m4a'; const AudioFile: FC = () => { const [isPlaying, setIsPlaying] = useState(false); @@ -22,28 +26,28 @@ const AudioFile: FC = () => { audioBufferSourceNodeRef.current = audioContextRef.current.createBufferSource(); - audioBufferSourceNodeRef.current.buffer; + audioBufferSourceNodeRef.current.connect( audioContextRef.current.destination ); }; - const fetchAudioBuffer = async () => { + const fetchAudioBuffer = useCallback(async () => { if (!audioContextRef.current) { audioContextRef.current = new AudioContext(); } - setAudioBuffer( - // audioContextRef.current.decodeAudioDataSource( - // '/Users/maciejmakowski/projects/react-native-audio-api/apps/common-app/src/examples/AudioFile/runaway_kanye_west.mp3' - // ) - audioContextRef.current.decodeAudioDataSource( - 'https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview18/v4/9c/db/54/9cdb54b3-5c52-3063-b1ad-abe42955edb5/mzaf_520282131402737225.plus.aac.p.m4a' - ) - ); - }; + const buffer = + await audioContextRef.current.decodeAudioDataSource(assetUrl); + + setAudioBuffer(buffer); + }, []); const handlePress = () => { + if (!audioBuffer) { + return; + } + if (isPlaying) { audioBufferSourceNodeRef.current?.stop(); } else { @@ -65,11 +69,12 @@ const AudioFile: FC = () => { return () => { audioContextRef.current?.close(); }; - }, []); + }, [fetchAudioBuffer]); return (