Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix audio processing edge case #237

Merged
merged 9 commits into from
Nov 1, 2024
Merged

Conversation

ZachNagengast
Copy link
Contributor

Resolves #228

This is a case where the framePosition of the AVAudioFile is not actually correctly being set after reaching the end of the file. For some reason, the frame count does not match the actual frame count of the file. However, reading the file still works even if it is told to read more frames than exist in the file, so we will track the frame position ourselves and stop trying to read the file after we have reached what we think is the end of the file, even if less frames have been returned. It appears to be an internal Apple bug.

@ZachNagengast ZachNagengast merged commit dd2eb73 into main Nov 1, 2024
9 checks passed
@Josscii
Copy link

Josscii commented Nov 2, 2024

Hi, I found this bug could be fixed by detecting the bad file and convert it to m4a.

In my transcribe code, I always try init a AVAudioFile, if it failed, I use AVAssetExportSession to convert it to a m4a, and then do the transcribe, because the bad file like this one.

var audioFile = try? AVAudioFile(forReading: fileURL)

if audioFile == nil || task.retry {
    let url = await AudioExporter.tryExport(url: fileURL, filename: "transcribe_temp")
    if Task.isCancelled {
        return false
    }

    if let url {
        fileURL = url
        audioFile = try AVAudioFile(forReading: fileURL)
    }
}

After converting it, it works ok with the original code.

An another possible solution to this bug.

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

Successfully merging this pull request may close these issues.

mp3 (attached) results in "Failed to process audio buffer" issue
3 participants