Skip to content

Commit

Permalink
Avoid calling fprintf with stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Nov 2, 2024
1 parent dcd0842 commit 5493ea1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 52 deletions.
10 changes: 5 additions & 5 deletions utils/makemhr/loadsofa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ using uint = unsigned int;
*/
auto PrepareLayout(const al::span<const float> xyzs, HrirDataT *hData) -> bool
{
fprintf(stdout, "Detecting compatible layout...\n");
fputs("Detecting compatible layout...\n", stdout);

auto fds = GetCompatibleLayout(xyzs);
if(fds.size() > MAX_FD_COUNT)
{
fprintf(stdout, "Incompatible layout (inumerable radii).\n");
fputs("Incompatible layout (inumerable radii).\n", stdout);
return false;
}

Expand All @@ -92,7 +92,7 @@ auto PrepareLayout(const al::span<const float> xyzs, HrirDataT *hData) -> bool

++fi;
}
fprintf(stdout, "Using %u of %zu IRs.\n", ir_total, xyzs.size()/3);
printf("Using %u of %zu IRs.\n", ir_total, xyzs.size()/3);
const auto azs = al::span{azCounts}.first<MAX_FD_COUNT>();
return PrepareHrirData(al::span{distances}.first(fi), evCounts, azs, hData);
}
Expand Down Expand Up @@ -422,8 +422,8 @@ bool LoadSofaFile(const std::string_view filename, const uint numThreads, const
MySofaHrtfPtr sofaHrtf{mysofa_load(std::string{filename}.c_str(), &err)};
if(!sofaHrtf)
{
fprintf(stdout, "Error: Could not load %.*s: %s (%d)\n", al::sizei(filename),
filename.data(), SofaErrorStr(err), err);
printf("Error: Could not load %.*s: %s (%d)\n", al::sizei(filename), filename.data(),
SofaErrorStr(err), err);
return false;
}

Expand Down
39 changes: 18 additions & 21 deletions utils/makemhr/makemhr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ void CalculateHrtds(const HeadModelT model, const double radius, HrirDataT *hDat
}
if(maxHrtd > MaxHrtd)
{
fprintf(stdout, " Scaling for max delay of %f samples to %f\n...\n", maxHrtd, MaxHrtd);
printf(" Scaling for max delay of %f samples to %f\n...\n", maxHrtd, MaxHrtd);
const double scale{MaxHrtd / maxHrtd};
for(auto &field : hData->mFds)
{
Expand Down Expand Up @@ -1158,12 +1158,11 @@ bool ProcessDefinition(std::string_view inName, const uint outRate, const Channe
{
HrirDataT hData;

fprintf(stdout, "Using %u thread%s.\n", numThreads, (numThreads==1)?"":"s");
printf("Using %u thread%s.\n", numThreads, (numThreads==1)?"":"s");
if(inName.empty() || inName == "-"sv)
{
inName = "stdin"sv;
fprintf(stdout, "Reading HRIR definition from %.*s...\n", al::sizei(inName),
inName.data());
printf("Reading HRIR definition from %.*s...\n", al::sizei(inName), inName.data());
if(!LoadDefInput(std::cin, {}, inName, fftSize, truncSize, outRate, chanMode, &hData))
return false;
}
Expand All @@ -1190,15 +1189,13 @@ bool ProcessDefinition(std::string_view inName, const uint outRate, const Channe
&& startbytes[3] == 'F')
{
input = nullptr;
fprintf(stdout, "Reading HRTF data from %.*s...\n", al::sizei(inName),
inName.data());
printf("Reading HRTF data from %.*s...\n", al::sizei(inName), inName.data());
if(!LoadSofaFile(inName, numThreads, fftSize, truncSize, outRate, chanMode, &hData))
return false;
}
else
{
fprintf(stdout, "Reading HRIR definition from %.*s...\n", al::sizei(inName),
inName.data());
printf("Reading HRIR definition from %.*s...\n", al::sizei(inName), inName.data());
if(!LoadDefInput(*input, startbytes, inName, fftSize, truncSize, outRate, chanMode,
&hData))
return false;
Expand All @@ -1213,43 +1210,43 @@ bool ProcessDefinition(std::string_view inName, const uint outRate, const Channe

if(hData.mFds.size() > 1)
{
fprintf(stdout, "Balancing field magnitudes...\n");
fputs("Balancing field magnitudes...\n", stdout);
BalanceFieldMagnitudes(&hData, c, m);
}
fprintf(stdout, "Calculating diffuse-field average...\n");
fputs("Calculating diffuse-field average...\n", stdout);
CalculateDiffuseFieldAverage(&hData, c, m, surface, limit, dfa);
fprintf(stdout, "Performing diffuse-field equalization...\n");
fputs("Performing diffuse-field equalization...\n", stdout);
DiffuseFieldEqualize(c, m, dfa, &hData);
}
if(hData.mFds.size() > 1)
{
fprintf(stdout, "Sorting %zu fields...\n", hData.mFds.size());
printf("Sorting %zu fields...\n", hData.mFds.size());
std::sort(hData.mFds.begin(), hData.mFds.end(),
[](const HrirFdT &lhs, const HrirFdT &rhs) noexcept
{ return lhs.mDistance < rhs.mDistance; });
if(farfield)
{
fprintf(stdout, "Clearing %zu near field%s...\n", hData.mFds.size()-1,
printf("Clearing %zu near field%s...\n", hData.mFds.size()-1,
(hData.mFds.size()-1 != 1) ? "s" : "");
hData.mFds.erase(hData.mFds.cbegin(), hData.mFds.cend()-1);
}
}
fprintf(stdout, "Synthesizing missing elevations...\n");
fputs("Synthesizing missing elevations...\n", stdout);
if(model == HM_Dataset)
SynthesizeOnsets(&hData);
SynthesizeHrirs(&hData);
fprintf(stdout, "Performing minimum phase reconstruction...\n");
fputs("Performing minimum phase reconstruction...\n", stdout);
ReconstructHrirs(&hData, numThreads);
fprintf(stdout, "Truncating minimum-phase HRIRs...\n");
fputs("Truncating minimum-phase HRIRs...\n", stdout);
hData.mIrPoints = truncSize;
fprintf(stdout, "Normalizing final HRIRs...\n");
fputs("Normalizing final HRIRs...\n", stdout);
NormalizeHrirs(&hData);
fprintf(stdout, "Calculating impulse delays...\n");
fputs("Calculating impulse delays...\n", stdout);
CalculateHrtds(model, (radius > DefaultCustomRadius) ? radius : hData.mRadius, &hData);

const auto rateStr = std::to_string(hData.mIrRate);
const auto expName = StrSubst(outName, "%r"sv, rateStr);
fprintf(stdout, "Creating MHR data set %s...\n", expName.c_str());
printf("Creating MHR data set %s...\n", expName.c_str());
return StoreMhr(&hData, expName);
}

Expand Down Expand Up @@ -1283,7 +1280,7 @@ int main(al::span<std::string_view> args)
{
if(args.size() < 2)
{
fprintf(stdout, "HRTF Processing and Composition Utility\n\n");
fputs("HRTF Processing and Composition Utility\n\n", stdout);
PrintHelp(args[0], stdout);
exit(EXIT_SUCCESS);
}
Expand Down Expand Up @@ -1496,7 +1493,7 @@ int main(al::span<std::string_view> args)
const int ret{ProcessDefinition(inName, outRate, chanMode, farfield, numThreads, fftSize,
equalize, surface, limit, truncSize, model, radius, outName)};
if(!ret) return -1;
fprintf(stdout, "Operation completed.\n");
fputs("Operation completed.\n", stdout);

return EXIT_SUCCESS;
}
Expand Down
4 changes: 2 additions & 2 deletions utils/openal-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void printList(const char *list, char separator)

if(!list || *list == '\0')
{
fprintf(stdout, "\n%s!!! none !!!\n", indent);
printf("\n%s!!! none !!!\n", indent);
return;
}

Expand All @@ -73,7 +73,7 @@ static void printList(const char *list, char separator)

if(len + col + 2 >= MaxWidth)
{
fprintf(stdout, "\n%s", indent);
printf("\n%s", indent);
col = strlen(indent);
}
else
Expand Down
32 changes: 16 additions & 16 deletions utils/sofa-info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void PrintSofaAttributes(const char *prefix, MYSOFA_ATTRIBUTE *attribute)
{
while(attribute)
{
fprintf(stdout, "%s.%s: %s\n", prefix, attribute->name, attribute->value);
printf("%s.%s: %s\n", prefix, attribute->name, attribute->value);
attribute = attribute->next;
}
}
Expand All @@ -57,10 +57,10 @@ void PrintSofaArray(const char *prefix, MYSOFA_ARRAY *array, bool showValues=tru
{
const auto values = al::span{array->values, array->elements};
for(size_t i{0u};i < values.size();++i)
fprintf(stdout, "%s[%zu]: %.6f\n", prefix, i, values[i]);
printf("%s[%zu]: %.6f\n", prefix, i, values[i]);
}
else
fprintf(stdout, "%s[...]: <%u values suppressed>\n", prefix, array->elements);
printf("%s[...]: <%u values suppressed>\n", prefix, array->elements);
}

/* Attempts to produce a compatible layout. Most data sets tend to be
Expand All @@ -76,7 +76,7 @@ void PrintCompatibleLayout(const al::span<const float> xyzs)
auto fds = GetCompatibleLayout(xyzs);
if(fds.empty())
{
fprintf(stdout, "No compatible field layouts in SOFA file.\n");
fputs("No compatible field layouts in SOFA file.\n", stdout);
return;
}

Expand All @@ -87,18 +87,18 @@ void PrintCompatibleLayout(const al::span<const float> xyzs)
used_elems += fds[fi].mAzCounts[ei];
}

fprintf(stdout, "Compatible Layout (%u of %zu measurements):\n\ndistance = %.3f", used_elems,
printf("Compatible Layout (%u of %zu measurements):\n\ndistance = %.3f", used_elems,
xyzs.size()/3, fds[0].mDistance);
for(size_t fi{1u};fi < fds.size();fi++)
fprintf(stdout, ", %.3f", fds[fi].mDistance);
printf(", %.3f", fds[fi].mDistance);

fprintf(stdout, "\nazimuths = ");
fputs("\nazimuths = ", stdout);
for(size_t fi{0u};fi < fds.size();++fi)
{
for(uint ei{0u};ei < fds[fi].mEvStart;++ei)
fprintf(stdout, "%d%s", fds[fi].mAzCounts[fds[fi].mEvCount - 1 - ei], ", ");
printf("%d%s", fds[fi].mAzCounts[fds[fi].mEvCount - 1 - ei], ", ");
for(uint ei{fds[fi].mEvStart};ei < fds[fi].mEvCount;++ei)
fprintf(stdout, "%d%s", fds[fi].mAzCounts[ei],
printf("%d%s", fds[fi].mAzCounts[ei],
(ei < (fds[fi].mEvCount - 1)) ? ", " :
(fi < (fds.size() - 1)) ? ";\n " : "\n");
}
Expand All @@ -111,25 +111,25 @@ void SofaInfo(const std::string &filename)
MySofaHrtfPtr sofa{mysofa_load(filename.c_str(), &err)};
if(!sofa)
{
fprintf(stdout, "Error: Could not load source file '%s' (%s).\n", filename.c_str(),
printf("Error: Could not load source file '%s' (%s).\n", filename.c_str(),
SofaErrorStr(err));
return;
}

/* NOTE: Some valid SOFA files are failing this check. */
err = mysofa_check(sofa.get());
if(err != MYSOFA_OK)
fprintf(stdout, "Warning: Supposedly malformed source file '%s' (%s).\n", filename.c_str(),
printf("Warning: Supposedly malformed source file '%s' (%s).\n", filename.c_str(),
SofaErrorStr(err));

mysofa_tocartesian(sofa.get());

PrintSofaAttributes("Info", sofa->attributes);

fprintf(stdout, "Measurements: %u\n", sofa->M);
fprintf(stdout, "Receivers: %u\n", sofa->R);
fprintf(stdout, "Emitters: %u\n", sofa->E);
fprintf(stdout, "Samples: %u\n", sofa->N);
printf("Measurements: %u\n", sofa->M);
printf("Receivers: %u\n", sofa->R);
printf("Emitters: %u\n", sofa->E);
printf("Samples: %u\n", sofa->N);

PrintSofaArray("SampleRate", &sofa->DataSamplingRate);
PrintSofaArray("DataDelay", &sofa->DataDelay);
Expand All @@ -142,7 +142,7 @@ int main(al::span<std::string_view> args)
{
if(args.size() != 2)
{
fprintf(stdout, "Usage: %.*s <sofa-file>\n", al::sizei(args[0]), args[0].data());
printf("Usage: %.*s <sofa-file>\n", al::sizei(args[0]), args[0].data());
return 0;
}

Expand Down
15 changes: 7 additions & 8 deletions utils/sofa-support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ auto GetCompatibleLayout(const al::span<const float> xyzs) -> std::vector<SofaFi
if(step <= 0.0)
{
if(elevs.empty())
fprintf(stdout, "No usable elevations on field distance %f.\n", dist);
printf("No usable elevations on field distance %f.\n", dist);
else
{
fprintf(stdout, "Non-uniform elevations on field distance %.3f.\nGot: %+.2f", dist,
printf("Non-uniform elevations on field distance %.3f.\nGot: %+.2f", dist,
elevs[0]);
for(size_t ei{1u};ei < elevs.size();++ei)
fprintf(stdout, ", %+.2f", elevs[ei]);
printf(", %+.2f", elevs[ei]);
fputc('\n', stdout);
}
continue;
Expand All @@ -230,7 +230,7 @@ auto GetCompatibleLayout(const al::span<const float> xyzs) -> std::vector<SofaFi
{
if(!(elevs[ei] < 0.0))
{
fprintf(stdout, "Too many missing elevations on field distance %f.\n", dist);
printf("Too many missing elevations on field distance %f.\n", dist);
return fds;
}

Expand All @@ -247,7 +247,7 @@ auto GetCompatibleLayout(const al::span<const float> xyzs) -> std::vector<SofaFi
const auto evCount = static_cast<uint>(std::round(180.0 / step)) + 1;
if(evCount < 5)
{
fprintf(stdout, "Too few uniform elevations on field distance %f.\n", dist);
printf("Too few uniform elevations on field distance %f.\n", dist);
continue;
}

Expand All @@ -267,7 +267,7 @@ auto GetCompatibleLayout(const al::span<const float> xyzs) -> std::vector<SofaFi
{
if(azims.size() != 1)
{
fprintf(stdout, "Non-singular poles on field distance %f.\n", dist);
printf("Non-singular poles on field distance %f.\n", dist);
return fds;
}
azCounts[ei] = 1;
Expand All @@ -277,8 +277,7 @@ auto GetCompatibleLayout(const al::span<const float> xyzs) -> std::vector<SofaFi
step = GetUniformAzimStep(0.1, azims);
if(step <= 0.0)
{
fprintf(stdout, "Non-uniform azimuths on elevation %f, field distance %f.\n",
ev, dist);
printf("Non-uniform azimuths on elevation %f, field distance %f.\n", ev, dist);
return fds;
}
azCounts[ei] = static_cast<uint>(std::round(360.0f / step));
Expand Down

0 comments on commit 5493ea1

Please sign in to comment.