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

VAD audio chunking #135

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Conversation

jkrukowski
Copy link
Contributor

@jkrukowski jkrukowski commented May 6, 2024

This PR introduces audio chunking with VAD. The VAD is used to detect speech segments in the audio file and then the audio is split into chunks based on the detected speech segments (and padded with zeros to match the 30sec length). Chunks are then processed in a batch resulting in a significant speedup.

Some benchmarks (on my mac book air m1):

Audio file 12:16 length

  • with VAD:
38.16s user 5.86s system 470% cpu 9.349 total
  • without VAD:
33.25s user 3.55s system 132% cpu 27.678 total

Audio file 40:26 length

  • with VAD:
126.54s user 18.41s system 500% cpu 28.952 total
  • without VAD:
96.55s user 10.47s system 133% cpu 1:20.08 total

To use it in WhisperKitCLI the user has to pass the chunking-strategy flag:

swift run -c release whisperkit-cli transcribe --audio-path /path/to/audio.wav --chunking-strategy vad

@jkrukowski jkrukowski mentioned this pull request May 6, 2024
@atiorh
Copy link
Contributor

atiorh commented May 6, 2024

Great work @jkrukowski! Did you see the Cut and Merge strategy in https://github.com/m-bain/whisperX?

Screenshot 2024-05-06 at 10 10 29 AM

If we don't attempt to pack short segments into 30 seconds like above before padding, the worst case performance might regress below baseline (e.g. padding ~1-3s chunks to 30s a lot). Let me know if you think Cut and Merge is an extension we should leave as future work or bundle here :)

Edit: Cut and Merge will also mean some additional bookkeeping to adjust word-level timetamps post-inference.

@atiorh
Copy link
Contributor

atiorh commented May 6, 2024

@Abhinay1997 Do you mind rebasing on top of this PR so we can add a WER check (w/ and w/o VAD-based chunking) on your long audio test sample? 🙏

@Abhinay1997
Copy link
Contributor

Hey @atiorh ! No worries, I'll do that by tomorrow. Want to make sure there are no bugs/crash prone code in my PR.

@jkrukowski
Copy link
Contributor Author

jkrukowski commented May 7, 2024

Great work @jkrukowski! Did you see the Cut and Merge strategy in https://github.com/m-bain/whisperX?

If we don't attempt to pack short segments into 30 seconds like above before padding, the worst case performance might regress below baseline (e.g. padding ~1-3s chunks to 30s a lot). Let me know if you think Cut and Merge is an extension we should leave as future work or bundle here :)

Edit: Cut and Merge will also mean some additional bookkeeping to adjust word-level timetamps post-inference.

I'd leave it as a future work if possible. After talking to @ZachNagengast the other day I took a bit different approach here -- using VAD I'm trying to find the best cut off point in the 2nd half of 30sec audio chunk. So there is no risk of having a bunch of small segments padded with zeros (because the segment will contain at least 15 sec of the original audio). Having said that I think that cut and merge is a better (but more complicated) approach

@atiorh
Copy link
Contributor

atiorh commented May 7, 2024

Great work @jkrukowski! Did you see the Cut and Merge strategy in https://github.com/m-bain/whisperX?
If we don't attempt to pack short segments into 30 seconds like above before padding, the worst case performance might regress below baseline (e.g. padding ~1-3s chunks to 30s a lot). Let me know if you think Cut and Merge is an extension we should leave as future work or bundle here :)
Edit: Cut and Merge will also mean some additional bookkeeping to adjust word-level timetamps post-inference.

I'd leave it as a future work if possible. After talking to @ZachNagengast the other day I took a bit different approach here -- using VAD I'm trying to find the best cut off point in the 2nd half of 30sec audio chunk. So there is no risk of having a bunch of small segments padded with zeros (because the segment will contain at least 15 sec of the original audio). Having said that I think that cut and merge is a better (but more complicated) approach

Makes sense, this is great.

Copy link
Contributor

@ZachNagengast ZachNagengast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really nice 👍 just a couple suggestions for clarity and future adaptability.

self.energyThreshold = energyThreshold
}

func voiceActivity(in waveform: [Float]) -> [Bool] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have a public helper method that returns the exact clip timestamps in case someone wants to run the chunking ahead of time in their app and pass them directly via clipTimestamps decoding option.

e.g.

let clips: [Int] = EnergyVAD().voiceActivity(in: audioArray)
let options = DecodingOptions(clipTimestamps: clips)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added calculateNonSilentChunks method in AudioProcessor which is backed by EnergyVAD, this way we can keep EnergyVAD class internal

import Accelerate

/// Voice activity detection based on energy threshold
final class EnergyVAD {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have some other VAD code in the repo, could you consolidate that code by using this new class? May need a protocol for the other vad methods we have coming up (mel analysis, ML based), but your call on API design. I think the enum and existing chunking protocol solves this pretty well too fwiw.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now I decided to move this other VAD code in our repo to isVoiceDetected method in AudioProcessor. This way we can keep EnergyVAD internal till the full public interface of this class is ready

Sources/WhisperKit/Core/EnergyVAD.swift Outdated Show resolved Hide resolved
Sources/WhisperKit/Core/EnergyVAD.swift Outdated Show resolved Hide resolved
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

Successfully merging this pull request may close these issues.

None yet

4 participants