How to make an input range slider span an octave(s) when triggering oscillator - web-audio-api

I don't understand how to programmatically hitch the formula 440×2n/12 to the input range slider to set the frequency of an oscillator in 12 step western scale increments. I set the range slider from 1 to 12.
This is as far as I've made it..blah.
<input id='input' type='range' min='1' max = '12' step=''></input>
oscillator.frequency.value = ($('#input').val() //(math stuff);

It's far, far easier to do this using "detune". Set the frequency value to whatever base note you want (A-440 is a fine choice) and then also set detune in cents - each note in the chromatic scale is +/- 100 cents.

Answer
<input id='input' type='range' min='100' max = '1200' step='100'></input>
oscillator.frequency.value = 440 ;
oscillator.detune.value = $('#input').val();

Related

offset in atr in tradingview - pine script

Need to have offset in ATR function in pine script
Background: The indicator script below is based on the hypothesis that this period's range will be mostly within [last period high + atr(14)] and [last period low - atr(14)]. I want to sell the high call option and low put option and enjoy the premium at the end of the period (week, month).
I have created a pine script that will calculate this period range based on [last period high + atr(14)] and [last period low - atr(14)].
However, because atr(14) applies to current period as well, it plots the dots that change with the current price.
I need to have an atr(14) days till the last period and not considering this current period. Could you please advise how to achieve that.
//#version=3
study(title="High and Low Levels", shorttitle="HL Levels", overlay = true)
Width = input(2, minval=1)
SelectPeriod = input("W", defval="W", type=string)
LookBack = input(1, minval=1)
xHigh = high[LookBack]
xHigh := xHigh + (atr(14))
xLow = low[LookBack] - atr(14)
vS1 = xHigh
vR1 = xLow
plot(vS1, color=#ff0000, title="S1", style = circles, linewidth = Width)
plot(vR1, color=#009600, title="R1", style = circles, linewidth = Width)
Expected: the dots plotted should be plotted based on last period high + last period atr(14) and last period low - last period atr(14)
Actual: the dots plotted based on last week high + atr(14) till the current period and last week low - atr(14) till the current period. This is changing the dots based on the current price movement.
Maybe, I got it wrong, but I think what you want is to take the previous value of atr(14). So it looks like that:
xLow = low[LookBack] - atr(14)[1]
I think, you've got my idea.
This should help....
plot(vS1[1], color=#ff0000, title="S1", style = circles, linewidth = Width)
plot(vR1[1], color=#009600, title="R1", style = circles, linewidth = Width)

Calculate hue angle range for red colours

I'm trying to correctly calculate colour hue angle range. Given an input default hue say 120 and a threshold value of 20 the range is between 100 - 140 (yes, I know - complex math).
Now in the application, when filtering an image, I can check if a given pixel falls into that range:
let inputHue = 120
let threshold = 20
let minHue = inputHue - threshold // 100
let maxHue = inputHue + threshold // 140
if (pixelHue > minHue && pixelHue < maxHue) {
// do something
}
Now the problem is with red colours range where the most saturated red colour is at 0/360 on the colour wheel. Given an input hue of 10 the minHue is now -10 (with a threshold of 20) and maxHue is 30. Because of that negative value for minHue the condition fails:
let pixelHue = 355 // this falls into a valid red range I want to get
let minHue = -10
let maxHue = 30
if (pixelHue > minHue && pixelHue < maxHue) {
// do something
}
Does anyone know how to tackle this problem of a colour wheel? I'm trying to develop a general solution that would work for any given input hue (not only red colours).
Thanks in advance.
Hues should be considered modulo 360. Hence the range for red is 0-30 AND 350-360.
Now your value of 355 passes because (355>350 AND 355<30) OR (350>30 AND (355>350 OR 355<30)).
The critical bit here starts after the first OR. In normal arithmetic, you check against lower and upper bound, and you have to pass both tests. But in modular arithmetic, if the lower bound is higher than the upper bound, the range wraps around 0 and you only need to pass one of the two both tests.
To recap: (hue>min AND hue<max) OR (min>max AND (hue>min OR hue<max))
A possible approach: Imagine both hue values as points on a circle, and compare the "shorter" angular distance between these points with the threshold:
let diff = abs(pixelHue - inputHue)
if min(diff, 360 - diff) <= threshold {
// do something
}

How to round off x-ticklabels to the nearest 50

I need to round off the X-ticklabels in an excel chart to the nearest 50. The charts are created in VBA, the data series is in a worksheet so I would be happy to use a solution in either. I have written the following function that rounds to the nearest 50:
Function RoundTo50(number As Double) As Double
RoundTo50 = WorksheetFunction.Round(number * 2, -2) / 2
End Function
I have applied it to the min and max x limits of the chart and it works for them, but I can't work out how to apply this to all the ticklabels in between. I thought of applying it to all the data before plotting but that would alter the plot which I don't want to do. I would prefer for the ticklabels to be slightly misaligned due the rounding.
Can you do this with a formatting string? Or any other way?
Thanks
So the answer is as follows (thanks to Sam Ward for the comment that pushed me in the right direction):
Use my RoundTo50() function to round of the min and max limits to the nearest 50. Calculate what the interval should be for a reasonable amount of grid lines, in my case 12. Round this interval to the nearest 50.
Function RoundTo50(number As Double) As Double
RoundTo50 = WorksheetFunction.Round(number * 2, -2) / 2
End Function
and
With Sheets("Report").ChartObjects.Add(...)
.Chart.Axes(xlCategory).MinimumScale = RoundTo50(Sheets(sheetName).Range("M4"))
.Chart.Axes(xlCategory).MaximumScale = RoundTo50(Sheets(sheetName).Range("M124"))
.Chart.Axes(xlCategory).MajorUnit = RoundTo50((.Chart.Axes(xlCategory).MaximumScale - .Chart.Axes(xlCategory).MinimumScale) / 12)
.Chart.Axes(xlCategory).MinorUnit = .Chart.Axes(xlCategory).MajorUnit / 3
I would still be very much interested in being able to do this with a formatting string though as I have a secondary axis in percent and the grid lines from rounding to 50 are slightly offset with the tick marks of the secondary axis. With a formatting string they would be perfectly aligned (because they would be slightly in the wrong place but I would prefer that).

Using CMTime without rounding?

I'm writing an application that uses AVFoundation and CMTime. I have logged the values of my CMTime instances created using CMTimeMake(). This value seems to be rounded to the nearest integer. I need a CMTime instance with a precise value, without rounding.
I have seen the rounding constants in the CMTime reference:
enum {
kCMTimeRoundingMethod_RoundHalfAwayFromZero = 1,
kCMTimeRoundingMethod_RoundTowardZero = 2,
kCMTimeRoundingMethod_RoundAwayFromZero = 3,
kCMTimeRoundingMethod_QuickTime = 4,
kCMTimeRoundingMethod_RoundTowardPositiveInfinity = 5,
kCMTimeRoundingMethod_RoundTowardNegativeInfinity = 6,
kCMTimeRoundingMethod_Default = kCMTimeRoundingMethod_RoundHalfAwayFromZero
};
There aren't any example of how I can control which of these strategies is applied to a CMTime instance? Or, if this isn't the right way, how can I extract a precise value from a CMTime instance?
Edit:
I have found and tested CMTIME_HAS_BEEN_ROUNDED(). I passed my CMTime instance to this function and it returns No (indicating that the value has not been rounded). So why am I losing precision?
If you read the documentation for CMTime you will see that it stores time as a rational number using a numerator and denominator. The numerator is int64_t while the denominator is int32_t.
The numerator specifies how many "ticks" have passed, and the denominator specifies how many "ticks" per second.
So 0.5 seconds can be stored as:
100/200: 100 ticks, 200 ticks per second
500/1000: 500 ticks, 1000 ticks per second
8/16, 8 ticks, 16 ticks per second
And so forth. The way you have done it, using
CMTimeMake([[Array objectAtIndex:i]floatValue], 1);
Is saying "there is one tick per second", and since the numerator is an integer, the float value is truncated so only the 1 is stored. Therefore you are specifying time as: 1/1, one tick has elapsed, one tick per second, so you are actually storing exactly 1 second.
To fix this, it depends on what you want to do and whether you care about the timescale. Apple recommends a timescale of 600, but if you don't care, you can just do this:
CMTimeMake([[Array objectAtIndex:i]floatValue]*1000, 1000);
That sets timescale to 1000, so 1000 ticks per second, so one millisecond per tick. It also converts the time in seconds to milliseconds. Note that it truncates the 4th digit, so if you had 1.2345 you just get 1.234 not 1.235. If that matters to you, see roundf.
CMTimeMake([[Array objectAtIndex:i]floatValue]*1000, 1000);
This helped me to get exact time in milliseconds. Cheers.

iPhone - AVAudioPlayer - convert decibel level into percent

I like to update an existing iPhone application which is using AudioQueue for playing audio files. The levels (peakPowerForChannel, averagePowerForChannel) were linear form 0.0f to 1.0f.
Now I like to use the simpler class AVAudioPlayer which works fine, the only issue is that the levels which are now in decibel, not linear from -120.0f to 0.0f.
Has anyone a formula to convert it back to the linear values between 0.0f and 1.0f?
Thanks
Tom
Several Apple examples use the following formula to convert the decibels into a linear range (from 0.0 to 1.0):
double percentage = pow (10, (0.05 * power));
where power is the value you get from one of the various level meter methods or functions, such as AVAudioPlayer's averagePowerForChannel:
Math behind the Linear and Logarithmic value conversion:
1. Linear to Decibel (logarithmic):
decibelValue = 20.0f * log10(linearValue)
Note: log is base 10
Suppose the linear value in the form of percentage range from [ 0 (min vol) to 100 (max vol)] then the decibelValue for half of the volume (50%) is
decibelValue = 20.0f * log10(50.0f/100.0f) = -6 dB
Full volume:
decibelValue = 20.0f * log10(100.0f/100.0f) = 0 dB
Complete mute:
decibelValue = 20.0f * log10(0/100.0f) = -infinity
2. Decibel(logarithmic) to Linear:
LinearValue = pow(10.0f, decibelValue/20.0f)
Apple uses a lookup table in their SpeakHere sample that converts from dB to a linear value displayed on a level meter.
I moulded their calculation in a small routine; see here.