<commit_before>/**
 * OpenAL cross platform audio library
 * Copyright (C) 1999-2007 by authors.
 * This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA  02111-1307, USA.
 * Or go to http://www.gnu.org/copyleft/lgpl.html
 */

#include "config.h"

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

#include "alMain.h"
#include "AL/al.h"
#include "AL/alc.h"
#include "alSource.h"
#include "alBuffer.h"
#include "alListener.h"
#include "alAuxEffectSlot.h"
#include "alu.h"
#include "bs2b.h"


/* Cone scalar */
ALfloat ConeScale = 0.5f;

/* Localized Z scalar for mono sources */
ALfloat ZScale = 1.0f;


static __inline ALvoid aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
{
    outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
    outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
    outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
}

static __inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
{
    return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
           inVector1[2]*inVector2[2];
}

static __inline ALvoid aluNormalize(ALfloat *inVector)
{
    ALfloat length, inverse_length;

    length = aluSqrt(aluDotproduct(inVector, inVector));
    if(length != 0.0f)
    {
        inverse_length = 1.0f/length;
        inVector[0] *= inverse_length;
        inVector[1] *= inverse_length;
        inVector[2] *= inverse_length;
    }
}

static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat w,ALfloat matrix[4][4])
{
    ALfloat temp[4] = {
        vector[0], vector[1], vector[2], w
    };

    vector[0] = temp[0]*matrix[0][0] + temp[1]*matrix[1][0] + temp[2]*matrix[2][0] + temp[3]*matrix[3][0];
    vector[1] = temp[0]*matrix[0][1] + temp[1]*matrix[1][1] + temp[2]*matrix[2][1] + temp[3]*matrix[3][1];
    vector[2] = temp[0]*matrix[0][2] + temp[1]*matrix[1][2] + temp[2]*matrix[2][2] + temp[3]*matrix[3][2];
}


ALvoid CalcNonAttnSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
{
    static const ALfloat angles_Mono[1] = { 0.0f };
    static const ALfloat angles_Stereo[2] = { -30.0f, 30.0f };
    static const ALfloat angles_Rear[2] = { -150.0f, 150.0f };
    static const ALfloat angles_Quad[4] = { -45.0f, 45.0f, -135.0f, 135.0f };
    static const ALfloat angles_X51[6] = { -30.0f, 30.0f, 0.0f, 0.0f,
                                           -110.0f, 110.0f };
    static const ALfloat angles_X61[7] = { -30.0f, 30.0f, 0.0f, 0.0f,
                                           180.0f, -90.0f, 90.0f };
    static const ALfloat angles_X71[8] = { -30.0f, 30.0f, 0.0f, 0.0f,
                                           -110.0f, 110.0f, -90.0f, 90.0f };

    static const enum Channel chans_Mono[1] = { FRONT_CENTER };
    static const enum Channel chans_Stereo[2] = { FRONT_LEFT, FRONT_RIGHT };
    static const enum Channel chans_Rear[2] = { BACK_LEFT, BACK_RIGHT };
    static const enum Channel chans_Quad[4] = { FRONT_LEFT, FRONT_RIGHT,
                                                BACK_LEFT, BACK_RIGHT };
    static const enum Channel chans_X51[6] = { FRONT_LEFT, FRONT_RIGHT,
                                               FRONT_CENTER, LFE,
                                               BACK_LEFT, BACK_RIGHT };
    static const enum Channel chans_X61[7] = { FRONT_LEFT, FRONT_RIGHT,
                                               FRONT_CENTER, LFE, BACK_CENTER,
                                               SIDE_LEFT, SIDE_RIGHT };
    static const enum Channel chans_X71[8] = { FRONT_LEFT, FRONT_RIGHT,
                                               FRONT_CENTER, LFE,
                                               BACK_LEFT, BACK_RIGHT,
                                               SIDE_LEFT, SIDE_RIGHT };

    ALCdevice *Device = ALContext->Device;
    ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
    ALbufferlistitem *BufferListItem;
    enum DevFmtChannels DevChans;
    enum FmtChannels Channels;
    ALfloat (*SrcMatrix)[MAXCHANNELS];
    ALfloat DryGain, DryGainHF;
    ALfloat WetGain[MAX_SENDS];
    ALfloat WetGainHF[MAX_SENDS];
    ALint NumSends, Frequency;
    const ALfloat *SpeakerGain;
    const ALfloat *angles = NULL;
    const enum Channel *chans = NULL;
    enum Resampler Resampler;
    ALint num_channels = 0;
    ALboolean VirtualChannels;
    ALfloat Pitch;
    ALfloat cw;
    ALuint pos;
    ALint i, c;

    /* Get device properties */
    DevChans  = ALContext->Device->FmtChans;
    NumSends  = ALContext->Device->NumAuxSends;
    Frequency = ALContext->Device->Frequency;

    /* Get listener properties */
    ListenerGain = ALContext->Listener.Gain;

    /* Get source properties */
    SourceVolume    = ALSource->flGain;
    MinVolume       = ALSource->flMinGain;
    MaxVolume       = ALSource->flMaxGain;
    Pitch           = ALSource->flPitch;
    Resampler       = ALSource->Resampler;
    VirtualChannels = ALSource->VirtualChannels;

    /* Calculate the stepping value */
    Channels = FmtMono;
    BufferListItem = ALSource->queue;
    while(BufferListItem != NULL)
    {
        ALbuffer *ALBuffer;
        if((ALBuffer=BufferListItem->buffer) != NULL)
        {
            ALint maxstep = STACK_DATA_SIZE / ALSource->NumChannels /
                                              ALSource->SampleSize;
            maxstep -= ResamplerPadding[Resampler] +
                       ResamplerPrePadding[Resampler] + 1;
            maxstep = mini(maxstep, INT_MAX>>FRACTIONBITS);

            Pitch = Pitch * ALBuffer->Frequency / Frequency;
            if(Pitch > (ALfloat)maxstep)
                ALSource->Params.Step = maxstep<<FRACTIONBITS;
            else
            {
                ALSource->Params.Step = fastf2i(Pitch*FRACTIONONE);
                if(ALSource->Params.Step == 0)
                    ALSource->Params.Step = 1;
            }

            Channels = ALBuffer->FmtChannels;
            break;
        }
        BufferListItem = BufferListItem->next;
    }
    if(ALSource->VirtualChannels && Device->Hrtf)
        ALSource->Params.DoMix = SelectHrtfMixer((ALSource->Params.Step==FRACTIONONE) ?
                                                 POINT_RESAMPLER : Resampler);
    else
        ALSource->Params.DoMix = SelectMixer((ALSource->Params.Step==FRACTIONONE) ?
                                             POINT_RESAMPLER : Resampler);

    /* Calculate gains */
    DryGain  = clampf(SourceVolume, MinVolume, MaxVolume);
    DryGain *= ALSource->DirectGain;
    DryGainHF = ALSource->DirectGainHF;
    for(i = 0;i < NumSends;i++)
    {
        WetGain[i]  = clampf(SourceVolume, MinVolume, MaxVolume);
        WetGain[i] *= ALSource->Send[i].WetGain;
        WetGainHF[i] = ALSource->Send[i].WetGainHF;
    }

    SrcMatrix = ALSource->Params.DryGains;
    for(i = 0;i < MAXCHANNELS;i++)
    {
        for(c = 0;c < MAXCHANNELS;c++)
            SrcMatrix[i][c] = 0.0f;
    }
    switch(Channels)
    {
    case FmtMono:
        angles = angles_Mono;
        chans = chans_Mono;
        num_channels = 1;<commit_msg>Use the local variable instead of re-reading the source property<commit_after>/**
 * OpenAL cross platform audio library
 * Copyright (C) 1999-2007 by authors.
 * This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA  02111-1307, USA.
 * Or go to http://www.gnu.org/copyleft/lgpl.html
 */

#include "config.h"

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

#include "alMain.h"
#include "AL/al.h"
#include "AL/alc.h"
#include "alSource.h"
#include "alBuffer.h"
#include "alListener.h"
#include "alAuxEffectSlot.h"
#include "alu.h"
#include "bs2b.h"


/* Cone scalar */
ALfloat ConeScale = 0.5f;

/* Localized Z scalar for mono sources */
ALfloat ZScale = 1.0f;


static __inline ALvoid aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
{
    outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
    outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
    outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
}

static __inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
{
    return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
           inVector1[2]*inVector2[2];
}

static __inline ALvoid aluNormalize(ALfloat *inVector)
{
    ALfloat length, inverse_length;

    length = aluSqrt(aluDotproduct(inVector, inVector));
    if(length != 0.0f)
    {
        inverse_length = 1.0f/length;
        inVector[0] *= inverse_length;
        inVector[1] *= inverse_length;
        inVector[2] *= inverse_length;
    }
}

static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat w,ALfloat matrix[4][4])
{
    ALfloat temp[4] = {
        vector[0], vector[1], vector[2], w
    };

    vector[0] = temp[0]*matrix[0][0] + temp[1]*matrix[1][0] + temp[2]*matrix[2][0] + temp[3]*matrix[3][0];
    vector[1] = temp[0]*matrix[0][1] + temp[1]*matrix[1][1] + temp[2]*matrix[2][1] + temp[3]*matrix[3][1];
    vector[2] = temp[0]*matrix[0][2] + temp[1]*matrix[1][2] + temp[2]*matrix[2][2] + temp[3]*matrix[3][2];
}


ALvoid CalcNonAttnSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
{
    static const ALfloat angles_Mono[1] = { 0.0f };
    static const ALfloat angles_Stereo[2] = { -30.0f, 30.0f };
    static const ALfloat angles_Rear[2] = { -150.0f, 150.0f };
    static const ALfloat angles_Quad[4] = { -45.0f, 45.0f, -135.0f, 135.0f };
    static const ALfloat angles_X51[6] = { -30.0f, 30.0f, 0.0f, 0.0f,
                                           -110.0f, 110.0f };
    static const ALfloat angles_X61[7] = { -30.0f, 30.0f, 0.0f, 0.0f,
                                           180.0f, -90.0f, 90.0f };
    static const ALfloat angles_X71[8] = { -30.0f, 30.0f, 0.0f, 0.0f,
                                           -110.0f, 110.0f, -90.0f, 90.0f };

    static const enum Channel chans_Mono[1] = { FRONT_CENTER };
    static const enum Channel chans_Stereo[2] = { FRONT_LEFT, FRONT_RIGHT };
    static const enum Channel chans_Rear[2] = { BACK_LEFT, BACK_RIGHT };
    static const enum Channel chans_Quad[4] = { FRONT_LEFT, FRONT_RIGHT,
                                                BACK_LEFT, BACK_RIGHT };
    static const enum Channel chans_X51[6] = { FRONT_LEFT, FRONT_RIGHT,
                                               FRONT_CENTER, LFE,
                                               BACK_LEFT, BACK_RIGHT };
    static const enum Channel chans_X61[7] = { FRONT_LEFT, FRONT_RIGHT,
                                               FRONT_CENTER, LFE, BACK_CENTER,
                                               SIDE_LEFT, SIDE_RIGHT };
    static const enum Channel chans_X71[8] = { FRONT_LEFT, FRONT_RIGHT,
                                               FRONT_CENTER, LFE,
                                               BACK_LEFT, BACK_RIGHT,
                                               SIDE_LEFT, SIDE_RIGHT };

    ALCdevice *Device = ALContext->Device;
    ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
    ALbufferlistitem *BufferListItem;
    enum DevFmtChannels DevChans;
    enum FmtChannels Channels;
    ALfloat (*SrcMatrix)[MAXCHANNELS];
    ALfloat DryGain, DryGainHF;
    ALfloat WetGain[MAX_SENDS];
    ALfloat WetGainHF[MAX_SENDS];
    ALint NumSends, Frequency;
    const ALfloat *SpeakerGain;
    const ALfloat *angles = NULL;
    const enum Channel *chans = NULL;
    enum Resampler Resampler;
    ALint num_channels = 0;
    ALboolean VirtualChannels;
    ALfloat Pitch;
    ALfloat cw;
    ALuint pos;
    ALint i, c;

    /* Get device properties */
    DevChans  = ALContext->Device->FmtChans;
    NumSends  = ALContext->Device->NumAuxSends;
    Frequency = ALContext->Device->Frequency;

    /* Get listener properties */
    ListenerGain = ALContext->Listener.Gain;

    /* Get source properties */
    SourceVolume    = ALSource->flGain;
    MinVolume       = ALSource->flMinGain;
    MaxVolume       = ALSource->flMaxGain;
    Pitch           = ALSource->flPitch;
    Resampler       = ALSource->Resampler;
    VirtualChannels = ALSource->VirtualChannels;

    /* Calculate the stepping value */
    Channels = FmtMono;
    BufferListItem = ALSource->queue;
    while(BufferListItem != NULL)
    {
        ALbuffer *ALBuffer;
        if((ALBuffer=BufferListItem->buffer) != NULL)
        {
            ALint maxstep = STACK_DATA_SIZE / ALSource->NumChannels /
                                              ALSource->SampleSize;
            maxstep -= ResamplerPadding[Resampler] +
                       ResamplerPrePadding[Resampler] + 1;
            maxstep = mini(maxstep, INT_MAX>>FRACTIONBITS);

            Pitch = Pitch * ALBuffer->Frequency / Frequency;
            if(Pitch > (ALfloat)maxstep)
                ALSource->Params.Step = maxstep<<FRACTIONBITS;
            else
            {
                ALSource->Params.Step = fastf2i(Pitch*FRACTIONONE);
                if(ALSource->Params.Step == 0)
                    ALSource->Params.Step = 1;
            }

            Channels = ALBuffer->FmtChannels;
            break;
        }
        BufferListItem = BufferListItem->next;
    }
    if(VirtualChannels && Device->Hrtf)
        ALSource->Params.DoMix = SelectHrtfMixer((ALSource->Params.Step==FRACTIONONE) ?
                                                 POINT_RESAMPLER : Resampler);
    else
        ALSource->Params.DoMix = SelectMixer((ALSource->Params.Step==FRACTIONONE) ?
                                             POINT_RESAMPLER : Resampler);

    /* Calculate gains */
    DryGain  = clampf(SourceVolume, MinVolume, MaxVolume);
    DryGain *= ALSource->DirectGain;
    DryGainHF = ALSource->DirectGainHF;
    for(i = 0;i < NumSends;i++)
    {
        WetGain[i]  = clampf(SourceVolume, MinVolume, MaxVolume);
        WetGain[i] *= ALSource->Send[i].WetGain;
        WetGainHF[i] = ALSource->Send[i].WetGainHF;
    }

    SrcMatrix = ALSource->Params.DryGains;
    for(i = 0;i < MAXCHANNELS;i++)
    {
        for(c = 0;c < MAXCHANNELS;c++)
            SrcMatrix[i][c] = 0.0f;
    }
    switch(Channels)
    {
    case FmtMono:
        angles = angles_Mono;
        chans = chans_Mono;
        num_channels = 1;
{'fraction_non_alphanumeric': 0.11542142538122886, 'fraction_numerical': 0.0321572130415364, 'mean_word_length': 2.3179508890770535, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 56, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3030474', 'n_tokens_mistral': 5342, 'n_tokens_neox': 5069, 'n_words': 1140}