{"id":16866141,"url":"https://github.com/jprjr/tflac","last_synced_at":"2025-06-13T08:06:45.738Z","repository":{"id":216877717,"uuid":"742586781","full_name":"jprjr/tflac","owner":"jprjr","description":"A single file, freestanding FLAC encoding library in C89","archived":false,"fork":false,"pushed_at":"2024-05-26T17:08:52.000Z","size":372,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-18T17:56:53.437Z","etag":null,"topics":["audio","audio-compression","audio-encoder","audio-encoding","c","encoder","flac","single-file-library","single-header-lib","single-header-library"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"0bsd","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jprjr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-12T20:09:20.000Z","updated_at":"2025-02-28T19:09:13.000Z","dependencies_parsed_at":"2025-03-18T17:41:26.312Z","dependency_job_id":"d40eeb65-1f3c-4dd2-b0f5-864ff7618d73","html_url":"https://github.com/jprjr/tflac","commit_stats":null,"previous_names":["jprjr/tflac"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jprjr/tflac","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jprjr%2Ftflac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jprjr%2Ftflac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jprjr%2Ftflac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jprjr%2Ftflac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jprjr","download_url":"https://codeload.github.com/jprjr/tflac/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jprjr%2Ftflac/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259606972,"owners_count":22883559,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["audio","audio-compression","audio-encoder","audio-encoding","c","encoder","flac","single-file-library","single-header-lib","single-header-library"],"created_at":"2024-10-13T14:49:36.094Z","updated_at":"2025-06-13T08:06:45.713Z","avatar_url":"https://github.com/jprjr.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tflac\n\nA single-file [FLAC](https://xiph.org/flac/) encoding library. Does not\nuse any C library functions, does not allocate memory directly (it leaves\nthat up to you to figure out). Compatible with C89.\n\nUse case: you want to generate FLAC audio without requiring an external\nlibrary, and simplicity is your main concern, not compression or speed.\n\n## Status\n\ntflac currently supports the following subframe encodings:\n\n* `CONSTANT`\n* `FIXED`\n* `VERBATIM`\n\nIt's roughly equivalent to compressing with FLAC's \"-0\" switch.\n\n## Building\n\nIn one C file define `TFLAC_IMPLEMENTATION` before including `tflac.h`.\n\n```c\n#define TFLAC_IMPLEMENTATION\n#include \"tflac.h\"\n```\n\n### Options\n\nThere's a few compile-time options you can set:\n\n* Define `TFLAC_32BIT_ONLY` and tflac will not use 64-bit types at all,\nand instead emulate 64-bit types with a pair of 32-bit ints.\n* Define `TFLAC_DISABLE_SSE2` to disable SSE2 detection.\n* Define `TFLAC_DISABLE_SSSE3` to disable SSSE3 detection.\n* Define `TFLAC_DISABLE_SSE4_1` to disable SSE4.1 detection.\n* Define `TFLAC_PUBLIC` if you need to customize function decorators\nfor public API functions.\n* Define `TFLAC_PRIVATE` if you need to customize function decorators\nfor private API functions.\n\nFor example, if you're building a DLL on Windows and want to export\nthe public API functions you could define `TFLAC_PUBLIC` as\n`__declspec(dllexport)` like so:\n\n```c\n#define TFLAC_IMPLEMENTATION\n#define TFLAC_PUBLIC __declspec(dllexport)\n#include \"tflac.h\"\n```\n\n## Usage\n\n### Detect your CPU features\n\nCall `tflac_detect_cpu()` before doing anything, like as early in your\nprogram as possible, before you create any threads etc.\n\nThis will attempt to check your CPU type, and set some internal\nglobal variables. For example, if your CPU supports SSE2, the library\nwill swap the default fixed-order calculators for a set that use\nSSE2.\n\n### Get memory and initialize things.\n\nYou'll have to create a tflac struct. The whole struct definition is\nprovided so you can allocate it on the stack, or call `tflac_size()`\nto get the structure size at runtime. Initialize it with `tflac_init()`.\n\nYou then need to set 4 parameters. You can either set the tflac struct fields\nyourself, or use setter functions (`tflac_set_blocksize()`,\n`tflac_set_samplerate()`, etc).\n\n1. audio block size\n2. channels\n3. bit depth\n4. sample rate\n\nYou then need to acquire a memory chunk, to be used by tflac internally\nfor storing residuals. You can get this needed memory size with a C\nmacro, or with a function. The only required parameter is your audio block\nsize.\n\nOnce you've set your parameters and acquired memory, call `tflac_validate()`\nwith the memory you've allocated for tflac's internals. It will make sure\nyour parameters are sane, that the memory chunk is large enough, and\nproceed to set up all of its internal pointers to the memory chunk.\n\nNote: the library will not free this memory for you since the library\ndidn't allocate it.\n\n### Generate some FLAC!\n\nYou'll need to write out the `fLaC` stream marker and a `STREAMINFO` block.\nThe library has a function for generating a `STREAMINFO` block, since\nthat's a required metadata block. Other blocks are pretty straightforward\nto create, I don't plan to include that functionality.\n\nYou'll need a buffer for storing encoded frames. You can find the maximum\nrequired buffer size with a C macro, or with a function. The parameters\nare your audio block size, channels, and bit depth.\n\nYou can have your audio in interleaved or planar format, and as either\n`tflac_s16` or `tflac_s32`. The encode functions are:\n\n* `tflac_encode_s16i`: `tflac_s16` samples in a 1-dimensional array (`tflac_s16*`), interleaved.\n* `tflac_encode_s16p`: `tflac_s16` samples in a multi-dimensional array (`tflac_s16**`).\n* `tflac_encode_s32i`: `tflac_int32` samples in a 1-dimensional array (`tflac_int32*`), interleaved.\n* `tflac_encode_s32p`: `tflac_int32` samples in a multi-dimensional array (`tflac_int32**`).\n\n`tflac_s16` is a typedef for `int16_t`, and `tflac_s32` is a typedef for `int32_t`\nin most cases.\n\n```C\n\n#define TFLAC_IMPLEMENTATION\n#include \"tflac.h\"\n\n#define BLOCKSIZE 1152\n#define CHANNELS 2\n#define BITDEPTH 16\n#define SAMPLERATE 44100\n\nint main(int argc, const char *argv[]) {\n    /* get the size we need to alloc for tflac internal memory */\n    tflac_u32 memory_size = tflac_size_memory(BLOCKSIZE);\n\n    /* get a chunk for tflac internal memory */\n    void* memory = malloc(memory_size);\n\n    /* get the size we need to alloc for the output buffer */\n    tflac_u32 buffer_size = tflac_size_frame(BLOCKSIZE, CHANNELS, BITDEPTH);\n\n    /* get a hunk for our buffer */\n    void* buffer = malloc(buffer_size);\n\n    /* this will be used to know how much of the buffer to write\n       after calls to encode */\n    tflac_u32 buffer_used = 0;\n\n    tflac t;\n\n    tflac_init(\u0026t);\n\n    /* set some parameters */\n    t.blocksize = BLOCKSIZE;\n    t.samplerate = SAMPLERATE;\n    t.bitdepth = BITDEPTH;\n    t.channels = CHANNELS;\n\n    /* validate our parameters and tell tflac what memory to use */\n    tflac_validate(\u0026t, memory, memory_size);\n\n    /* open a file and write out the stream marker */\n    FILE* output = fopen(\"some-file.flac\",\"wb\");\n    fwrite(\"fLaC\", 1, 4, output);\n\n    /* encode the STREAMINFO block and write it out */\n    tflac_encode_streaminfo(\u0026t, 1, buffer, buffer_size, \u0026buffer_used);\n    fwrite(buffer, 1, buffer_used, output);\n\n    /* loop until you run out of audio */\n    while(have_audio()) {\n\n        /* hand-waving over this part, here we're using a 2d array of audio samples\n        and assuming it's always BLOCKSIZE samples */\n        tflac_s32** samples = get_some_audio_somehow();\n\n        /* encode your audio samples into a FLAC frame */\n        tflac_encode_s32p(\u0026t, BLOCKSIZE, samples, buffer, buffer_size, \u0026buffer_used);\n\n        /* and write it out */\n        fwrite(buffer, 1, buffer_used, output);\n    }\n\n    free(buffer);\n    free(ptr);\n}\n```\n\n\n## LICENSE\n\nBSD Zero Clause (see the `LICENSE` file).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjprjr%2Ftflac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjprjr%2Ftflac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjprjr%2Ftflac/lists"}