Skip to content

Add support for audio spectrum#2

Merged
jcavar merged 1 commit intomainfrom
feature/spectrum
May 20, 2025
Merged

Add support for audio spectrum#2
jcavar merged 1 commit intomainfrom
feature/spectrum

Conversation

@jcavar
Copy link
Collaborator

@jcavar jcavar commented Apr 4, 2025

Add support for generating frequency spectrum snapshots of audio buffers using FFT analysis.

This includes:

  • New spectrum view for visualizing frequency data using SwiftUI Charts
  • Extension for AVAudioPCMBuffer to compute spectrum data
  • Test coverage for various waveforms including pure tones and noise

@jcavar jcavar force-pushed the feature/spectrum branch 3 times, most recently from 1b2c337 to 38f6555 Compare April 4, 2025 10:51
@jcavar jcavar force-pushed the feature/spectrum branch 2 times, most recently from 1c2dc80 to aa7d3d7 Compare April 26, 2025 07:35
@jcavar jcavar requested a review from hhrvoic April 26, 2025 14:02
@jcavar jcavar marked this pull request as ready for review April 26, 2025 14:02
Add support for generating frequency spectrum snapshots of audio buffers using FFT analysis.

This includes:

- New spectrum view for visualizing frequency data using SwiftUI Charts
- Extension for AVAudioPCMBuffer to compute spectrum data
- Test coverage for various waveforms including pure tones and noise
@jcavar jcavar force-pushed the feature/spectrum branch from aa7d3d7 to 6af7978 Compare April 26, 2025 14:09
Copy link
Contributor

@hhrvoic hhrvoic left a comment

Choose a reason for hiding this comment

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

Looks good to me

func spectrum(window: [Float]) -> [FrequencyAmplitude] {
let mono = mixToMono()
guard let data = mono.floatChannelData?[0] else {
fatalError("Not a float audio format")
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we throw an error here instead of exiting the process here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I would like to avoid making API throwing. mixToMono returns float channel data, so this is just a limitation of API that we are using. This case should never happen.

}
}

private func fft(
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe to have the method name written in full instead of abbreviation. I had to google it :D

Suggested change
private func fft(
private func fastFourierTransform(

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FFT is a well-known abbreviation in DSP. As you can see in the implementation of this function, Accelerate also uses vDSP.FFT, so I feel it is ok to keep it as is

}

private func fft(
n: Int,
Copy link
Contributor

Choose a reason for hiding this comment

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

should we call this n frameLength or something that is more user friendly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I would like to keep this one too. Notice how vDSP.FFT uses log2n in parameter name. I think it is worth keeping it this way in our code for clarity and consistency.

@jcavar jcavar merged commit 7c12f27 into main May 20, 2025
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.

2 participants