Class: FrequencyDomain
Overview
Transforms between time-domain and frequency-domain audio, including buffering, windowing (HanningZ) and window overlap handling (default: 4:1). One instance allocates around 131 kb memory.
Steps to use:
- Audio input using addInput().
- Call timeDomainToFrequencyDomain(), if it returns false go back to 1.
- The output of timeDomainToFrequencyDomain is frequency domain data you can work with.
- Call advance() (if required).
- Call frequencyDomainToTimeDomain() to create time domain audio from frequency domain data.
Pseudo-example
// Constructor.let fd = new Superpowered.FrequencyDomain(11, // FFT log size, between 8 and 13 (FFT 256 - 8192). The default value (11) provides a good compromise in precision (~22 Hz per bin), CPU load and time-domain event sensitivity.4 // [Maximum overlap]:1 (default: 4:1).);// Add some audio input. Has no return value.fd.addInput(input, // Pointer to floating point numbers. 32-bit interleaved stereo input.512 // The number of input frames.);// In the frequency domain we are working with 1024 magnitudes and phases for every channel (left, right), if the fft size is 2048.// Put this into an iteration, because it may have more frames available than a single loop. Or none!while (// Converts the audio input (added by addInput()) to the frequency domain.// Each frequency bin is (samplerate / [FFT SIZE] / 2) wide.// Returns true, if a conversion was possible (enough frames were available).fd.timeDomainToFrequencyDomain(magnitudeL, // Pointer to floating point numbers. Magnitudes for each frequency bin, left side. Must be at least [FFT SIZE] big.magnitudeR, // Pointer to floating point numbers. Magnitudes for each frequency bin, right side. Must be at least [FFT SIZE] big.phaseL, // Pointer to floating point numbers. Phases for each frequency bin, left side. Must be at least [FFT SIZE] big.phaseR, // Pointer to floating point numbers. Phases for each frequency bin, right side. Must be at least [FFT SIZE] big.0, // Value of Pi. Pi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI.false, // If true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases).0 // The index of the stereo pair to process.)) {// You can work with frequency domain data inside there, available in magnitudeL, magnitudeR, phaseL and phaseR.// After the frequency domain data is modified, maybe you want to go back to the time domain.// The method below converts frequency domain data to audio output. Has no return value.fd.frequencyDomainToTimeDomain(magnitudeL, // Pointer to floating point numbers. Magnitudes for each frequency bin, left side. Must be at least [FFT SIZE] big.magnitudeR, // Pointer to floating point numbers. Magnitudes for each frequency bin, right side. Must be at least [FFT SIZE] big.phaseL, // Pointer to floating point numbers. Phases for each frequency bin, left side. Must be at least [FFT SIZE] big.phaseR, // Pointer to floating point numbers. Phases for each frequency bin, right side. Must be at least [FFT SIZE] big.output, // Pointer to floating point numbers. 32-bit interleaved stereo output.0, // Value of Pi. Pi can be translated to any value (Google: the tau manifesto). Leave it at 0 for M_PI.0, // For advanced use, if you know how window overlapping works. Use 0 (the default value) otherwise for a 4:1 overlap (good compromise in audio quality).false, // If true, then the magnitude and phase inputs represent complex numbers (magnitude: real, phase: imag).0 // The index of the stereo pair to process.);// Advances the input buffer (removes the earliest frames). Has no return value.fd.advance(0 // Number of frames. For advanced use, if you know how window overlapping works. Use 0 (the default value) otherwise for a 4:1 overlap (good compromise in audio quality).);} // end while// Returns with how many frames of input should be provided to produce some output.let frames = fd.getNumberOfInputFramesNeeded();// Destructor (to free up memory).fd.destruct();
#include "SuperpoweredFrequencyDomain.h"// Constructor.frequencyDomain = new Superpowered::FrequencyDomain(11, // FFT log size, between 8 and 13 (FFT 256 - 8192). The default value (11) provides a good compromise in precision (~22 Hz per bin), CPU load and time-domain event sensitivity.4 // [Maximum overlap]:1 (default: 4:1).);// Add some audio input. Has no return value.frequencyDomain->addInput(input, // Pointer to floating point numbers. 32-bit interleaved stereo input.512 // The number of input frames.);// In the frequency domain we are working with 1024 magnitudes and phases for every channel (left, right), if the fft size is 2048.// Put this into an iteration, because it may have more frames available than a single loop. Or none!while (// Converts the audio input (added by addInput()) to the frequency domain.// Each frequency bin is (samplerate / [FFT SIZE] / 2) wide.// Returns true, if a conversion was possible (enough frames were available).frequencyDomain->timeDomainToFrequencyDomain(magnitudeL, // Pointer to floating point numbers. Magnitudes for each frequency bin, left side. Must be at least [FFT SIZE] big.magnitudeR, // Pointer to floating point numbers. Magnitudes for each frequency bin, right side. Must be at least [FFT SIZE] big.phaseL, // Pointer to floating point numbers. Phases for each frequency bin, left side. Must be at least [FFT SIZE] big.phaseR, // Pointer to floating point numbers. Phases for each frequency bin, right side. Must be at least [FFT SIZE] big.0, // Value of Pi. Pi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI.false, // If true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases).0 // The index of the stereo pair to process.)) {// You can work with frequency domain data inside there, available in magnitudeL, magnitudeR, phaseL and phaseR.// After the frequency domain data is modified, maybe you want to go back to the time domain.// The method below converts frequency domain data to audio output. Has no return value.frequencyDomain->frequencyDomainToTimeDomain(magnitudeL, // Pointer to floating point numbers. Magnitudes for each frequency bin, left side. Must be at least [FFT SIZE] big.magnitudeR, // Pointer to floating point numbers. Magnitudes for each frequency bin, right side. Must be at least [FFT SIZE] big.phaseL, // Pointer to floating point numbers. Phases for each frequency bin, left side. Must be at least [FFT SIZE] big.phaseR, // Pointer to floating point numbers. Phases for each frequency bin, right side. Must be at least [FFT SIZE] big.output, // Pointer to floating point numbers. 32-bit interleaved stereo output.0, // Value of Pi. Pi can be translated to any value (Google: the tau manifesto). Leave it at 0 for M_PI.0, // For advanced use, if you know how window overlapping works. Use 0 (the default value) otherwise for a 4:1 overlap (good compromise in audio quality).false, // If true, then the magnitude and phase inputs represent complex numbers (magnitude: real, phase: imag).0 // The index of the stereo pair to process.);// Advances the input buffer (removes the earliest frames). Has no return value.frequencyDomain->advance(0 // Number of frames. For advanced use, if you know how window overlapping works. Use 0 (the default value) otherwise for a 4:1 overlap (good compromise in audio quality).);} // end while// Returns with how many frames of input should be provided to produce some output.unsigned int frames = frequencyDomain->getNumberOfInputFramesNeeded();
Read-only properties
inputList
PROPERTYFor advanced uses: access to the internal audio input pointer list.
Type | Min | Max | Default | Superpowered::AudiopointerList |
---|
Methods
constructor
METHODCreates an instance of FrequencyDomain class.ParametersReturnsName Type Description fftLogSize Number
FFT log size, between 8 and 13 (FFT 256 - 8192). The default value (11) provides a good compromise in precision (~22 Hz per bin), CPU load and time-domain event sensitivity. maxOverlap Number
[Maximum overlap]:1. Type Description Superpowered.FrequencyDomain
The constructed instance. constructor
METHODCreates an instance of FrequencyDomain class.ParametersReturnsName Type Default Description fftLogSize unsigned int
11 FFT log size, between 8 and 13 (FFT 256 - 8192). The default value (11) provides a good compromise in precision (~22 Hz per bin), CPU load and time-domain event sensitivity. maxOverlap unsigned int
4 [Maximum overlap]:1. Type Description Superpowered::FrequencyDomain
The constructed instance. addAudiopointerlistElement
METHODAdd some audio input (advanced use).ParametersReturnsName Type Default Description input AudiopointerlistElement *
The input buffer. NoneaddInput
METHODAdd some audio input.ParametersReturnsName Type Description input Number (pointer in Linear Memory)
32-bit interleaved stereo input. numberOfFrames Number
The number of input frames. NoneaddInput
METHODAdd some audio input.ParametersReturnsName Type Default Description input float *
32-bit interleaved stereo input. numberOfFrames int
The number of input frames. Noneadvance
METHODAdvances the input buffer (removes the earliest frames).ParametersReturnsName Type Description numberOfFrames Number
For advanced use, if you know how window overlapping works. Use 0 (the default value) otherwise for a 4:1 overlap (good compromise in audio quality). Noneadvance
METHODAdvances the input buffer (removes the earliest frames).ParametersReturnsName Type Default Description numberOfFrames int
0 For advanced use, if you know how window overlapping works. Use 0 (the default value) otherwise for a 4:1 overlap (good compromise in audio quality). Nonedestruct
METHODDestructor to free Linear Memory.ParametersNoneReturnsNonefrequencyDomainToTimeDomain
METHODConverts frequency domain data to audio output.ParametersReturnsName Type Description magnitudeL Number (pointer in Linear Memory)
Magnitudes for each frequency bin, left side. Must be at least [FFT SIZE] big. magnitudeR Number (pointer in Linear Memory)
Magnitudes for each frequency bin, right side. Must be at least [FFT SIZE] big. phaseL Number (pointer in Linear Memory)
Phases for each frequency bin, left side. Must be at least [FFT SIZE] big. phaseR Number (pointer in Linear Memory)
Phases for each frequency bin, right side. Must be at least [FFT SIZE] big. output Number (pointer in Linear Memory)
32-bit interleaved stereo output. valueOfPi Number
Pi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. incrementFrames Number
For advanced use, if you know how window overlapping works. Use 0 (the default value) otherwise for a 4:1 overlap (good compromise in audio quality). complexMode Boolean
If true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). stereoPairIndex Number
The index of the stereo pair to process. NonefrequencyDomainToTimeDomain
METHODConverts frequency domain data to audio output.ParametersReturnsName Type Default Description magnitudeL float *
Magnitudes for each frequency bin, left side. Must be at least [FFT SIZE] big. magnitudeR float *
Magnitudes for each frequency bin, right side. Must be at least [FFT SIZE] big. phaseL float *
Phases for each frequency bin, left side. Must be at least [FFT SIZE] big. phaseR float *
Phases for each frequency bin, right side. Must be at least [FFT SIZE] big. output float *
32-bit interleaved stereo output. valueOfPi float
0 Pi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. incrementFrames int
For advanced use, if you know how window overlapping works. Use 0 (the default value) otherwise for a 4:1 overlap (good compromise in audio quality). complexMode bool
false If true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). stereoPairIndex int
0 The index of the stereo pair to process. NonegetNumberOfInputFramesNeeded
METHODReturns with how many frames of input should be provided to produce some output.ParametersNoneReturnsType Description Number
Number of frames. getNumberOfInputFramesNeeded
METHODReturns with how many frames of input should be provided to produce some output.ParametersNoneReturnsType Description unsigned int
Number of frames. reset
METHODReset all internals, sets the instance as good as new.ParametersNoneReturnsNonereset
METHODReset all internals, sets the instance as good as new.ParametersNoneReturnsNonesetStereoPairs
METHODThis class can handle one stereo audio channel pair by default (left + right). You can extend it to handle more.ParametersReturnsName Type Description numStereoPairs Number
The number of stereo audio channel pairs. Valid values: one (stereo) to four (8 channels). dontFree Boolean
If true, this function will not free up any memory if numStereoPairs is less than before, so no reallocation happens if numStereoPairs needs to be increased later. NonesetStereoPairs
METHODThis class can handle one stereo audio channel pair by default (left + right). You can extend it to handle more.ParametersReturnsName Type Default Description numStereoPairs unsigned int
The number of stereo audio channel pairs. Valid values: one (stereo) to four (8 channels). dontFree bool
false If true, this function will not free up any memory if numStereoPairs is less than before, so no reallocation happens if numStereoPairs needs to be increased later. NonetimeDomainToFrequencyDomain
METHODConverts the audio input (added by addInput()) to the frequency domain. Each frequency bin is (samplerate / [FFT SIZE] / 2) wide.ParametersReturnsName Type Description magnitudeL Number (pointer in Linear Memory)
Magnitudes for each frequency bin, left side. Must be at least [FFT SIZE] big. magnitudeR Number (pointer in Linear Memory)
Magnitudes for each frequency bin, right side. Must be at least [FFT SIZE] big. phaseL Number (pointer in Linear Memory)
Phases for each frequency bin, left side. Must be at least [FFT SIZE] big. phaseR Number (pointer in Linear Memory)
Phases for each frequency bin, right side. Must be at least [FFT SIZE] big. valueOfPi Number
Pi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. complexMode Boolean
If true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). stereoPairIndex Number
The index of the stereo pair to process. Type Description Boolean
True, if a conversion was possible (enough frames were available). timeDomainToFrequencyDomain
METHODConverts the audio input (added by addInput()) to the frequency domain. Each frequency bin is (samplerate / [FFT SIZE] / 2) wide.ParametersReturnsName Type Default Description magnitudeL float *
Magnitudes for each frequency bin, left side. Must be at least [FFT SIZE] big. magnitudeR float *
Magnitudes for each frequency bin, right side. Must be at least [FFT SIZE] big. phaseL float *
Phases for each frequency bin, left side. Must be at least [FFT SIZE] big. phaseR float *
Phases for each frequency bin, right side. Must be at least [FFT SIZE] big. valueOfPi float
0 Pi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. complexMode bool
false If true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). stereoPairIndex int
0 The index of the stereo pair to process. Type Description bool
True, if a conversion was possible (enough frames were available). timeDomainToFrequencyDomainMono
METHODConverts mono audio input (added by addInput()) to the frequency domain. Each frequency bin is (samplerate / [FFT SIZE] / 2) wide.ParametersReturnsName Type Description magnitude Number (pointer in Linear Memory)
Magnitudes for each frequency bin. Must be at least [FFT SIZE] big. phase Number (pointer in Linear Memory)
Phases for each frequency bin. Must be at least [FFT SIZE] big. valueOfPi Number
Pi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. complexMode Boolean
If true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). Type Description Boolean
True, if a conversion was possible (enough frames were available). timeDomainToFrequencyDomainMono
METHODConverts mono audio input (added by addInput()) to the frequency domain. Each frequency bin is (samplerate / [FFT SIZE] / 2) wide.ParametersReturnsName Type Default Description magnitude float *
Magnitudes for each frequency bin. Must be at least [FFT SIZE] big. phase float *
Phases for each frequency bin. Must be at least [FFT SIZE] big. valueOfPi float
0 Pi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. complexMode bool
false If true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). Type Description bool
True, if a conversion was possible (enough frames were available).