4#ifndef MACH1SPATIALSDK_MACH1ENCODE_H
5#define MACH1SPATIALSDK_MACH1ENCODE_H
7#include <Mach1Point3D.h>
12#include "Mach1EncodeCAPI.h"
14#ifdef M1ENCODE_INLINE_DECODE
15# include "Mach1DecodeCAPI.h"
18template <
typename PCM>
50 std::vector<std::vector<PCM> >
getGains();
201 void setOutputGain(
float outputGainMultipler,
bool isDecibel);
268 inline void encodeBuffer(std::vector<std::vector<PCM> > &in, std::vector<std::vector<PCM> > &out,
int size);
276 inline void encodeBufferRebuffer(std::vector<std::vector<PCM> > &in, std::vector<std::vector<PCM> > &out,
int size);
290 inline void encodeBufferInPlace(std::vector<std::vector<PCM> > &buffer,
size_t buffer_size);
292#ifdef M1ENCODE_INLINE_DECODE
300 std::vector<float> getResultingCoeffsDecoded(Mach1DecodeMode decodeMode, std::vector<float> &decodeResult);
306 std::vector<std::vector<PCM> > last_gains;
307 std::vector<std::vector<PCM> > intermediary_buffer;
309 int ib_channel_count{};
310 size_t ib_buffer_size{};
316#ifndef MACH1SPATIALSDK_MACH1ENCODE_TPP
317#define MACH1SPATIALSDK_MACH1ENCODE_TPP
319template <
typename PCM>
321 M1obj = Mach1EncodeCAPI_create();
324template <
typename PCM>
326 Mach1EncodeCAPI_delete(M1obj);
329template <
typename PCM>
331 M1obj = Mach1EncodeCAPI_create();
332 Mach1EncodeCAPI_copy(other.M1obj, M1obj);
335template <
typename PCM>
337 if (
this != &other) {
339 if (M1obj !=
nullptr) {
340 Mach1EncodeCAPI_delete(M1obj);
345 M1obj = Mach1EncodeCAPI_create();
348 Mach1EncodeCAPI_copy(other.M1obj, M1obj);
353template <
typename PCM>
356 other.M1obj =
nullptr;
359template <
typename PCM>
361 if (
this != &other) {
363 if (M1obj !=
nullptr) {
364 Mach1EncodeCAPI_delete(M1obj);
369 other.M1obj =
nullptr;
374template <
typename PCM>
376 std::vector<Mach1Point3D> vec(Mach1EncodeCAPI_getPointsCount(M1obj));
378 auto *arr = (
Mach1Point3D *)Mach1EncodeCAPI_getPoints(M1obj);
379 for (
int i = 0; i < vec.size(); i++)
385template <
typename PCM>
387 std::vector<std::vector<float> > vec(Mach1EncodeCAPI_getPointsCount(M1obj));
389 auto **arr = (
float **)Mach1EncodeCAPI_getGains(M1obj);
391 for (
int i = 0; i < vec.size(); i++) {
392 vec[i].resize(Mach1EncodeCAPI_getOutputChannelsCount(M1obj));
394 for (
int j = 0; j < vec[i].size(); j++) {
395 vec[i][j] = arr[i][j];
402template <
typename PCM>
404 std::vector<std::string> vec(Mach1EncodeCAPI_getPointsCount(M1obj));
406 auto **arr = (
char **)Mach1EncodeCAPI_getPointsNames(M1obj);
408 for (
int i = 0; i < vec.size(); i++) {
415template <
typename PCM>
417 std::vector<float> vec(Mach1EncodeCAPI_getOutputChannelsCount(M1obj));
419 auto *arr = (
float *)Mach1EncodeCAPI_getGainsForInputChannelNamed(M1obj, (
char *)pointName.c_str());
421 for (
int i = 0; i < vec.size(); i++) {
428template <
typename PCM>
430 Mach1EncodeCAPI_generatePointResults(M1obj);
433template <
typename PCM>
435 return Mach1EncodeCAPI_getPointsCount(M1obj);
438template <
typename PCM>
440 return Mach1EncodeCAPI_getInputMode(M1obj);
443template <
typename PCM>
445 return Mach1EncodeCAPI_getOutputMode(M1obj);
448template <
typename PCM>
450 return Mach1EncodeCAPI_getPannerMode(M1obj);
453template <
typename PCM>
455 return Mach1EncodeCAPI_getAutoOrbit(M1obj);
458template <
typename PCM>
460 return Mach1EncodeCAPI_getInputChannelsCount(M1obj);
463template <
typename PCM>
465 return Mach1EncodeCAPI_getOutputChannelsCount(M1obj);
468template <
typename PCM>
470 Mach1EncodeCAPI_setInputMode(M1obj, inputMode);
473template <
typename PCM>
475 Mach1EncodeCAPI_setOutputMode(M1obj, outputMode);
478template <
typename PCM>
480 Mach1EncodeCAPI_setAzimuth(M1obj, azimuthFromMinus1To1);
483template <
typename PCM>
485 Mach1EncodeCAPI_setAzimuthDegrees(M1obj, azimuthDegrees);
488template <
typename PCM>
490 Mach1EncodeCAPI_setAzimuthRadians(M1obj, azimuthRadians);
493template <
typename PCM>
495 Mach1EncodeCAPI_setDiverge(M1obj, divergeFromMinus1To1);
498template <
typename PCM>
500 Mach1EncodeCAPI_setElevation(M1obj, elevationFromMinus1to1);
503template <
typename PCM>
505 Mach1EncodeCAPI_setElevationDegrees(M1obj, elevationFromMinus90to90);
508template <
typename PCM>
510 Mach1EncodeCAPI_setElevationRadians(M1obj, elevationFromMinusHalfPItoHalfPI);
513template <
typename PCM>
515 Mach1EncodeCAPI_setPannerMode(M1obj, pannerMode);
518template <
typename PCM>
520 Mach1EncodeCAPI_setFrontSurroundPerspective(M1obj, frontSurroundPerspective);
523template <
typename PCM>
525 Mach1EncodeCAPI_getOutputGain(M1obj, isDecibel);
528template <
typename PCM>
530 Mach1EncodeCAPI_setOutputGain(M1obj, outputGainMultipler, isDecibel);
533template <
typename PCM>
535 return Mach1EncodeCAPI_getGainCompensation(M1obj, isDecibel);
538template <
typename PCM>
540 return Mach1EncodeCAPI_getGainCompensationActive(M1obj);
543template <
typename PCM>
545 Mach1EncodeCAPI_setGainCompensationActive(M1obj, active);
548template <
typename PCM>
550 Mach1EncodeCAPI_setOrbitRotation(M1obj, orbitRotationFromMinusOnetoOne);
553template <
typename PCM>
555 Mach1EncodeCAPI_setOrbitRotationDegrees(M1obj, orbitRotationDegrees);
558template <
typename PCM>
560 Mach1EncodeCAPI_setOrbitRotationRadians(M1obj, orbitRotationRadians);
563template <
typename PCM>
565 Mach1EncodeCAPI_setStereoSpread(M1obj, sSpreadFrom0to1);
568template <
typename PCM>
570 Mach1EncodeCAPI_setAutoOrbit(M1obj, autoOrbit);
573#ifdef M1ENCODE_INLINE_DECODE
574template <
typename PCM>
576 std::vector<float> vec(Mach1EncodeCAPI_getPointsCount(M1obj) * 2);
578 float *arr = (
float *)Mach1EncodeCAPI_getResultingCoeffsDecoded(M1obj, decodeMode, decodeResult.data());
580 for (
int i = 0; i < vec.size(); i++) {
588template <
typename PCM>
591 auto encode_gains = getGains();
593 if (last_gains.size() != encode_gains.size()) {
594 last_gains = encode_gains;
597 float size_reciprocal = 1.0f / (float)size;
600 for (
int point_idx = 0; point_idx < getPointsCount(); point_idx++) {
601 for (
int output_idx = 0; output_idx < getOutputChannelsCount(); output_idx++) {
602 auto encode_gain = encode_gains[point_idx][output_idx];
603 auto last_encode_gain = last_gains[point_idx][output_idx];
605 for (
int sample_idx = 0; sample_idx < size; sample_idx++) {
606 auto prc = (float)sample_idx * size_reciprocal;
607 auto gain = encode_gain * (1.0f - prc) + last_encode_gain * prc;
609 auto val = in[output_idx][sample_idx];
610 out[output_idx][sample_idx] = gain * val;
616template <
typename PCM>
618 encodeBuffer(buffer, buffer, buffer_size);
621template <
typename PCM>
625 auto encode_gains = getGains();
626 auto output_channel_count = getOutputChannelsCount();
628 if (last_gains.size() != encode_gains.size()) {
629 last_gains = encode_gains;
633 if (ib_channel_count != output_channel_count || ib_buffer_size != size) {
634 intermediary_buffer.resize(output_channel_count);
636 for (
int i = 0; i < output_channel_count; i++) {
637 intermediary_buffer[i].resize(size);
640 ib_channel_count = output_channel_count;
641 ib_buffer_size = size;
643 for (
int i = 0; i < ib_channel_count; i++) {
644 memset(intermediary_buffer[i].data(), 0,
sizeof(
float) * ib_buffer_size);
649 for (
int i = 0; i < ib_channel_count; i++) {
650 memset(intermediary_buffer[i].data(), 0,
sizeof(
float) * ib_buffer_size);
653 float size_reciprocal = 1.0f / (float)size;
656 for (
int point_idx = 0; point_idx < getPointsCount(); point_idx++) {
657 for (
int output_idx = 0; output_idx < output_channel_count; output_idx++) {
658 auto encode_gain = encode_gains[point_idx][output_idx];
659 auto last_encode_gain = last_gains[point_idx][output_idx];
661 for (
int sample_idx = 0; sample_idx < size; sample_idx++) {
662 auto prc = (float)sample_idx * size_reciprocal;
663 auto gain = encode_gain * (1.0f - prc) + last_encode_gain * prc;
664 auto val = in[output_idx][sample_idx];
665 intermediary_buffer[output_idx][sample_idx] += gain * val;
671 for (
int output_idx = 0; output_idx < output_channel_count; output_idx++) {
672 memcpy(out[output_idx].data(), intermediary_buffer[output_idx].data(),
sizeof(
float) * size);
675 last_gains = encode_gains;
678template <
typename PCM>
680 encodeBufferRebuffer(buffer, buffer, buffer_size);
Definition Mach1Encode.h:19
std::vector< float > getGainsForInputChannelNamed(const std::string &pointName)
Given the name of an input channel/point, return array of gain coefficients.
Definition Mach1Encode.h:416
Mach1EncodeInputMode getInputMode()
Get the format in which this Mach1Encode expects to receive audio for encoding.
Definition Mach1Encode.h:439
void setOutputMode(Mach1EncodeOutputMode outputMode)
Set the format in which this Mach1Encode will output encoded audio.
Definition Mach1Encode.h:474
std::vector< Mach1Point3D > getPoints()
Returns the control center reference point's normalized coordinate location (XYZ) within the vector p...
Definition Mach1Encode.h:375
bool getAutoOrbit()
Get whether auto-orbit mode is enabled. If true, then orbit rotation is extrapolated from the current...
Definition Mach1Encode.h:454
void encodeBufferRebuffer(std::vector< std::vector< PCM > > &in, std::vector< std::vector< PCM > > &out, int size)
Definition Mach1Encode.h:623
bool getGainCompensationActive()
Gets whether gain compensation is active.
Definition Mach1Encode.h:539
void setOrbitRotationDegrees(float orbitRotationDegrees)
Sets the two stereo points around the axis of the center point between them.
Definition Mach1Encode.h:554
void setPannerMode(enum Mach1EncodePannerMode pannerMode)
Sets the style and mode of panner input calculation.
Definition Mach1Encode.h:514
std::vector< std::vector< PCM > > getGains()
Returns an array per input channel, resulting in an array of array results each internal array (per i...
Definition Mach1Encode.h:386
void setElevationDegrees(float elevationFromMinus90to90)
Sets the point(s) up/down the vector space.
Definition Mach1Encode.h:504
std::vector< std::string > getPointsNames()
Return the string name for each input channel/point.
Definition Mach1Encode.h:403
void setAzimuthRadians(float azimuthRadians)
Sets the point(s) azimuth rotation of the vector space.
Definition Mach1Encode.h:489
void setAzimuth(float azimuthFromMinus1To1)
Sets the point(s) azimuth rotation of the vector space.
Definition Mach1Encode.h:479
int getOutputChannelsCount()
Get the number of output channels that this Mach1Encode expects to receive for encoding,...
Definition Mach1Encode.h:464
void setElevation(float elevationFromMinus1to1)
Sets the point(s) up/down the vector space.
Definition Mach1Encode.h:499
void setOrbitRotation(float orbitRotationFromMinusOnetoOne)
Sets the two stereo points around the axis of the center point between them.
Definition Mach1Encode.h:549
void generatePointResults()
Recalculate and update the encoder with the currently set spatial configuration.
Definition Mach1Encode.h:429
Mach1EncodePannerMode getPannerMode()
Get the calculation strategy by which the points in this Mach1Encode are calculated.
Definition Mach1Encode.h:449
void setAutoOrbit(bool autoOrbit)
Sets whether encoding behavior acts evenly with distribution across all azimuth/rotation angles and a...
Definition Mach1Encode.h:569
void encodeBufferInPlace(std::vector< std::vector< PCM > > &buffer, size_t buffer_size)
Definition Mach1Encode.h:617
void setDiverge(float divergeFromMinus1To1)
Sets the point(s) to/from center origin of the vector space.
Definition Mach1Encode.h:494
float getOutputGain(bool isDecibel)
Definition Mach1Encode.h:524
void setOutputGain(float outputGainMultipler, bool isDecibel)
Definition Mach1Encode.h:529
void setOrbitRotationRadians(float orbitRotationRadians)
Sets the two stereo points around the axis of the center point between them.
Definition Mach1Encode.h:559
void setElevationRadians(float elevationFromMinusHalfPItoHalfPI)
Sets the point(s) up/down the vector space.
Definition Mach1Encode.h:509
void setFrontSurroundPerspective(bool frontSurroundPerspective)
Definition Mach1Encode.h:519
void encodeBuffer(std::vector< std::vector< PCM > > &in, std::vector< std::vector< PCM > > &out, int size)
Definition Mach1Encode.h:590
void setStereoSpread(float sSpreadFrom0to1)
Sets the space between the two stereo points.
Definition Mach1Encode.h:564
float getGainCompensation(bool isDecibel)
Gets the current gain compensation value.
Definition Mach1Encode.h:534
void encodeBufferInPlaceRebuffer(std::vector< std::vector< PCM > > &buffer, size_t buffer_size)
Definition Mach1Encode.h:679
int getPointsCount()
Get the number of point results that have been generated after a call to generatePointResults.
Definition Mach1Encode.h:434
void setGainCompensationActive(bool active)
Sets whether gain compensation is active.
Definition Mach1Encode.h:544
Mach1EncodeOutputMode getOutputMode()
Get the format in which this Mach1Encode will output encoded audio.
Definition Mach1Encode.h:444
void setInputMode(Mach1EncodeInputMode inputMode)
Definition Mach1Encode.h:469
void setAzimuthDegrees(float azimuthDegrees)
Sets the point(s) azimuth rotation of the vector space.
Definition Mach1Encode.h:484
int getInputChannelsCount()
Get the number of input channels that this Mach1Encode expects to receive for encoding,...
Definition Mach1Encode.h:459
Definition Mach1Point3D.h:17