-
-
Notifications
You must be signed in to change notification settings - Fork 29
[blocked] feat: worklets support #625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
poneciak57
wants to merge
31
commits into
main
Choose a base branch
from
feat/worklets-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,516
−1,908
Open
Changes from 21 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
08a1d81
feat: simple worklet callback (ios only)
poneciak57 3c603f1
fix: fixed worklets usage and dependencies
poneciak57 389dee3
feat: simple worklet callback (android)
poneciak57 585729d
chore: formatting and podfile.lock
poneciak57 e143434
feat: created example showcasing worklets usage
poneciak57 79946ca
fix: fixed issue with worklets sideeffects not being visible
poneciak57 da987ca
chore: formatting and package.json deps updt
poneciak57 45c5833
fix: fixed test and added feature flag
poneciak57 db5829c
chore: fixed yarn.lock and formatting
poneciak57 2bcdb29
chore: removed worklet part from recorder example
poneciak57 1a20444
feat: moved all worklets functionality into runner
poneciak57 e711fdb
feat: made worklets completly optional (android only)
poneciak57 e23e0a2
chore: minor changes
poneciak57 c0e5845
feat: made worklets completly optional (ios too)
poneciak57 490b2d3
chore: ios imports formatting
poneciak57 345f480
fix: added error for using worklets callback when worklets unavailable
poneciak57 11528ba
chore: neatpicks
poneciak57 470599c
docs: updated docs
poneciak57 6bf19fa
fix: performed requested changes
poneciak57 fcb4389
Merge branch 'main' into feat/worklets-support
poneciak57 4fee078
fix: removed unused kotlin import
poneciak57 211082c
feat: worklet node implementation
poneciak57 21dbc75
docs: small fixes
poneciak57 6c0bba8
fix: fixed tests
poneciak57 e62fcb7
docs: added documentation for worklet node
poneciak57 9f6772c
chore: fixed formatting
poneciak57 4c3c080
chore: updated podfile lock
poneciak57 fc6203b
Merge branch 'main' into feat/worklets-support
poneciak57 a755afd
chore: cmake redundancy fix
poneciak57 1ff0b4c
chore: removed worklets from recorder
poneciak57 854fdc0
feat: switched to array buffers for faster arguments preparation
poneciak57 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -922,4 +922,4 @@ is-windows/index.js: | |
* Copyright © 2015-2018, Jon Schlinkert. | ||
* Released under the MIT License. | ||
*) | ||
*/ | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { useEffect, useRef } from "react"; | ||
import { Text, Button, View, StyleSheet } from 'react-native'; | ||
import { | ||
AudioContext, | ||
AudioManager, | ||
AudioRecorder, | ||
RecorderAdapterNode, | ||
} from 'react-native-audio-api'; | ||
import { Container } from "../../components"; | ||
import { Extrapolation, useSharedValue } from "react-native-reanimated"; | ||
import Animated, { | ||
useAnimatedStyle, | ||
withSpring, | ||
interpolate, | ||
} from "react-native-reanimated"; | ||
|
||
|
||
function Worklets() { | ||
const SAMPLE_RATE = 16000; | ||
const recorderRef = useRef<AudioRecorder | null>(null); | ||
const aCtxRef = useRef<AudioContext | null>(null); | ||
const recorderAdapterRef = useRef<RecorderAdapterNode | null>(null); | ||
|
||
const bar0 = useSharedValue(0); | ||
const bar1 = useSharedValue(0); | ||
const bar2 = useSharedValue(0); // center bar | ||
const bar3 = useSharedValue(0); | ||
const bar4 = useSharedValue(0); | ||
|
||
useEffect(() => { | ||
AudioManager.setAudioSessionOptions({ | ||
iosCategory: 'playAndRecord', | ||
iosMode: 'spokenAudio', | ||
iosOptions: ['defaultToSpeaker', 'allowBluetoothA2DP'], | ||
}); | ||
|
||
AudioManager.requestRecordingPermissions(); | ||
recorderRef.current = new AudioRecorder({ | ||
sampleRate: SAMPLE_RATE, | ||
bufferLengthInSamples: 512, | ||
}); | ||
}, []); | ||
|
||
const start = () => { | ||
if (!recorderRef.current) { | ||
console.error("Recorder is not initialized"); | ||
return; | ||
} | ||
|
||
recorderRef.current.onAudioReadyWorklet((audioData: Float32Array, timestamp: number) => { | ||
'worklet'; | ||
|
||
// Calculates RMS amplitude | ||
let sum = 0; | ||
for (let i = 0; i < audioData.length; i++) { | ||
sum += audioData[i] * audioData[i]; | ||
} | ||
const rms = Math.sqrt(sum / audioData.length); | ||
const scaledAmplitude = Math.min(rms * 150, 1); | ||
|
||
console.log(`RMS: ${rms}, Scaled: ${scaledAmplitude}`); | ||
|
||
bar0.value = bar1.value; | ||
bar1.value = bar2.value; | ||
bar3.value = bar2.value; | ||
bar4.value = bar3.value; | ||
bar2.value = scaledAmplitude; | ||
}); | ||
|
||
aCtxRef.current = new AudioContext({ sampleRate: SAMPLE_RATE }); | ||
recorderAdapterRef.current = aCtxRef.current.createRecorderAdapter(); | ||
recorderAdapterRef.current.connect(aCtxRef.current.destination); | ||
recorderRef.current.connect(recorderAdapterRef.current); | ||
recorderRef.current.start(); | ||
console.log("Recording started"); | ||
|
||
if (aCtxRef.current.state === 'suspended') { | ||
aCtxRef.current.resume(); | ||
} | ||
} | ||
|
||
const stop = () => { | ||
if (!recorderRef.current) { | ||
console.error("Recorder is not initialized"); | ||
return; | ||
} | ||
recorderRef.current.stop(); | ||
recorderAdapterRef.current = null; | ||
aCtxRef.current = null; | ||
console.log("Recording stopped"); | ||
bar0.value = 0; | ||
bar1.value = 0; | ||
bar2.value = 0; | ||
bar3.value = 0; | ||
bar4.value = 0; | ||
} | ||
|
||
const createBarStyle = (index: number) => { | ||
return useAnimatedStyle(() => { | ||
let amplitude = 0; | ||
|
||
switch (index) { | ||
case 0: amplitude = bar0.value; break; | ||
case 1: amplitude = bar1.value; break; | ||
case 2: amplitude = bar2.value; break; | ||
case 3: amplitude = bar3.value; break; | ||
case 4: amplitude = bar4.value; break; | ||
} | ||
|
||
const centerIndex = 2; | ||
const distanceFromCenter = Math.abs(index - centerIndex); | ||
|
||
const height = interpolate( | ||
amplitude, | ||
[0, 1], | ||
[10, 200], | ||
Extrapolation.CLAMP | ||
); | ||
|
||
const backgroundColor = interpolate( | ||
amplitude, | ||
[0, 0.5, 1], | ||
[0, 0.5, 1], | ||
Extrapolation.CLAMP | ||
); | ||
|
||
const barWidth = 40 - (distanceFromCenter * 5); | ||
const opacity = 1 - (distanceFromCenter * 0.15); | ||
|
||
return { | ||
height: withSpring(height, { damping: 20, stiffness: 200 }), | ||
width: barWidth, | ||
backgroundColor: `rgba(${Math.floor(backgroundColor * 255)}, ${Math.floor((1 - backgroundColor) * 255)}, 100, ${opacity})`, | ||
}; | ||
}); | ||
}; | ||
|
||
return ( | ||
<Container> | ||
<Text style={styles.title}>Audio Worklets Visualizer</Text> | ||
<Text style={styles.subtitle}>Speak into the microphone to see the animation</Text> | ||
|
||
<View style={styles.visualizer}> | ||
<View style={styles.barsContainer}> | ||
{Array.from({ length: 5 }, (_, index) => ( | ||
<Animated.View | ||
key={index} | ||
style={[styles.bar, createBarStyle(index)]} | ||
/> | ||
))} | ||
</View> | ||
</View> | ||
|
||
<Button onPress={start} title="Start Recording" /> | ||
<Button onPress={stop} title="Stop Recording" /> | ||
</Container> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
title: { | ||
fontSize: 20, | ||
fontWeight: 'bold', | ||
marginBottom: 10, | ||
textAlign: 'center', | ||
}, | ||
subtitle: { | ||
fontSize: 14, | ||
color: '#666', | ||
marginBottom: 30, | ||
textAlign: 'center', | ||
}, | ||
visualizer: { | ||
height: 250, | ||
justifyContent: 'flex-end', | ||
alignItems: 'center', | ||
marginVertical: 30, | ||
backgroundColor: '#f0f0f0', | ||
borderRadius: 10, | ||
padding: 20, | ||
}, | ||
barsContainer: { | ||
flexDirection: 'row', | ||
alignItems: 'flex-end', | ||
justifyContent: 'center', | ||
gap: 8, | ||
}, | ||
bar: { | ||
borderRadius: 20, | ||
minHeight: 10, | ||
}, | ||
}); | ||
|
||
export default Worklets; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.