Debian Codegraph @

dwtools/SoundFrames.cpp

raw symbols SCIP
1 /* SoundFrames.cpp
2 *
3 * Copyright (C) 2025 David Weenink
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "SoundFrames.h"
20
21 Thing_implement (SoundFrames, Thing, 0);
22
23 void structSoundFrames :: initForToSampled (constSound input, double effectiveAnalysisWidth, double timeStep,
24 kSound_windowShape windowShape, bool subtractChannelMean)
25 {
26 our inputSound = input;
27 our physicalAnalysisWidth = getPhysicalAnalysisWidth (effectiveAnalysisWidth, windowShape);
28 if (timeStep == 0.0) {
29 // calculate output_dt
30 }
31 output -> dx = timeStep;
32 Sampled_shortTermAnalysis (inputSound, physicalAnalysisWidth, output -> dx, & output -> nx, & output -> x1);
33 initCommon (windowShape, subtractChannelMean);
34 }
35
36 void structSoundFrames :: initForIntoSampled (constSound input, mutableSampled output, double effectiveAnalysisWidth,
37 kSound_windowShape windowShape, bool subtractChannelMean)
38 {
39 Melder_assert (input -> xmin == output -> xmin && input -> xmax == output -> xmax);
40 our inputSound = input;
41 our output = output;
42 our physicalAnalysisWidth = getPhysicalAnalysisWidth (effectiveAnalysisWidth, windowShape);
43 initCommon (windowShape, subtractChannelMean);
44 }
45
46 void structSoundFrames :: initCommon (kSound_windowShape windowShape, bool subtractChannelMean)
47 {
48 our windowShape = windowShape;
49 our subtractChannelMean = subtractChannelMean;
50 soundFrameSize = getSoundFrameSize (physicalAnalysisWidth, inputSound -> dx);
51 windowFunction = raw_VEC (soundFrameSize); // TODO: move out of thread repetition
52 soundFrame = raw_VEC (soundFrameSize);
53 windowShape_into_VEC (windowShape, windowFunction.get());
54 frameAsSound = Sound_create (inputSound -> ny, 0.0, soundFrameSize * inputSound -> dx, soundFrameSize,
55 inputSound -> dx, 0.5 * inputSound -> dx);
56 Melder_assert (soundFrame.size == soundFrameSize);
57 }
58
59 Sound structSoundFrames :: getFrame (integer iframe) {
60 const double midTime = Sampled_indexToX (output, iframe);
61 integer startSample = Sampled_xToNearestIndex (inputSound, midTime - 0.5 * physicalAnalysisWidth); // approximation
62 const integer numberOfChannels = inputSound -> ny;
63 for (integer ichannel = 1; ichannel <= numberOfChannels; ichannel ++) {
64 VEC soundChannel = inputSound -> z.row (ichannel), frameChannel = frameAsSound -> z.row (ichannel);
65 integer currentSample = startSample;
66 for (integer i = 1; i <= soundFrame.size; i ++, currentSample ++)
67 frameChannel [i] = (( currentSample > 0 && currentSample <= inputSound -> nx) ? soundChannel [currentSample] : 0.0 );
68
69 if (subtractChannelMean)
70 centre_VEC_inout (frameChannel, nullptr);
71
72 frameChannel *= windowFunction.get();
73 }
74
75 return frameAsSound.get();
76 }
77
78 VEC structSoundFrames :: getMonoFrame (integer iframe) {
79 getFrame (iframe);
80 for (integer i = 1; i <= soundFrameSize; i ++)
81 soundFrame [i] = Sampled_getValueAtSample (frameAsSound.get(), i, 0, 0); // average channels
82 return soundFrame.get();
83 }
84
85 autoSoundFrames SoundFrames_createForIntoSampled (constSound input, mutableSampled output, double effectiveAnalysisWidth,
86 kSound_windowShape windowShape, bool subtractFrameMean)
87 {
88 try {
89 autoSoundFrames me = Thing_new (SoundFrames);
90 my initForIntoSampled (input, output, effectiveAnalysisWidth, windowShape, subtractFrameMean);
91 return me;
92 } catch (MelderError) {
93 Melder_throw (U"SoundFrames (with Sampled) could not be created.");
94 }
95 }
96
97 autoSoundFrames SoundFrames_create (constSound input, double effectiveAnalysisWidth,
98 double timeStep, kSound_windowShape windowShape,
99 bool subtractFrameChannelMean)
100 {
101 try {
102 autoSoundFrames me = Thing_new (SoundFrames);
103 my initForToSampled (input, effectiveAnalysisWidth, timeStep, windowShape, subtractFrameChannelMean);
104 return me;
105 } catch (MelderError) {
106 Melder_throw (U"SoundFrames could not be created.");
107 }
108 }
109
110 /* End of file SoundFrames.cpp */