Audacity 3.2.0
FormantShifterLogger.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 FormantShifterLogger.cpp
7
8 Matthieu Hodgkinson
9
10**********************************************************************/
13#include <algorithm>
14#include <cmath>
15#include <vector>
16
17namespace
18{
19std::string GetLogDir()
20{
21 const char* appDataDir = getenv("APPDATA");
22 if (appDataDir)
23 return std::string(appDataDir) + "/TimeAndPitchTuning";
24 else
25 return "";
26}
27
28template <typename Iterator>
30 std::ofstream& ofs, Iterator begin, Iterator end, const char* name)
31{
32 ofs << name << " = [";
33 std::for_each(begin, end, [&](float x) { ofs << x << ","; });
34 ofs << "]\n";
35}
36} // namespace
37
39 : mSampleRate { sampleRate }
40 , mLogSample { logSample }
41{
42}
43
45{
46}
47
49{
52 {
53 // Ready for logging.
54 mOfs = std::make_unique<std::ofstream>(
56 "/FormantShifterLog.py");
57 *mOfs << "sampleRate = " << mSampleRate << "\n";
58 mWasLogged = true;
59 }
60}
61
62void FormantShifterLogger::Log(int value, const char* name) const
63{
64 if (mOfs)
65 *mOfs << name << " = " << value << "\n";
66}
67
69 const float* samples, size_t size, const char* name) const
70{
71 if (!mOfs)
72 // Keep it lightweight if we're not logging.
73 return;
74 PrintPythonVector(*mOfs, samples, samples + size, name);
75}
76
78 const std::complex<float>* cv, size_t cvSize, const char* name,
79 const std::function<float(const std::complex<float>&)>& transform) const
80{
81 if (!mOfs)
82 return;
83 std::vector<float> v(cvSize);
84 std::transform(cv, cv + cvSize, v.begin(), transform);
85 PrintPythonVector(*mOfs, v.begin(), v.end(), name);
86}
87
89 std::complex<float>* spectrum, size_t fftSize)
90{
91 if (!mOfs)
92 return;
93 // Such a spectrum of only (1 + 0j) is that of a click, which should be
94 // audible ...
95 std::fill(spectrum, spectrum + fftSize / 2 + 1, 1.f);
96 mOfs.reset();
97}
wxString name
Definition: TagsEditor.cpp:166
std::unique_ptr< std::ofstream > mOfs
void NewSamplesComing(int sampleCount) override
FormantShifterLogger(int sampleRate, int logTimeInSamples)
void Log(int value, const char *name) const override
void ProcessFinished(std::complex< float > *spectrum, size_t fftSize) override
If not already, disables the logging and marks the spectrum with an audible event to make clear where...
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
void PrintPythonVector(std::ofstream &ofs, Iterator begin, Iterator end, const char *name)
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
const char * begin(const char *str) noexcept
Definition: StringUtils.h:101