+int S_GetSoundRate(void)
+{
+ return snd_renderbuffer ? snd_renderbuffer->format.speed : 0;
+}
+
+
+static qboolean S_ChooseCheaperFormat (snd_format_t* format, qboolean fixed_speed, qboolean fixed_width, qboolean fixed_channels)
+{
+ static const snd_format_t thresholds [] =
+ {
+ // speed width channels
+ { SND_MIN_SPEED, SND_MIN_WIDTH, SND_MIN_CHANNELS },
+ { 11025, 1, 2 },
+ { 22050, 2, 2 },
+ { 44100, 2, 2 },
+ { 48000, 2, 6 },
+ { SND_MAX_SPEED, SND_MAX_WIDTH, SND_MAX_CHANNELS },
+ };
+ const unsigned int nb_thresholds = sizeof(thresholds) / sizeof(thresholds[0]);
+ unsigned int speed_level, width_level, channels_level;
+
+ // If we have reached the minimum values, there's nothing more we can do
+ if ((format->speed == thresholds[0].speed || fixed_speed) &&
+ (format->width == thresholds[0].width || fixed_width) &&
+ (format->channels == thresholds[0].channels || fixed_channels))
+ return false;
+
+ // Check the min and max values
+ #define CHECK_BOUNDARIES(param) \
+ if (format->param < thresholds[0].param) \
+ { \
+ format->param = thresholds[0].param; \
+ return true; \
+ } \
+ if (format->param > thresholds[nb_thresholds - 1].param) \
+ { \
+ format->param = thresholds[nb_thresholds - 1].param; \
+ return true; \
+ }
+ CHECK_BOUNDARIES(speed);
+ CHECK_BOUNDARIES(width);
+ CHECK_BOUNDARIES(channels);
+ #undef CHECK_BOUNDARIES
+
+ // Find the level of each parameter
+ #define FIND_LEVEL(param) \
+ param##_level = 0; \
+ while (param##_level < nb_thresholds - 1) \
+ { \
+ if (format->param <= thresholds[param##_level].param) \
+ break; \
+ \
+ param##_level++; \
+ }
+ FIND_LEVEL(speed);
+ FIND_LEVEL(width);
+ FIND_LEVEL(channels);
+ #undef FIND_LEVEL
+
+ // Decrease the parameter with the highest level to the previous level
+ if (channels_level >= speed_level && channels_level >= width_level && !fixed_channels)
+ {
+ format->channels = thresholds[channels_level - 1].channels;
+ return true;
+ }
+ if (speed_level >= width_level && !fixed_speed)
+ {
+ format->speed = thresholds[speed_level - 1].speed;
+ return true;
+ }
+
+ format->width = thresholds[width_level - 1].width;
+ return true;
+}
+
+
+#define SWAP_LISTENERS(l1, l2, tmpl) { tmpl = (l1); (l1) = (l2); (l2) = tmpl; }
+
+static void S_SetChannelLayout (void)
+{
+ unsigned int i;
+ listener_t swaplistener;
+ listener_t *listeners;
+ int layout;
+
+ for (i = 0; i < SND_SPEAKERLAYOUTS; i++)
+ if (snd_speakerlayouts[i].channels == snd_renderbuffer->format.channels)
+ break;
+ if (i >= SND_SPEAKERLAYOUTS)
+ {
+ Con_Printf("S_SetChannelLayout: can't find the speaker layout for %hu channels. Defaulting to mono output\n",
+ snd_renderbuffer->format.channels);
+ i = SND_SPEAKERLAYOUTS - 1;
+ }
+
+ snd_speakerlayout = snd_speakerlayouts[i];
+ listeners = snd_speakerlayout.listeners;
+
+ // Swap the left and right channels if snd_swapstereo is set
+ if (snd_swapstereo.integer)
+ {
+ switch (snd_speakerlayout.channels)
+ {
+ case 8:
+ SWAP_LISTENERS(listeners[6], listeners[7], swaplistener);
+ // no break
+ case 4:
+ case 6:
+ SWAP_LISTENERS(listeners[2], listeners[3], swaplistener);
+ // no break
+ case 2:
+ SWAP_LISTENERS(listeners[0], listeners[1], swaplistener);
+ break;
+
+ default:
+ case 1:
+ // Nothing to do
+ break;
+ }
+ }
+
+ // Sanity check
+ if (snd_channellayout.integer < SND_CHANNELLAYOUT_AUTO ||
+ snd_channellayout.integer > SND_CHANNELLAYOUT_ALSA)
+ Cvar_SetValueQuick (&snd_channellayout, SND_CHANNELLAYOUT_STANDARD);
+
+ if (snd_channellayout.integer == SND_CHANNELLAYOUT_AUTO)
+ {
+ // If we're in the sound engine initialization
+ if (current_channellayout_used == SND_CHANNELLAYOUT_AUTO)
+ {
+ layout = SND_CHANNELLAYOUT_STANDARD;
+ Cvar_SetValueQuick (&snd_channellayout, layout);
+ }
+ else
+ layout = current_channellayout_used;
+ }
+ else
+ layout = snd_channellayout.integer;
+
+ // Convert our layout (= ALSA) to the standard layout if necessary
+ if (snd_speakerlayout.channels == 6 || snd_speakerlayout.channels == 8)
+ {
+ if (layout == SND_CHANNELLAYOUT_STANDARD)
+ {
+ SWAP_LISTENERS(listeners[2], listeners[4], swaplistener);
+ SWAP_LISTENERS(listeners[3], listeners[5], swaplistener);
+ }
+
+ Con_Printf("S_SetChannelLayout: using %s speaker layout for 3D sound\n",
+ (layout == SND_CHANNELLAYOUT_ALSA) ? "ALSA" : "standard");
+ }
+
+ current_swapstereo = snd_swapstereo.integer;
+ current_channellayout = snd_channellayout.integer;
+ current_channellayout_used = layout;
+}
+
+
+void S_Startup (void)