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

Refactor/panning algorithm #102

Merged
merged 3 commits into from
Aug 27, 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
16 changes: 12 additions & 4 deletions android/src/main/java/com/audiocontext/nodes/StereoPannerNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import com.audiocontext.context.BaseAudioContext
import com.audiocontext.parameters.AudioParam
import com.audiocontext.parameters.PlaybackParameters
import com.audiocontext.utils.Constants
import kotlin.math.min
import kotlin.math.*

// https://webaudio.github.io/web-audio-api/#stereopanner-algorithm

class StereoPannerNode(context: BaseAudioContext): AudioNode(context) {
override val numberOfInputs: Int = 1
Expand All @@ -16,9 +18,15 @@ class StereoPannerNode(context: BaseAudioContext): AudioNode(context) {

@RequiresApi(Build.VERSION_CODES.N)
override fun process(playbackParameters: PlaybackParameters) {
val currentTime = context.getCurrentTime()
playbackParameters.leftPan *= min(1.0 - pan.getValueAtTime(currentTime), 1.0)
playbackParameters.rightPan *= min(1.0 + pan.getValueAtTime(currentTime), 1.0)
val pan = pan.getValueAtTime(context.getCurrentTime())
val x = (pan + 1) / 2;

val gainL = cos(x * PI / 2);
val gainR = sin(x * PI / 2);

playbackParameters.leftPan *= gainL
playbackParameters.rightPan *= gainR

super.process(playbackParameters)
}
}
15 changes: 11 additions & 4 deletions ios/nodes/StereoPannerNode/StereoPannerNode.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#import "StereoPannerNode.h"
#import "AudioContext.h"

// https://webaudio.github.io/web-audio-api/#stereopanner-algorithm

@implementation StereoPannerNode

- (instancetype)initWithContext:(AudioContext *)context {
Expand All @@ -18,10 +20,15 @@ - (void)cleanup {
}

- (void)processWithParameters:(PlaybackParameters *)parameters {
double currentTime = [self.context getCurrentTime];
parameters.leftGain *= fmin(1.0 - [_panParam getValueAtTime:currentTime], 1.0);
parameters.rightGain *= fmin(1.0 + [_panParam getValueAtTime:currentTime], 1.0);

double pan = [_panParam getValueAtTime:[self.context getCurrentTime]];
double x = (pan + 1) / 2;

double gainL = cos(x * M_PI / 2);
double gainR = sin(x * M_PI / 2);

parameters.leftGain *= gainL;
parameters.rightGain *= gainR;

[super processWithParameters:parameters];
}

Expand Down