Sunday, January 7, 2024
HomeiOS DevelopmentAudio Pan to maneuver sound from left proper and vice versa in...

Audio Pan to maneuver sound from left proper and vice versa in iOS


I’m writing code the place consumer can set the pan worth from slider from -1 to 1 in decimals like

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

When worth is ready a scheduler is being began that mechanically units the pan worth in vary that consumer has chosen. Let say consumer has chosen -0.9, it means the schedular will alter audio pan from -0.9 to 0.9

Challenge:

When i modify the Pan in schedular,
let say i transfer the worth from -0.1 in direction of -1.0, it produces a pur / noise sound in proper speaker
and let say if i transfer the worth from 0.1 in direction of 1.0, it produces a pur / noise sound in left speaker

Evaluation:

i) At any time when audio pan worth is ready in direction of left audio system it generates a noise in reverse (proper) speaker

ii) At any time when audio pan worth is ready in direction of proper audio system it generates a noise in reverse (left) speaker

I’ve additionally added code for extra clarification

- (void) setupAutoPanAdjuster:(double)panValue velocity:(double)speedValue {
    self.velocity = speedValue;
    self.vary = (int)fabs((panValue*100.0));
    if (self.velocity == 0.0) {
        self.velocity = 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.vary = (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.vary) {
                self.isIncreasing = NO;
            }
        } else {
            self.currentValue--;
            if (self.currentValue <= -self.vary) {
                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 enter failed");
    });
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments