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 NumberFFT 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.FrequencyDomainThe constructed instance. constructor
METHODCreates an instance of FrequencyDomain class.ParametersReturnsName Type Default Description fftLogSize unsigned int11 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 int4 [Maximum overlap]:1. Type Description Superpowered::FrequencyDomainThe 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 NumberThe number of input frames. NoneaddInput
METHODAdd some audio input.ParametersReturnsName Type Default Description input float *32-bit interleaved stereo input. numberOfFrames intThe number of input frames. Noneadvance
METHODAdvances the input buffer (removes the earliest frames).ParametersReturnsName Type Description numberOfFrames NumberFor 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 int0 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 NumberPi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. incrementFrames NumberFor 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 BooleanIf true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). stereoPairIndex NumberThe 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 float0 Pi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. incrementFrames intFor 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 boolfalse If true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). stereoPairIndex int0 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 NumberNumber of frames. getNumberOfInputFramesNeeded
METHODReturns with how many frames of input should be provided to produce some output.ParametersNoneReturnsType Description unsigned intNumber 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 NumberThe number of stereo audio channel pairs. Valid values: one (stereo) to four (8 channels). dontFree BooleanIf 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 intThe number of stereo audio channel pairs. Valid values: one (stereo) to four (8 channels). dontFree boolfalse 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 NumberPi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. complexMode BooleanIf true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). stereoPairIndex NumberThe index of the stereo pair to process. Type Description BooleanTrue, 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 float0 Pi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. complexMode boolfalse If true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). stereoPairIndex int0 The index of the stereo pair to process. Type Description boolTrue, 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 NumberPi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. complexMode BooleanIf true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). Type Description BooleanTrue, 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 float0 Pi can be translated to any value (Google: the tau manifesto). Keep it at 0 for M_PI. complexMode boolfalse If true, then it returns with complex numbers (magnitude: real, phase: imag). Performs polar transform otherwise (the output is magnitudes and phases). Type Description boolTrue, if a conversion was possible (enough frames were available).