| 1 | #cxx . . $ `<file>/dwtools/SoundFrames.h`/ifndef _SoundFrames_h_ |
| 2 | #define _SoundFrames_h_cxx . . $ `dwtools/SoundFrames.h:2:9`! |
| 3 | /* SoundFrames.h |
| 4 | * |
| 5 | * Copyright (C) 2024-2025 David Weenink |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | //#include "melder.h" |
| 22 | #include "NUMFourier.h"cxx . . $ `<file>/dwsys/NUMFourier.h`/ |
| 23 | #include "Sound_extensions.h"cxx . . $ `<file>/dwtools/Sound_extensions.h`/ |
| 24 | #include "Spectrum.h"cxx . . $ `<file>/fon/Spectrum.h`/ |
| 25 | |
| 26 | inline integercxx . . $ integer# getSoundFrameSize_oddcxx . . $ getSoundFrameSize_odd(e4170d96af58b73c). (double approximatePhysicalAnalysisWidth, double samplingPeriod) { |
| 27 | const double halfFrameDuration = 0.5 * approximatePhysicalAnalysisWidth; |
| 28 | const integercxx . . $ integer# halfFrameSamples = Melder_ifloorcxx . . $ Melder_iroundDown(a6e4a4c8b00c35a3). (halfFrameDuration / samplingPeriod); |
| 29 | return 2 * halfFrameSamples + 1; |
| 30 | } |
| 31 | |
| 32 | inline integercxx . . $ integer# getSoundFrameSizecxx . . $ getSoundFrameSize(e4170d96af58b73c). (double physicalAnalysisWidth, double samplingPeriod) { |
| 33 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (physicalAnalysisWidth > 0.0); |
| 34 | Melder_assertcxx . . $ abort(15d318a79ddce9e0). (samplingPeriod > 0.0); |
| 35 | return Melder_iroundcxx . . $ Melder_iround_tieUp(a6e4a4c8b00c35a3). (physicalAnalysisWidth / samplingPeriod); |
| 36 | } |
| 37 | |
| 38 | inline double getPhysicalAnalysisWidthcxx . . $ getPhysicalAnalysisWidth(746d57f737047cdc). (double effectiveAnalysisWidth, kSound_windowShapecxx . . $ kSound_windowShape# windowShape) { |
| 39 | const double physicalAnalysisWidth = ( |
| 40 | windowShape == kSound_windowShapecxx . . $ kSound_windowShape#::RECTANGULARcxx . . $ kSound_windowShape#RECTANGULAR. || |
| 41 | windowShape == kSound_windowShapecxx . . $ kSound_windowShape#::TRIANGULARcxx . . $ kSound_windowShape#TRIANGULAR. || windowShape == kSound_windowShapecxx . . $ kSound_windowShape#::HAMMINGcxx . . $ kSound_windowShape#HAMMING. || |
| 42 | windowShape == kSound_windowShapecxx . . $ kSound_windowShape#::HANNINGcxx . . $ kSound_windowShape#HANNING. ? effectiveAnalysisWidth : 2.0 * effectiveAnalysisWidth |
| 43 | ); |
| 44 | return physicalAnalysisWidth; |
| 45 | } |
| 46 | |
| 47 | Thing_definecxx . . $ SoundFrames# (SoundFrames, Thing) { |
| 48 | |
| 49 | constSoundcxx . . $ constSound# inputSoundcxx . . $ structSoundFrames#inputSound.; |
| 50 | mutableSampledcxx . . $ mutableSampled# outputcxx . . $ structSoundFrames#output.; |
| 51 | double physicalAnalysisWidthcxx . . $ structSoundFrames#physicalAnalysisWidth.; // depends on the effectiveAnalysiswidth and the window window shape |
| 52 | integercxx . . $ integer# soundFrameSizecxx . . $ structSoundFrames#soundFrameSize.; // determined by the physicalAnalysisWidth and the samplingFrequency of the Sound |
| 53 | autoSoundcxx . . $ autoSound# frameAsSoundcxx . . $ structSoundFrames#frameAsSound.; // the (possibly multichannel) frame as a Sound |
| 54 | autoVECcxx . . $ autoVEC# windowFunctioncxx . . $ structSoundFrames#windowFunction.; // the actual window used of size soundFrameSize |
| 55 | autoVECcxx . . $ autoVEC# soundFramecxx . . $ structSoundFrames#soundFrame.; // convenience: average of the channels |
| 56 | kSound_windowShapecxx . . $ kSound_windowShape# windowShapecxx . . $ structSoundFrames#windowShape.; // Type: Rectangular, triangular, hamming, etc.. |
| 57 | bool subtractChannelMeancxx . . $ structSoundFrames#subtractChannelMean. = true; // if true, the frame mean of each channel will be subtracted before windowing |
| 58 | |
| 59 | private: |
| 60 | |
| 61 | void initCommoncxx . . $ structSoundFrames#initCommon(e51dc816e62fffe6). (kSound_windowShapecxx . . $ kSound_windowShape# windowShape, bool subtractFrameMean); |
| 62 | |
| 63 | public: |
| 64 | |
| 65 | /* |
| 66 | Calculate the output sampling from the input parameters, |
| 67 | this is the default case |
| 68 | */ |
| 69 | void initForToSampledcxx . . $ structSoundFrames#initForToSampled(b5ea0db16098c158). (constSoundcxx . . $ constSound# input, double effectiveAnalysisWidth, double timeStep, |
| 70 | kSound_windowShapecxx . . $ kSound_windowShape# windowShape, bool subtractChannelMean); |
| 71 | |
| 72 | /* |
| 73 | In special cases we need the sampling (x1, dx, nx) of the output Sampled: |
| 74 | like for the FormantPath. |
| 75 | */ |
| 76 | void initForIntoSampledcxx . . $ structSoundFrames#initForIntoSampled(ecb22456c6d7c3c). (constSoundcxx . . $ constSound# input, mutableSampledcxx . . $ mutableSampled# output, double effectiveAnalysisWidth, |
| 77 | kSound_windowShapecxx . . $ kSound_windowShape# windowShape, bool subtractChannelMean); |
| 78 | |
| 79 | Soundcxx . . $ Sound# getFramecxx . . $ structSoundFrames#getFrame(4fb1c00207b05668). (integercxx . . $ integer# iframe); |
| 80 | |
| 81 | VECcxx . . $ VEC# getMonoFramecxx . . $ structSoundFrames#getMonoFrame(516a12db1224da40). (integercxx . . $ integer# iframe); |
| 82 | }; |
| 83 | |
| 84 | autoSoundFramescxx . . $ autoSoundFrames# SoundFrames_createForIntoSampledcxx . . $ SoundFrames_createForIntoSampled(bc9244df65bbc83e). (constSoundcxx . . $ constSound# input, mutableSampledcxx . . $ mutableSampled# output, double effectiveAnalysisWidth, |
| 85 | kSound_windowShapecxx . . $ kSound_windowShape# windowShape, bool subtractChannelMean); |
| 86 | |
| 87 | autoSoundFramescxx . . $ autoSoundFrames# SoundFrames_createForToSampledcxx . . $ SoundFrames_createForToSampled(5e7f91e561736224). (constSoundcxx . . $ constSound# input, double effectiveAnalysisWidth, double timeStep, |
| 88 | kSound_windowShapecxx . . $ kSound_windowShape# windowShape, bool subtractChannelMean); |
| 89 | |
| 90 | #if 0 |
| 91 | Thing_define (SoundFrameIntoSpectrum, SoundFrames) { |
| 92 | autoSpectrum spectrum; |
| 93 | integer fftInterpolationFactor = 1; // 0 = DFT, 1 = FFT, 2, 4, 8 FFT with extra zero's |
| 94 | integer numberOfFourierSamples; |
| 95 | autoVEC fourierSamples; // size = numberOfFourierSamples |
| 96 | autoVEC fourierSamples_channel; |
| 97 | autoVEC power_channelAveraged; |
| 98 | autoNUMFourierTable fourierTable; // of dimension numberOfFourierSamples; |
| 99 | |
| 100 | void init (constSound input, double effectiveAnalysisWidth, double timeStep, |
| 101 | kSound_windowShape windowShape, bool subtractChannelMean) |
| 102 | { |
| 103 | SoundFrameIntoSpectrum_Parent :: init (input, effectiveAnalysisWidth, timeStep, windowShape, subtractChannelMean); |
| 104 | init_spectralPart (); |
| 105 | } |
| 106 | |
| 107 | Spectrum getSpectrum () { |
| 108 | |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | |
| 113 | void init_spectralPart () { |
| 114 | our numberOfFourierSamples = our frameAsSound -> nx; |
| 115 | if (our fftInterpolationFactor > 0) { |
| 116 | our numberOfFourierSamples = Melder_iroundUpToPowerOfTwo (numberOfFourierSamples); |
| 117 | for (integer imultiply = fftInterpolationFactor; imultiply > 1; imultiply --) |
| 118 | our numberOfFourierSamples *= 2; |
| 119 | } |
| 120 | our fourierSamples = raw_VEC (numberOfFourierSamples); |
| 121 | our fourierSamples_channel = raw_VEC (numberOfFourierSamples); |
| 122 | our power_channelAveraged = raw_VEC (numberOfFourierSamples); |
| 123 | const integer numberOfFrequencies = numberOfFourierSamples / 2 + 1; |
| 124 | our fourierTable = NUMFourierTable_create (our numberOfFourierSamples); |
| 125 | our spectrum = Spectrum_create (0.5 / our frameAsSound -> dx, numberOfFrequencies); |
| 126 | our spectrum -> dx = 1.0 / (our frameAsSound -> dx * our numberOfFourierSamples); |
| 127 | } |
| 128 | |
| 129 | void soundFrameToPower_channelAveraged () { |
| 130 | const integer numberOfChannels = our frameAsSound -> ny; |
| 131 | power_channelAveraged.get() <<= 0.0; |
| 132 | for (integer ichannel = 1; ichannel <= numberOfChannels; ichannel ++) { |
| 133 | fourierSamples.part (1, soundFrameSize) <<= frameAsSound -> z.row (ichannel); |
| 134 | fourierSamples.part (soundFrameSize + 1, numberOfFourierSamples) <<= 0.0; |
| 135 | NUMfft_forward (fourierTable.get(), fourierSamples.get()); |
| 136 | for (integer i = 1; i <= numberOfFourierSamples; i ++) |
| 137 | power_channelAveraged [i] += fourierSamples [i] * fourierSamples [i]; |
| 138 | } |
| 139 | if (numberOfChannels > 1) |
| 140 | power_channelAveraged.get() /= numberOfChannels; |
| 141 | } |
| 142 | |
| 143 | }; |
| 144 | #endif /* 0 */ |
| 145 | |
| 146 | #endif /* _SoundFrames_h_ */ |