Skip to content

Commit

Permalink
Multiple changes:
Browse files Browse the repository at this point in the history
- Adjust to latest reaper-rs volume API changes
- Playtime: Improve volume adjustment in lots of places
  • Loading branch information
helgoboss committed Feb 17, 2024
1 parent e9e4c55 commit ad12a14
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 98 deletions.
4 changes: 1 addition & 3 deletions base/src/peak_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ pub fn peaks_should_be_hidden(track: &Track) -> bool {
///
/// This takes VU mode / channel count intricacies into account. It returns peaks even if another
/// track is soloed! See [`peaks_should_be_hidden`].
pub fn get_track_peaks(
track: MediaTrack,
) -> impl Iterator<Item = ReaperVolumeValue> + ExactSizeIterator<Item = ReaperVolumeValue> {
pub fn get_track_peaks(track: MediaTrack) -> impl ExactSizeIterator<Item = ReaperVolumeValue> {
let reaper = Reaper::get().medium_reaper();
let vu_mode =
unsafe { reaper.get_media_track_info_value(track, TrackAttributeKey::VuMode) as i32 };
Expand Down
11 changes: 6 additions & 5 deletions main/src/domain/targets/playtime_slot_volume_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ mod playtime_impl {
base::ClipMatrixEvent,
rt::{ClipChangeEvent, QualifiedClipChangeEvent},
};
use reaper_high::Volume;
use reaper_high::SliderVolume;
use reaper_medium::Db;
use std::borrow::Cow;

Expand Down Expand Up @@ -105,8 +105,9 @@ mod playtime_impl {
value: ControlValue,
context: MappingControlContext,
) -> Result<HitResponse, &'static str> {
let volume = Volume::try_from_soft_normalized_value(value.to_unit_value()?.get())
.unwrap_or_default();
let volume =
SliderVolume::try_from_normalized_slider_value(value.to_unit_value()?.get())
.unwrap_or_default();
let db = volume.db();
let api_db = playtime_api::persistence::Db::new(db.get())?;
Backbone::get()
Expand Down Expand Up @@ -162,11 +163,11 @@ mod playtime_impl {
}

impl PlaytimeSlotVolumeTarget {
fn volume(&self, context: ControlContext) -> Option<Volume> {
fn volume(&self, context: ControlContext) -> Option<SliderVolume> {
Backbone::get()
.with_clip_matrix(context.instance(), |matrix| {
let db = matrix.find_slot(self.slot_coordinates)?.volume().ok()?;
Some(Volume::from_db(Db::new(db.get())))
Some(SliderVolume::from_db(Db::new(db.get())))
})
.ok()?
}
Expand Down
13 changes: 8 additions & 5 deletions main/src/domain/targets/route_volume_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::domain::{
UnresolvedReaperTargetDef, DEFAULT_TARGET,
};
use helgoboss_learn::{AbsoluteValue, ControlType, ControlValue, NumericValue, Target, UnitValue};
use reaper_high::{ChangeEvent, Project, ReaperError, Track, TrackRoute, Volume};
use reaper_high::{ChangeEvent, Project, ReaperError, SliderVolume, Track, TrackRoute};
use reaper_medium::EditMode;
use std::borrow::Cow;

Expand Down Expand Up @@ -75,9 +75,12 @@ impl RealearnTarget for RouteVolumeTarget {
value: ControlValue,
_: MappingControlContext,
) -> Result<HitResponse, &'static str> {
let volume = Volume::try_from_soft_normalized_value(value.to_unit_value()?.get());
let volume = SliderVolume::try_from_normalized_slider_value(value.to_unit_value()?.get());
self.route
.set_volume(volume.unwrap_or(Volume::MIN), EditMode::NormalTweak)
.set_volume(
volume.unwrap_or(SliderVolume::MIN).reaper_value(),
EditMode::NormalTweak,
)
.map_err(|_| "couldn't set route volume")?;
Ok(HitResponse::processed_with_effect())
}
Expand Down Expand Up @@ -138,8 +141,8 @@ impl RealearnTarget for RouteVolumeTarget {
}

impl RouteVolumeTarget {
fn volume(&self) -> Result<Volume, ReaperError> {
self.route.volume()
fn volume(&self) -> Result<SliderVolume, ReaperError> {
Ok(SliderVolume::from_reaper_value(self.route.volume()?))
}
}

Expand Down
8 changes: 4 additions & 4 deletions main/src/domain/targets/track_peak_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::domain::{
};
use base::peak_util;
use helgoboss_learn::{AbsoluteValue, ControlType, NumericValue, Target, UnitValue};
use reaper_high::{Project, Track, Volume};
use reaper_high::{Project, SliderVolume, Track};
use reaper_medium::ReaperVolumeValue;
use std::borrow::Cow;

Expand Down Expand Up @@ -60,9 +60,9 @@ impl<'a> Target<'a> for TrackPeakTarget {
}

impl TrackPeakTarget {
fn peak(&self) -> Option<Volume> {
fn peak(&self) -> Option<SliderVolume> {
if peak_util::peaks_should_be_hidden(&self.track) {
return Some(Volume::MIN);
return Some(SliderVolume::MIN);
}
let peaks = peak_util::get_track_peaks(self.track.raw());
let channel_count = peaks.len();
Expand All @@ -72,7 +72,7 @@ impl TrackPeakTarget {
let sum: f64 = peaks.map(|v| v.get()).sum();
let avg = sum / channel_count as f64;
let vol = ReaperVolumeValue::new(avg);
Some(Volume::from_reaper_value(vol))
Some(SliderVolume::from_reaper_value(vol))
}
}

Expand Down
12 changes: 6 additions & 6 deletions main/src/domain/targets/track_volume_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::domain::{
TrackGangBehavior, UnresolvedReaperTargetDef, DEFAULT_TARGET,
};
use helgoboss_learn::{AbsoluteValue, ControlType, ControlValue, NumericValue, Target, UnitValue};
use reaper_high::{ChangeEvent, Project, Track, Volume};
use reaper_high::{ChangeEvent, Project, SliderVolume, Track};
use std::borrow::Cow;

#[derive(Debug)]
Expand Down Expand Up @@ -81,14 +81,14 @@ impl RealearnTarget for TrackVolumeTarget {
value: ControlValue,
_: MappingControlContext,
) -> Result<HitResponse, &'static str> {
let volume = Volume::try_from_soft_normalized_value(value.to_unit_value()?.get());
let volume = SliderVolume::try_from_normalized_slider_value(value.to_unit_value()?.get());
with_gang_behavior(
self.track.project(),
self.gang_behavior,
&TRACK_VOLUME_TARGET,
|gang_behavior, grouping_behavior| {
self.track.set_volume(
volume.unwrap_or(Volume::MIN),
volume.unwrap_or(SliderVolume::MIN).reaper_value(),
gang_behavior,
grouping_behavior,
);
Expand Down Expand Up @@ -121,7 +121,7 @@ impl RealearnTarget for TrackVolumeTarget {
(
true,
Some(AbsoluteValue::Continuous(volume_unit_value(
Volume::from_reaper_value(e.new_value),
SliderVolume::from_reaper_value(e.new_value),
))),
)
}
Expand All @@ -143,8 +143,8 @@ impl RealearnTarget for TrackVolumeTarget {
}

impl TrackVolumeTarget {
fn volume(&self) -> Volume {
self.track.volume()
fn volume(&self) -> SliderVolume {
SliderVolume::from_reaper_value(self.track.volume())
}
}

Expand Down
23 changes: 14 additions & 9 deletions main/src/domain/ui_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use helgoboss_learn::{
};
use helgoboss_midi::{RawShortMessage, ShortMessage};
use itertools::Itertools;
use reaper_high::{Reaper, Volume};
use reaper_high::{Reaper, SliderVolume};
use reaper_medium::Db;
use rosc::{OscMessage, OscPacket};
use std::convert::TryInto;
Expand Down Expand Up @@ -43,18 +43,22 @@ pub fn parse_from_double_percentage(text: &str) -> Result<UnitValue, &'static st
(doble_unit_value / 2.0).try_into()
}

/// Parses the given string as a dB value up to 12 dB.
pub fn parse_value_from_db(text: &str) -> Result<UnitValue, &'static str> {
let decimal: f64 = text.parse().map_err(|_| "not a decimal value")?;
let db: Db = decimal.try_into().map_err(|_| "not in dB range")?;
Volume::from_db(db).soft_normalized_value().try_into()
SliderVolume::from_db(db)
.normalized_slider_value()
.try_into()
}

pub fn format_value_as_db_without_unit(value: UnitValue) -> String {
let volume = Volume::try_from_soft_normalized_value(value.get()).unwrap_or(Volume::MIN);
let volume =
SliderVolume::try_from_normalized_slider_value(value.get()).unwrap_or(SliderVolume::MIN);
format_volume_as_db_without_unit(volume)
}

pub fn format_volume_as_db_without_unit(volume: Volume) -> String {
pub fn format_volume_as_db_without_unit(volume: SliderVolume) -> String {
let db = volume.db();
if db == Db::MINUS_INF {
"-inf".to_string()
Expand All @@ -65,14 +69,15 @@ pub fn format_volume_as_db_without_unit(volume: Volume) -> String {

#[allow(unused)]
pub fn db_unit_value(volume: Db) -> UnitValue {
volume_unit_value(Volume::from_db(volume))
volume_unit_value(SliderVolume::from_db(volume))
}

pub fn volume_unit_value(volume: Volume) -> UnitValue {
/// Returns the given volume as unit value, clamping to 1.0 if the volume is higher than 12 dB.
pub fn volume_unit_value(volume: SliderVolume) -> UnitValue {
// The soft-normalized value can be > 1.0, e.g. when we have a volume of 12 dB and then
// lower the volume fader limit to a lower value. In that case we just report the
// highest possible value ... not much else we can do.
UnitValue::new_clamped(volume.soft_normalized_value())
UnitValue::new_clamped(volume.normalized_slider_value())
}

pub fn convert_bool_to_unit_value(on: bool) -> UnitValue {
Expand All @@ -84,8 +89,8 @@ pub fn convert_bool_to_unit_value(on: bool) -> UnitValue {
}

pub fn format_value_as_db(value: UnitValue) -> String {
Volume::try_from_soft_normalized_value(value.get())
.unwrap_or(Volume::MIN)
SliderVolume::try_from_normalized_slider_value(value.get())
.unwrap_or(SliderVolume::MIN)
.to_string()
}

Expand Down
2 changes: 1 addition & 1 deletion main/src/infrastructure/plugin/backbone_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use crate::infrastructure::plugin::{
};
use crate::infrastructure::server::services::Services;
use crate::infrastructure::ui::instance_panel::InstancePanel;
use crate::infrastructure::ui::util::{open_child_panel, open_child_panel_dyn};
use crate::infrastructure::ui::util::open_child_panel;
use crate::infrastructure::ui::welcome_panel::WelcomePanel;
use anyhow::bail;
use base::hash_util::NonCryptoHashSet;
Expand Down
7 changes: 4 additions & 3 deletions main/src/infrastructure/proto/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ mod playtime_impl {
Laziness, Timeline,
};
use reaper_high::{
AvailablePanValue, ChangeEvent, Guid, OrCurrentProject, PanExt, Project, Track, Volume,
AvailablePanValue, ChangeEvent, Guid, OrCurrentProject, PanExt, Project, Track,
};
use reaper_medium::Db;
use std::collections::HashMap;

impl ProtoHub {
Expand Down Expand Up @@ -556,7 +557,7 @@ mod playtime_impl {
for event in events {
let update: Option<R> = match event {
ChangeEvent::TrackVolumeChanged(e) => {
let db = Volume::from_reaper_value(e.new_value).db();
let db = e.new_value.to_db_ex(Db::MINUS_INF);
if e.track.is_master_track() {
Some(R::Matrix(occasional_matrix_update::Update::volume(db)))
} else {
Expand Down Expand Up @@ -741,7 +742,7 @@ mod playtime_impl {
return vec![];
}
peak_util::get_track_peaks(track.raw())
.map(|vol| Volume::from_reaper_value(vol).db().get())
.map(|vol| vol.to_db_ex(Db::MINUS_INF).get())
.collect()
}
}
5 changes: 3 additions & 2 deletions main/src/infrastructure/proto/initial_playtime_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::infrastructure::proto::{
use base::hash_util::NonCryptoHashMap;
use playtime_clip_engine::base::{Matrix, PlaytimeTrackInputProps};
use reaper_high::{Guid, OrCurrentProject, Track};
use reaper_medium::Db;
use std::iter;

pub fn create_initial_matrix_updates(matrix: Option<&Matrix>) -> Vec<OccasionalMatrixUpdate> {
Expand All @@ -24,7 +25,7 @@ pub fn create_initial_matrix_updates(matrix: Option<&Matrix>) -> Vec<OccasionalM
let master_track = project.master_track().expect("project gone");
let updates = [
Update::MatrixExists(true),
Update::volume(master_track.volume().db()),
Update::volume(master_track.volume().to_db_ex(Db::MINUS_INF)),
Update::pan(master_track.pan().reaper_value()),
Update::mute(master_track.is_muted()),
Update::tempo(matrix.tempo()),
Expand Down Expand Up @@ -77,7 +78,7 @@ pub fn create_initial_track_updates(
Update::mute(track.is_muted()),
Update::solo(track.is_solo()),
Update::selected(track.is_selected()),
Update::volume(track.volume().db()),
Update::volume(track.volume().to_db_ex(Db::MINUS_INF)),
Update::pan(track.pan().reaper_value()),
]
.into_iter()
Expand Down
6 changes: 3 additions & 3 deletions main/src/infrastructure/proto/playtime_request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use helgoboss_learn::UnitValue;
use playtime_clip_engine::{
base::ClipAddress, base::Matrix, rt::ColumnPlaySlotOptions, ClipEngine,
};
use reaper_high::{GroupingBehavior, Guid, OrCurrentProject, Pan, Reaper, Track, Volume};
use reaper_high::{GroupingBehavior, Guid, OrCurrentProject, Pan, Reaper, Track};
use reaper_medium::{Bpm, Db, GangBehavior, ReaperPanValue, SoloMode};
use tonic::{Response, Status};

Expand Down Expand Up @@ -457,7 +457,7 @@ impl PlaytimeProtoRequestHandler {
self.handle_matrix_command(&req.matrix_id, |matrix| {
let project = matrix.permanent_project().or_current_project();
project.master_track()?.set_volume(
Volume::from_db(db),
db.to_reaper_volume_value(),
GangBehavior::DenyGang,
GroupingBehavior::PreventGrouping,
);
Expand All @@ -483,7 +483,7 @@ impl PlaytimeProtoRequestHandler {
let db = Db::try_from(req.db).map_err(|e| Status::invalid_argument(e.as_ref()))?;
self.handle_track_command(&req.track_address, |_matrix, track| {
track.set_volume(
Volume::from_db(db),
db.to_reaper_volume_value(),
GangBehavior::DenyGang,
GroupingBehavior::PreventGrouping,
);
Expand Down
Loading

0 comments on commit ad12a14

Please sign in to comment.