If you want to do this directly to the buffer then it’s real easy to do if you don’t need to do it while audio is running.

you can read a buffer sample by sample with peek (or poke, i forget) and then use the sample index as the argument to a windowing function (eg triangle: 1 – 2/sampleLength*abs(index-sampleLength/2). multiply the sample by the windowing function and peek it into a new buffer.

If you want to do this dynamically to new buffers, then you might be better off not manipulating actual buffers and instead add a windowing function separately. one solution is to write your windowing function to a buffer. Read your grain with wave~ and use the same ramp driving the wave~ to read the window and *~ them together.

here’s a schematic with a really simple grain scheduler. This would be improved by putting everything after the sel object inside a poly~ so that you don’t interrupt one grain to start another. (also, scheduling grains via control path has given me lots of problems. )

as my samples will vary in size, ideally i’d like the shorter ones to be windowed completely, but for the longer samples i’d prefer if it didn’t window the entire thing, just the start and end points of it.

is there a way to do this by altering the patch below?

– Pasted Max Patch, click to expand. –

Copy all of the following text. Then, in Max, select New From Clipboard.

this is one of those cases where doing it in javascript would be way easier IMHO, but i’ve solved the problem for you with max. (since i have important other work to do, i spent some time doing this for you. i’m logical that way.)

the basic idea is that you have a windowing function, in this case a gaussian/bell curve, and you read through it, pausing at the maximum if your buffer is long enough.

I tried to make all the steps clear inside the patch, but let me know if you have any questions.

the difficulty is that you’re complicating the problem by requiring a conditional response. Whereas before you just had to elongate the triangle to the length of the buffer, now you want to have two different kinds of responses depending on the buffer length.

Rather than giving you a cleaner fish, i’m going to attempt to fashion a pole for you. Basically, you just break the problem down into parts and then create the parts you need and fit them together.

i’ll scheme out what you want your patch to do, and hopefully you’ll how my solution works a little better, or find your own solution.

1) determine if the buffer length L is less than the minimum M
2.a) if yes, use old windowing procedure
if no:
2) create a ramp up of fixed length M/2
3) followed by a flat region of length L-M
4) followed by a ramp down of length M/2

You could implement steps 2-4 in several ways. one would be to write three window segments (ramp up, flat, ramp down) and trigger them at the appropriate times (which is to say, route your index to the right function). Another would be to multiply your triangle function so that it goes up above one and then clip it at one to get a trapezoid. with a teensy bit of algebra, you could make it so that it hits one at the right place coming up and down. Since you already wrote the triangle expr i’m guessing you can do this without too much trouble.

The way i implemented steps 2-4 is as follows: use a windowing function of length M W(x). my window is nice and smooth and fancy-pants, but a triangle works just as well. for xM/2 it ramps down, W(M/2) is exactly 1. Now, you’re reading through the buffer from positions 1 to L, let the position you’re reading be called i. When reading the buffer from indices 1 to M/2 (i

just think clearly and break the problem down into parts you can solve.

i’m sorry this is so mathematical, i’m in the middle of writing a paper on mathematical ecology.

hey, not sure what you mean by windowing a buffer~, storing a window into a buffer? or taking the audio stored in a buffer~, windowing it, and then writing it back into the buffer~?….

in any case, just in case it helps, this is a pseudo-hanning window(a hanning window with variable attack-release defined in milliseconds). Doesn’t require a buffer~ and is pretty simple. I came up with this because I wanted short grains to be windowed nicely, while longer beat-cutting could also be windowed(for clicks) without reducing the impact of a percussive hit, if you like this, let me know as I’ve just coded an external that does it all with one single object and I can pass this on to you. but if I’m completely misunderstanding what you want, i wish you the best of luck in finding the right solution for you:

– Pasted Max Patch, click to expand. –

Copy all of the following text. Then, in Max, select New From Clipboard.