OpusDotNet
OpusDotNet copied to clipboard
Could you please add an example with the new method signature? (and with a real PCM input as well if possible)
I am trying to use it but when a pass a real PCM byte array as input it never count a valid frame size when this line is called it return a huge number. double frameSize = API.GetFrameSize(pcmLength, SampleRate, Channels);
What could I be doing wrong?
Here is my code:
public Task<byte[]> ConvertAsync(byte[] content)
{
using var encoder = new OpusEncoder(Application.Audio, 48000, 2)
{
Bitrate = 128000,
VBR = true // Variable bitrate
};
var opusBytes = encoder.Encode(content, content.Length, out var encodedLength);
return Task.FromResult(opusBytes);
}
I know it's late, but in case you still need help: You have to encode your PCM data frame by frame. The first parameter for Encode has to be a buffer with PCM data and a length corresponding to 2.5, 5, 10, 20, 40 or 60 ms (with regards to the waveformat used). For example, with 16kHz 16bit mono, a 60ms frame occupies 60162*1 = 1920 bytes. If you give a maximum encoded buffer length of 128 bytes, the maximum bit rate will be about 2 KB/s.
hth, mav