Documentation ¶
Overview ¶
Package audio_utils.convert deals with translating waveform samples so they can be used between systems, e.g.: * Local microphone produces 16bit samples with 44,100 khz * OpenAI TTS produces mp3, flac, opus with 24,000 sample rate * OpenAI Whisper takes wav * Twilio Telephony requires mulaw encoded 8bit with 8khz sample rate Usage: 1.) Convert your format to audio.IntBuffer 2.) Convert audio.IntBuffer to your desired format NOTE: * Everything happens in-memory to make deployment as easy as possible, * i.e. NO ffmpeg, files or other external libraries to loose hair for.
TODO(P1, devx): Might be(en) worthwhile looking / migrating into https://github.com/faiface/beep/
Index ¶
- func DecodeFromFlac(rawFlacBytes []byte) (*audio.IntBuffer, error)
- func DecodeFromMp3(rawAudioBytes []byte) (*audio.IntBuffer, error)
- func DecodeFromMulaw(byteData []byte, inputSampleRate int) *audio.IntBuffer
- func EncodeToMulaw(intBuffer *audio.IntBuffer, outputSampleRate int) ([]byte, error)
- func EncodeToWav(inputBuffer *audio.IntBuffer, bitDepth int, audioFormat int) (result []byte, err error)
- func EncodeToWavSimple(inputBuffer *audio.IntBuffer) (result []byte, err error)
- func ResampleSimple(input []int, inputSampleRate, outputSampleRate int) []int
- func StereoToMono(stereo []int) []int
- func TwoByteDataToIntSlice(audioData []byte) []int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeFromFlac ¶
DecodeFromFlac is a slightly modified version of flac2wav.go https://github.com/apatterson-cogo/flac/blob/e97902945092/cmd/flac2wav/flac2wav.go
func DecodeFromMulaw ¶
DecodeFromMulaw assumes one channel and encoding 7 (or one byte per value)
func EncodeToMulaw ¶
func EncodeToWav ¶
func EncodeToWav(inputBuffer *audio.IntBuffer, bitDepth int, audioFormat int) (result []byte, err error)
EncodeToWav takes inputBuffer and outputs encoded .wav with the desired sample format. NOTE: Empirically, we should keep the original sampleRate/bitDepth/channels if possible. TODO: I am semi-sure that audioFormat is ignored, as it can be derived from bitDepth / inputBuffer.SourceBithDepth.
func EncodeToWavSimple ¶
EncodeToWavSimple is like EncodeToWav, but just uses the same sample formats as in the input.
func ResampleSimple ¶
ResampleSimple resamples the input audio data from inputSampleRate to outputSampleRate. The most straightforward approach for resampling is linear interpolation, suitable for small changes in sample rates. https://chat.openai.com/share/22c33099-f66c-4b90-b2aa-4d03ccb8e7fb
TODO(P0, ux): For better quality, more sophisticated techniques like polyphase filtering or windowed Sinc interpolation are preferred. UNFORTUNATELY, all or most of them require an external library like ffmpeg, libsoxr, libsamplerate, libswresample
func TwoByteDataToIntSlice ¶
Types ¶
This section is empty.