Using the code from Go's Pion WebRTC library play-from-disk example I'm able to create a WebRTC connection and send audio from a local Ogg file to peers.
The play-from-disk example README.md details how to first convert the page size of the Ogg file to 20,000 using ffmpeg, like so:
ffmpeg -i $INPUT_FILE -c:a libopus -page_duration 20000 -vn output.ogg
I'd like to make this same adjustment to Ogg data natively in Go, without using ffmpeg. How can this be done?
I've tried using Pion's oggreader and oggwriter, but using these requires deep Opus file format and RTP protocol knowledge that neither I nor ChatGPT seem to have.
For additional context, I'm using a Text-to-Speech (TTS) API to generate my Ogg data as follows:
req, err := http.NewRequest("POST", "https://api.openai.com/v1/audio/speech", bytes.NewBuffer([]byte(fmt.Sprintf(`{
"model": "tts-1",
"voice": "alloy",
"input": %q,
"response_format": "opus",
}`, text))))
req.Header.Add("Authorization", "Bearer "+token)
req.Header.Add("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
resp, err := client.Do(req)
As I'm trying to create a real-time audio app, ideally, I'd like to pipe the response to WebRTC performing the conversion on chunks as these are received so that peers can start to listen to the audio before it has been fully received on the server.