Skip to content

Commit

Permalink
Merge pull request #1 from tu-studio/develop_fix_cart2spherical_conve…
Browse files Browse the repository at this point in the history
…rsions

Develop fix cart2spherical conversions
  • Loading branch information
themaxw authored Oct 8, 2024
2 parents bfcea37 + c60c182 commit 54b5d62
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
// Rotary Slider
//==============================================================================

class InvertibleSlider : public juce::Slider
{
// TODO does the default con/destructor need to be overwritten?
public:
double proportionOfLengthToValue (double proportion) { return m_is_inverted ? juce::Slider::proportionOfLengthToValue(1.0f-proportion) : juce::Slider::proportionOfLengthToValue(proportion) ;}
double valueToProportionOfLength (double value) { return m_is_inverted ? 1.0f-(juce::Slider::valueToProportionOfLength(value)): juce::Slider::valueToProportionOfLength(value) ; }
void setInverted(bool is_inverted){ m_is_inverted = is_inverted;}
private:
bool m_is_inverted = false;
};

class CostumRotarySlider : public juce::Component {
public:
CostumRotarySlider(juce::String sliderName);
Expand All @@ -24,7 +35,7 @@ class CostumRotarySlider : public juce::Component {
void addSliderAttachment(juce::AudioProcessorValueTreeState& state, const juce::String& parameterID);
void setDoubleClickReturnValue(double valueToSetOnDoubleClick);

juce::Slider slider;
InvertibleSlider slider;

private:
void paint(juce::Graphics& g) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SphericalSliderBox::SphericalSliderBox(juce::AudioProcessorValueTreeState &a) :

azimuthSlider.setDoubleClickReturnValue(0.f);
azimuthSlider.slider.setRange(-180.f, 180.f, 0.01f);
azimuthSlider.slider.setInverted(true);
azimuthSlider.slider.getValueObject().referTo(apvts.state.getChildWithName("Settings").getPropertyAsValue(PluginParameters::AZIMUTH_ID, nullptr));
addAndMakeVisible(azimuthSlider);

Expand Down Expand Up @@ -70,8 +71,9 @@ void SphericalSliderBox::addParameterAttachment(juce::RangedAudioParameter& para
xAttachment = std::make_unique<juce::ParameterAttachment>(
parameter,
// callback function on parameter change
[this, &parameter] (float newValue) {
parameterValueChanged(parameter, newValue);
[this] (float newValue) {
this->m_x = newValue;
this->updateSphericalCoordinates();
},
// undo manager
nullptr);
Expand All @@ -80,8 +82,9 @@ void SphericalSliderBox::addParameterAttachment(juce::RangedAudioParameter& para
yAttachment = std::make_unique<juce::ParameterAttachment>(
parameter,
// callback function on parameter change
[this, &parameter] (float newValue) {
parameterValueChanged(parameter, newValue);
[this] (float newValue) {
this->m_y = newValue;
this->updateSphericalCoordinates();
},
// undo manager
nullptr);
Expand All @@ -90,43 +93,31 @@ void SphericalSliderBox::addParameterAttachment(juce::RangedAudioParameter& para
zAttachment = std::make_unique<juce::ParameterAttachment>(
parameter,
// callback function on parameter change
[this, &parameter] (float newValue) {
parameterValueChanged(parameter, newValue);
[this] (float newValue) {
this->m_z = newValue;
this->updateSphericalCoordinates();
},
// undo manager
nullptr);
zAttachment->sendInitialUpdate();
}
}

void SphericalSliderBox::parameterValueChanged(juce::RangedAudioParameter& parameter, float newValue) {
void SphericalSliderBox::updateSphericalCoordinates() {
if (!activeDrag) {
float x = apvts.getParameterAsValue(OSCParameters::POS_X_ID.getParamID()).toString().getFloatValue();
float y = apvts.getParameterAsValue(OSCParameters::POS_Y_ID.getParamID()).toString().getFloatValue();
float z = apvts.getParameterAsValue(OSCParameters::POS_Z_ID.getParamID()).toString().getFloatValue();

if (parameter.getParameterID() == OSCParameters::POS_X_ID.getParamID()) {
x = newValue;
} else if (parameter.getParameterID() == OSCParameters::POS_Y_ID.getParamID()) {
y = newValue;
} else if (parameter.getParameterID() == OSCParameters::POS_Z_ID.getParamID()) {
z = newValue;
}
updateSphericalCoordinates(x, y, z);
}
}
float radius, azimuth, elevation;

void SphericalSliderBox::updateSphericalCoordinates(float x, float y, float z) {
float radius, azimuth, elevation;
radius = sqrtf(powf(m_x, 2) + powf(m_y, 2) + powf(m_z, 2));
azimuth = atan2f(m_y, m_x) * 180 / (float) M_PI;
if (radius != 0.f) elevation = asinf(m_z / radius) * 180 / (float) M_PI;
else elevation = 0.f;

radius = sqrtf(powf(x, 2) + powf(y, 2) + powf(z, 2));
azimuth = atan2f(y, x) * 180 / (float) M_PI;
if (radius != 0.f) elevation = asinf(z / radius) * 180 / (float) M_PI;
else elevation = 0.f;
// std::printf("update spherical from x=%f, y=%f, z=%f, new a=%f, e=%f, d=%f\n", m_x, m_y, m_z, azimuth, elevation, radius);

apvts.state.getChildWithName("Settings").setProperty(PluginParameters::RADIUS_ID, radius, nullptr);
apvts.state.getChildWithName("Settings").setProperty(PluginParameters::AZIMUTH_ID, azimuth, nullptr);
apvts.state.getChildWithName("Settings").setProperty(PluginParameters::ELEVATION_ID, elevation, nullptr);
apvts.state.getChildWithName("Settings").setProperty(PluginParameters::RADIUS_ID, radius, nullptr);
apvts.state.getChildWithName("Settings").setProperty(PluginParameters::AZIMUTH_ID, azimuth, nullptr);
apvts.state.getChildWithName("Settings").setProperty(PluginParameters::ELEVATION_ID, elevation, nullptr);
}
}

void SphericalSliderBox::sliderValueChanged(juce::Slider* slider) {
Expand Down Expand Up @@ -156,27 +147,28 @@ void SphericalSliderBox::sliderDragEnded(juce::Slider* slider) {

void SphericalSliderBox::updateCartesianCoordinates(float radius, float azimuth, float elevation) {
float x = limitMetricValue(radius * cosf(azimuth * (float) M_PI / 180) * cosf(elevation * (float) M_PI / 180));
x = normalizeMetricValue(x);
m_x = normalizeMetricValue(x);
float y = limitMetricValue(radius * sinf(azimuth * (float) M_PI / 180) * cosf(elevation * (float) M_PI / 180));
y = normalizeMetricValue(y);
m_y = normalizeMetricValue(y);
float z = limitMetricValue(radius * sinf(elevation * (float) M_PI / 180));
z = normalizeMetricValue(z);
m_z = normalizeMetricValue(z);

// std::printf("update cartesian from a=%f, e=%f, d=%f, new x=%f, y=%f, z=%f\n", azimuth, elevation, radius, x, y, z);
juce::AudioProcessorParameterWithID *xParam = apvts.getParameter(OSCParameters::POS_X_ID.getParamID());
juce::AudioProcessorParameterWithID *yParam = apvts.getParameter(OSCParameters::POS_Y_ID.getParamID());
juce::AudioProcessorParameterWithID *zParam = apvts.getParameter(OSCParameters::POS_Z_ID.getParamID());


xParam->beginChangeGesture();
xParam->setValueNotifyingHost(x);
xParam->setValueNotifyingHost(m_x);
xParam->endChangeGesture();

yParam->beginChangeGesture();
yParam->setValueNotifyingHost(y);
yParam->setValueNotifyingHost(m_y);
yParam->endChangeGesture();

zParam->beginChangeGesture();
zParam->setValueNotifyingHost(z);
zParam->setValueNotifyingHost(m_z);
zParam->endChangeGesture();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ class SphericalSliderBox : public juce::Component, private juce::Slider::Listene

private:
void addParameterAttachment(juce::RangedAudioParameter& parameter);
void parameterValueChanged(juce::RangedAudioParameter& parameter, float newValue);
void sliderValueChanged(juce::Slider* slider) override;
void sliderDragStarted(juce::Slider* slider) override;
void sliderDragEnded(juce::Slider* slider) override;
void updateSphericalCoordinates(float x, float y, float z);
void updateSphericalCoordinates();
void updateCartesianCoordinates(float radius, float azimuth, float elevation);
float limitMetricValue(float value);
float normalizeMetricValue(float value);
Expand All @@ -41,6 +40,7 @@ class SphericalSliderBox : public juce::Component, private juce::Slider::Listene
CostumRotarySlider azimuthSlider {"azimuth"};
CostumRotarySlider elevationSlider {"elevation"};

float m_x = 0.f, m_y = 0.f, m_z = 0.f;
bool activeDrag = false;
bool cartesianUpdate = false;
float cartesian_coordinate_limit = 1.f;
Expand Down
17 changes: 10 additions & 7 deletions SeamLess_Main/source/sourceTree/SourceTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,34 @@ void SourceTree::removeListener(Listener* listener) {
}

void SourceTree::newPluginConnection(PluginConnection *pluginConnection) {
int n_clients = 0;
for (auto & source : sources) {
if (source.pluginConnection != nullptr) n_clients++;
if (pluginConnection == source.pluginConnection) return;
}

Source newSource;
newSource.pluginConnection = pluginConnection;
sources.push_back(newSource);

// TODO when sources without connections are added, this would be false
setTreePropertyAsync(apvts.state.getChildWithName("Settings"), PluginParameters::NUM_CLIENTS_ID, (int) sources.size());
setTreePropertyAsync(apvts.state.getChildWithName("Settings"), PluginParameters::NUM_CLIENTS_ID, n_clients + 1);

#if JUCE_DEBUG
std::cout << "SourceTree has new source! N = " << sources.size() << std::endl;
std::cout << "SourceTree has new client! n_clients = " << n_clients + 1 << std::endl;
#endif
}

void SourceTree::deletedPluginConnection(PluginConnection *pluginConnection) {
for (unsigned long i = 0; i < sources.size(); i++) {
if (pluginConnection == sources[i].pluginConnection) sources.erase(sources.begin() + (long) i);
int n_clients = 0;
for (auto & source : sources) {
if (pluginConnection == source.pluginConnection) source.pluginConnection = nullptr;
if (source.pluginConnection != nullptr) n_clients++;
}

setTreePropertyAsync(apvts.state.getChildWithName("Settings"), PluginParameters::NUM_CLIENTS_ID, (int) sources.size());
setTreePropertyAsync(apvts.state.getChildWithName("Settings"), PluginParameters::NUM_CLIENTS_ID, n_clients);

#if JUCE_DEBUG
std::cout << "SourceTree deleted source! N = " << sources.size() << std::endl;
std::cout << "SourceTree deleted client! n_clients = " << n_clients << std::endl;
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
- [x] Change structure of gains to allow addressing them by index, and not needing 4 different enum elements for them
- [x] when changing the source_index of a plugin check if in the sources there is another source with the same index but without a pluginconnection, if there is take it over
- [x] fix aed->xyz conv (when moving a source using mouse, radius is correctly between 0 and 1.73 (when it goes into a corner), but when directly changin aed, it is in the old range)
- [ ] fix xyz->aed conversions:
- [x] fix xyz->aed conversions:
when moving in the xy-pad, x and y are updated seperately. This also triggers two seperate calls to `SphericalSliderBox::updateSphericalCoordinates()`, one with the new x and the old y, and one with the new y and old x. only the second update is used to actually update the values, so when just clicking anywhere on the xy-pad once, it often leads to discontinuities.
**Solution**: either bundle updates together, or only update single coordinates at a time
**Solution**: bundle updates together, or only update single coordinates at a time, or internally keep track of state
- [ ] move cartesian-coordinate-limit from sphericalsliderbox to a global setting

0 comments on commit 54b5d62

Please sign in to comment.