rlm@0: /** rlm@0: * OpenAL cross platform audio library rlm@0: * Copyright (C) 1999-2010 by authors. rlm@0: * This library is free software; you can redistribute it and/or rlm@0: * modify it under the terms of the GNU Library General Public rlm@0: * License as published by the Free Software Foundation; either rlm@0: * version 2 of the License, or (at your option) any later version. rlm@0: * rlm@0: * This library is distributed in the hope that it will be useful, rlm@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of rlm@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU rlm@0: * Library General Public License for more details. rlm@0: * rlm@0: * You should have received a copy of the GNU Library General Public rlm@0: * License along with this library; if not, write to the rlm@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, rlm@0: * Boston, MA 02111-1307, USA. rlm@0: * Or go to http://www.gnu.org/copyleft/lgpl.html rlm@0: */ rlm@0: rlm@0: #include "config.h" rlm@0: rlm@0: #include rlm@0: #include rlm@0: #include rlm@0: #include rlm@0: #include rlm@0: rlm@0: #include "alMain.h" rlm@0: #include "AL/al.h" rlm@0: #include "AL/alc.h" rlm@0: #include "alu.h" rlm@0: rlm@0: static void SetSpeakerArrangement(const char *name, ALfloat SpeakerAngle[MAXCHANNELS], rlm@0: enum Channel Speaker2Chan[MAXCHANNELS], ALint chans) rlm@0: { rlm@0: char layout_str[256]; rlm@0: char *confkey, *next; rlm@0: char *sep, *end; rlm@0: enum Channel val; rlm@0: int i; rlm@0: rlm@0: if(!ConfigValueExists(NULL, name)) rlm@0: name = "layout"; rlm@0: rlm@0: strncpy(layout_str, GetConfigValue(NULL, name, ""), sizeof(layout_str)); rlm@0: layout_str[sizeof(layout_str)-1] = 0; rlm@0: rlm@0: if(!layout_str[0]) rlm@0: return; rlm@0: rlm@0: next = confkey = layout_str; rlm@0: while(next && *next) rlm@0: { rlm@0: confkey = next; rlm@0: next = strchr(confkey, ','); rlm@0: if(next) rlm@0: { rlm@0: *next = 0; rlm@0: do { rlm@0: next++; rlm@0: } while(isspace(*next) || *next == ','); rlm@0: } rlm@0: rlm@0: sep = strchr(confkey, '='); rlm@0: if(!sep || confkey == sep) rlm@0: continue; rlm@0: rlm@0: end = sep - 1; rlm@0: while(isspace(*end) && end != confkey) rlm@0: end--; rlm@0: *(++end) = 0; rlm@0: rlm@0: if(strcmp(confkey, "fl") == 0 || strcmp(confkey, "front-left") == 0) rlm@0: val = FRONT_LEFT; rlm@0: else if(strcmp(confkey, "fr") == 0 || strcmp(confkey, "front-right") == 0) rlm@0: val = FRONT_RIGHT; rlm@0: else if(strcmp(confkey, "fc") == 0 || strcmp(confkey, "front-center") == 0) rlm@0: val = FRONT_CENTER; rlm@0: else if(strcmp(confkey, "bl") == 0 || strcmp(confkey, "back-left") == 0) rlm@0: val = BACK_LEFT; rlm@0: else if(strcmp(confkey, "br") == 0 || strcmp(confkey, "back-right") == 0) rlm@0: val = BACK_RIGHT; rlm@0: else if(strcmp(confkey, "bc") == 0 || strcmp(confkey, "back-center") == 0) rlm@0: val = BACK_CENTER; rlm@0: else if(strcmp(confkey, "sl") == 0 || strcmp(confkey, "side-left") == 0) rlm@0: val = SIDE_LEFT; rlm@0: else if(strcmp(confkey, "sr") == 0 || strcmp(confkey, "side-right") == 0) rlm@0: val = SIDE_RIGHT; rlm@0: else rlm@0: { rlm@0: ERR("Unknown speaker for %s: \"%s\"\n", name, confkey); rlm@0: continue; rlm@0: } rlm@0: rlm@0: *(sep++) = 0; rlm@0: while(isspace(*sep)) rlm@0: sep++; rlm@0: rlm@0: for(i = 0;i < chans;i++) rlm@0: { rlm@0: if(Speaker2Chan[i] == val) rlm@0: { rlm@0: long angle = strtol(sep, NULL, 10); rlm@0: if(angle >= -180 && angle <= 180) rlm@0: SpeakerAngle[i] = angle * M_PI/180.0f; rlm@0: else rlm@0: ERR("Invalid angle for speaker \"%s\": %ld\n", confkey, angle); rlm@0: break; rlm@0: } rlm@0: } rlm@0: } rlm@0: rlm@0: for(i = 0;i < chans;i++) rlm@0: { rlm@0: int min = i; rlm@0: int i2; rlm@0: rlm@0: for(i2 = i+1;i2 < chans;i2++) rlm@0: { rlm@0: if(SpeakerAngle[i2] < SpeakerAngle[min]) rlm@0: min = i2; rlm@0: } rlm@0: rlm@0: if(min != i) rlm@0: { rlm@0: ALfloat tmpf; rlm@0: enum Channel tmpc; rlm@0: rlm@0: tmpf = SpeakerAngle[i]; rlm@0: SpeakerAngle[i] = SpeakerAngle[min]; rlm@0: SpeakerAngle[min] = tmpf; rlm@0: rlm@0: tmpc = Speaker2Chan[i]; rlm@0: Speaker2Chan[i] = Speaker2Chan[min]; rlm@0: Speaker2Chan[min] = tmpc; rlm@0: } rlm@0: } rlm@0: } rlm@0: rlm@0: static ALfloat aluLUTpos2Angle(ALint pos) rlm@0: { rlm@0: if(pos < QUADRANT_NUM) rlm@0: return aluAtan((ALfloat)pos / (ALfloat)(QUADRANT_NUM - pos)); rlm@0: if(pos < 2 * QUADRANT_NUM) rlm@0: return M_PI_2 + aluAtan((ALfloat)(pos - QUADRANT_NUM) / (ALfloat)(2 * QUADRANT_NUM - pos)); rlm@0: if(pos < 3 * QUADRANT_NUM) rlm@0: return aluAtan((ALfloat)(pos - 2 * QUADRANT_NUM) / (ALfloat)(3 * QUADRANT_NUM - pos)) - M_PI; rlm@0: return aluAtan((ALfloat)(pos - 3 * QUADRANT_NUM) / (ALfloat)(4 * QUADRANT_NUM - pos)) - M_PI_2; rlm@0: } rlm@0: rlm@0: ALint aluCart2LUTpos(ALfloat re, ALfloat im) rlm@0: { rlm@0: ALint pos = 0; rlm@0: ALfloat denom = aluFabs(re) + aluFabs(im); rlm@0: if(denom > 0.0f) rlm@0: pos = (ALint)(QUADRANT_NUM*aluFabs(im) / denom + 0.5); rlm@0: rlm@0: if(re < 0.0) rlm@0: pos = 2 * QUADRANT_NUM - pos; rlm@0: if(im < 0.0) rlm@0: pos = LUT_NUM - pos; rlm@0: return pos%LUT_NUM; rlm@0: } rlm@0: rlm@0: ALvoid aluInitPanning(ALCdevice *Device) rlm@0: { rlm@0: ALfloat SpeakerAngle[MAXCHANNELS]; rlm@0: enum Channel *Speaker2Chan; rlm@0: ALfloat Alpha, Theta; rlm@0: ALint pos; rlm@0: ALuint s; rlm@0: rlm@0: Speaker2Chan = Device->Speaker2Chan; rlm@0: switch(Device->FmtChans) rlm@0: { rlm@0: case DevFmtMono: rlm@0: Device->NumChan = 1; rlm@0: Speaker2Chan[0] = FRONT_CENTER; rlm@0: SpeakerAngle[0] = 0.0f * M_PI/180.0f; rlm@0: break; rlm@0: rlm@0: case DevFmtStereo: rlm@0: Device->NumChan = 2; rlm@0: Speaker2Chan[0] = FRONT_LEFT; rlm@0: Speaker2Chan[1] = FRONT_RIGHT; rlm@0: SpeakerAngle[0] = -90.0f * M_PI/180.0f; rlm@0: SpeakerAngle[1] = 90.0f * M_PI/180.0f; rlm@0: SetSpeakerArrangement("layout_STEREO", SpeakerAngle, Speaker2Chan, Device->NumChan); rlm@0: break; rlm@0: rlm@0: case DevFmtQuad: rlm@0: Device->NumChan = 4; rlm@0: Speaker2Chan[0] = BACK_LEFT; rlm@0: Speaker2Chan[1] = FRONT_LEFT; rlm@0: Speaker2Chan[2] = FRONT_RIGHT; rlm@0: Speaker2Chan[3] = BACK_RIGHT; rlm@0: SpeakerAngle[0] = -135.0f * M_PI/180.0f; rlm@0: SpeakerAngle[1] = -45.0f * M_PI/180.0f; rlm@0: SpeakerAngle[2] = 45.0f * M_PI/180.0f; rlm@0: SpeakerAngle[3] = 135.0f * M_PI/180.0f; rlm@0: SetSpeakerArrangement("layout_QUAD", SpeakerAngle, Speaker2Chan, Device->NumChan); rlm@0: break; rlm@0: rlm@0: case DevFmtX51: rlm@0: Device->NumChan = 5; rlm@0: Speaker2Chan[0] = BACK_LEFT; rlm@0: Speaker2Chan[1] = FRONT_LEFT; rlm@0: Speaker2Chan[2] = FRONT_CENTER; rlm@0: Speaker2Chan[3] = FRONT_RIGHT; rlm@0: Speaker2Chan[4] = BACK_RIGHT; rlm@0: SpeakerAngle[0] = -110.0f * M_PI/180.0f; rlm@0: SpeakerAngle[1] = -30.0f * M_PI/180.0f; rlm@0: SpeakerAngle[2] = 0.0f * M_PI/180.0f; rlm@0: SpeakerAngle[3] = 30.0f * M_PI/180.0f; rlm@0: SpeakerAngle[4] = 110.0f * M_PI/180.0f; rlm@0: SetSpeakerArrangement("layout_51CHN", SpeakerAngle, Speaker2Chan, Device->NumChan); rlm@0: break; rlm@0: rlm@0: case DevFmtX51Side: rlm@0: Device->NumChan = 5; rlm@0: Speaker2Chan[0] = SIDE_LEFT; rlm@0: Speaker2Chan[1] = FRONT_LEFT; rlm@0: Speaker2Chan[2] = FRONT_CENTER; rlm@0: Speaker2Chan[3] = FRONT_RIGHT; rlm@0: Speaker2Chan[4] = SIDE_RIGHT; rlm@0: SpeakerAngle[0] = -90.0f * M_PI/180.0f; rlm@0: SpeakerAngle[1] = -30.0f * M_PI/180.0f; rlm@0: SpeakerAngle[2] = 0.0f * M_PI/180.0f; rlm@0: SpeakerAngle[3] = 30.0f * M_PI/180.0f; rlm@0: SpeakerAngle[4] = 90.0f * M_PI/180.0f; rlm@0: SetSpeakerArrangement("layout_51SIDECHN", SpeakerAngle, Speaker2Chan, Device->NumChan); rlm@0: break; rlm@0: rlm@0: case DevFmtX61: rlm@0: Device->NumChan = 6; rlm@0: Speaker2Chan[0] = SIDE_LEFT; rlm@0: Speaker2Chan[1] = FRONT_LEFT; rlm@0: Speaker2Chan[2] = FRONT_CENTER; rlm@0: Speaker2Chan[3] = FRONT_RIGHT; rlm@0: Speaker2Chan[4] = SIDE_RIGHT; rlm@0: Speaker2Chan[5] = BACK_CENTER; rlm@0: SpeakerAngle[0] = -90.0f * M_PI/180.0f; rlm@0: SpeakerAngle[1] = -30.0f * M_PI/180.0f; rlm@0: SpeakerAngle[2] = 0.0f * M_PI/180.0f; rlm@0: SpeakerAngle[3] = 30.0f * M_PI/180.0f; rlm@0: SpeakerAngle[4] = 90.0f * M_PI/180.0f; rlm@0: SpeakerAngle[5] = 180.0f * M_PI/180.0f; rlm@0: SetSpeakerArrangement("layout_61CHN", SpeakerAngle, Speaker2Chan, Device->NumChan); rlm@0: break; rlm@0: rlm@0: case DevFmtX71: rlm@0: Device->NumChan = 7; rlm@0: Speaker2Chan[0] = BACK_LEFT; rlm@0: Speaker2Chan[1] = SIDE_LEFT; rlm@0: Speaker2Chan[2] = FRONT_LEFT; rlm@0: Speaker2Chan[3] = FRONT_CENTER; rlm@0: Speaker2Chan[4] = FRONT_RIGHT; rlm@0: Speaker2Chan[5] = SIDE_RIGHT; rlm@0: Speaker2Chan[6] = BACK_RIGHT; rlm@0: SpeakerAngle[0] = -150.0f * M_PI/180.0f; rlm@0: SpeakerAngle[1] = -90.0f * M_PI/180.0f; rlm@0: SpeakerAngle[2] = -30.0f * M_PI/180.0f; rlm@0: SpeakerAngle[3] = 0.0f * M_PI/180.0f; rlm@0: SpeakerAngle[4] = 30.0f * M_PI/180.0f; rlm@0: SpeakerAngle[5] = 90.0f * M_PI/180.0f; rlm@0: SpeakerAngle[6] = 150.0f * M_PI/180.0f; rlm@0: SetSpeakerArrangement("layout_71CHN", SpeakerAngle, Speaker2Chan, Device->NumChan); rlm@0: break; rlm@0: } rlm@0: rlm@0: for(pos = 0; pos < LUT_NUM; pos++) rlm@0: { rlm@0: ALfloat *PanningLUT = Device->PanningLUT[pos]; rlm@0: rlm@0: /* clear all values */ rlm@0: for(s = 0; s < MAXCHANNELS; s++) rlm@0: PanningLUT[s] = 0.0f; rlm@0: rlm@0: if(Device->NumChan == 1) rlm@0: { rlm@0: PanningLUT[Speaker2Chan[0]] = 1.0f; rlm@0: continue; rlm@0: } rlm@0: rlm@0: /* source angle */ rlm@0: Theta = aluLUTpos2Angle(pos); rlm@0: rlm@0: /* set panning values */ rlm@0: for(s = 0; s < Device->NumChan - 1; s++) rlm@0: { rlm@0: if(Theta >= SpeakerAngle[s] && Theta < SpeakerAngle[s+1]) rlm@0: { rlm@0: /* source between speaker s and speaker s+1 */ rlm@0: Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) / rlm@0: (SpeakerAngle[s+1]-SpeakerAngle[s]); rlm@0: PanningLUT[Speaker2Chan[s]] = cos(Alpha); rlm@0: PanningLUT[Speaker2Chan[s+1]] = sin(Alpha); rlm@0: break; rlm@0: } rlm@0: } rlm@0: if(s == Device->NumChan - 1) rlm@0: { rlm@0: /* source between last and first speaker */ rlm@0: if(Theta < SpeakerAngle[0]) rlm@0: Theta += 2.0f * M_PI; rlm@0: Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) / rlm@0: (2.0f * M_PI + SpeakerAngle[0]-SpeakerAngle[s]); rlm@0: PanningLUT[Speaker2Chan[s]] = cos(Alpha); rlm@0: PanningLUT[Speaker2Chan[0]] = sin(Alpha); rlm@0: } rlm@0: } rlm@0: }