Why does this event pattern not produce octave jumps? - supercollider

I'm trying to learn how to use event patterns. I typed out the following, expecting a sequence which 'jumps down' an octave each time:
p = Pbind(*[
instrument: \mySynth,
midinote: Pseq([108, 96, 84, 72, 60, 48, 36, 24, 12], inf),
dur: 0.2
]);
SynthDef(
"mySynth",
{
|midinote, gate = 1|
var stereofreq = [midinote, midinote];
var audio = Pulse.ar(stereofreq, 0.5, mul: 0.8);
var env = Linen.kr(gate, 0.01, 1, 0.1, doneAction: 2);
OffsetOut.ar(0, audio * env);
}
).add;
p.play;
I certainly get a descending sequence, but the interval is not an octave. Am I missing some detail of the midinote key?

Yes you are missing something: the data in the midinote key is magically transformed into Hertz values in the freq key when the pattern is played. So when you write your synthdef, you shouldn't use midinote, instead use freq.
It might seem like suspicious magic, but think about it this way: you can write a SynthDef once, using freq, and thereafter you are free to use midinote or freq or degree in your patterns, and they'll all be converted, without you having to rewrite your SynthDef to use the different-named control.
To understand more about what is going on, this page is very helpful: Pattern Guide 07: Value Conversions

Related

Mapping an image to a quadrilateral in p5.js without using WEBGL

I'm trying to map a custom image to a 4-sided quad with a non-rectangular shape in p5.js. I know this is possible(and quite easy) using a WEBGL canvas and the texture() command, but I'm trying not to use WEBGL in my code simply because I don't like the WEBGL coding environment; and it seems kind of overkill to swap to a 3D canvas just for this(I don't need any other 3D objects in my program).
I'm looking for an in-built solution, or a custom library with something of this matter in it. I've tried both to some degree and have turned up empty-handed; which is odd because this seems like a relatively simple thing to ask for.
I'm also kind of stupid and I don't understand HTML in general. I use p5.js because of this, but I'm not against any kind of help: all is appreciated.
I've tried using a mixture of shearX() and shearY() but those would only work for an orthographic view; I'm going for perspective.
I have looked into brute-forcing it by literally going through each pixel in the quad and calculating the pixel color it should have based on the image, but haven't had this work yet. It also seems hecka laggy; and I'm looking for this quad to render in real-time.
If you don't want to use WebGL (or p5.js) there are other js libraries that can apply perspective warp via canvas, such as perspective.js.
Here's their example:
// ctx (CanvasRenderingContext2D): The 2D context of a HTML5 canvas element.
// image (Image): The image to transform.
var p = new Perspective(ctx, image);
p.draw([
[30, 30], // Top-left [x, y]
[image.width - 50, 50], // Top-right [x, y]
[image.width - 70, image.height - 30], // bottom-right [x, y]
[10, image.height] // bottom-left [x, y]
]);
This may be bit overkill, but warpPerspective() in opencv.js also support a similar transform.
Here's their example:
let src = cv.imread('canvasInput');
let dst = new cv.Mat();
let dsize = new cv.Size(src.rows, src.cols);
// (data32F[0], data32F[1]) is the first point
// (data32F[2], data32F[3]) is the sescond point
// (data32F[4], data32F[5]) is the third point
// (data32F[6], data32F[7]) is the fourth point
let srcTri = cv.matFromArray(4, 1, cv.CV_32FC2, [56, 65, 368, 52, 28, 387, 389, 390]);
let dstTri = cv.matFromArray(4, 1, cv.CV_32FC2, [0, 0, 300, 0, 0, 300, 300, 300]);
let M = cv.getPerspectiveTransform(srcTri, dstTri);
// You can try more different parameters
cv.warpPerspective(src, dst, M, dsize, cv.INTER_LINEAR, cv.BORDER_CONSTANT, new cv.Scalar());
cv.imshow('canvasOutput', dst);
src.delete(); dst.delete(); M.delete(); srcTri.delete(); dstTri.delete();

what is output format of Utils? (using chartjs in vanillajs, not react/angular)

I'm trying to load data for a bubbles chart from datafiles, the examples use a chartjs function to generate data.
Working through some of the examples, want to load csv/json files via d3.js as will be using data files from an api and front end not in react/angular.
examples so far.
https://github.com/aspiringguru/chartjs_3.7.1_vanilla
I can't see the output format of Utils.* used in the examples.
expecting will need x,y,radius for each point. just need to work out what format chartjs expects the data in.
https://www.chartjs.org/docs/next/samples/other-charts/bubble.html
uses
data: Utils.bubbles(NUMBER_CFG)
dataset.data = Utils.bubbles({count: chart.data.labels.length, rmin: 5, rmax: 15, min: 0, max: 100});
had a look at
https://github.com/chartjs/Chart.js/blob/master/docs/scripts/utils.js
and I can't understand the output generated from
function bubbles(config)
As can be read here in the documentation. The datastructure for bubble charts consists out of an array containing objects where each object has an x key for the x value, an y key for the y value and a r key for the radius.
{
// X Value
x: number,
// Y Value
y: number,
// Bubble radius in pixels (not scaled).
r: number
}
const data = [{x: 5, y: 7, r: 9}, {x: 6, y: 3, r: 1}];

What are color conversion codes ending in FULL for?

Several color conversion codes in OpenCV 3.2 have two versions, one ending in _FULL, and one not, e.g.:
cv::COLOR_BGR2HSV_FULL = 66,
cv::COLOR_RGB2HSV_FULL = 67,
cv::COLOR_BGR2HLS_FULL = 68,
cv::COLOR_RGB2HLS_FULL = 69,
cv::COLOR_HSV2BGR_FULL = 70,
cv::COLOR_HSV2RGB_FULL = 71,
cv::COLOR_HLS2BGR_FULL = 72,
cv::COLOR_HLS2RGB_FULL = 73,
What is the difference between the two? I couldn't find it in the documentation. Specifically, when I use a color conversion code like in:
converted_img = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
Thanks!
The result will be different in H dimension.
According to my observation, the options with "_FULL" suffix has a larger H range than the ones without it. What's more, the L and S dimension should be the same.
The hue range in Open CV is [0,179], the saturation and intensity range from [0,255], as per Official Docs. So one has to normalise the hue to the range [0,255]. The options with "_FULL" have the hue range defined to [0-255].

Why does ArcTo sometimes not update the current position

Background
I'm working a legacy MFC application which uses GDI draw its content.
I need to draw rounded rectangles where each corner has a (potentially) different radius.
This means that I can no longer use RoundRect and have to roll my own using ArcTo.
I'm using SetWindowExtEx, SetWindowOrgEx, SetViewportExtEx and SetViewportOrgExt to implement zooming.
This works fine in most situations.
Problem
On certain zoom levels, my code fails to construct a proper path of the outline of the roundrect.
The following screenshots is of my RoundRect code used to create a path, then used to clip a bigger rectangle (to get an idea of it's shape).
The clipping region created by this path is sometimes missing a corner, clips everything (two missing corners?) or clips nothing.
My guess is that due to rounding errors, the arcs are too small, and is skipped alltogether by GDI.
I find this hard to believe though since it is working correctly for smaller zoom factors than the ones pictured here.
Working correctly:
Missing a corner:
The Code
I have tried to reduce the code needed to reproduce it and have ended up with the following. Note that the number in the screenshots is the value of zoomFactor, the only variable.
You should be able to paste this code into the OnPaint function of a newly created Win32 application project and manually declare zoomFactor a constant.
SetMapMode(hdc, MM_ISOTROPIC);
SetWindowOrgEx(hdc, 0, 40, nullptr);
SetWindowExtEx(hdc, 8000, 6000, nullptr);
SetViewportOrgEx(hdc, 16, 56, nullptr);
SetViewportExtEx(hdc, 16 + (396)*zoomFactor/1000,
48 + (279)*zoomFactor/1000, nullptr);
BeginPath(hdc);
MoveToEx(hdc, 70, 1250, nullptr);
ArcTo(hdc,
50, 1250, 90, 1290,
70, 1250,
50, 1270);
ArcTo(hdc,
50, 2311, 90, 2351,
50, 2331,
70, 2351);
ArcTo(hdc,
1068, 2311, 1108, 2351,
1088, 2351,
1108, 2331);
ArcTo(hdc,
1068, 1250, 1108, 1290,
1108, 1270,
1088, 1250);
CloseFigure(hdc);
EndPath(hdc);
SelectClipPath(hdc, RGN_AND);
HBRUSH br = CreateSolidBrush(RGB(255,0,255));
const RECT r = {0, 0, 8000, 6000};
FillRect(hdc, &r, br);
Here is a simpler bit of code to illustrate the problem:
const int r = 20;
MoveToEx(hdc, 200, 100, 0);
BOOL b = ArcTo(hdc,
100 + 2 * r, 100,
100, 100 + 2 * r,
100 + r, 100,
100, 100 + r);
POINT p;
GetCurrentPositionEx(hdc, &p);
This draws a single corner of radius r. This works fine for non-zero values of r and the position p is correctly updated to match the end of the arc: (100, 100+r), give or take a pixel.
However, when r is zero ArcTo returns TRUE but the position is not updated: p contains the starting position of (200,100).
The documentation states that "If no error occurs, the current position is set to the ending point of the arc." The function returned TRUE indicating success so the position should have been updated.
In my view this a bug. The function should return FALSE because the rectangle is empty so there is no arc and thus no well-defined endpoint. However, it would be more useful in practice if the function returned TRUE and updated the current position to match the final coordinate pair in the parameter list. But it does neither of these things. EDIT: An even better implementation in your case would be to calculate the arc end points in logical coordinates before converting to device coordinates, but GDI in general doesn't work like this.
The problem occurs in your code because your coordinate transformation collapses the second arc's rectangle to an empty rectangle when the zoom is 266. You can see this yourself by adding the following to your code to transform the coordinates of the second arc:
POINT points[4] = {{50,2311},{90,2351},{50,2331},{70,2351}};
LPtoDP(hdc, points, 4);
With the zoom set to 266 the points are transformed to (17,90), (17,91), (17,91), (17,91) so the rectangle has no width and is empty. And you hit the ArcTo bug.
I guess it works for smaller zooms when the rounding happens to put the x-coordinates into adjacent integers rather than the same integer.
A simple fix would be to create a MyArcTo function that replaces the arc with a LineTo when it is too small to be visible.

Pitch modification in praat

I want to modify the pitch at two different parts of a wav file. To do that , i have the information of starting time and ending time from the corresponding textgrid file of the wav file. Is it possible to modify the pitch at two parts.
You can use a Manipulation object to make any changes you want to the original sound's pitch.
# Original sound made of three consecutive notes
snd[1] = Create Sound as pure tone: "A", 1, 0, 0.3, 44100, 220, 0.2, 0.01, 0.01
snd[2] = Create Sound as pure tone: "B", 1, 0, 0.3, 44100, 247, 0.2, 0.01, 0.01
snd[3] = Create Sound as pure tone: "C", 1, 0, 0.3, 44100, 277, 0.2, 0.01, 0.01
selectObject(snd[1], snd[2], snd[3])
sound = Concatenate
Rename: "original"
removeObject(snd[1], snd[2], snd[3])
selectObject(sound)
Play
# We will invert the pitch, so that the notes play in the opposite direction
manipulation = To Manipulation: 0.01, 200, 300
pitchtier = Extract pitch tier
# We copy it because we want to modify it, not create one from scratch
# and we want to be able to read the values of the original from somewhere
original = Copy: "old"
points = Get number of points
# This for loop looks at the values of the original pitch tier and writes them
# onto the new pitch tier
for p to points
selectObject(original)
f = Get value at index: points - p + 1
t = Get time from index: p
# If you uncomment the if block, the changes will only affect the first and last
# quarter of the sound
# if t < 0.25 or t > 0.75
selectObject(pitchtier)
Remove point: p
Add point: t, f
# endif
endfor
# We replace the pitch tier
selectObject(pitchtier, manipulation)
Replace pitch tier
# Resynthesize
selectObject(manipulation)
new_sound = Get resynthesis (overlap-add)
# And clean up
removeObject(original, pitchtier, manipulation)
selectObject(new_sound)
Rename: "modified"
Play
You change the pitch tier by adding points at different times with different pitch values (in Hertz), and when you do the resynthesis Praat will modify the original values so they match the ones you specified.
In your case, you can use the time values from the TextGrid to know when the modified PitchTier points need to be added and leave the rest alone. You can also manipulate duration like this.
In the example, the script changes the value of each of the points in the original pitch tier with the value of the points in the inverted order, so that the first point will have the value of the last one. The if block inside the for is one way of limiting those changes to a subset of the pitch tier, but how you do this will depend on the sort of changes you are trying to make.

Resources