#!/usr/bin/env node

// Copyright 2014-2025 Jesper Larsson
//
// This file is part of Klipspringer, <https://klipspringer.avadeaux.net/>
//
// Klipspringer is free software: you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// Klipspringer 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with Klipspringer. If
// not, see <https://www.gnu.org/licenses/>.

const pf = "SND_PCM_FORMAT_"

const emitSwitch = (s) => {
    console.log("        switch (b) {");
    console.log("        case 8: return "+pf+s+"8;");
    for (const b of [16, 32]) {
        console.log("        case "+b+": return be ? "+pf+s+b+"_BE : "+pf+s+b+"_LE;");
    }
    for (const b of [18]) {
        console.log("        case "+b+": return be ? SND_PCM_FORMAT_"+s+b+"_3BE : SND_PCM_FORMAT_"+s+b+"_3LE;");
    }
    for (const b of [20, 24]) {
        console.log("        case "+b+": return ss == 3");
        console.log("                ? (be ? "+pf+s+b+"_3BE : "+pf+s+b+"_3LE)");
        console.log("                : (be ? "+pf+s+b+"_BE : "+pf+s+b+"_LE);");
    }
    console.log("        default:\n            return "+pf+"UNKNOWN;\n        }");
}

console.log("// This file is generated by alsa-pcm-choice.mjs during build.");
console.log("\n#include <stdbool.h>");
console.log("#include <alsa/asoundlib.h>");
console.log("#include \"choose_format.h\"");
console.log("\nsnd_pcm_format_t get_format(bool s, unsigned b, unsigned ss, bool be) {");
console.log("    if (s) {");
emitSwitch("S");
console.log("    } else {");
emitSwitch("U");
console.log("    }\n}\n");

console.log("bool split_format(snd_pcm_format_t f, bool *s, unsigned *b, unsigned *ss, bool *be) {");
console.log("    switch (f) {");
for (const s of ["S", "U"]) {
    console.log("    case "+pf+s+"8:");
    console.log("        *s = "+(s === "S")+";");
    console.log("        *b = 8;");
    console.log("        *ss = 1;");
    console.log("        *be = false;");
    console.log("        return true;");
}
for (const [b, ss] of [[16, 2], [18, 3], [20, 3], [20, 4], [24, 3], [24, 4], [32, 4]]) {
    for (const e of ["LE", "BE"]) {
        for (const s of ["S", "U"]) {
            console.log("    case "+pf+s+b+(ss === 3 ? "_3" : "_")+e+":");
            console.log("        *s = "+(s === "S")+";");
            console.log("        *b = "+b+";");
            console.log("        *ss = "+ss+";");
            console.log("        *be = "+(e === "BE")+";");
            console.log("        return true;");
        }
    }
}
console.log("    default:\n        return false;");
console.log("    }\n}");

Version: v4.3.2.2 (2026-05-16T17:03:34+02:00)
Raw file
Source code overview
Klipspringer home