{"id":19670034,"url":"https://github.com/native-bindings/libopus","last_synced_at":"2025-10-19T19:03:05.581Z","repository":{"id":149994305,"uuid":"622687281","full_name":"native-bindings/libopus","owner":"native-bindings","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-10T04:28:55.000Z","size":1401,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-26T07:44:24.609Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/native-bindings.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2023-04-02T20:39:50.000Z","updated_at":"2024-04-03T07:40:11.000Z","dependencies_parsed_at":"2023-10-16T14:14:29.899Z","dependency_job_id":"2517f6ef-50f5-4515-8596-307a5bafaa70","html_url":"https://github.com/native-bindings/libopus","commit_stats":{"total_commits":113,"total_committers":1,"mean_commits":113.0,"dds":0.0,"last_synced_commit":"972f0800440817b6510f72bca95dc6de212eff11"},"previous_names":["native-bindings/libopus","victorqueiroz/node-opusenc"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/native-bindings%2Flibopus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/native-bindings%2Flibopus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/native-bindings%2Flibopus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/native-bindings%2Flibopus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/native-bindings","download_url":"https://codeload.github.com/native-bindings/libopus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240980791,"owners_count":19888342,"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":[],"created_at":"2024-11-11T17:03:47.409Z","updated_at":"2025-10-19T19:03:05.527Z","avatar_url":"https://github.com/native-bindings.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @native-bindings/libopus\n\n## Installation\n\n```\nnpm install @native-bindings/libopus\n```\n\n## Description\n\nEntirely synchronous API to create .ogg files encoded using libopus library. It supports several sample rates (16000,48000 and more.).\n\n## Usage of libopus\n\nBelow it's an example on how to use the opus codec by itself, without interference of the libopusenc. AFAIK, the encoded data cannot be directly placed in a .ogg file for playing. If you need that, see the next example.\n\n```ts\nimport opus from \"@native-bindings/libopus\";\n\nexport function createTestCodec() {\n    const frameSize = 2880;\n    const sampleRate = 48000;\n    const channels = 1;\n\n    const enc = new opus.Encoder(\n        sampleRate,\n        channels,\n        opus.constants.OPUS_APPLICATION_RESTRICTED_LOWDELAY\n    );\n    const dec = new opus.Decoder(sampleRate, channels);\n\n    const pcmOut = new Float32Array(\n        frameSize * channels * Float32Array.BYTES_PER_ELEMENT\n    );\n\n    const encoded = new Uint8Array(10000);\n    const maxDataBytes = 1000; // it can be increased up to whatever `encoded` size is\n\n    const encodeFloat = (buf: Float32Array) =\u003e {\n        const encodedSampleCount = enc.encodeFloat(\n            buf,\n            frameSize,\n            encoded,\n            maxDataBytes\n        );\n        console.log(\"encoded samples: %d\", encodedSampleCount);\n        return encodedSampleCount;\n    };\n\n    const decodeFloat = (buf: Uint8Array, samples: number) =\u003e {\n        const decodedSamples = dec.decodeFloat(\n            buf,\n            samples,\n            pcmOut,\n            frameSize,\n            0\n        );\n        return new Uint8Array(pcmOut.slice(0, decodedSamples).buffer);\n    };\n\n    const encodeAndDecode = (buf: Float32Array) =\u003e {\n        const encodedSampleCount = encodeFloat(buf);\n        return decodeFloat(encoded, encodedSampleCount);\n    };\n\n    return {\n        decode,\n        encode,\n        encodeAndDecode,\n    };\n}\n```\n\n## Usage of libopusenc\n\nThis package comes with libopusenc embedded in it, so you can create .ogg/.opus files directly with it with any sample rate. It will use speexdsp in case a sample rate that is not supported by libopus is used when creating the encoder. See the example below:\n\n```ts\nimport opus from \"@native-bindings/libopus\";\n\nconst comments = new opus.opusenc.Comments();\nconst enc = new opus.opusenc.Encoder();\nenc.createFile(comments, path.resolve(__dirname, \"test1.ogg\"), 48000, 1, 0);\nconst pcm = child_process.spawn(\"arecord\", [\n    \"-f\",\n    \"FLOAT_LE\",\n    \"-c\",\n    \"1\",\n    \"-r\",\n    \"48000\",\n    \"-d\",\n    \"4\",\n]);\npcm.stdout.on(\"data\", (chunk) =\u003e {\n    assert.strict.ok(Buffer.isBuffer(chunk));\n    const samples = chunk.byteLength / Float32Array.BYTES_PER_ELEMENT;\n    const buf = new Float32Array(samples);\n    buf.set(new Float32Array(chunk.buffer, chunk.byteOffset, samples));\n\n    enc.writeFloat(buf, samples);\n});\nreturn new Promise\u003cvoid\u003e((resolve) =\u003e {\n    pcm.stdout.on(\"exit\", (code) =\u003e {\n        assert.strict.ok(code === 0);\n        enc.drain();\n        resolve();\n    });\n});\n```\n\n## Usage of opusfile\n\n```ts\nimport { OpusFile } from \"@native-bindings/libopus\";\n\nconst of = new OpusFile();\nof.openFile(path.resolve(__dirname, \"sample-3.opus\"));\nconst pcm = new Float32Array(2880);\nassert.strict.equal(of.pcmTell(), 0);\nassert.strict.deepEqual(of.readFloat(pcm), {\n    sampleCount: 648,\n    linkIndex: 0,\n});\nassert.strict.deepEqual(of.pcmTell(), 648);\nassert.strict.deepEqual(of.readFloat(pcm), {\n    sampleCount: 960,\n    linkIndex: 0,\n});\nassert.strict.equal(of.pcmTell(), 1608);\nassert.strict.equal(of.pcmSeek(0), undefined);\nassert.strict.equal(of.pcmTell(), 0);\nassert.strict.deepEqual(of.readFloat(pcm), {\n    sampleCount: 648,\n    linkIndex: 0,\n});\nassert.strict.equal(of.channelCount(0), 2);\nassert.strict.equal(of.channelCount(1), 2);\nassert.strict.equal(of.linkCount(), 1);\nassert.strict.equal(of.rawTotal(0), 705632);\n```\n\nYou can also open the file from memory:\n\n```ts\nimport { OpusFile } from \"@native-bindings/libopus\";\n\nconst of = new OpusFile();\nof.openMemory(await fs.promises.readFile(\"./sample.opus\"));\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnative-bindings%2Flibopus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnative-bindings%2Flibopus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnative-bindings%2Flibopus/lists"}