audio_utils

package
v0.0.0-...-712fcf5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2023 License: MIT Imports: 11 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeFromFlac

func DecodeFromFlac(rawFlacBytes []byte) (*audio.IntBuffer, error)

DecodeFromFlac is a slightly modified version of flac2wav.go https://github.com/apatterson-cogo/flac/blob/e97902945092/cmd/flac2wav/flac2wav.go

func DecodeFromMp3

func DecodeFromMp3(rawAudioBytes []byte) (*audio.IntBuffer, error)

func DecodeFromMulaw

func DecodeFromMulaw(byteData []byte, inputSampleRate int) *audio.IntBuffer

DecodeFromMulaw assumes one channel and encoding 7 (or one byte per value)

func EncodeToMulaw

func EncodeToMulaw(intBuffer *audio.IntBuffer, outputSampleRate int) ([]byte, error)

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

func EncodeToWavSimple(inputBuffer *audio.IntBuffer) (result []byte, err error)

EncodeToWavSimple is like EncodeToWav, but just uses the same sample formats as in the input.

func ResampleSimple

func ResampleSimple(input []int, inputSampleRate, outputSampleRate int) []int

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 StereoToMono

func StereoToMono(stereo []int) []int

StereoToMono was generated by GPT4.

func TwoByteDataToIntSlice

func TwoByteDataToIntSlice(audioData []byte) []int

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL