From 57b5e0442cd83d26bf02100a4ef60aad4f77e567 Mon Sep 17 00:00:00 2001 From: Takeshi Ikuma Date: Sun, 28 Feb 2021 12:00:25 -0600 Subject: [PATCH] Reverted format to master --- src/parselmouth/Pitch.cpp | 160 +++++++++++++++--------------- src/parselmouth/Vector.cpp | 197 +++++++++++++++++-------------------- 2 files changed, 171 insertions(+), 186 deletions(-) diff --git a/src/parselmouth/Pitch.cpp b/src/parselmouth/Pitch.cpp index 3770df49..8a01a0cd 100644 --- a/src/parselmouth/Pitch.cpp +++ b/src/parselmouth/Pitch.cpp @@ -69,16 +69,16 @@ PRAAT_STRUCT_BINDING(Frame, Pitch_Frame) { def_readonly("intensity", &structPitch_Frame::intensity); def_property("selected", - [](Pitch_Frame self) { return &self->candidates[1]; }, - [](Pitch_Frame self, Pitch_Candidate candidate) { - for (long j = 1; j <= self->nCandidates; j++) { - if (&self->candidates[j] == candidate) { - std::swap(self->candidates[1], self->candidates[j]); - return; - } - } - throw py::value_error("'candidate' is not a Pitch Candidate of this frame"); - }); + [](Pitch_Frame self) { return &self->candidates[1]; }, + [](Pitch_Frame self, Pitch_Candidate candidate) { + for (long j = 1; j <= self->nCandidates; j++) { + if (&self->candidates[j] == candidate) { + std::swap(self->candidates[1], self->candidates[j]); + return; + } + } + throw py::value_error("'candidate' is not a Pitch Candidate of this frame"); + }); def_property_readonly("candidates", [](Pitch_Frame self) { return std::vector(&self->candidates[1], &self->candidates[self->nCandidates + 1]); }); @@ -93,19 +93,19 @@ PRAAT_STRUCT_BINDING(Frame, Pitch_Frame) { }); def("select", - [](Pitch_Frame self, Pitch_Candidate candidate) { - for (long j = 1; j <= self->nCandidates; j++) { - if (self->candidates[j].frequency == candidate->frequency && self->candidates[j].strength == candidate->strength) { - std::swap(self->candidates[1], self->candidates[j]); - return; - } - } - throw py::value_error("'candidate' is not a Pitch Candidate of this frame"); - }, - "candidate"_a.none(false)); + [](Pitch_Frame self, Pitch_Candidate candidate) { + for (long j = 1; j <= self->nCandidates; j++) { + if (self->candidates[j].frequency == candidate->frequency && self->candidates[j].strength == candidate->strength) { + std::swap(self->candidates[1], self->candidates[j]); + return; + } + } + throw py::value_error("'candidate' is not a Pitch Candidate of this frame"); + }, + "candidate"_a.none(false)); def("select", - [](Pitch_Frame self, long i) { + [](Pitch_Frame self, long i) { if (i < 0) i += self->nCandidates; // Python-style negative indexing if (i < 0 || i >= self->nCandidates) throw py::index_error("Pitch Frame index out of range"); return std::swap(self->candidates[1], self->candidates[i + 1]); @@ -113,7 +113,7 @@ PRAAT_STRUCT_BINDING(Frame, Pitch_Frame) { "i"_a); def("__getitem__", - [](Pitch_Frame self, long i) { + [](Pitch_Frame self, long i) { if (i < 0) i += self->nCandidates; // Python-style negative indexing if (i < 0 || i >= self->nCandidates) throw py::index_error("Pitch Frame index out of range"); return self->candidates[i + 1]; // Not a(n) (internal) reference, because unvoice and select would then change the value of a returned Pitch_Candidate @@ -144,33 +144,33 @@ PRAAT_CLASS_BINDING(Pitch) { "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); def("to_sound_hum", - [](Pitch self, std::optional fromTime, std::optional toTime) { return Pitch_to_Sound(self, fromTime.value_or(self->xmin), toTime.value_or(self->xmax), true); }, - "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); + [](Pitch self, std::optional fromTime, std::optional toTime) { return Pitch_to_Sound(self, fromTime.value_or(self->xmin), toTime.value_or(self->xmax), true); }, + "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); def("to_sound_sine", - [](Pitch self, std::optional fromTime, std::optional toTime, Positive samplingFrequency, double roundToNearestZeroCrossing) { return Pitch_to_Sound_sine(self, fromTime.value_or(self->xmin), toTime.value_or(self->xmax), samplingFrequency, roundToNearestZeroCrossing); }, - "from_time"_a = std::nullopt, "to_time"_a = std::nullopt, "sampling_frequency"_a = 44100.0, "round_to_nearest_zero_crossing"_a = true); + [](Pitch self, std::optional fromTime, std::optional toTime, Positive samplingFrequency, double roundToNearestZeroCrossing) { return Pitch_to_Sound_sine(self, fromTime.value_or(self->xmin), toTime.value_or(self->xmax), samplingFrequency, roundToNearestZeroCrossing); }, + "from_time"_a = std::nullopt, "to_time"_a = std::nullopt, "sampling_frequency"_a = 44100.0, "round_to_nearest_zero_crossing"_a = true); def("count_voiced_frames", - &Pitch_countVoicedFrames); + &Pitch_countVoicedFrames); def("get_value_at_time", - [](Pitch self, double time, kPitch_unit unit, kVector_valueInterpolation interpolation) { - if (interpolation != kVector_valueInterpolation::NEAREST && interpolation != kVector_valueInterpolation::LINEAR) - Melder_throw(U"Pitch values can only be queried using NEAREST or LINEAR interpolation"); - auto value = Sampled_getValueAtX(self, time, Pitch_LEVEL_FREQUENCY, static_cast(unit), interpolation == kVector_valueInterpolation::LINEAR); - return Function_convertToNonlogarithmic(self, value, Pitch_LEVEL_FREQUENCY, static_cast(unit)); - }, - "time"_a, "unit"_a = kPitch_unit::HERTZ, "interpolation"_a = kVector_valueInterpolation::LINEAR); + [](Pitch self, double time, kPitch_unit unit, kVector_valueInterpolation interpolation) { + if (interpolation != kVector_valueInterpolation::NEAREST && interpolation != kVector_valueInterpolation::LINEAR) + Melder_throw(U"Pitch values can only be queried using NEAREST or LINEAR interpolation"); + auto value = Sampled_getValueAtX(self, time, Pitch_LEVEL_FREQUENCY, static_cast(unit), interpolation == kVector_valueInterpolation::LINEAR); + return Function_convertToNonlogarithmic(self, value, Pitch_LEVEL_FREQUENCY, static_cast(unit)); + }, + "time"_a, "unit"_a = kPitch_unit::HERTZ, "interpolation"_a = kVector_valueInterpolation::LINEAR); // TODO get_strength_at_time ? -> Pitch strength unit enum def("get_value_in_frame", - [](Pitch self, long frameNumber, kPitch_unit unit) { - auto value = Sampled_getValueAtSample(self, frameNumber, Pitch_LEVEL_FREQUENCY, static_cast(unit)); - return Function_convertToNonlogarithmic(self, value, Pitch_LEVEL_FREQUENCY, static_cast(unit)); - }, - "frame_number"_a, "unit"_a = kPitch_unit::HERTZ); + [](Pitch self, long frameNumber, kPitch_unit unit) { + auto value = Sampled_getValueAtSample(self, frameNumber, Pitch_LEVEL_FREQUENCY, static_cast(unit)); + return Function_convertToNonlogarithmic(self, value, Pitch_LEVEL_FREQUENCY, static_cast(unit)); + }, + "frame_number"_a, "unit"_a = kPitch_unit::HERTZ); // TODO Minimum, Time of minimum, Maximum, Time of maximum, ... @@ -214,16 +214,16 @@ PRAAT_CLASS_BINDING(Pitch) { }); def("count_differences", - [](Pitch self, Pitch other) { - MelderInfoInterceptor info; - Pitch_difference(self, other); - return info.get(); - }, - "other"_a.none(false)); + [](Pitch self, Pitch other) { + MelderInfoInterceptor info; + Pitch_difference(self, other); + return info.get(); + }, + "other"_a.none(false)); def("formula", - [](Pitch self, const std::u32string &formula) { Pitch_formula(self, formula.c_str(), nullptr); }, - "formula"_a); + [](Pitch self, const std::u32string &formula) { Pitch_formula(self, formula.c_str(), nullptr); }, + "formula"_a); // TODO To TextGrid..., To TextTier, To IntervalTier: depends TextGrid and Tiers // TODO To PointProcess: depends on PointProcess @@ -232,15 +232,15 @@ PRAAT_CLASS_BINDING(Pitch) { &Pitch_interpolate); def("smooth", - args_cast<_, Positive<_>>(Pitch_smooth), - "bandwidth"_a = 10.0); + args_cast<_, Positive<_>>(Pitch_smooth), + "bandwidth"_a = 10.0); def("subtract_linear_fit", &Pitch_subtractLinearFit, - "unit"_a = kPitch_unit::HERTZ); + "unit"_a = kPitch_unit::HERTZ); def("kill_octave_jumps", - &Pitch_killOctaveJumps); + &Pitch_killOctaveJumps); // TODO To PitchTier: depends on PitchTier @@ -252,14 +252,14 @@ PRAAT_CLASS_BINDING(Pitch) { def_readonly("max_n_candidates", &structPitch::maxnCandidates); def("get_frame", - [](Pitch self, Positive frameNumber) { - if (frameNumber > self->nx) Melder_throw(U"Frame number out of range"); - return &self->frames[frameNumber]; - }, - "frame_number"_a, py::return_value_policy::reference_internal); + [](Pitch self, Positive frameNumber) { + if (frameNumber > self->nx) Melder_throw(U"Frame number out of range"); + return &self->frames[frameNumber]; + }, + "frame_number"_a, py::return_value_policy::reference_internal); def("__getitem__", - [](Pitch self, long i) { + [](Pitch self, long i) { if (i < 0) i += self->nx; // Python-style negative indexing if (i < 0 || i >= self->nx) throw py::index_error("Pitch index out of range"); return &self->frames[i + 1]; @@ -267,8 +267,8 @@ PRAAT_CLASS_BINDING(Pitch) { "i"_a, py::return_value_policy::reference_internal); def("__getitem__", - [](Pitch self, std::tuple ij) { - auto &[i, j] = ij; + [](Pitch self, std::tuple ij) { + auto &[i, j] = ij; if (i < 0) i += self->nx; // Python-style negative indexing if (i < 0 || i >= self->nx) throw py::index_error("Pitch index out of range"); auto &frame = self->frames[i + 1]; @@ -325,37 +325,37 @@ PRAAT_CLASS_BINDING(Pitch) { "silence_threshold"_a = 0.03, "voicing_threshold"_a = 0.45, "octave_cost"_a = 0.01, "octave_jump_cost"_a = 0.35, "voiced_unvoiced_cost"_a = 0.14, "ceiling"_a = 600.0, "pull_formants"_a = false); def("step", - [](Pitch self, double step, Positive precision, std::optional fromTime, std::optional toTime) { Pitch_step(self, step, precision, fromTime.value_or(self->xmin), toTime.value_or(self->xmax)); }, - "step"_a, "precision"_a = 0.1, "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); + [](Pitch self, double step, Positive precision, std::optional fromTime, std::optional toTime) { Pitch_step(self, step, precision, fromTime.value_or(self->xmin), toTime.value_or(self->xmax)); }, + "step"_a, "precision"_a = 0.1, "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); def("octave_up", - [](Pitch self, std::optional fromTime, std::optional toTime) { - Pitch_step(self, 2.0, 0.1, fromTime.value_or(self->xmin), toTime.value_or(self->xmax)); - }, - "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); + [](Pitch self, std::optional fromTime, std::optional toTime) { + Pitch_step(self, 2.0, 0.1, fromTime.value_or(self->xmin), toTime.value_or(self->xmax)); + }, + "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); def("fifth_up", - [](Pitch self, std::optional fromTime, std::optional toTime) { - Pitch_step(self, 1.5, 0.1, fromTime.value_or(self->xmin), toTime.value_or(self->xmax)); - }, - "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); + [](Pitch self, std::optional fromTime, std::optional toTime) { + Pitch_step(self, 1.5, 0.1, fromTime.value_or(self->xmin), toTime.value_or(self->xmax)); + }, + "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); def("fifth_down", - [](Pitch self, std::optional fromTime, std::optional toTime) { - Pitch_step(self, 1 / 1.5, 0.1, fromTime.value_or(self->xmin), toTime.value_or(self->xmax)); - }, - "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); + [](Pitch self, std::optional fromTime, std::optional toTime) { + Pitch_step(self, 1 / 1.5, 0.1, fromTime.value_or(self->xmin), toTime.value_or(self->xmax)); + }, + "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); def("octave_down", - [](Pitch self, std::optional fromTime, std::optional toTime) { - Pitch_step(self, 0.5, 0.1, fromTime.value_or(self->xmin), toTime.value_or(self->xmax)); - }, - "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); + [](Pitch self, std::optional fromTime, std::optional toTime) { + Pitch_step(self, 0.5, 0.1, fromTime.value_or(self->xmin), toTime.value_or(self->xmax)); + }, + "from_time"_a = std::nullopt, "to_time"_a = std::nullopt); def("unvoice", - [](Pitch self, std::optional fromTime, std::optional toTime) { - long ileft = Sampled_xToHighIndex(self, fromTime.value_or(self->xmin)); - long iright = Sampled_xToLowIndex(self, toTime.value_or(self->xmax)); + [](Pitch self, std::optional fromTime, std::optional toTime) { + long ileft = Sampled_xToHighIndex(self, fromTime.value_or(self->xmin)); + long iright = Sampled_xToLowIndex(self, toTime.value_or(self->xmax)); if (ileft < 1) ileft = 1; if (iright > self->nx) iright = self->nx; diff --git a/src/parselmouth/Vector.cpp b/src/parselmouth/Vector.cpp index d0d51d0b..29be8473 100644 --- a/src/parselmouth/Vector.cpp +++ b/src/parselmouth/Vector.cpp @@ -30,119 +30,104 @@ namespace py = pybind11; using namespace py::literals; -namespace parselmouth +namespace parselmouth { + +PRAAT_ENUM_BINDING(ValueInterpolation) { + value("NEAREST", kVector_valueInterpolation::NEAREST); + value("LINEAR", kVector_valueInterpolation::LINEAR); + value("CUBIC", kVector_valueInterpolation::CUBIC); + value("SINC70", kVector_valueInterpolation::SINC70); + value("SINC700", kVector_valueInterpolation::SINC700); + + make_implicitly_convertible_from_string(*this); +} + + +PRAAT_ENUM_BINDING(PeakInterpolation) { + value("NONE", kVector_peakInterpolation::NONE); + value("PARABOLIC", kVector_peakInterpolation::PARABOLIC); + value("CUBIC", kVector_peakInterpolation::CUBIC); + value("SINC70", kVector_peakInterpolation::SINC70); + value("SINC700", kVector_peakInterpolation::SINC700); + + make_implicitly_convertible_from_string(*this); +} + +PRAAT_CLASS_BINDING(Vector) { + using signature_cast_placeholder::_; + + // TODO Something to get rid of duplicate functions with different names? + def("add", + &Vector_addScalar, + "number"_a); + + def("__iadd__", + [](Vector self, double number) { Vector_addScalar(self, number); return self; }, + "number"_a, py::is_operator()); + + def("__add__", + [](Vector self, double number) { auto result = Data_copy(self); Vector_addScalar(result.get(), number); return result; }, + "number"_a, py::is_operator()); + + def("__radd__", + [](Vector self, double number) { auto result = Data_copy(self); Vector_addScalar(result.get(), number); return result; }, + "number"_a, py::is_operator()); + + def("subtract", + [](Vector self, double number) { Vector_addScalar(self, -number); }, + "number"_a); - PRAAT_ENUM_BINDING(ValueInterpolation) - { - value("NEAREST", kVector_valueInterpolation::NEAREST); - value("LINEAR", kVector_valueInterpolation::LINEAR); - value("CUBIC", kVector_valueInterpolation::CUBIC); - value("SINC70", kVector_valueInterpolation::SINC70); - value("SINC700", kVector_valueInterpolation::SINC700); - - make_implicitly_convertible_from_string(*this); - } - - PRAAT_ENUM_BINDING(PeakInterpolation) - { - value("NONE", kVector_peakInterpolation::NONE); - value("PARABOLIC", kVector_peakInterpolation::PARABOLIC); - value("CUBIC", kVector_peakInterpolation::CUBIC); - value("SINC70", kVector_peakInterpolation::SINC70); - value("SINC700", kVector_peakInterpolation::SINC700); - - make_implicitly_convertible_from_string(*this); - } - - PRAAT_CLASS_BINDING(Vector) - { - using signature_cast_placeholder::_; - - // TODO Something to get rid of duplicate functions with different names? - def("add", - &Vector_addScalar, - "number"_a); - - def( - "__iadd__", - [](Vector self, double number) { Vector_addScalar(self, number); return self; }, - "number"_a, py::is_operator()); - - def( - "__add__", - [](Vector self, double number) { auto result = Data_copy(self); Vector_addScalar(result.get(), number); return result; }, - "number"_a, py::is_operator()); - - def( - "__radd__", - [](Vector self, double number) { auto result = Data_copy(self); Vector_addScalar(result.get(), number); return result; }, - "number"_a, py::is_operator()); - - def( - "subtract", - [](Vector self, double number) { Vector_addScalar(self, -number); }, - "number"_a); - - def( - "__isub__", - [](Vector self, double number) { Vector_addScalar(self, -number); return self; }, - "number"_a, py::is_operator()); - - def( - "__sub__", - [](Vector self, double number) { auto result = Data_copy(self); Vector_addScalar(result.get(), -number); return result; }, - "number"_a, py::is_operator()); - - def("subtract_mean", - &Vector_subtractMean); - - def("multiply", - &Vector_multiplyByScalar, - "factor"_a); - - def( - "__imul__", - [](Vector self, double factor) { Vector_multiplyByScalar(self, factor); return self; }, - "factor"_a, py::is_operator()); - - def( - "__mul__", - [](Vector self, double factor) { auto result = Data_copy(self); Vector_multiplyByScalar(result.get(), factor); return result; }, - "factor"_a, py::is_operator()); - - def( - "__rmul__", - [](Vector self, double factor) { auto result = Data_copy(self); Vector_multiplyByScalar(result.get(), factor); return result; }, - "factor"_a, py::is_operator()); - - def( - "divide", // TODO Not zero? - [](Vector self, double factor) { Vector_multiplyByScalar(self, 1 / factor); }, - "factor"_a); - - def( - "__itruediv__", - [](Vector self, double factor) { Vector_multiplyByScalar(self, 1 / factor); return self; }, - "factor"_a, py::is_operator()); - - def( - "__truediv__", - [](Vector self, double factor) { auto result = Data_copy(self); Vector_multiplyByScalar(result.get(), 1 / factor); return result; }, - "factor"_a, py::is_operator()); + def("__isub__", + [](Vector self, double number) { Vector_addScalar(self, -number); return self; }, + "number"_a, py::is_operator()); + + def("__sub__", + [](Vector self, double number) { auto result = Data_copy(self); Vector_addScalar(result.get(), -number); return result; }, + "number"_a, py::is_operator()); + + def("subtract_mean", + &Vector_subtractMean); + + def("multiply", + &Vector_multiplyByScalar, + "factor"_a); + + def("__imul__", + [](Vector self, double factor) { Vector_multiplyByScalar(self, factor); return self; }, + "factor"_a, py::is_operator()); + + def("__mul__", + [](Vector self, double factor) { auto result = Data_copy(self); Vector_multiplyByScalar(result.get(), factor); return result; }, + "factor"_a, py::is_operator()); + + def("__rmul__", + [](Vector self, double factor) { auto result = Data_copy(self); Vector_multiplyByScalar(result.get(), factor); return result; }, + "factor"_a, py::is_operator()); + + def("divide", // TODO Not zero? + [](Vector self, double factor) { Vector_multiplyByScalar(self, 1 / factor); }, + "factor"_a); + + def("__itruediv__", + [](Vector self, double factor) { Vector_multiplyByScalar(self, 1 / factor); return self; }, + "factor"_a, py::is_operator()); + + def("__truediv__", + [](Vector self, double factor) { auto result = Data_copy(self); Vector_multiplyByScalar(result.get(), 1 / factor); return result; }, + "factor"_a, py::is_operator()); def("scale", args_cast<_, Positive<_>>(Vector_scale), "scale"_a); - def("scale_peak", - args_cast<_, Positive<_>>(Vector_scale), - "new_peak"_a = 0.99); + def("scale_peak", + args_cast<_, Positive<_>>(Vector_scale), + "new_peak"_a = 0.99); - def( - "get_value", // TODO Default for interpolation? Different for Sound (SINC70), Harmonicity/Intensity/Formants (CUBIC) and Ltas (LINEAR); take praat_TimeFunction.h into account - [](Vector self, double x, std::optional channel, kVector_valueInterpolation interpolation) { return Vector_getValueAtX(self, x, channel.value_or(Vector_CHANNEL_AVERAGE), interpolation); }, - "x"_a, "channel"_a = std::nullopt, "interpolation"_a = kVector_valueInterpolation::CUBIC); - } + def("get_value", // TODO Default for interpolation? Different for Sound (SINC70), Harmonicity/Intensity/Formants (CUBIC) and Ltas (LINEAR); take praat_TimeFunction.h into account + [](Vector self, double x, std::optional channel, kVector_valueInterpolation interpolation) { return Vector_getValueAtX (self, x, channel.value_or(Vector_CHANNEL_AVERAGE), interpolation); }, + "x"_a, "channel"_a = std::nullopt, "interpolation"_a = kVector_valueInterpolation::CUBIC); +} } // namespace parselmouth