Skip to content
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ const AudioVisualizer: React.FC = () => {
}

if (!analyserRef.current) {
analyserRef.current = audioContextRef.current.createAnalyser();
analyserRef.current.fftSize = FFT_SIZE;
analyserRef.current.smoothingTimeConstant = 0.2;

analyserRef.current = new AnalyserNode(audioContextRef.current, { fftSize: FFT_SIZE, smoothingTimeConstant: 0.2 });
analyserRef.current.connect(audioContextRef.current.destination);
}

Expand Down
12 changes: 12 additions & 0 deletions packages/audiodocs/docs/core/audio-node.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ If no arguments provided node disconnects from all outgoing connections.

#### Returns `undefined`.

### `AudioNodeOptions`

It is used to constructing majority of all `AudioNodes`.

| Parameter | Type | Default | Description |
| :---: | :---: | :----: | :---- |
| `channelCount` <Optional /> | `number` | 2 | Indicates number of channels used in mixing of node. |
| `channelCountMode` <Optional /> | [`ChannelCountMode`](/docs/types/channel-count-mode) | `max` | Determines how the number of input channels affects the number of output channels in an audio node. |
| `channelInterpretation` <Optional /> | [`ChannelInterpretation`](/docs/types/channel-interpretation) | `speakers` | Specifies how input channels are mapped out to output channels when the number of them are different. |

If any of these values are not provided, default values are used.

## Remarks

#### `numberOfInputs`
Expand Down
15 changes: 14 additions & 1 deletion packages/audiodocs/docs/effects/gain-node.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sidebar_position: 2
---

import AudioNodePropsTable from "@site/src/components/AudioNodePropsTable"
import { ReadOnly } from '@site/src/components/Badges';
import { Optional, ReadOnly } from '@site/src/components/Badges';
import { useGainAdsrPlayground } from '@site/src/components/InteractivePlayground/GainAdsrExample/useGainAdsrPlayground';
import InteractivePlayground from '@site/src/components/InteractivePlayground';

Expand Down Expand Up @@ -43,6 +43,19 @@ You can read more about envelopes and ADSR on [Wikipedia](<https://en.wikipedia.

## Constructor

```tsx
constructor(context: BaseAudioContext, gainOptions?: GainOptions)
```

### `GainOptions`

Inherits all properties from [`AudioNodeOptions`](/docs/core/audio-node#audionodeoptions)

| Parameter | Type | Default | Description |
| :---: | :---: | :----: | :---- |
| `gain` <Optional /> | `number` | 1.0 | Number representing gain value |

Or by using `BaseAudioContext` factory method:
[`BaseAudioContext.createGain()`](/docs/core/base-audio-context#creategain)

## Properties
Expand Down
15 changes: 14 additions & 1 deletion packages/audiodocs/docs/effects/stereo-panner-node.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sidebar_position: 4
---

import AudioNodePropsTable from "@site/src/components/AudioNodePropsTable"
import { ReadOnly } from '@site/src/components/Badges';
import { Optional, ReadOnly } from '@site/src/components/Badges';

# StereoPannerNode

Expand All @@ -15,6 +15,19 @@ The `StereoPannerNode` interface represents the change in ratio between two outp

## Constructor

```tsx
constructor(context: BaseAudioContext, stereoPannerOptions?: StereoPannerOptions)
```

### `StereoPannerOptions`

Inherits all properties from [`AudioNodeOptions`](/docs/core/audio-node#audionodeoptions)

| Parameter | Type | Default | Description |
| :---: | :---: | :----: | :---- |
| `pan` <Optional /> | `number` | 0.0 | Number representing pan value |

Or by using `BaseAudioContext` factory method:
[`BaseAudioContext.createStereoPanner()`](/docs/core/base-audio-context#createstereopanner)

## Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <audioapi/HostObjects/sources/WorkletSourceNodeHostObject.h>
#include <audioapi/core/BaseAudioContext.h>

#include <audioapi/HostObjects/utils/NodeOptionsParser.h>
#include <memory>
#include <vector>

Expand Down Expand Up @@ -153,7 +154,10 @@ JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createRecorderAdapter) {
}

JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createOscillator) {
auto oscillator = context_->createOscillator();
auto options = args[0].asObject(runtime);
std::shared_ptr<OscillatorOptions> oscillatorOptions =
audioapi::option_parser::parseOscillatorOptions(runtime, options);
auto oscillator = context_->createOscillator(oscillatorOptions);
auto oscillatorHostObject = std::make_shared<OscillatorNodeHostObject>(oscillator);
return jsi::Object::createFromHostObject(runtime, oscillatorHostObject);
}
Expand All @@ -167,39 +171,55 @@ JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createStreamer) {
}

JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createConstantSource) {
auto constantSource = context_->createConstantSource();
auto options = args[0].asObject(runtime);
std::shared_ptr<ConstantSourceOptions> constantSourceOptions =
audioapi::option_parser::parseConstantSourceOptions(runtime, options);
auto constantSource = context_->createConstantSource(constantSourceOptions);
auto constantSourceHostObject = std::make_shared<ConstantSourceNodeHostObject>(constantSource);
return jsi::Object::createFromHostObject(runtime, constantSourceHostObject);
}

JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createGain) {
auto gain = context_->createGain();
auto options = args[0].asObject(runtime);
std::shared_ptr<GainOptions> gainOptions =
audioapi::option_parser::parseGainOptions(runtime, options);
auto gain = context_->createGain(gainOptions);
auto gainHostObject = std::make_shared<GainNodeHostObject>(gain);
return jsi::Object::createFromHostObject(runtime, gainHostObject);
}

JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createStereoPanner) {
auto stereoPanner = context_->createStereoPanner();
auto options = args[0].asObject(runtime);
std::shared_ptr<StereoPannerOptions> stereoPannerOptions =
audioapi::option_parser::parseStereoPannerOptions(runtime, options);
auto stereoPanner = context_->createStereoPanner(stereoPannerOptions);
auto stereoPannerHostObject = std::make_shared<StereoPannerNodeHostObject>(stereoPanner);
return jsi::Object::createFromHostObject(runtime, stereoPannerHostObject);
}

JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createBiquadFilter) {
auto biquadFilter = context_->createBiquadFilter();
auto options = args[0].asObject(runtime);
std::shared_ptr<BiquadFilterOptions> biquadFilterOptions =
audioapi::option_parser::parseBiquadFilterOptions(runtime, options);
auto biquadFilter = context_->createBiquadFilter(biquadFilterOptions);
auto biquadFilterHostObject = std::make_shared<BiquadFilterNodeHostObject>(biquadFilter);
return jsi::Object::createFromHostObject(runtime, biquadFilterHostObject);
}

JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createBufferSource) {
auto pitchCorrection = args[0].asBool();
auto bufferSource = context_->createBufferSource(pitchCorrection);
auto options = args[0].asObject(runtime);
std::shared_ptr<AudioBufferSourceOptions> audioBufferSourceOptions =
audioapi::option_parser::parseAudioBufferSourceOptions(runtime, options);
auto bufferSource = context_->createBufferSource(audioBufferSourceOptions);
auto bufferSourceHostObject = std::make_shared<AudioBufferSourceNodeHostObject>(bufferSource);
return jsi::Object::createFromHostObject(runtime, bufferSourceHostObject);
}

JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createBufferQueueSource) {
auto pitchCorrection = args[0].asBool();
auto bufferSource = context_->createBufferQueueSource(pitchCorrection);
auto options = args[0].asObject(runtime);
std::shared_ptr<BaseAudioBufferSourceOptions> baseAudioBufferSourceOptions =
audioapi::option_parser::parseBaseAudioBufferSourceOptions(runtime, options);
auto bufferSource = context_->createBufferQueueSource(baseAudioBufferSourceOptions);
auto bufferStreamSourceHostObject =
std::make_shared<AudioBufferQueueSourceNodeHostObject>(bufferSource);
return jsi::Object::createFromHostObject(runtime, bufferStreamSourceHostObject);
Expand Down Expand Up @@ -243,20 +263,19 @@ JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createPeriodicWave) {
}

JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createAnalyser) {
auto analyser = context_->createAnalyser();
auto options = args[0].asObject(runtime);
std::shared_ptr<AnalyserOptions> analyserOptions =
audioapi::option_parser::parseAnalyserOptions(runtime, options);
auto analyser = context_->createAnalyser(analyserOptions);
auto analyserHostObject = std::make_shared<AnalyserNodeHostObject>(analyser);
return jsi::Object::createFromHostObject(runtime, analyserHostObject);
}

JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createConvolver) {
auto disableNormalization = args[1].getBool();
std::shared_ptr<ConvolverNode> convolver;
if (args[0].isUndefined()) {
convolver = context_->createConvolver(nullptr, disableNormalization);
} else {
auto bufferHostObject = args[0].getObject(runtime).asHostObject<AudioBufferHostObject>(runtime);
convolver = context_->createConvolver(bufferHostObject->audioBuffer_, disableNormalization);
}
auto options = args[0].asObject(runtime);
std::shared_ptr<ConvolverOptions> convolverOptions =
audioapi::option_parser::parseConvolverOptions(runtime, options);
auto convolver = context_->createConvolver(convolverOptions);
auto convolverHostObject = std::make_shared<ConvolverNodeHostObject>(convolver);
auto jsiObject = jsi::Object::createFromHostObject(runtime, convolverHostObject);
if (!args[0].isUndefined()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

#include <memory>

#include <audioapi/core/effects/PeriodicWave.h>
#include <audioapi/core/sources/AudioBuffer.h>
#include <audioapi/core/types/BiquadFilterType.h>
#include <audioapi/core/types/ChannelCountMode.h>
#include <audioapi/core/types/ChannelInterpretation.h>
#include <audioapi/core/types/OscillatorType.h>

namespace audioapi {
struct AudioNodeOptions {
int channelCount = 2;
ChannelCountMode channelCountMode = ChannelCountMode::MAX;
ChannelInterpretation channelInterpretation = ChannelInterpretation::SPEAKERS;
};

struct GainOptions : AudioNodeOptions {
float gain = 1.0f;
};

struct StereoPannerOptions : AudioNodeOptions {
float pan = 0.0f;
};

struct ConvolverOptions : AudioNodeOptions {
std::shared_ptr<AudioBuffer> bus = nullptr;
bool disableNormalization = false;
};

struct ConstantSourceOptions {
float offset = 1.0f;
};

struct AnalyserOptions : AudioNodeOptions {
int fftSize = 2048;
float minDecibels = -100.0f;
float maxDecibels = -30.0f;
float smoothingTimeConstant = 0.8f;
};

struct BiquadFilterOptions : AudioNodeOptions {
BiquadFilterType type = BiquadFilterType::LOWPASS;
float frequency = 350.0f;
float detune = 0.0f;
float Q = 1.0f;
float gain = 0.0f;
};

struct OscillatorOptions {
std::shared_ptr<PeriodicWave> periodicWave = nullptr;
float frequency = 440.0f;
float detune = 0.0f;
OscillatorType type = OscillatorType::SINE;
};

struct BaseAudioBufferSourceOptions {
float detune = 0.0f;
bool pitchCorrection = false;
float playbackRate = 1.0f;
};

struct AudioBufferSourceOptions : BaseAudioBufferSourceOptions {
std::shared_ptr<AudioBuffer> buffer = nullptr;
bool loop = false;
float loopStart = 0.0f;
float loopEnd = 0.0f;
};
} // namespace audioapi
Loading