Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,45 @@
#include <audioapi/utils/CircularOverflowableAudioArray.h>

#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

namespace audioapi {

namespace {
/// Maps the JS-facing preset name to Oboe's InputPreset. An unknown or empty
/// name yields no preset call, preserving Oboe's own default
/// (InputPreset::VoiceRecognition) exactly as before this option existed.
std::optional<oboe::InputPreset> inputPresetFromString(const std::string &name) {
if (name == "generic") {
return oboe::InputPreset::Generic;
}
if (name == "camcorder") {
return oboe::InputPreset::Camcorder;
}
if (name == "voiceRecognition") {
return oboe::InputPreset::VoiceRecognition;
}
if (name == "voiceCommunication") {
return oboe::InputPreset::VoiceCommunication;
}
if (name == "unprocessed") {
return oboe::InputPreset::Unprocessed;
}
if (name == "voicePerformance") {
return oboe::InputPreset::VoicePerformance;
}
return std::nullopt;
}
} // namespace

AndroidAudioRecorder::AndroidAudioRecorder(
const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry)
const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry,
const std::string &inputPreset)
: AudioRecorder(audioEventHandlerRegistry),
inputPreset_(inputPreset),
streamSampleRate_(0.0),
streamChannelCount_(0),
streamMaxBufferSizeInFrames_(0) {}
Expand Down Expand Up @@ -74,6 +104,10 @@ Result<NoneType, std::string> AndroidAudioRecorder::openAudioStream() {
->setDataCallback(shared_from_this())
->setErrorCallback(shared_from_this());

if (auto preset = inputPresetFromString(inputPreset_)) {
builder.setInputPreset(*preset);
}

auto result = builder.openStream(mStream_);

if (result != oboe::Result::OK || mStream_ == nullptr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class AndroidAudioRecorder : public oboe::AudioStreamCallback,
public std::enable_shared_from_this<AndroidAudioRecorder> {
public:
explicit AndroidAudioRecorder(
const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry);
const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry,
const std::string &inputPreset = "");
~AndroidAudioRecorder() override;
void cleanup();

Expand Down Expand Up @@ -63,6 +64,7 @@ class AndroidAudioRecorder : public oboe::AudioStreamCallback,
private:
std::shared_ptr<AudioBuffer> deinterleavingBuffer_;

std::string inputPreset_;
std::atomic<float> streamSampleRate_;
int32_t streamChannelCount_;
int32_t streamMaxBufferSizeInFrames_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@ class AudioAPIModuleInstaller {
const jsi::Value &thisValue,
const jsi::Value *args,
size_t count) -> jsi::Value {
std::string androidInputPreset;
if (count > 0 && args[0].isString()) {
androidInputPreset = args[0].getString(runtime).utf8(runtime);
}
auto audioRecorderHostObject = std::make_shared<AudioRecorderHostObject>(
audioEventHandlerRegistry, &runtime, jsCallInvoker);
audioEventHandlerRegistry, &runtime, jsCallInvoker, androidInputPreset);

auto jsiObject = jsi::Object::createFromHostObject(runtime, audioRecorderHostObject);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ namespace audioapi {
AudioRecorderHostObject::AudioRecorderHostObject(
const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry,
jsi::Runtime *runtime,
const std::shared_ptr<react::CallInvoker> &callInvoker) {
const std::shared_ptr<react::CallInvoker> &callInvoker,
const std::string &androidInputPreset) {
#ifdef ANDROID
audioRecorder_ = std::make_shared<AndroidAudioRecorder>(audioEventHandlerRegistry);
audioRecorder_ = std::make_shared<AndroidAudioRecorder>(audioEventHandlerRegistry, androidInputPreset);
#else
// The input preset is an Android concept (Oboe); iOS configures its input
// through the AVAudioSession mode instead.
audioRecorder_ = std::make_shared<IOSAudioRecorder>(audioEventHandlerRegistry);
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <audioapi/jsi/JsiPromise.h>

#include <memory>
#include <string>

namespace audioapi {
using namespace facebook;
Expand All @@ -17,7 +18,8 @@ class AudioRecorderHostObject : public HostObject {
AudioRecorderHostObject(
const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry,
jsi::Runtime *runtime,
const std::shared_ptr<react::CallInvoker> &callInvoker);
const std::shared_ptr<react::CallInvoker> &callInvoker,
const std::string &androidInputPreset);

JSI_HOST_FUNCTION_DECL(start);
JSI_HOST_FUNCTION_DECL(stop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare global {
sampleRate: number
) => IOfflineAudioContext;

var createAudioRecorder: () => IAudioRecorder;
var createAudioRecorder: (androidInputPreset: string) => IAudioRecorder;

var createAudioBuffer: (
numberOfChannels: number,
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-audio-api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export * from './Audio';
export { default as Audio } from './Audio';
export { default as AudioControls } from './Audio/controls/AudioControls';
export type { MediaElementAudioSourceOptions } from './core/MediaElementAudioSourceNode';
export type { AudioRecorderOptions, AndroidInputPreset } from './core/AudioRecorder';
export type { default as AudioEventSubscription } from './events/AudioEventSubscription';
export { default as FilePreset } from './utils/filePresets';

Expand Down
27 changes: 25 additions & 2 deletions packages/react-native-audio-api/src/core/AudioRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ function withDefaultOptions(
};
}

/**
* Options applied when the recorder is created.
*/
export interface AudioRecorderOptions {
/**
* Android only: the Oboe input preset for the capture stream. Android
* routes the microphone through a preprocessing chain selected by this
* preset; Oboe's implicit default is `voiceRecognition`, which applies NO
* acoustic echo cancellation. Duplex voice use cases (VoIP-style apps that
* play audio while recording) should pass `voiceCommunication` to engage
* the platform AEC/NS chain. Has no effect on iOS.
*/
androidInputPreset?: AndroidInputPreset;
}

export type AndroidInputPreset =
| 'generic'
| 'camcorder'
| 'voiceRecognition'
| 'voiceCommunication'
| 'unprocessed'
| 'voicePerformance';

export default class AudioRecorder {
protected onAudioReadySubscription: AudioEventSubscription | null = null;
protected onErrorSubscription: AudioEventSubscription | null = null;
Expand All @@ -51,8 +74,8 @@ export default class AudioRecorder {
globalThis.AudioEventEmitter
);

constructor() {
this.recorder = globalThis.createAudioRecorder();
constructor(options?: AudioRecorderOptions) {
this.recorder = globalThis.createAudioRecorder(options?.androidInputPreset ?? '');
}

/**
Expand Down