Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/ios/audio destination node #93

Merged
merged 4 commits into from
Aug 21, 2024
Merged
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
9 changes: 6 additions & 3 deletions cpp/AudioContext/ios/AudioContextWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ namespace audiocontext
return std::make_shared<OscillatorNodeWrapper>(audiocontext_);
}

std::shared_ptr<AudioDestinationNodeWrapper> AudioContextWrapper::getDestination() const {
// TODO: Add AudioDestinationNode implementation
return std::make_shared<AudioDestinationNodeWrapper>();
std::shared_ptr<AudioDestinationNodeWrapper> AudioContextWrapper::getDestination() {
return std::make_shared<AudioDestinationNodeWrapper>(audiocontext_);
}

std::shared_ptr<GainNodeWrapper> AudioContextWrapper::createGain() {
Expand All @@ -36,5 +35,9 @@ namespace audiocontext
int AudioContextWrapper::getSampleRate() const {
return audiocontext_->getSampleRate();
}

void AudioContextWrapper::close() {
//TODO
}
} // namespace audiocontext
#endif
11 changes: 9 additions & 2 deletions cpp/AudioDestinationNode/AudioDestinationNodeWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#ifdef ANDROID
#include "AudioDestinationNode.h"
#include "AudioNodeWrapper.h"
#else
#include "IOSAudioDestinationNode.h"
#include "IOSAudioContext.h"
#endif

namespace audiocontext {
Expand All @@ -18,8 +21,12 @@ namespace audiocontext {
public:
explicit AudioDestinationNodeWrapper(AudioDestinationNode *destinationNode) : AudioNodeWrapper(destinationNode) {}
#else
public:
explicit AudioDestinationNodeWrapper() {}
private:
std::shared_ptr<IOSAudioDestinationNode> destination_;
public:
explicit AudioDestinationNodeWrapper(std::shared_ptr<IOSAudioContext> context) : AudioNodeWrapper() {
node_ = destination_ = std::make_shared<IOSAudioDestinationNode>(context);
}
#endif
};
} // namespace audiocontext
8 changes: 2 additions & 6 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Button, Platform, StyleSheet, Text, View } from 'react-native';
import { Button, StyleSheet, Text, View } from 'react-native';
import { useRef, useState, useEffect } from 'react';
import { Slider } from '@miblanchard/react-native-slider';
import { Kick } from './sound-engines/Kick';
Expand Down Expand Up @@ -48,11 +48,7 @@ const App: React.FC = () => {

oscillatorRef.current.connect(gainRef.current);
gainRef.current.connect(panRef.current);

if (Platform.OS === 'android') {
const destination = audioContextRef.current.destination;
panRef.current.connect(destination!);
}
panRef.current.connect(audioContextRef.current.destination);
}

return () => {
Expand Down
7 changes: 2 additions & 5 deletions example/src/sound-engines/Kick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
type GainNode,
type OscillatorNode,
} from 'react-native-audio-context';
import { Platform } from 'react-native';
import type { SoundEngine } from './SoundEngine';

export class Kick implements SoundEngine {
Expand All @@ -24,11 +23,9 @@ export class Kick implements SoundEngine {
setup() {
this.gain = this.audioContext.createGain();
this.oscillator = this.audioContext.createOscillator();
this.oscillator.connect(this.gain);

if (Platform.OS === 'android') {
this.gain.connect(this.audioContext.destination!);
}
this.oscillator.connect(this.gain);
this.gain.connect(this.audioContext.destination!);
}

play(time: number) {
Expand Down
12 changes: 12 additions & 0 deletions ios/nodes/AudioDestinationNode/AudioDestinationNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#import "AudioNode.h"

#import <limits.h>

@interface AudioDestinationNode : AudioNode

- (instancetype)initWithContext:(AudioContext *)context;
- (void)clean;

@end
23 changes: 23 additions & 0 deletions ios/nodes/AudioDestinationNode/AudioDestinationNode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#import <AudioDestinationNode.h>
#import "AudioContext.h"

@implementation AudioDestinationNode

- (instancetype)initWithContext:(AudioContext *)context {
if (self = [super initWithContext:context]) {
self.numberOfInputs = INT_MAX;
self.numberOfOutputs = 0;
}

return self;
}

- (void)clean {
// Do nothing
}

- (void)process:(float *)buffer frameCount:(AVAudioFrameCount)frameCount {
// Do nothing
}

@end
24 changes: 24 additions & 0 deletions ios/nodes/AudioDestinationNode/IOSAudioDestinationNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#ifdef __OBJC__ // when compiled as Objective-C++
#import <AudioDestinationNode.h>
#else // when compiled as C++
typedef struct objc_object AudioDestinationNode;
#endif // __OBJC__

#include "IOSAudioNode.h"
#include "IOSAudioContext.h"

#import <memory>

namespace audiocontext {
class IOSAudioDestinationNode : public IOSAudioNode {
public:
explicit IOSAudioDestinationNode(std::shared_ptr<IOSAudioContext> context);
~IOSAudioDestinationNode();

protected:
AudioDestinationNode *destination_;
};
} // namespace audiocontext

13 changes: 13 additions & 0 deletions ios/nodes/AudioDestinationNode/IOSAudioDestinationNode.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <IOSAudioDestinationNode.h>

namespace audiocontext {

IOSAudioDestinationNode::IOSAudioDestinationNode(std::shared_ptr<IOSAudioContext> context) {
audioNode_ = destination_ = [[AudioDestinationNode alloc] initWithContext:context->audioContext_];
}

IOSAudioDestinationNode::~IOSAudioDestinationNode() {
[destination_ clean];
audioNode_ = destination_ = nil;
}
} // namespace audiocontext
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ import type {
import { installACModule } from './utils/install';

export class AudioContext implements BaseAudioContext {
readonly destination: AudioDestinationNode | null;
readonly destination: AudioDestinationNode;
readonly sampleRate: number;

constructor() {
this.destination = null;

if (global.__AudioContext == null) {
installACModule();
}

if (Platform.OS === 'android') {
this.destination = global.__AudioContext.destination;
}
this.destination = global.__AudioContext.destination;
this.sampleRate = global.__AudioContext.sampleRate;
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from '../types';

type AudioContext = {
destination: AudioDestinationNode | null;
destination: AudioDestinationNode;
state: ContextState;
sampleRate: number;
currentTime: number;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface BaseAudioContext {
readonly destination: AudioDestinationNode | null;
readonly destination: AudioDestinationNode;
readonly state: ContextState;
readonly sampleRate: number;
readonly currentTime: number;
Expand Down