Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BasicClippingShortMixer.java « audio « jumble « morlunk « com « java « main « src - gitlab.com/quite/humla.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0cff93ea24a8f2917d9b2a9a7081e8cb812b4ca0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.morlunk.jumble.audio;

import java.util.Collection;

/**
 * A simple mixer that downsamples source floating point PCM to shorts, clipping naively.
 */
public class BasicClippingShortMixer implements IAudioMixer<float[], short[]> {
    @Override
    public void mix(Collection<IAudioMixerSource<float[]>> sources, short[] buffer, int bufferOffset,
                    int bufferLength) {
        for (int i = 0; i < bufferLength; i++) {
            float mix = 0;
            for (IAudioMixerSource<float[]> source : sources) {
                mix += source.getSamples()[i];
            }
            // Clip to [-1,1].
            if (mix > 1)
                mix = 1;
            else if (mix < -1)
                mix = -1;
            buffer[i + bufferOffset] = (short) (mix * Short.MAX_VALUE);
        }
    }
}