gtreshchev / AudioAnalysisTools

Audio Analysis Tools plugin for Unreal Engine. Provides a variety of functions for analyzing audio data. Works in conjunction with the Runtime Audio Importer plugin.
MIT License
92 stars 16 forks source link

Beat position calculation #3

Closed Caffiendish closed 2 years ago

Caffiendish commented 2 years ago

Is there a way to work out the time position of beats in a song?

I'm trying to generate a map of the beats in a given song at runtime, to use as events, but I'm finding "IsBeat" functions more like an audio visualiser. For example, given a simple beat like https://www.youtube.com/watch?v=HTmKgbT3PFA, an incremented int reaches 120 long before a minute passes, and watching the printed strings, it looks like it'd work really well as a visualiser.

Is there a function to work out slightly more precisely where a beat is? Something like https://github.com/aubio/aubio/blob/master/src/tempo/tempo.c ?

Definitely going to use your plugins, thanks for your work! 😄

gtreshchev commented 2 years ago

You can detect beat on the basis of multiple sub-bands using a "IsBeatRange" function. Also, if you want to change the maximum FFT sub-bands size, you can call "UpdateFFTSubbandsSize" in a BeatDetection object (which can be accessed from AudioAnalysisToolsLibrary object)

You can detect beats based on multiple FFT sub-bands using the "IsBeatRange" function. Also, if you want to change the maximum sub-band size, you can call "UpdateFFTSubbandsSize" in the BeatDetection object (which can be accessed from the AudioAnalysisToolsLibrary object) to use wider sub-band ranges for beat detection.

Caffiendish commented 2 years ago

Also, if you want to change the maximum sub-band size, you can call "UpdateFFTSubbandsSize" in the BeatDetection object

With some quick testing, that gives the following exception when attempting to process the audio frame, after setting the sub band size > 32:

Assertion failed: (Index >= 0) & (Index < ArrayNum) [File:C:\Program Files\Epic Games\UE_5.0\Engine\Source\Runtime\Core\Public\Containers\Array.h] [Line: 691] Array index out of bounds: 0 from an array of size 0

UnrealEditor_AudioAnalysisTools!UBeatDetection::UpdateFFT() [Plugins\AudioAnalysisTools\Source\AudioAnalysisTools\Private\Analyzers\BeatDetection.cpp:81] UnrealEditor_Core UnrealEditor_Core UnrealEditor_Core UnrealEditor_Core UnrealEditor_Core UnrealEditor_Core UnrealEditor_Core UnrealEditor_Core UnrealEditor_Core UnrealEditor_Core kernel32 ntdll

I guess I'm not doing something right there! 😅

gtreshchev commented 2 years ago

You apparently didn't correctly specify FFTSubbandsSize. Regarding your crash, you have it equal to zero.

Caffiendish commented 2 years ago

You apparently didn't correctly specify FFTSubbandsSize. Regarding your crash, you have it equal to zero.

Sorry, I didn't specify, I can set it lower than 32, but anything higher throws that exception on the first audio process call.

Caffiendish commented 2 years ago

Perhaps I'm using the wrong node? image

Caffiendish commented 2 years ago

Just checked, the EnergyHistory double array gets resized, but never gets the new array resized, so I'd have to have called UpdateEnergyHistorySize as well.

Perhaps the UpdateFFTSubbandsSize should call this itself, to not encounter this error in the future?

Edit: I made a PR for it, along with a couple of other issues I noticed.

gtreshchev commented 2 years ago

Thank you! I will look into it at the weekend

Caffiendish commented 2 years ago

I'm actually still working on this, I've not had a lot of consistent success with the IsBeatRange functionality (honestly, I'm still assuming that I'm utilising it poorly), and in my case, it would be much more helpful to know upfront where the beats are, to better generate content.

If one wanted to procedurally generate content, you'd need to know where beats are in advance, so you don't have long moments of downtime where a song gets quiet, and your user has no beat/energy derived content, for example. You can't just assume that a couple of seconds without a beat is going to remain that way, and so being able to analyse a user provided song in advance of actually using it to create content is extremely useful.

I've been looking at a couple of implementations of KISS FFT, and they use some analysis methods that aren't in here, so I've been working on adding those methods, and attempting to create some functionality that will output a list of beat positions.