audio-go is a lightweight audio processing framework built on Go and FFmpeg. It is designed to provide developers with a simple and intuitive way to handle real-time streaming audio (e.g., WebSocket voice streams, real-time transcoding) and offline audio files.
With a highly abstracted API, you can easily implement audio format conversion, channel splitting, and multi-stream real-time synthesis without getting bogged down in complex FFmpeg command-line arguments.
- Dual-Mode Architecture:
- Stream Mode: Powered by
io.Pipecommunication, specifically designed for low-latency, high-concurrency real-time scenarios. - File Mode: Leverages FFmpeg to efficiently process disk files with support for batch conversion.
- Stream Mode: Powered by
- Multifunctional Toolbox:
- Format Convert: Seamless conversion between various audio formats.
- Channel Split: Split stereo audio into independent Left/Right mono channels.
- Audio Merge: Synthesize multiple mono streams, supporting "Side-by-Side" (Stereo mapping) or "Mix" modes.
- Intelligent Resampling: Built-in
aresamplefilter to automatically align sample rates, channel counts, and encoding formats during processing. - Robust Error Handling: Automatically captures FFmpeg
stderroutput and wraps it into standard Go errors, making it easy to debug issues caused by corrupted audio or parameter mismatches.
- Go: 1.20+
- FFmpeg: Ensure FFmpeg dynamic libraries or binaries are pre-installed and
ffmpegis in your systemPATH. FFmpeg Official Download.
Bash
go get github.com/QuincyGao/audio-go
Please refer to example/main.go
Important Usage Notes
- Mandatory Parameters for PCM: When the input format is
PCM(e.g.,S16LE), you must explicitly provide theSampleRateandChannels. For other encoded formats (likeMP3orWAV), these parameters are optional as they can be automatically detected by the engine. - Configuration Shorthand: During audio channel splitting or merging, if both channels share the same
AudioFileFormat,SampleRateandChannels, you only need to provide one configuration entry in theInputArgsorOutputArgsslice. The engine will automatically apply it to both streams. - Channel Limitations: Currently, the framework strictly supports the merging of two mono streams into one stereo stream, or the splitting of one stereo stream into two mono streams.
audio-go manages communication between Go and FFmpeg processes by handling Standard Input (stdin), Standard Output (stdout), and Extended File Descriptors (ExtraFiles).
| Mode | Input Channels (Go -> FFmpeg) | Output Channels (FFmpeg -> Go) | Use Case |
|---|---|---|---|
| Convert | pipe:0(Primary) |
pipe:1(Left) |
Real-time transcoding, resampling |
| Split | pipe:0(Primary) |
pipe:1,pipe:3 |
Channel separation (e.g., extracting Left channel) |
| Merge | pipe:0,pipe:3 |
pipe:1(Left) |
Voice intercom merging, BGM overlay |
The configuration supports slices, allowing unique parameters to be specified for each input/output stream.
- AudioFileFormat: Supports
WAV,MP3,AAC,S16LE(Raw PCM), and more. - SampleRate: Supports any sample rate (Automatic resampling built-in).
- Channels: Supports conversion between Mono (1) and Stereo (2).
If you encounter any issues or have suggestions for new features, please feel free to submit an Issue or Pull Request.
- Fork the project.
- Create your feature branch (
git checkout -b feature/AmazingFeature). - Commit your changes (
git commit -m 'feat: add new audio filter'). - Open a Pull Request.
This project is licensed under the MIT License.
