Opus stream to wav?
How can I take the incoming opus stream from a voice channel and save it as a wave file?
I have the full opus audio sample as a ByteArray, how do I actually convert this to a wav file?
65 Replies
Wav is just raw audio data
It's not a codec
Okay so that's one less step at least
You just write it to file afaik
I need to convert opus to raw audio
Yeah that's the tricky part
You'll need to interface with libopus
There must be a wrapper for it
You might need to find one though
I think I may have one
So I can just write the bytearray of raw audio directly to WAV?
That's convinient
Seems lavaplayer can do it
yeah lavaplayer/lavalink can probably do it
I have a ByteBuffer in from Opus, and it wants a ShortBuffer out, but I'm not sure what size the output buffer should be..
lavaplayer probably wouldn't be a great fit here
you would be better off using something like https://github.com/discord-java/opus-java and interacting with libopus manually
or you can use something like https://github.com/bramp/ffmpeg-cli-wrapper and write the opus packets to a stream that is piped to this
GitHub
GitHub - bramp/ffmpeg-cli-wrapper: Java wrapper around the FFmpeg c...
Java wrapper around the FFmpeg command line tool. Contribute to bramp/ffmpeg-cli-wrapper development by creating an account on GitHub.
the lavaplayer API seems simple enough
But I'm getting errors that are just numbers lol
Not helpful
well if they're opus errors
that's because libopus is a C library
Not even a snippet of example code
Why do people release libs like that
it's a binding for opus natives
you'd read the opus docs for that
btw lavaplayer doesn't have wav encoding stuff
that's why i suggested using ffmpeg instead
well I would say wav is a terrible container
you probably want something compressed
well regardless of container, lavaplayer is purely for decoding
As long as it's one of these formats
flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
ogg probably
Isn't ogg related to opus?
no
i'd go for mkv or mp4
it's ogg vorbis
ogg has opus and flac support
mkv or mp4 for audio? god no
:clueless:
ogg is a terrible audio format
it's a fine and common one
ogg is good no?
it's good for streaming
Are you just a hater
but that's about it
it's not proprietary unlike any of the m- formats
if you want a lightweight format, ogg is fine though
Yeah ogg will be fine
So I need opus > ogg
It's being streamed back anyway
mkv then
if you actually want a format you can use
Is discord audio 1 channel?
2
opus is primarily 2 channels 48khz
Okay so I had error -1, -4, now -2
https://opus-codec.org/docs/html_api-1.0.3/group__opus__errorcodes.html
god c docs are so bad
It doesn't say what's what lol
GitHub
opus/include/opus_defines.h at main · xiph/opus
Modern audio compression for the internet. Contribute to xiph/opus development by creating an account on GitHub.
Okay buffer too small
If was making a wrapper I would personally include those errors in it...
it probably does
opus_packet_get_nb_samples
discord audio is one channel per participant
wat
that's not how it works ? there's an opus packet per participant, each opus packet is 2 channels 48 khz
I'm getting -4 now, which is corrupt packet
I'm pretty sure its 2 channels because music bots can play stuff in either ear
Am I doing this right?
I'm just throwing all the packets captured into one buffer
whether the client records 2 channels idk
But it's saying its corrupted
it
being an AudioFrame?Yes
opus_decode operates on one packet at a time
Surely not
yup
it makes perfect sense
also
put
is just writing at index 0 everytime anyway...no
ByteBuffers have an internal read/write head
put writes at index 0
read the declaration of read(byte[], int, int)
that's putting bytes 0 to src.length within
src
inside the buffer at the current write headOkay
So I need to handle it one packet at a time
so if you're trying to merge packets from all participants you will need to decode all packets and then mix them together
And write to my output buffer
Then save
How do I size the output array?
for opus_decode?
Yes
opus_packet_get_nb_samples(packet buffer)
library for encoding ogg vorbis?
This is driving me fucking crazy
So many c wrappers with no docs
omg the decoder forces me to use a short array, but the other things expects a byte array
My head is litearlly blowing off
Now I'm getting
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'opus_decoder_create': The specified procedure could not be found.
I'm not sure what to do
Okay I have managed to write the stream to a file
But it's corrupt as I can't play the file...
If I write the raw data to a raw file and import raw to audacity, I can hear the audio
So must be something with the raw > vorbis failing
The opus decoder decodes to a 16 bit 2 channel format
But I don't see where to specify that on the audio output
Okay I'm fucking ogg off
I've managed to save it as a wav file, but it's too fast now lol
Okay yea it was 1 channel
not 2