dspx 1.3.6 → 1.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dspx",
3
- "version": "1.3.6",
3
+ "version": "1.3.7",
4
4
  "description": "High-performance DSP library with native C++ acceleration and Redis state persistence",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
Binary file
@@ -106,22 +106,14 @@ namespace dsp
106
106
  *
107
107
  * @param serializer The TOON serializer to write binary data to.
108
108
  */
109
- virtual void serializeToon(toon::Serializer &serializer) const
110
- {
111
- // Fallback: throw if not implemented for a specific stage
112
- throw std::runtime_error("TOON serialization not implemented for this stage type");
113
- }
109
+ virtual void serializeToon(toon::Serializer &serializer) const = 0;
114
110
 
115
111
  /**
116
112
  * @brief Deserializes internal state from TOON binary format.
117
113
  *
118
114
  * @param deserializer The TOON deserializer to read binary data from.
119
115
  */
120
- virtual void deserializeToon(toon::Deserializer &deserializer)
121
- {
122
- throw std::runtime_error("TOON deserialization not implemented for this stage type");
123
- }
124
-
116
+ virtual void deserializeToon(toon::Deserializer &deserializer) = 0;
125
117
  /**
126
118
  * @brief Resets the stage's internal state to initial values.
127
119
  */
@@ -185,6 +185,37 @@ namespace dsp
185
185
  // No state to reset
186
186
  }
187
187
 
188
+ void serializeToon(toon::Serializer &serializer) const override
189
+ {
190
+ serializer.writeInt32(m_numInputChannels);
191
+ serializer.writeInt32(static_cast<int>(m_mapping.size()));
192
+ for (int ch : m_mapping)
193
+ {
194
+ serializer.writeInt32(ch);
195
+ }
196
+ }
197
+
198
+ void deserializeToon(toon::Deserializer &deserializer) override
199
+ {
200
+ int numInputChannels = deserializer.readInt32();
201
+ int mappingSize = deserializer.readInt32();
202
+
203
+ if (numInputChannels != m_numInputChannels || mappingSize != static_cast<int>(m_mapping.size()))
204
+ {
205
+ throw std::runtime_error("ChannelMerge: Configuration mismatch during TOON deserialization");
206
+ }
207
+
208
+ // Verify mapping indices match
209
+ for (int i = 0; i < mappingSize; ++i)
210
+ {
211
+ int ch = deserializer.readInt32();
212
+ if (ch != m_mapping[i])
213
+ {
214
+ throw std::runtime_error("ChannelMerge: Mapping index mismatch during TOON deserialization");
215
+ }
216
+ }
217
+ }
218
+
188
219
  int getNumInputChannels() const { return m_numInputChannels; }
189
220
  const std::vector<int> &getMapping() const { return m_mapping; }
190
221
 
@@ -177,6 +177,37 @@ namespace dsp
177
177
  // No state to reset
178
178
  }
179
179
 
180
+ void serializeToon(toon::Serializer &serializer) const override
181
+ {
182
+ serializer.writeInt32(m_numInputChannels);
183
+ serializer.writeInt32(static_cast<int>(m_channels.size()));
184
+ for (int ch : m_channels)
185
+ {
186
+ serializer.writeInt32(ch);
187
+ }
188
+ }
189
+
190
+ void deserializeToon(toon::Deserializer &deserializer) override
191
+ {
192
+ int numInputChannels = deserializer.readInt32();
193
+ int numChannels = deserializer.readInt32();
194
+
195
+ if (numInputChannels != m_numInputChannels || numChannels != static_cast<int>(m_channels.size()))
196
+ {
197
+ throw std::runtime_error("ChannelSelect: Configuration mismatch during TOON deserialization");
198
+ }
199
+
200
+ // Verify channel indices match
201
+ for (int i = 0; i < numChannels; ++i)
202
+ {
203
+ int ch = deserializer.readInt32();
204
+ if (ch != m_channels[i])
205
+ {
206
+ throw std::runtime_error("ChannelSelect: Channel index mismatch during TOON deserialization");
207
+ }
208
+ }
209
+ }
210
+
180
211
  int getNumInputChannels() const { return m_numInputChannels; }
181
212
  const std::vector<int> &getChannels() const { return m_channels; }
182
213
 
@@ -147,6 +147,23 @@ namespace dsp
147
147
  // No state to reset
148
148
  }
149
149
 
150
+ void serializeToon(toon::Serializer &serializer) const override
151
+ {
152
+ serializer.writeInt32(m_numInputChannels);
153
+ serializer.writeInt32(m_numOutputChannels);
154
+ }
155
+
156
+ void deserializeToon(toon::Deserializer &deserializer) override
157
+ {
158
+ int numInputChannels = deserializer.readInt32();
159
+ int numOutputChannels = deserializer.readInt32();
160
+
161
+ if (numInputChannels != m_numInputChannels || numOutputChannels != m_numOutputChannels)
162
+ {
163
+ throw std::runtime_error("ChannelSelector: Channel count mismatch during TOON deserialization");
164
+ }
165
+ }
166
+
150
167
  int getNumInputChannels() const { return m_numInputChannels; }
151
168
  int getNumOutputChannels() const { return m_numOutputChannels; }
152
169
 
@@ -528,6 +528,26 @@ namespace dsp
528
528
  m_format = static_cast<OutputFormat>(state.Get("format").As<Napi::Number>().Int32Value());
529
529
  }
530
530
 
531
+ void FftStage::serializeToon(toon::Serializer &serializer) const
532
+ {
533
+ serializer.writeInt32(static_cast<int32_t>(m_fftSize));
534
+ serializer.writeInt32(static_cast<int>(m_type));
535
+ serializer.writeBool(m_forward);
536
+ serializer.writeInt32(static_cast<int>(m_format));
537
+ }
538
+
539
+ void FftStage::deserializeToon(toon::Deserializer &deserializer)
540
+ {
541
+ size_t fftSize = static_cast<size_t>(deserializer.readInt32());
542
+ {
543
+ throw std::runtime_error("FFT size mismatch during TOON deserialization");
544
+ }
545
+
546
+ m_type = static_cast<TransformType>(deserializer.readInt32());
547
+ m_forward = deserializer.readBool();
548
+ m_format = static_cast<OutputFormat>(deserializer.readInt32());
549
+ }
550
+
531
551
  FftStage::TransformType FftStage::parseTransformType(const std::string &typeStr)
532
552
  {
533
553
  if (typeStr == "fft")
@@ -77,6 +77,8 @@ namespace dsp
77
77
  // State serialization
78
78
  Napi::Object serializeState(Napi::Env env) const override;
79
79
  void deserializeState(const Napi::Object &state) override;
80
+ void serializeToon(toon::Serializer &serializer) const override;
81
+ void deserializeToon(toon::Deserializer &deserializer) override;
80
82
 
81
83
  // Helper to parse transform type from string
82
84
  static TransformType parseTransformType(const std::string &typeStr);
@@ -259,6 +259,50 @@ namespace dsp
259
259
  // No internal state to clear (matrices are fixed)
260
260
  }
261
261
 
262
+ void serializeToon(toon::Serializer &serializer) const override
263
+ {
264
+ serializer.writeInt32(m_numChannels);
265
+
266
+ // Serialize steering weights
267
+ for (int i = 0; i < m_numChannels; ++i)
268
+ {
269
+ serializer.writeFloat(m_steering_weights(i));
270
+ }
271
+
272
+ // Serialize blocking matrix (column-major)
273
+ const float *matrixData = m_blocking_matrix.data();
274
+ size_t totalElements = m_numChannels * (m_numChannels - 1);
275
+ for (size_t i = 0; i < totalElements; ++i)
276
+ {
277
+ serializer.writeFloat(matrixData[i]);
278
+ }
279
+ }
280
+
281
+ void deserializeToon(toon::Deserializer &deserializer) override
282
+ {
283
+ int numChannels = deserializer.readInt32();
284
+
285
+ if (numChannels != m_numChannels)
286
+ {
287
+ throw std::runtime_error("GscPreprocessor: Channel count mismatch during TOON deserialization");
288
+ }
289
+
290
+ // Restore steering weights
291
+ for (int i = 0; i < m_numChannels; ++i)
292
+ {
293
+ m_steering_weights(i) = deserializer.readFloat();
294
+ }
295
+
296
+ // Restore blocking matrix
297
+ std::vector<float> matrixData(m_numChannels * (m_numChannels - 1));
298
+ for (size_t i = 0; i < matrixData.size(); ++i)
299
+ {
300
+ matrixData[i] = deserializer.readFloat();
301
+ }
302
+ m_blocking_matrix = Eigen::Map<const Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>(
303
+ matrixData.data(), m_numChannels, m_numChannels - 1);
304
+ }
305
+
262
306
  /**
263
307
  * @brief Get number of input channels.
264
308
  */
@@ -238,6 +238,54 @@ namespace dsp
238
238
  // No internal state to clear (matrix and mean are fixed)
239
239
  }
240
240
 
241
+ void serializeToon(toon::Serializer &serializer) const override
242
+ {
243
+ serializer.writeString(m_transformType);
244
+ serializer.writeInt32(m_numChannels);
245
+ serializer.writeInt32(m_numComponents);
246
+
247
+ // Serialize mean vector
248
+ for (int i = 0; i < m_numChannels; ++i)
249
+ {
250
+ serializer.writeFloat(m_mean(i));
251
+ }
252
+
253
+ // Serialize transformation matrix (column-major)
254
+ const float *matrixData = m_matrix.data();
255
+ size_t totalElements = m_numChannels * m_numComponents;
256
+ for (size_t i = 0; i < totalElements; ++i)
257
+ {
258
+ serializer.writeFloat(matrixData[i]);
259
+ }
260
+ }
261
+
262
+ void deserializeToon(toon::Deserializer &deserializer) override
263
+ {
264
+ std::string transformType = deserializer.readString();
265
+ int numChannels = deserializer.readInt32();
266
+ int numComponents = deserializer.readInt32();
267
+
268
+ if (numChannels != m_numChannels || numComponents != m_numComponents)
269
+ {
270
+ throw std::runtime_error("MatrixTransform: Dimension mismatch during TOON deserialization");
271
+ }
272
+
273
+ // Restore mean vector
274
+ for (int i = 0; i < m_numChannels; ++i)
275
+ {
276
+ m_mean(i) = deserializer.readFloat();
277
+ }
278
+
279
+ // Restore transformation matrix
280
+ std::vector<float> matrixData(m_numChannels * m_numComponents);
281
+ for (size_t i = 0; i < matrixData.size(); ++i)
282
+ {
283
+ matrixData[i] = deserializer.readFloat();
284
+ }
285
+ m_matrix = Eigen::Map<const Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>(
286
+ matrixData.data(), m_numChannels, m_numComponents);
287
+ }
288
+
241
289
  /**
242
290
  * @brief Get number of input channels.
243
291
  */
@@ -202,6 +202,39 @@ namespace dsp::adapters
202
202
  // Stateless - no reset needed
203
203
  }
204
204
 
205
+ void serializeToon(toon::Serializer &serializer) const override
206
+ {
207
+ serializer.writeInt32(static_cast<int32_t>(m_numBins));
208
+ serializer.writeInt32(static_cast<int32_t>(m_numMelBands));
209
+
210
+ // Serialize filterbank matrix (row-major)
211
+ for (size_t i = 0; i < m_numMelBands; ++i)
212
+ {
213
+ for (size_t j = 0; j < m_numBins; ++j)
214
+ {
215
+ serializer.writeFloat(m_filterbank(i, j));
216
+ }
217
+ }
218
+ }
219
+
220
+ void deserializeToon(toon::Deserializer &deserializer) override
221
+ {
222
+ size_t numBins = static_cast<size_t>(deserializer.readInt32());
223
+ size_t numMelBands = static_cast<size_t>(deserializer.readInt32());
224
+ {
225
+ throw std::runtime_error("MelSpectrogram: Dimension mismatch during TOON deserialization");
226
+ }
227
+
228
+ // Restore filterbank matrix
229
+ for (size_t i = 0; i < m_numMelBands; ++i)
230
+ {
231
+ for (size_t j = 0; j < m_numBins; ++j)
232
+ {
233
+ m_filterbank(i, j) = deserializer.readFloat();
234
+ }
235
+ }
236
+ }
237
+
205
238
  private:
206
239
  size_t m_numBins; // Number of input frequency bins
207
240
  size_t m_numMelBands; // Number of output Mel bands
@@ -202,6 +202,28 @@ namespace dsp::adapters
202
202
  // Stateless - no reset needed
203
203
  }
204
204
 
205
+ void serializeToon(toon::Serializer &serializer) const override
206
+ {
207
+ serializer.writeInt32(static_cast<int32_t>(m_numMelBands));
208
+ serializer.writeInt32(static_cast<int32_t>(m_numCoefficients));
209
+ serializer.writeBool(m_useLogEnergy);
210
+ serializer.writeFloat(m_lifterCoefficient);
211
+ }
212
+
213
+ void deserializeToon(toon::Deserializer &deserializer) override
214
+ {
215
+ size_t numMelBands = static_cast<size_t>(deserializer.readInt32());
216
+ size_t numCoefficients = static_cast<size_t>(deserializer.readInt32());
217
+
218
+ if (numMelBands != m_numMelBands || numCoefficients != m_numCoefficients)
219
+ {
220
+ throw std::runtime_error("MFCC: Dimension mismatch during TOON deserialization");
221
+ }
222
+
223
+ deserializer.readBool(); // useLogEnergy
224
+ deserializer.readFloat(); // lifterCoefficient
225
+ }
226
+
205
227
  private:
206
228
  size_t m_numMelBands; // Number of input Mel bands
207
229
  size_t m_numCoefficients; // Number of MFCC coefficients to output
@@ -94,6 +94,22 @@ namespace dsp::adapters
94
94
  // Stateless - nothing to reset
95
95
  }
96
96
 
97
+ void serializeToon(toon::Serializer &serializer) const override
98
+ {
99
+ serializer.writeString(m_wavelet_name);
100
+ serializer.writeInt32(static_cast<int32_t>(m_filter_length));
101
+ }
102
+
103
+ void deserializeToon(toon::Deserializer &deserializer) override
104
+ {
105
+ std::string loaded_name = deserializer.readString();
106
+ if (loaded_name != m_wavelet_name)
107
+ {
108
+ throw std::runtime_error("Wavelet name mismatch during TOON deserialization");
109
+ }
110
+ deserializer.readInt32(); // filter_length (just validate it matches)
111
+ }
112
+
97
113
  private:
98
114
  /**
99
115
  * Apply symmetric padding (reflect about edge)