Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 50 additions & 17 deletions apps/common-app/src/examples/Oscillator/Oscillator.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useRef, useState, useEffect, FC } from 'react';
import { StyleSheet, Text, View, Pressable } from 'react-native';
import { StyleSheet, Text, View, Pressable, Image } from 'react-native';
import {
AudioContext,
GainNode,
OscillatorNode,
StereoPannerNode,
} from 'react-native-audio-api';
import type { OscillatorType } from 'react-native-audio-api';
import type { OscillatorType, AudioBuffer, ConvolverNode } from 'react-native-audio-api';

import { Container, Slider, Spacer, Button } from '../../components';
import { layout, colors } from '../../styles';
Expand All @@ -29,16 +29,48 @@ const Oscillator: FC = () => {

const audioContextRef = useRef<AudioContext | null>(null);
const oscillatorRef = useRef<OscillatorNode | null>(null);
const oscillatorRef2 = useRef<OscillatorNode | null>(null);
const gainRef = useRef<GainNode | null>(null);
const panRef = useRef<StereoPannerNode | null>(null);
const convolverRef = useRef<ConvolverNode | null>(null);

const setup = () => {
useEffect(() => {
const fetchImpulseResponse = async () => {
if (!audioContextRef.current) {
audioContextRef.current = new AudioContext();
}

const length = audioContextRef.current.sampleRate * 2; // 2 seconds
const impulse = audioContextRef.current.createBuffer(2, length, audioContextRef.current.sampleRate);

for (let channel = 0; channel < impulse.numberOfChannels; channel++) {
const channelData = impulse.getChannelData(channel);
for (let i = 0; i < length; i++) {
// Exponentially decay the impulse
channelData[i] = (Math.random() * 2 - 1) * Math.pow(1 - i / length, 2);
}
}

convolverRef.current = audioContextRef.current?.createConvolver();
convolverRef.current.buffer = impulse;
}

fetchImpulseResponse();

return () => {
audioContextRef.current?.close();
};
}, []);

const setup = (reverb: boolean) => {
if (!audioContextRef.current) {
audioContextRef.current = new AudioContext();
}

oscillatorRef.current = audioContextRef.current.createOscillator();
oscillatorRef2.current = audioContextRef.current.createOscillator();
oscillatorRef.current.frequency.value = frequency;
oscillatorRef2.current.frequency.value = frequency;
oscillatorRef.current.detune.value = detune;
oscillatorRef.current.type = oscillatorType;

Expand All @@ -50,7 +82,12 @@ const Oscillator: FC = () => {

oscillatorRef.current.connect(gainRef.current);
gainRef.current.connect(panRef.current);
panRef.current.connect(audioContextRef.current.destination);
if (convolverRef.current && reverb) {
panRef.current.connect(convolverRef.current);
convolverRef.current.connect(audioContextRef.current.destination);
} else {
panRef.current.connect(audioContextRef.current.destination);
}
};

const handleGainChange = (newValue: number) => {
Expand Down Expand Up @@ -85,12 +122,17 @@ const Oscillator: FC = () => {
}
};

const handlePlayPause = () => {
const handlePlayPause = (reverb: boolean) => {
if (isPlaying) {
oscillatorRef.current?.stop(0);
} else {
setup();
setup(reverb);
oscillatorRef.current?.start(0);
oscillatorRef.current?.stop(audioContextRef.current?.currentTime!! + 2);
gainRef.current?.gain.setValueAtTime(0, audioContextRef.current?.currentTime!! + 2);
oscillatorRef2.current?.start(audioContextRef.current?.currentTime!! + 2);
oscillatorRef2.current?.connect(gainRef.current!);
oscillatorRef2.current?.stop(audioContextRef.current?.currentTime!! + 3);
}

setIsPlaying((prev) => !prev);
Expand All @@ -103,19 +145,10 @@ const Oscillator: FC = () => {
}
};

useEffect(() => {
if (!audioContextRef.current) {
audioContextRef.current = new AudioContext();
}

return () => {
audioContextRef.current?.close();
};
}, []);

return (
<Container centered>
<Button onPress={handlePlayPause} title={isPlaying ? 'Pause' : 'Play'} />
<Button onPress={() => handlePlayPause(false)} title={isPlaying ? 'Pause' : 'Play without reverb'} />
<Button onPress={() => handlePlayPause(true)} title={isPlaying ? 'Pause' : 'Play reverb'} />
<Spacer.Vertical size={49} />
<Slider
label="Gain"
Expand Down
26 changes: 14 additions & 12 deletions apps/fabric-example/ios/FabricExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,14 @@
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-FabricExample/Pods-FabricExample-frameworks-${CONFIGURATION}-input-files.xcfilelist",
);
inputPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-FabricExample/Pods-FabricExample-frameworks-${CONFIGURATION}-output-files.xcfilelist",
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-FabricExample/Pods-FabricExample-frameworks.sh\"\n";
Expand Down Expand Up @@ -230,10 +234,14 @@
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-FabricExample/Pods-FabricExample-resources-${CONFIGURATION}-input-files.xcfilelist",
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-FabricExample/Pods-FabricExample-resources-${CONFIGURATION}-output-files.xcfilelist",
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-FabricExample/Pods-FabricExample-resources.sh\"\n";
Expand All @@ -260,7 +268,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = WC59N2X3FV;
DEVELOPMENT_TEAM = A973VC3GVK;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = FabricExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.1;
Expand All @@ -274,7 +282,7 @@
"-ObjC",
"-lc++",
);
PRODUCT_BUNDLE_IDENTIFIER = "fdsfsdfsdcom-esegsefser.--PRODUCT-NAME-rfc1034identifier-";
PRODUCT_BUNDLE_IDENTIFIER = "fdsfsdfsdcom-esegsefser.--PRODUCT-NAME-rfc1034identifier-dydmichal";
PRODUCT_NAME = FabricExample;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
Expand All @@ -289,7 +297,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = WC59N2X3FV;
DEVELOPMENT_TEAM = A973VC3GVK;
INFOPLIST_FILE = FabricExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.1;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -302,7 +310,7 @@
"-ObjC",
"-lc++",
);
PRODUCT_BUNDLE_IDENTIFIER = "fdsfsdfsdcom-esegsefser.--PRODUCT-NAME-rfc1034identifier-";
PRODUCT_BUNDLE_IDENTIFIER = "fdsfsdfsdcom-esegsefser.--PRODUCT-NAME-rfc1034identifier-dydmichal";
PRODUCT_NAME = FabricExample;
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
Expand Down Expand Up @@ -378,10 +386,7 @@
"-DFOLLY_CFG_NO_COROUTINES=1",
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
);
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
OTHER_LDFLAGS = "$(inherited) ";
REACT_NATIVE_PATH = "${PODS_ROOT}/../../../../node_modules/react-native";
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG";
Expand Down Expand Up @@ -450,10 +455,7 @@
"-DFOLLY_CFG_NO_COROUTINES=1",
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
);
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
OTHER_LDFLAGS = "$(inherited) ";
REACT_NATIVE_PATH = "${PODS_ROOT}/../../../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <audioapi/HostObjects/PeriodicWaveHostObject.h>
#include <audioapi/HostObjects/StereoPannerNodeHostObject.h>
#include <audioapi/HostObjects/AnalyserNodeHostObject.h>
#include <audioapi/HostObjects/ConvolverNodeHostObject.h>
#include <audioapi/HostObjects/RecorderAdapterNodeHostObject.h>
#include <audioapi/HostObjects/StreamerNodeHostObject.h>

Expand Down Expand Up @@ -52,6 +53,7 @@ class BaseAudioContextHostObject : public JsiHostObject {
JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createBuffer),
JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createPeriodicWave),
JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createAnalyser),
JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createConvolver),
JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, decodeAudioData),
JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, decodeAudioDataSource),
JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, decodePCMAudioDataInBase64));
Expand Down Expand Up @@ -179,6 +181,13 @@ class BaseAudioContextHostObject : public JsiHostObject {
return jsi::Object::createFromHostObject(runtime, analyserHostObject);
}

JSI_HOST_FUNCTION(createConvolver) {
auto convolver = context_->createConvolver();
auto convolverHostObject =
std::make_shared<ConvolverNodeHostObject>(convolver);
return jsi::Object::createFromHostObject(runtime, convolverHostObject);
}

JSI_HOST_FUNCTION(decodeAudioDataSource) {
auto sourcePath = args[0].getString(runtime).utf8(runtime);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <audioapi/HostObjects/AudioNodeHostObject.h>
#include <audioapi/core/effects/ConvolverNode.h>
#include <audioapi/HostObjects/AudioBufferHostObject.h>
#include <iostream>

#include <memory>
#include <vector>

namespace audioapi {
using namespace facebook;

class ConvolverNodeHostObject : public AudioNodeHostObject {
public:
explicit ConvolverNodeHostObject(const std::shared_ptr<ConvolverNode> &node)
: AudioNodeHostObject(node) {
addGetters(JSI_EXPORT_PROPERTY_GETTER(ConvolverNodeHostObject, normalize),
JSI_EXPORT_PROPERTY_GETTER(ConvolverNodeHostObject, buffer));
addSetters(
JSI_EXPORT_PROPERTY_SETTER(ConvolverNodeHostObject, normalize),
JSI_EXPORT_PROPERTY_SETTER(ConvolverNodeHostObject, buffer));
}

JSI_PROPERTY_GETTER(normalize) {
auto convolverNode = std::static_pointer_cast<ConvolverNode>(node_);
return {convolverNode->getNormalize_()};
}

JSI_PROPERTY_GETTER(buffer) {
auto convolverNode = std::static_pointer_cast<ConvolverNode>(node_);
auto buffer = convolverNode->getBuffer();
auto bufferHostObject =
std::make_shared<AudioBufferHostObject>(buffer);
return jsi::Object::createFromHostObject(runtime, bufferHostObject);
}

JSI_PROPERTY_SETTER(normalize) {
auto convolverNode = std::static_pointer_cast<ConvolverNode>(node_);
convolverNode->setNormalize(value.getBool());
}

JSI_PROPERTY_SETTER(buffer) {
auto convolverNode =
std::static_pointer_cast<ConvolverNode>(node_);
if (value.isNull()) {
convolverNode->setBuffer(std::shared_ptr<AudioBuffer>(nullptr));
return;
}

auto bufferHostObject =
value.getObject(runtime).asHostObject<AudioBufferHostObject>(runtime);
convolverNode->setBuffer(bufferHostObject->audioBuffer_);
}
};
} // namespace audioapi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <audioapi/core/analysis/AnalyserNode.h>
#include <audioapi/core/destinations/AudioDestinationNode.h>
#include <audioapi/core/effects/BiquadFilterNode.h>
#include <audioapi/core/effects/ConvolverNode.h>
#include <audioapi/core/effects/CustomProcessorNode.h>
#include <audioapi/core/effects/GainNode.h>
#include <audioapi/core/effects/StereoPannerNode.h>
#include <audioapi/core/sources/AudioBuffer.h>
Expand All @@ -16,6 +18,7 @@
#include <audioapi/utils/AudioArray.h>
#include <audioapi/utils/AudioBus.h>
#include <audioapi/utils/CircularAudioArray.h>
#include <iostream>

namespace audioapi {

Expand Down Expand Up @@ -132,6 +135,12 @@ std::shared_ptr<AnalyserNode> BaseAudioContext::createAnalyser() {
return analyser;
}

std::shared_ptr<ConvolverNode> BaseAudioContext::createConvolver() {
auto convolver = std::make_shared<ConvolverNode>(this);
nodeManager_->addProcessingNode(convolver);
return convolver;
}

std::shared_ptr<AudioBuffer> BaseAudioContext::decodeAudioDataSource(
const std::string &path) {
auto audioBus = audioDecoder_->decodeWithFilePath(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class AudioBufferQueueSourceNode;
class AudioDecoder;
class AnalyserNode;
class AudioEventHandlerRegistry;
class ConvolverNode;
class IAudioEventHandlerRegistry;
class RecorderAdapterNode;
class StreamerNode;
Expand Down Expand Up @@ -59,6 +60,7 @@ class BaseAudioContext {
bool disableNormalization,
int length);
std::shared_ptr<AnalyserNode> createAnalyser();
std::shared_ptr<ConvolverNode> createConvolver();

std::shared_ptr<AudioBuffer> decodeAudioDataSource(const std::string &path);
std::shared_ptr<AudioBuffer> decodeAudioData(const void *data, size_t size);
Expand Down
Loading
Loading