Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A working example with AVAudioPCMBuffer #42

Open
ondrejkorol opened this issue Sep 8, 2024 · 1 comment
Open

A working example with AVAudioPCMBuffer #42

ondrejkorol opened this issue Sep 8, 2024 · 1 comment

Comments

@ondrejkorol
Copy link

Hi everyone,

I’m currently working on a project using AVAudioEngine and need assistance with properly encoding and decoding AVAudioPCMBuffer to and from the Opus format.

Would anyone be able to share a working solution or example, please?
Any help or pointers would be greatly appreciated!

Thank you,
Ondrej

@jasperaelvoet
Copy link

It's actually fairly simple to do using this package.

// set the opus encoding format
var encodingFormat = AVAudioFormat(
  opusPCMFormat: .float32,
  sampleRate: .opus48khz,
  channels: 1
)

// Initialize the encoder
guard var opusEncoder = try? Opus.Encoder(format: encodingFormat) else {
  print("Failed to create opus encoder")
  return
}

// and now you can use the encoder to encode your AVAudioPCMBuffer. The encoding migh fail if the samplerate does not match, sou you might need to convert the sample rate first
var buffer: AVAudioPCMBuffer = example_buffer // your buffer
var opusData = Data(count: 10000) // not sure why, but setting the data count to something like this fixes weird errors I've encountered
_ = try opusEncoder.encode(buffer, to: &opusData)
print(opusData)

Was stuck on this myself for weeks.
Thank you @ydnar for creating this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants