{"id":29151348,"url":"https://github.com/karpeleslab/static-opus","last_synced_at":"2025-07-11T05:04:49.176Z","repository":{"id":134252543,"uuid":"315528853","full_name":"KarpelesLab/static-opus","owner":"KarpelesLab","description":"Statically linked libopus in Go","archived":false,"fork":false,"pushed_at":"2023-11-02T10:41:52.000Z","size":1282,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-01T00:09:03.265Z","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/KarpelesLab.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":"2020-11-24T05:33:30.000Z","updated_at":"2025-04-13T07:34:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"47c6299d-01ff-4597-9ad9-dbc43f7b37c9","html_url":"https://github.com/KarpelesLab/static-opus","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/KarpelesLab/static-opus","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KarpelesLab%2Fstatic-opus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KarpelesLab%2Fstatic-opus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KarpelesLab%2Fstatic-opus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KarpelesLab%2Fstatic-opus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KarpelesLab","download_url":"https://codeload.github.com/KarpelesLab/static-opus/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KarpelesLab%2Fstatic-opus/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262870877,"owners_count":23377314,"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":"2025-07-01T00:09:03.475Z","updated_at":"2025-07-01T00:09:08.062Z","avatar_url":"https://github.com/KarpelesLab.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# libopus statically linked for Go\n\nlibopus from https://opus-codec.org/downloads/ (BSD licensed, see https://opus-codec.org/license/ for details)\n\nExtra go code from https://github.com/hraban/opus (MIT licensed)\n\n[![Test](https://github.com/KarpelesLab/static-opus/workflows/Test/badge.svg)](https://github.com/KarpelesLab/static-opus/actions?query=workflow%3ATest)\n\n## Go wrapper for Opus\n\nThis package provides Go bindings for the xiph.org C library libopus.\n\nThe C library and docs are hosted at https://opus-codec.org/. This package\njust handles the wrapping in Go and includes a copy of the said library, but\nis unaffiliated with xiph.org.\n\n## Details\n\nThis wrapper provides a Go translation layer for two elements from the\nxiph.org opus libs:\n\n* encoders\n* decoders\n\n### Import\n\n```go\nimport \"github.com/KarpelesLab/static-opus/opus\"\n```\n\n### Encoding\n\nTo encode raw audio to the Opus format, create an encoder first:\n\n```go\nconst sampleRate = 48000\nconst channels = 1 // mono; 2 for stereo\n\nenc, err := opus.NewEncoder(sampleRate, channels, opus.AppVoIP)\nif err != nil {\n    ...\n}\n```\n\nThen pass it some raw PCM data to encode.\n\nMake sure that the raw PCM data you want to encode has a legal Opus frame size.\nThis means it must be exactly 2.5, 5, 10, 20, 40 or 60 ms long. The number of\nbytes this corresponds to depends on the sample rate (see the [libopus\ndocumentation](https://www.opus-codec.org/docs/opus_api-1.1.3/group__opus__encoder.html)).\n\n```go\nvar pcm []int16 = ... // obtain your raw PCM data somewhere\nconst bufferSize = 1000 // choose any buffer size you like. 1k is plenty.\n\n// Check the frame size. You don't need to do this if you trust your input.\nframeSize := len(pcm) // must be interleaved if stereo\nframeSizeMs := float32(frameSize) / channels * 1000 / sampleRate\nswitch frameSizeMs {\ncase 2.5, 5, 10, 20, 40, 60:\n    // Good.\ndefault:\n    return fmt.Errorf(\"Illegal frame size: %d bytes (%f ms)\", frameSize, frameSizeMs)\n}\n\ndata := make([]byte, bufferSize)\nn, err := enc.Encode(pcm, data)\nif err != nil {\n    ...\n}\ndata = data[:n] // only the first N bytes are opus data. Just like io.Reader.\n```\n\nNote that you must choose a target buffer size, and this buffer size will affect\nthe encoding process:\n\n\u003e Size of the allocated memory for the output payload. This may be used to\n\u003e impose an upper limit on the instant bitrate, but should not be used as the\n\u003e only bitrate control. Use `OPUS_SET_BITRATE` to control the bitrate.\n\n-- https://opus-codec.org/docs/opus_api-1.1.3/group__opus__encoder.html\n\n### Decoding\n\nTo decode opus data to raw PCM format, first create a decoder:\n\n```go\ndec, err := opus.NewDecoder(sampleRate, channels)\nif err != nil {\n    ...\n}\n```\n\nNow pass it the opus bytes, and a buffer to store the PCM sound in:\n\n```go\nvar frameSizeMs float32 = ...  // if you don't know, go with 60 ms.\nframeSize := channels * frameSizeMs * sampleRate / 1000\npcm := make([]int16, int(frameSize))\nn, err := dec.Decode(data, pcm)\nif err != nil {\n    ...\n}\n\n// To get all samples (interleaved if multiple channels):\npcm = pcm[:n*channels] // only necessary if you didn't know the right frame size\n\n// or access sample per sample, directly:\nfor i := 0; i \u003c n; i++ {\n    ch1 := pcm[i*channels+0]\n    // For stereo output: copy ch1 into ch2 in mono mode, or deinterleave stereo\n    ch2 := pcm[(i*channels)+(channels-1)]\n}\n```\n\nTo handle packet loss from an unreliable network, see the\n[DecodePLC](https://godoc.org/gopkg.in/hraban/opus.v2#Decoder.DecodePLC) and\n[DecodeFEC](https://godoc.org/gopkg.in/hraban/opus.v2#Decoder.DecodeFEC)\noptions.\n\n### API Docs\n\nGo wrapper API reference:\nhttps://godoc.org/github.com/KarpelesLab/static-opus/opus\n\nFull libopus C API reference:\nhttps://www.opus-codec.org/docs/opus_api-1.1.3/\n\nFor more examples, see the `_test.go` files.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarpeleslab%2Fstatic-opus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkarpeleslab%2Fstatic-opus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarpeleslab%2Fstatic-opus/lists"}