Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/VocalTractLabApi/VocalTractLabApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extern "C"{ /* start extern "C" */
#endif // WIN32

#include <stdbool.h>
#include <stddef.h>

// ****************************************************************************
// The exported C-compatible functions.
Expand Down Expand Up @@ -202,6 +203,23 @@ C_EXPORT int vtlGetTractParams(const char *shapeName, double *tractParams);

C_EXPORT int vtlExportTractSvg(double *tractParams, const char *fileName);

// ****************************************************************************
// Exports the vocal tract contours for the given vector of vocal tract
// parameters as a string.
//
// Function return value:
// 0: success.
// 1: The API has not been initialized.
// 2: Writing the SVG file failed.
// ****************************************************************************

C_EXPORT int vtlExportTractSvgToStr(double *tractParams, char **svgStr, size_t *svgStrSize);

// ****************************************************************************
// Provide a custom free function to free the memory allocated for the SVG string.
// ****************************************************************************

C_EXPORT void vtlFree(void* p);

// ****************************************************************************
// Provides the tube data (especially the area function) for the given vector
Expand Down
9 changes: 6 additions & 3 deletions include/VocalTractLabBackend/VocalTract.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ class VocalTract
// ****************************************************************

void calcSurfaces();
void calcLimitedSurfaces();
void calcLips();
void getImportantLipPoints(Point3D &onset, Point3D &corner, Point3D &F0, Point3D &F1, double &yClose);
void calcRadiation(Point3D lipCorner);
Expand Down Expand Up @@ -450,9 +451,11 @@ class VocalTract
void getCrossSection(double *upperProfile, double *lowerProfile, CrossSection *section);

bool exportCrossSections(const string &fileName);
bool exportTractContourSvg(std::ostream& os, bool addCenterLine, bool addCutVectors);
bool exportTractContourSvg(const string &fileName, bool addCenterLine, bool addCutVectors);
void addRibPointsSvg(ostream &os, Surface *s, int rib, int firstRibPoint, int lastRibPoint);
void addRibsSvg(ostream &os, Surface *s, int firstRib, int lastRib, int ribPoint);
std::string exportTractContourSvgToStr(bool addCenterLine, bool addCutVectors);
void addRibPointsSvg(std::ostream &os, Surface *s, int rib, int firstRibPoint, int lastRibPoint);
void addRibsSvg(std::ostream &os, Surface *s, int firstRib, int lastRib, int ribPoint);

// ****************************************************************
// Calculate the piecewise constant area function.
Expand All @@ -475,7 +478,7 @@ class VocalTract
void clearUnsavedChanges();

void storeControlParams();
void restoreControlParams();
void restoreControlParams(bool recalculate = true);

// **************************************************************************
/// Private data.
Expand Down
68 changes: 68 additions & 0 deletions src/VocalTractLabApi/VocalTractLabApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,74 @@ int vtlExportTractSvg(double *tractParams, const char *fileName)
}
}

// ****************************************************************************
// Exports the vocal tract contours for the given vector of vocal tract
// parameters as a string.
//
// Function return value:
// 0: success.
// 1: The API has not been initialized.
// 2: Writing the SVG file failed.
// ****************************************************************************

int vtlExportTractSvgToStr(double *tractParams, char **svgStr, size_t *svgStrSize)
{
if (!vtlApiInitialized)
{
printf("Error: The API has not been initialized.\n");
return 1;
}

*svgStr = NULL;
*svgStrSize = 0;

// Store the current control parameter values.
vocalTract->storeControlParams();

// Set the given vocal tract parameters.
int i;
for (i = 0; i < VocalTract::NUM_PARAMS; i++)
{
vocalTract->param[i].x = tractParams[i];
}

vocalTract->calcLimitedSurfaces();

// Save the contour as SVG file.
std::string s = vocalTract->exportTractContourSvgToStr(false, false);
bool ok = !s.empty();

// Restore the previous control parameter values and
// recalculate the vocal tract shape.

vocalTract->restoreControlParams(false);
vocalTract->calcLimitedSurfaces();

if (ok)
{
size_t n = s.size();
*svgStr = (char*)std::malloc(n+1);
if(!*svgStr)
{
printf("Error: Memory allocation failed in vtlExportTractSvgToStr().\n");
return 2;
}

memcpy(*svgStr, s.c_str(), n);
(*svgStr)[n] = '\0'; // keep svgStr as a valid C string (null-terminated) if maybe required later
*svgStrSize = n; // return the size of the string (without the terminating null character)
return 0;
}
else
{
return 2;
}
}

void vtlFree(void* p)
{
std::free(p);
}

// ****************************************************************************
// Provides the tube data (especially the area function) for the given vector
Expand Down
1 change: 1 addition & 0 deletions src/VocalTractLabApi/VocalTractLabApi.def
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ vtlGetGlottisParamInfo
vtlGetGlottisParams
vtlGetTractParams
vtlExportTractSvg
vtlExportTractSvgToStr
vtlTractToTube
vtlFastTractToTube
vtlGetDefaultTransferFunctionOptions
Expand Down
68 changes: 55 additions & 13 deletions src/VocalTractLabBackend/VocalTract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2390,6 +2390,27 @@ void VocalTract::calculateAll()
}


void VocalTract::calcLimitedSurfaces()
{
// ****************************************************************
// Set the limited parameter values equal to the set values.
// ****************************************************************

int i;
for (i=0; i < NUM_PARAMS; i++)
{
restrictParam(i);
param[i].limitedX = param[i].x;
}

// ****************************************************************
// Do the calculations.
// ****************************************************************

calcSurfaces();
}


// ****************************************************************************
/// Calculate all surfaces of the model.
// ****************************************************************************
Expand Down Expand Up @@ -6813,10 +6834,10 @@ bool VocalTract::exportCrossSections(const string &fileName)


// ****************************************************************************
// Save the vocal tract contour lines as SVG file.
// Write the vocal tract contour lines to a stream
// ****************************************************************************

bool VocalTract::exportTractContourSvg(const string &fileName, bool addCenterLine, bool addCutVectors)
bool VocalTract::exportTractContourSvg(std::ostream& os, bool addCenterLine, bool addCutVectors)
{
const double SCALE = 37.8; // 1 cm in Corel Draw are 37.8 "default units" (pixels?)
int indent = 0;
Expand All @@ -6829,14 +6850,6 @@ bool VocalTract::exportTractContourSvg(const string &fileName, bool addCenterLin
bool includeTeeth = true;
bool includeRibs = false;

ofstream os(fileName);

if (!os)
{
printf("Error: Could not open the SVG file\n");
return false;
}

// Write the "header and open the svg- and the group element.

os << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << endl;
Expand Down Expand Up @@ -7071,12 +7084,38 @@ bool VocalTract::exportTractContourSvg(const string &fileName, bool addCenterLin
indent -= 2;
os << "</svg>" << endl;

// Close the file *************************************************
return true;
}

// ****************************************************************************
// Save the vocal tract contour lines as SVG file.
// ****************************************************************************
bool VocalTract::exportTractContourSvg(const string &fileName, bool addCenterLine, bool addCutVectors)
{
ofstream os(fileName);
if (!os)
{
printf("Error: Could not open file '%s' for writing.\n", fileName.c_str());
return false;
}

exportTractContourSvg(os, addCenterLine, addCutVectors);

os.close();

return true;
}

// ****************************************************************************
// Save the vocal tract contour lines to a string.
// ****************************************************************************
std::string VocalTract::exportTractContourSvgToStr(bool addCenterLine, bool addCutVectors)
{
std::ostringstream oss;
exportTractContourSvg(oss, addCenterLine, addCutVectors);
return oss.str();
}


// ****************************************************************************
// Adds the point coordinates of some rib points to an output stream.
Expand Down Expand Up @@ -7639,7 +7678,7 @@ void VocalTract::storeControlParams()
/// Restore the temporarily stored (cached) control parameter values.
// ****************************************************************************

void VocalTract::restoreControlParams()
void VocalTract::restoreControlParams(bool recalculate)
{
if (hasStoredControlParams == false)
{
Expand All @@ -7653,7 +7692,10 @@ void VocalTract::restoreControlParams()
}
hasStoredControlParams = false;

calculateAll();
if (recalculate)
{
calculateAll();
}
}


Expand Down
Loading