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

decoding ERR #34

Open
JatosDetking opened this issue Jan 2, 2024 · 3 comments
Open

decoding ERR #34

JatosDetking opened this issue Jan 2, 2024 · 3 comments

Comments

@JatosDetking
Copy link

when decoding it gives me this error
Error(rawValue: -4)

@rafus15
Copy link

rafus15 commented Dec 3, 2024

Did you fix this? i'm having the same error

@ydnar
Copy link
Contributor

ydnar commented Dec 3, 2024

What are you trying to decode? Can you share reproducible code?

@rafus15
Copy link

rafus15 commented Dec 3, 2024

I'm trying to decode oga files from Telegram.
My first try was obviously flawed because i was not chunking my file (this is a test function, excuse the code mess jeje)

func play(audioUrl: URL) throws {
      var encodingFormat = AVAudioFormat(
          opusPCMFormat: .float32,
          sampleRate: .opus48khz,
        channels: 1
      )
      var opusDecoder = try Opus.Decoder(format: encodingFormat!)
      print("decoder created!")
      let audioData = try Data(contentsOf: audioUrl)
      let mainMixer = audioEngine.mainMixerNode
      audioEngine.attach(audioFilePlayer)
      audioEngine.connect(audioFilePlayer, to: mainMixer, format: encodingFormat!)
      try audioEngine.start()
      audioFilePlayer.play()
      let dec = try opusDecoder.decode(audioData)
      audioFilePlayer.scheduleBuffer(dec)
}

Later looking through the other issues in this repo i found out that i have to parse individual packets and schedule the generated buffers. But i keep getting the same errors for some chunks and estrange sounds.

this is my current code:

func play(audioUrl: URL) throws {
                    var encodingFormat = AVAudioFormat(
            opusPCMFormat: .float32,
            sampleRate: .opus48khz,
          channels: 1
        )
        var opusDecoder = try Opus.Decoder(format: encodingFormat!)
        print("decoder created!")
        let audioData = try Data(contentsOf: audioUrl)
        let mainMixer = audioEngine.mainMixerNode
        audioEngine.attach(audioFilePlayer)
        audioEngine.connect(audioFilePlayer, to: mainMixer, format: encodingFormat!)
       try audioEngine.start()
        var buf = AVAudioPCMBuffer(pcmFormat: encodingFormat!, frameCapacity: 100000)
        audioFilePlayer.play()
        let frameSizeInBytes = 78
var offset = 312
                   print("file size: \(audioData.count)")
                   print("frame size: \(frameSizeInBytes)")
                   while offset < audioData.count {
                       let upperLimit = min(offset + frameSizeInBytes, audioData.count)
                       let opusFrame = audioData[offset..<upperLimit]
                       do {
                           try opusFrame.withUnsafeBytes { ptr in
                               let input = ptr.bindMemory(to: UInt8.self)
                               try opusDecoder.decode(input, to:buf!)
                               audioFilePlayer.scheduleBuffer(buf!)
                           }     
                       } catch {
                           print("failed decoding opus frame ---------- \(error)")
                       }
                       offset += frameSizeInBytes
                   }
        }

Thank you for your time and effort!

EDIT:

As url you can use https://sample-files-online.com/samples/countdownload/280 this sample file

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

3 participants