Skip to content

Commit

Permalink
feat: add possibility to read frequency value
Browse files Browse the repository at this point in the history
  • Loading branch information
hubgan committed Jul 16, 2024
1 parent 9db5511 commit 8a03471
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
8 changes: 7 additions & 1 deletion cpp/OscillatorHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ namespace audiocontext {
});
}

if (propName == "frequency") {
return jsi::Value(frequency_);
}

throw std::runtime_error("Not yet implemented!");
}

Expand All @@ -45,7 +49,9 @@ namespace audiocontext {
auto propName = propNameId.utf8(runtime);

if (propName == "frequency") {
return platformOscillator_.changeFrequency(static_cast<float>(value.asNumber()));
float frequency = static_cast<float>(value.asNumber());
frequency_ = frequency;
return platformOscillator_.changeFrequency(frequency);
}

throw std::runtime_error("Not yet implemented!");
Expand Down
53 changes: 28 additions & 25 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -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<OscillatorNode | null>(null);
const osc2 = useRef<OscillatorNode | null>(null);
const osc = useRef<OscillatorNode | null>(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 (
<View style={styles.container}>
Expand All @@ -46,8 +50,7 @@ const App: React.FC = () => {
onChangeText={(text) => setFrequency(text)}
/>
<Button title="Change frequency" onPress={changeFrequency} />
<Button title="Start1" onPress={start2} />
<Button title="Stop1" onPress={stop2} />
<Button title="Get current frequency" onPress={getFrequency} />
</View>
);
};
Expand Down
1 change: 0 additions & 1 deletion ios/PlatformOscillator.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ namespace audiocontext {

protected:
IOSOscillator *iosOscillator_;
float frequency_;
};
} // namespace audiocontext
2 changes: 1 addition & 1 deletion ios/PlatformOscillator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace audiocontext {

PlatformOscillator::PlatformOscillator(const float frequency) : frequency_(frequency) {
PlatformOscillator::PlatformOscillator(const float frequency) {
iosOscillator_ = [[IOSOscillator alloc] init:frequency];
}

Expand Down

0 comments on commit 8a03471

Please sign in to comment.