Move audio from left to right and right to left in iOS using Audio Pan

68 views Asked by At

I am writing code where user can set the pan value from slider from -1 to 1 in decimals like

-1.0, -0.9, 0.8 ...... 0 ..... 0.1 .... 0.9, 1.0

When value is set a scheduler is being started that automatically sets the pan value in range that user has selected. Let say user has selected -0.9, it means the schedular will adjust audio pan from -0.9 to 0.9

Issue:

When i change the Pan in schedular, let say i move the value from -0.1 towards -1.0, it produces a pur / noise sound in right speaker and let say if i move the value from 0.1 towards 1.0, it produces a pur / noise sound in left speaker

Analysis:

i) Whenever audio pan value is set towards left speakers it generates a noise in opposite (right) speaker

ii) Whenever audio pan value is set towards right speakers it generates a noise in opposite (left) speaker

I have also added code for more clarification

- (void) setupAutoPanAdjuster:(double)panValue speed:(double)speedValue {
    self.speed = speedValue;
    self.range = (int)fabs((panValue*100.0));
    if (self.speed == 0.0) {
        self.speed = 0.1/kPanDivider;
    }
    [self startSliderUpdateLoop];
}

- (void) startSliderUpdateLoop {
    [self stopSliderUpdateLoop];
    self.sliderUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:self.speed
                                                              target:self
                                                            selector:@selector(updateSlider)
                                                            userInfo:nil
                                                             repeats:YES];
}

- (void) stopSliderUpdateLoop {
    [self.sliderUpdateTimer invalidate];
    self.sliderUpdateTimer = nil;
}

- (void) resetPan {
    [self stopSliderUpdateLoop];
    [self updatePanValue:0.0];
}

- (void) updateRange:(double) panValue {
    self.range = (int)((panValue*100.0));
    [self updatePanValue:(self.currentValue)/100.0];
}

// MARK: - HANDLE PAN

- (void) updateSlider {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.isIncreasing) {
            self.currentValue++;
            if (self.currentValue >= self.range) {
                self.isIncreasing = NO;
            }
        } else {
            self.currentValue--;
            if (self.currentValue <= -self.range) {
                self.isIncreasing = YES;
            }
        }
        
        [self updatePanValue:(self.currentValue)/100.0];
    });
}

-(void) updatePanValue:(double) panValue {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        [self setPanForMixerInput:swPlayer.eqMixerUnit inputNum:envInputNum panValue: panValue];
        [self setPanForMixerInput:swPlayer.eqMixerUnit inputNum:alarmInputNum panValue: panValue];
        [self setPanForMixerInput:swPlayer.outputMixerUnit inputNum:renderInputNum panValue: panValue];
        [self setPanForMixerInput:swPlayer.outputMixerUnit inputNum:eqInputNum panValue: panValue];
        [self setPanForMixerInput:swPlayer.outputMixerUnit inputNum:alarmInputNum panValue: panValue];
    });
}

- (void)setPanForMixerInput:(AudioUnit)mixerUnit inputNum:(UInt32)inputBus panValue:(AudioUnitParameterValue)panValue {
    dispatch_async(dispatch_get_main_queue(), ^{
        CheckError(AudioUnitSetParameter(
                        mixerUnit,
                        kMultiChannelMixerParam_Pan,
                        kAudioUnitScope_Input,
                        inputBus,
                        panValue,
                        0
                        ), "Setting pan for mixer input failed");
    });
}
0

There are 0 answers