K
Kord7d ago
Shaun

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
gdude
gdude7d ago
Wav is just raw audio data It's not a codec
Shaun
ShaunOP7d ago
Okay so that's one less step at least
gdude
gdude7d ago
You just write it to file afaik
Shaun
ShaunOP7d ago
I need to convert opus to raw audio
gdude
gdude7d ago
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
Shaun
ShaunOP7d ago
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
gdude
gdude7d ago
yeah lavaplayer/lavalink can probably do it
Shaun
ShaunOP7d ago
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..
viztea
viztea7d ago
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
viztea
viztea7d ago
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.
Shaun
ShaunOP7d ago
the lavaplayer API seems simple enough But I'm getting errors that are just numbers lol Not helpful
viztea
viztea7d ago
well if they're opus errors that's because libopus is a C library
Shaun
ShaunOP7d ago
Not even a snippet of example code Why do people release libs like that
gdude
gdude7d ago
it's a binding for opus natives you'd read the opus docs for that
viztea
viztea7d ago
btw lavaplayer doesn't have wav encoding stuff that's why i suggested using ffmpeg instead
gdude
gdude7d ago
well I would say wav is a terrible container you probably want something compressed
viztea
viztea7d ago
well regardless of container, lavaplayer is purely for decoding
Shaun
ShaunOP7d ago
As long as it's one of these formats flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
gdude
gdude7d ago
ogg probably
Shaun
ShaunOP7d ago
Isn't ogg related to opus?
gdude
gdude7d ago
no
viztea
viztea7d ago
i'd go for mkv or mp4
gdude
gdude7d ago
it's ogg vorbis
viztea
viztea7d ago
ogg has opus and flac support
gdude
gdude7d ago
mkv or mp4 for audio? god no
viztea
viztea7d ago
:clueless: ogg is a terrible audio format
gdude
gdude7d ago
it's a fine and common one
Shaun
ShaunOP7d ago
ogg is good no?
viztea
viztea7d ago
it's good for streaming
Shaun
ShaunOP7d ago
Are you just a hater
viztea
viztea7d ago
but that's about it
gdude
gdude7d ago
it's not proprietary unlike any of the m- formats
viztea
viztea7d ago
if you want a lightweight format, ogg is fine though
Shaun
ShaunOP7d ago
Yeah ogg will be fine So I need opus > ogg It's being streamed back anyway
viztea
viztea7d ago
mkv then if you actually want a format you can use
Shaun
ShaunOP7d ago
Is discord audio 1 channel?
viztea
viztea7d ago
2 opus is primarily 2 channels 48khz
Shaun
ShaunOP7d ago
Okay so I had error -1, -4, now -2
Shaun
ShaunOP7d ago
It doesn't say what's what lol
viztea
viztea7d ago
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.
Shaun
ShaunOP7d ago
Okay buffer too small If was making a wrapper I would personally include those errors in it...
viztea
viztea7d ago
it probably does opus_packet_get_nb_samples
gdude
gdude7d ago
discord audio is one channel per participant
viztea
viztea7d ago
wat that's not how it works ? there's an opus packet per participant, each opus packet is 2 channels 48 khz
Shaun
ShaunOP7d ago
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?
val buffer = ByteBuffer.allocateDirect(bufferSize)
audio.forEach {
buffer.put(it.data)
}
buffer.flip()
val buffer = ByteBuffer.allocateDirect(bufferSize)
audio.forEach {
buffer.put(it.data)
}
buffer.flip()
I'm just throwing all the packets captured into one buffer
viztea
viztea7d ago
whether the client records 2 channels idk
Shaun
ShaunOP7d ago
But it's saying its corrupted
viztea
viztea7d ago
it being an AudioFrame?
Shaun
ShaunOP7d ago
Yes
viztea
viztea7d ago
opus_decode operates on one packet at a time
Shaun
ShaunOP7d ago
Surely not
viztea
viztea7d ago
yup it makes perfect sense
Shaun
ShaunOP7d ago
also put is just writing at index 0 everytime anyway...
viztea
viztea7d ago
no ByteBuffers have an internal read/write head
Shaun
ShaunOP7d ago
put writes at index 0
public final ByteBuffer put(byte[] src) {
return put(src, 0, src.length);
}
public final ByteBuffer put(byte[] src) {
return put(src, 0, src.length);
}
viztea
viztea7d ago
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 head
Shaun
ShaunOP7d ago
Okay So I need to handle it one packet at a time
viztea
viztea7d ago
so if you're trying to merge packets from all participants you will need to decode all packets and then mix them together
Shaun
ShaunOP7d ago
And write to my output buffer Then save How do I size the output array?
viztea
viztea7d ago
for opus_decode?
Shaun
ShaunOP7d ago
Yes
viztea
viztea7d ago
opus_packet_get_nb_samples(packet buffer)
Shaun
ShaunOP7d ago
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
Want results from more Discord servers?
Add your server