diff --git a/cpp/OscillatorHostObject.cpp b/cpp/OscillatorHostObject.cpp index 0c721c39..aa28040f 100644 --- a/cpp/OscillatorHostObject.cpp +++ b/cpp/OscillatorHostObject.cpp @@ -37,6 +37,10 @@ namespace audiocontext { }); } + if (propName == "frequency") { + return jsi::Value(frequency_); + } + throw std::runtime_error("Not yet implemented!"); } @@ -45,7 +49,9 @@ namespace audiocontext { auto propName = propNameId.utf8(runtime); if (propName == "frequency") { - return platformOscillator_.changeFrequency(static_cast(value.asNumber())); + float frequency = static_cast(value.asNumber()); + frequency_ = frequency; + return platformOscillator_.changeFrequency(frequency); } throw std::runtime_error("Not yet implemented!"); diff --git a/example/src/App.tsx b/example/src/App.tsx index 2c403b20..fb5f2389 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -1,41 +1,45 @@ -import React, { useEffect, useRef, useState } from 'react'; +import React, { useCallback, useRef, useState } from 'react'; import { StyleSheet, View, Button, TextInput } from 'react-native'; import { AudioContext, type OscillatorNode } from 'react-native-audio-context'; const App: React.FC = () => { const [frequency, setFrequency] = useState('440'); - const osc1 = useRef(null); - const osc2 = useRef(null); + const osc = useRef(null); - useEffect(() => { - if (!osc2.current) { + const start = useCallback(() => { + if (!osc.current) { const context = new AudioContext(); - osc1.current = context.createOscillator(440); - osc2.current = context.createOscillator(880); + osc.current = context.createOscillator(+frequency); } + + osc.current.start(); + }, [frequency]); + + const stop = useCallback(() => { + if (!osc.current) { + return; + } + + osc.current.stop(); }, []); - const start = () => { - osc1.current?.start(); - }; + const changeFrequency = useCallback(() => { + if (!osc.current) { + return; + } - const stop = () => { - osc1.current?.stop(); - }; + osc.current.frequency = parseFloat(frequency); + }, [frequency]); - const changeFrequency = () => { - if (osc1.current) { - osc1.current.frequency = parseFloat(frequency); + const getFrequency = useCallback(() => { + if (!osc.current) { + return; } - }; - const start2 = () => { - osc2.current?.start(); - }; + console.log(osc.current.frequency); - const stop2 = () => { - osc2.current?.stop(); - }; + return osc.current.frequency; + }, []); return ( @@ -46,8 +50,7 @@ const App: React.FC = () => { onChangeText={(text) => setFrequency(text)} />