4#ifndef MACH1SPATIALSDK_MACH1TRANSCODE_H
5#define MACH1SPATIALSDK_MACH1TRANSCODE_H
7#include "Mach1TranscodeCAPI.h"
11template <
typename PCM>
112 void setLFESub(std::vector<int> subChannelIndices,
int sampleRate);
152 void setCustomPointsSamplerCallback(
Mach1Point3D *(*callback)(
long long,
int &));
179 void processConversion(std::vector<std::vector<PCM> > &in, std::vector<std::vector<PCM> > &out,
int numSamples);
181#ifndef __EMSCRIPTEN__
194 std::vector<PCM> getAvgSamplesDiff();
196 PCM db2level(PCM db);
198 PCM level2db(PCM level);
203 std::vector<PCM *> input_ptrs;
204 std::vector<PCM *> output_ptrs;
206 std::vector<std::vector<PCM> > input_conversion_buffer;
207 std::vector<std::vector<PCM> > output_conversion_buffer;
209 int ic_channel_count{};
210 size_t ic_buffer_size{};
212 int oc_channel_count{};
213 size_t oc_buffer_size{};
219#ifndef MACH1SPATIALSDK_MACH1TRANSCODE_TPP
220#define MACH1SPATIALSDK_MACH1TRANSCODE_TPP
224template <
typename PCM>
226 M1obj = Mach1TranscodeCAPI_create();
229template <
typename PCM>
231 Mach1TranscodeCAPI_delete(M1obj);
234template <
typename PCM>
236 return Mach1TranscodeCAPI_getInputFormat(M1obj);
239template <
typename PCM>
241 return Mach1TranscodeCAPI_getOutputFormat(M1obj);
244template <
typename PCM>
246 return Mach1TranscodeCAPI_getInputNumChannels(M1obj);
249template <
typename PCM>
251 Mach1TranscodeCAPI_setInputFormat(M1obj, inFmt);
254template <
typename PCM>
256 return Mach1TranscodeCAPI_getOutputNumChannels(M1obj);
259template <
typename PCM>
261 Mach1TranscodeCAPI_setOutputFormat(M1obj, outFmt);
264template <
typename PCM>
266 return Mach1TranscodeCAPI_getFormatFromString(M1obj, (
char *)str.c_str());
269template <
typename PCM>
271 const char *name = Mach1TranscodeCAPI_getFormatName(M1obj, fmt);
272 return name ? std::string(name) : std::string();
275template <
typename PCM>
277 const char **formats = Mach1TranscodeCAPI_getAllFormatNames(M1obj);
278 std::vector<std::string> formatNames;
279 for (
size_t i = 0; formats[i] !=
nullptr; i++) {
280 formatNames.push_back(std::string(formats[i]));
285template <
typename PCM>
287 return Mach1TranscodeCAPI_getFormatsCount(M1obj);
290#ifndef __EMSCRIPTEN__
291template <
typename PCM>
293 return Mach1TranscodeCAPI_processNormalization(M1obj, bufs, numSamples);
296template <
typename PCM>
298 Mach1TranscodeCAPI_processMasterGain(M1obj, bufs, numSamples, masterGain);
302template <
typename PCM>
304 if (bufs.size() == 0)
307 float **b =
new float *[bufs.size()];
308 for (
int i = 0; i < bufs.size(); i++) {
309 b[i] = bufs[i].data();
311 float peak = Mach1TranscodeCAPI_processNormalization(M1obj, b, (
int)bufs[0].size());
317template <
typename PCM>
319 if (bufs.size() == 0)
322 float **b =
new float *[bufs.size()];
323 for (
int i = 0; i < bufs.size(); i++) {
324 b[i] = bufs[i].data();
326 Mach1TranscodeCAPI_processMasterGain(M1obj, b, (
int)bufs[0].size(), masterGain);
330template <
typename PCM>
332 return Mach1TranscodeCAPI_db2level(M1obj, db);
335template <
typename PCM>
337 return Mach1TranscodeCAPI_level2db(M1obj, level);
340template <
typename PCM>
342 Mach1TranscodeCAPI_setLFESub(M1obj, subChannelIndices.data(), (
int)subChannelIndices.size(), sampleRate);
345template <
typename PCM>
347 Mach1TranscodeCAPI_setSpatialDownmixer(M1obj, corrThreshold);
350template <
typename PCM>
352 return Mach1TranscodeCAPI_getSpatialDownmixerPossibility(M1obj);
355template <
typename PCM>
357 float *avg = Mach1TranscodeCAPI_getAvgSamplesDiff(M1obj);
358 return std::vector<float>(avg, avg + 4);
361template <
typename PCM>
363 Mach1TranscodeCAPI_setInputFormatCustomPointsJson(M1obj, (
char *)inJson.c_str());
366template <
typename PCM>
368 Mach1TranscodeCAPI_setInputFormatCustomPoints(M1obj, points.data(), (
int)points.size());
371template <
typename PCM>
373 Mach1TranscodeCAPI_setOutputFormatCustomPointsJson(M1obj, (
char *)strJson.c_str());
376template <
typename PCM>
378 Mach1TranscodeCAPI_setInputFormatCustomPoints(M1obj, points.data(), (
int)points.size());
381template <
typename PCM>
383 Mach1TranscodeCAPI_setCustomPointsSamplerCallback(M1obj, callback);
386template <
typename PCM>
388 return Mach1TranscodeCAPI_processConversionPath(M1obj);
391template <
typename PCM>
393 float *matrix =
new float[getInputNumChannels() * getOutputNumChannels()];
394 Mach1TranscodeCAPI_getMatrixConversion(M1obj, matrix);
396 std::vector<std::vector<float> > vec;
397 vec.resize(getOutputNumChannels());
398 for (
size_t i = 0; i < vec.size(); i++) {
399 vec[i].resize(getInputNumChannels());
400 for (
size_t j = 0; j < vec[i].size(); j++) {
401 vec[i][j] = matrix[i * getInputNumChannels() + j];
408#ifndef __EMSCRIPTEN__
409template <
typename PCM>
411 Mach1TranscodeCAPI_processConversion(M1obj, inBufs, outBufs, numSamples);
415template <
typename PCM>
417 if (in.size() == 0 || out.size() == 0)
421 if (ic_channel_count != in.size() || ic_buffer_size != numSamples) {
422 input_conversion_buffer.resize(in.size());
423 input_ptrs.resize(in.size());
425 for (
int i = 0; i < in.size(); i++) {
426 input_conversion_buffer[i].resize(numSamples);
429 ic_channel_count = in.size();
430 ic_buffer_size = numSamples;
433 for (
int i = 0; i < in.size(); i++) {
434 input_ptrs[i] = in[i].data();
438 if (oc_channel_count != out.size() || oc_buffer_size != numSamples) {
439 output_conversion_buffer.resize(out.size());
440 output_ptrs.resize(out.size());
442 for (
int i = 0; i < out.size(); i++) {
443 output_conversion_buffer[i].resize(numSamples);
446 oc_channel_count = out.size();
447 oc_buffer_size = numSamples;
450 for (
int i = 0; i < out.size(); i++) {
451 output_ptrs[i] = out[i].data();
454 Mach1TranscodeCAPI_processConversion(M1obj, input_ptrs.data(), output_ptrs.data(), numSamples);
457template <
typename PCM>
459 if (in.size() == 0 || out.size() == 0) {
464 if (ic_channel_count != in.size() || ic_buffer_size != numSamples) {
465 input_conversion_buffer.resize(in.size());
466 input_ptrs.resize(in.size());
468 for (
int i = 0; i < in.size(); i++) {
469 input_conversion_buffer[i].resize(numSamples);
472 ic_channel_count = in.size();
473 ic_buffer_size = numSamples;
476 for (
int i = 0; i < ic_channel_count; i++) {
477 memcpy(input_conversion_buffer[i].data(), in.data(),
sizeof(PCM) * ic_buffer_size);
478 input_ptrs[i] = input_conversion_buffer[i].data();
482 if (oc_channel_count != out.size() || oc_buffer_size != numSamples) {
483 output_conversion_buffer.resize(out.size());
484 output_ptrs.resize(out.size());
486 for (
int i = 0; i < out.size(); i++) {
487 output_conversion_buffer[i].resize(numSamples);
490 oc_channel_count = out.size();
491 oc_buffer_size = numSamples;
494 for (
int i = 0; i < oc_channel_count; i++) {
495 memcpy(output_conversion_buffer[i].data(), out.data(),
sizeof(PCM) * oc_buffer_size);
496 output_ptrs[i] = output_conversion_buffer[i].data();
499 Mach1TranscodeCAPI_processConversion(M1obj, input_ptrs.data(), output_ptrs.data(), numSamples);
501 for (
int i = 0; i < oc_channel_count; i++) {
502 memcpy(out.data(), output_conversion_buffer[i].data(),
sizeof(PCM) * oc_buffer_size);
506template <
typename PCM>
509 int *arr = Mach1TranscodeCAPI_getFormatConversionPath(M1obj, &count);
511 std::vector<int> vec(arr, arr + count);
Definition Mach1Transcode.h:12
void setInputFormatCustomPoints(std::vector< Mach1Point3D > points)
Sets the input format for transcoding from CustomPoints directly.
Definition Mach1Transcode.h:367
int getInputFormat()
Get the input format that this Mach1Transcode has been configured to process.
Definition Mach1Transcode.h:235
PCM processNormalization(std::vector< std::vector< PCM > > &bufs)
Get the largest PCM value in the given buffer, which would be used for normalization.
Definition Mach1Transcode.h:303
void setInputFormatCustomPointsJson(std::string inJson)
Sets the input format for transcoding from CustomPoints directly.
Definition Mach1Transcode.h:362
std::string getFormatName(int fmt)
Given an input/output format index, return its human-readable name.
Definition Mach1Transcode.h:270
void setOutputFormatCustomPointsJson(std::string outJson)
Sets the output format for transcoding from an external JSON source.
Definition Mach1Transcode.h:372
bool processConversionPath()
Use this function to control when to call for calculating the format transcoding calculations.
Definition Mach1Transcode.h:387
std::vector< std::vector< PCM > > getMatrixConversion()
Get the transcoding matrix of coefficients based on the set input and output formats.
Definition Mach1Transcode.h:392
void setOutputFormatCustomPoints(std::vector< Mach1Point3D > points)
Sets the output format for transcoding from CustomPoints directly.
Definition Mach1Transcode.h:377
void setOutputFormat(int outFmt)
Sets the output format for transcoding from this Mach1Transcode options.
Definition Mach1Transcode.h:260
void processMasterGain(std::vector< std::vector< PCM > > &bufs, PCM masterGain)
Apply an input gain to the output sound field.
Definition Mach1Transcode.h:318
int getOutputFormat()
Get the output format that this Mach1Transcode has been configured to process.
Definition Mach1Transcode.h:240
void setInputFormat(int inFmt)
Sets the input format for transcoding from this Mach1Transcode options.
Definition Mach1Transcode.h:250
bool getSpatialDownmixerPossibility()
Get whether the compared signals are less than the setSpatialDownmixer(corrThreshold).
Definition Mach1Transcode.h:351
std::vector< int > getFormatConversionPath()
Get the shortest found conversion path to get from input format X to output format Y,...
Definition Mach1Transcode.h:507
std::vector< std::string > getAllFormatNames()
Get a list of all the names of available formats in Mach1Transcode API.
Definition Mach1Transcode.h:276
void setSpatialDownmixer(float corrThreshold=0.1)
Sets the threshold float for getSpatialDownmixerPossibility calculation.
Definition Mach1Transcode.h:346
void setLFESub(std::vector< int > subChannelIndices, int sampleRate)
Apply a low pass filter (LPF) to each indicated channel index of the input format and sound field.
Definition Mach1Transcode.h:341
int getFormatFromString(std::string str)
the enum for indicated format's string name.
Definition Mach1Transcode.h:265
int getInputNumChannels()
Get the number of channels that this Mach1Transcode has been configured to process,...
Definition Mach1Transcode.h:245
void processConversion(std::vector< std::vector< PCM > > &in, std::vector< std::vector< PCM > > &out, int numSamples)
Process the conversion as set by previous functions.
Definition Mach1Transcode.h:416
int getOutputNumChannels()
Get the number of channels that this Mach1Transcode has been configured to convert to,...
Definition Mach1Transcode.h:255
int getFormatsCount()
Get the amount of available formats in Mach1Transcode API.
Definition Mach1Transcode.h:286
void processConversionRebuffer(std::vector< std::vector< PCM > > &in, std::vector< std::vector< PCM > > &out, int numSamples)
Process the conversion as set by previous functions.
Definition Mach1Transcode.h:458
Definition Mach1Point3D.h:17