~hadronized/splines#2: 
Use `Result` instead of `Option` for `sample` and `clamp_sample`.

Hi! Thank you for pointing out the issues tab :)

I’m open to suggestions. What is your usecase?

Alright, so my usecase is, that I'm currently trying to "parse" audio for a music visualizer and for that, I'd like to use interpolated splines, which your crate provides, to be able to pick various different points without requiring them from the audio source itself.

I'll use the terminology of your Key for better understanding.

So the ts from my spline are going to be normalized in the range [0; 1]. Here are the keys from my logs:

[Key { t: 0.0, value: 0.06075588, interpolation: CatmullRom }, Key { t: 0.008264462, value: 0.06586848, interpolation: CatmullRom }, Key { t: 0.016528925, value: 0.04756607, interpolation: CatmullRom }, Key { t: 0.024793386, value: 0.08188911, interpolation: CatmullRom }, Key { t: 0.03305785, value: 0.026175793, interpolation: CatmullRom }, Key { t: 0.041322313, value: 0.05716713, interpolation: CatmullRom }, Key { t: 0.049586773, value: 0.059848532, interpolation: CatmullRom }, Key { t: 0.057851236, value: 0.031837847, interpolation: CatmullRom }, Key { t: 0.0661157, value: 0.11103129, interpolation: CatmullRom }, Key { t: 0.07438016, value: 0.11997002, interpolation: CatmullRom }, Key { t: 0.08264463, value: 0.09973231, interpolation: CatmullRom }, Key { t: 0.090909086, value: 0.14274381, interpolation: CatmullRom }, Key { t: 0.099173546, value: 0.13841155, interpolation: CatmullRom }, Key { t: 0.10743801, value: 0.07309231, interpolation: CatmullRom }, Key { t: 0.11570247, value: 0.03759352, interpolation: CatmullRom }, Key { t: 0.12396694, value: 0.05463978, interpolation: CatmullRom }, Key { t: 0.1322314, value: 0.012476415, interpolation: CatmullRom }, Key { t: 0.14049587, value: 0.018026648, interpolation: CatmullRom }, Key { t: 0.14876032, value: 0.010010115, interpolation: CatmullRom }, Key { t: 0.15702479, value: 0.00894008, interpolation: CatmullRom }, Key { t: 0.16528925, value: 0.0026659863, interpolation: CatmullRom }, Key { t: 0.1735537, value: 0.004315419, interpolation: CatmullRom }, Key { t: 0.18181817, value: 0.0016437625, interpolation: CatmullRom }, Key { t: 0.19008264, value: 0.0011013547, interpolation: CatmullRom }, Key { t: 0.19834709, value: 0.0019513046, interpolation: CatmullRom }, Key { t: 0.20661156, value: 0.00212988, interpolation: CatmullRom }, Key { t: 0.21487603, value: 0.00183247, interpolation: CatmullRom }, Key { t: 0.2231405, value: 0.0008817702, interpolation: CatmullRom }, Key { t: 0.23140495, value: 0.0009197538, interpolation: CatmullRom }, Key { t: 0.23966941, value: 0.0011211867, interpolation: CatmullRom }, Key { t: 0.24793388, value: 0.0006786048, interpolation: CatmullRom }, Key { t: 0.25619835, value: 0.0007918067, interpolation: CatmullRom }, Key { t: 0.2644628, value: 0.0005620776, interpolation: CatmullRom }, Key { t: 0.27272725, value: 0.0041896375, interpolation: CatmullRom }, Key { t: 0.28099173, value: 0.0024089124, interpolation: CatmullRom }, Key { t: 0.2892562, value: 0.0033652298, interpolation: CatmullRom }, Key { t: 0.29752064, value: 0.012252028, interpolation: CatmullRom }, Key { t: 0.30578512, value: 0.0052324603, interpolation: CatmullRom }, Key { t: 0.31404957, value: 0.013929391, interpolation: CatmullRom }, Key { t: 0.32231402, value: 0.004984354, interpolation: CatmullRom }, Key { t: 0.3305785, value: 0.0027780219, interpolation: CatmullRom }, Key { t: 0.33884296, value: 0.0019151116, interpolation: CatmullRom }, Key { t: 0.3471074, value: 0.0015733909, interpolation: CatmullRom }, Key { t: 0.3553719, value: 0.00081391033, interpolation: CatmullRom }, Key { t: 0.36363634, value: 0.0019578284, interpolation: CatmullRom }, Key { t: 0.3719008, value: 0.001843274, interpolation: CatmullRom }, Key { t: 0.38016528, value: 0.0010189618, interpolation: CatmullRom }, Key { t: 0.38842973, value: 0.00091309845, interpolation: CatmullRom }, Key { t: 0.39669418, value: 0.00083318935, interpolation: CatmullRom }, Key { t: 0.40495867, value: 0.001192713, interpolation: CatmullRom }, Key { t: 0.41322312, value: 0.0025753272, interpolation: CatmullRom }, Key { t: 0.4214876, value: 0.002041352, interpolation: CatmullRom }, Key { t: 0.42975205, value: 0.00042717633, interpolation: CatmullRom }, Key { t: 0.4380165, value: 0.0037246493, interpolation: CatmullRom }, Key { t: 0.446281, value: 0.0047486345, interpolation: CatmullRom }, Key { t: 0.45454544, value: 0.009577193, interpolation: CatmullRom }, Key { t: 0.4628099, value: 0.009767146, interpolation: CatmullRom }, Key { t: 0.47107437, value: 0.012774024, interpolation: CatmullRom }, Key { t: 0.47933882, value: 0.024172515, interpolation: CatmullRom }, Key { t: 0.48760328, value: 0.011293551, interpolation: CatmullRom }, Key { t: 0.49586776, value: 0.006724675, interpolation: CatmullRom }, Key { t: 0.5041322, value: 0.0041684746, interpolation: CatmullRom }, Key { t: 0.5123967, value: 0.0077908025, interpolation: CatmullRom }, Key { t: 0.5206611, value: 0.0074437605, interpolation: CatmullRom }, Key { t: 0.5289256, value: 0.0062143207, interpolation: CatmullRom }, Key { t: 0.5371901, value: 0.006081434, interpolation: CatmullRom }, Key { t: 0.5454545, value: 0.006245958, interpolation: CatmullRom }, Key { t: 0.553719, value: 0.004050541, interpolation: CatmullRom }, Key { t: 0.56198347, value: 0.0021534124, interpolation: CatmullRom }, Key { t: 0.5702479, value: 0.004648339, interpolation: CatmullRom }, Key { t: 0.5785124, value: 0.0038979126, interpolation: CatmullRom }, Key { t: 0.58677685, value: 0.001959871, interpolation: CatmullRom }, Key { t: 0.5950413, value: 0.0007072607, interpolation: CatmullRom }, Key { t: 0.60330576, value: 0.0005526712, interpolation: CatmullRom }, Key { t: 0.61157024, value: 0.00027209986, interpolation: CatmullRom }, Key { t: 0.61983466, value: 0.000106428684, interpolation: CatmullRom }, Key { t: 0.62809914, value: 8.6867345e-5, interpolation: CatmullRom }, Key { t: 0.6363636, value: 7.922182e-5, interpolation: CatmullRom }, Key { t: 0.64462805, value: 8.300973e-5, interpolation: CatmullRom }, Key { t: 0.65289253, value: 7.205546e-5, interpolation: CatmullRom }, Key { t: 0.661157, value: 5.9085392e-5, interpolation: CatmullRom }, Key { t: 0.66942143, value: 5.5553304e-5, interpolation:CatmullRom }, Key { t: 0.6776859, value: 5.0454244e-5, interpolation: CatmullRom }, Key { t: 0.6859504, value: 4.678896e-5, interpolation: CatmullRom }, Key { t: 0.6942148, value: 4.3661123e-5, interpolation: CatmullRom }, Key { t: 0.7024793, value: 4.092223e-5, interpolation: CatmullRom }, Key { t: 0.7107438, value: 3.8273996e-5, interpolation: CatmullRom }, Key { t: 0.7190082, value: 3.5916604e-5, interpolation: CatmullRom }, Key { t: 0.7272727, value: 3.362823e-5, interpolation: CatmullRom }, Key { t: 0.7355372, value: 3.1714062e-5, interpolation: CatmullRom }, Key { t: 0.7438016, value: 2.9693094e-5, interpolation: CatmullRom }, Key { t: 0.7520661, value: 2.797851e-5, interpolation: CatmullRom }, Key { t: 0.76033056, value: 2.6392563e-5, interpolation: CatmullRom }, Key { t: 0.768595, value: 2.4738427e-5, interpolation: CatmullRom }, Key { t: 0.77685946, value: 2.338637e-5, interpolation: CatmullRom }, Key { t: 0.78512394, value: 2.2113529e-5, interpolation: CatmullRom }, Key { t: 0.79338837, value: 2.078673e-5, interpolation: CatmullRom }, Key { t: 0.80165285, value: 1.9670977e-5, interpolation: CatmullRom }, Key { t: 0.80991733, value: 1.856318e-5, interpolation: CatmullRom }, Key { t: 0.8181818, value: 1.7612321e-5, interpolation: CatmullRom }, Key { t: 0.82644624, value: 1.6621218e-5, interpolation: CatmullRom }, Key { t: 0.8347107, value: 1.5726384e-5, interpolation: CatmullRom }, Key { t: 0.8429752, value: 1.49427115e-5, interpolation: CatmullRom }, Key { t: 0.8512396, value: 1.4160626e-5, interpolation: CatmullRom }, Key { t: 0.8595041, value: 1.3443428e-5, interpolation: CatmullRom }, Key { t: 0.8677686, value: 1.2782206e-5,interpolation: CatmullRom }, Key { t: 0.876033, value: 1.2149562e-5, interpolation: CatmullRom }, Key { t: 0.8842975, value: 1.1584595e-5, interpolation: CatmullRom }, Key { t: 0.892562, value: 1.1059502e-5, interpolation: CatmullRom }, Key { t: 0.9008264, value: 1.05717045e-5, interpolation: CatmullRom }, Key { t: 0.9090909, value: 1.0119243e-5, interpolation: CatmullRom }, Key { t: 0.91735536, value: 9.713197e-6, interpolation: CatmullRom }, Key { t: 0.9256198, value: 9.349267e-6, interpolation: CatmullRom }, Key { t: 0.93388426, value: 9.003883e-6, interpolation: CatmullRom }, Key { t: 0.94214875, value: 8.718625e-6, interpolation: CatmullRom }, Key { t: 0.95041317, value: 8.452729e-6, interpolation: CatmullRom }, Key { t: 0.95867765, value: 8.250685e-6, interpolation: CatmullRom }, Key { t: 0.96694213, value: 8.071291e-6, interpolation: CatmullRom }, Key { t: 0.97520655, value: 7.948441e-6, interpolation: CatmullRom }, Key { t: 0.98347104, value: 7.877437e-6, interpolation: CatmullRom }, Key { t: 0.9917355, value: 7.875323e-6, interpolation: CatmullRom }]

The following image shows, what my music visualizer currently shows: https://filebrowser.tornaxo7.de/share/b2svaNRm As you can see, there's a little gap. This is there because I'm defaulting to 0 if clamp_sample returns None. I'm a bit confused why it's there and some feedback of the function would be nice to debug the issue for example: "There are not enough keys to compute the sample at t = <bla>." or something like that. So returning something like a Result<V, &'static str> or Result<V, SampleError> would be nice in my opinion :)

Status
REPORTED
Submitter
~tornax
Assigned to
No-one
Submitted
a month ago
Updated
a month ago
Labels
No labels applied.

~hadronized a month ago

I'm a bit confused why it's there and some feedback of the function would be nice to debug the issue for example: "There are not enough keys to compute the sample at t = <bla>." or something like that. So returning something like a Result<V, &'static str> or Result<V, SampleError> would be nice in my opinion :)

Hi again,

So, I don’t think it would be too hard to introduce this new feature. I think that we could introduce a new method for this usecase, so that people not caring about the actual reason for the value not being there do not have a price to pay for the error handling part (which might require cloning behind the scene, at the last the key — for most people it should be something like f64 but in theory it could be anything.

I’ll try to allocate some time to work on that soon so that I can release a new minor version.

Thanks again for reporting!

Register here or Log in to comment, or comment via email.