Sync buffer recording to zero cross

Charley Lanusse's icon

I'm trying to record real-time audio into a buffer starting at a zero cross. I'm using a zerox~ and edge~ to trigger the record~ but the buffer contents never sync up. I'm must be doing something silly.

Any help is much appreciated!

Max Patch
Copy patch and select New From Clipboard in Max.

-Charley

Roth's icon

The problem you are having is because you are using edge~ which converts your click from zerox~ to a bang causing you to leave the signal domain making you lose sample-rate accuracy.

Depending on how you will use this, it might be easier just to recording into a buffer, then use peek~ to search for the first zero crossing, then that sample location as your start point for playback.

I know it isn't exactly the same as what you are looking for, but check out this thread: https://cycling74.com/forums/buffer-snap-to-peak-in-sample

If you look in the help file for the mxj I posted to that thread, you will see a subpatcher that shows the algorithm for that mxj implemented in Max.

Max Patch
Copy patch and select New From Clipboard in Max.

More to the point of your question, here is an example of how to trigger recording from a zerocrossing with sample accuracy. It might look a little strange, but works.

Charley Lanusse's icon

Thanks very much Roth! I see now. I need to stay in the signal domain.

I'm going to try to write a gen~ path to encapsulate zero cross detection and writing to the buffer all in one. I'll report back when I make some progress.

Roth's icon

Yeah, I'd go for doing that in gen~ probably myself—just figured MSP would provide a more general example. Off the top of my head, it seems like you might be able to make it more elegant with gen~ getting rid of the need for sah~ and some of the other conditional logic because you can do real ifs with signals in gen~.

Charley Lanusse's icon

I was just wondering about if statements in genExpr. I couldn't find a reference to it in the Max docs. Would you show me an example of syntax?

Is there a repository of genExpr example code somewhere? I think I get the main syntax but I'm still not 100%.

Roth's icon

Turns out I may have said something misleading—real response to come later when I'm done working.

Roth's icon

Sorry to get your hopes up, but it looks like support for if-statements might not be ready yet. I was talking about branching/control-flow stuff with some of the devs at the expo and got the impression that it was already implemented in GenExpr. From trying it out myself, it seems like if-statements (and perhaps other parts of codebox) are a work in progress. Normally, bad GenExpr syntax gives a parsing error in the Max window. The following GenExpr code in a codebox generates no errors, but does not function correctly.

if (in > 0) {
    out = 1;
}
else {
    out = 0;
}

I think the GenExpr documentation is still in progress. You should check out this thread: https://cycling74.com/forums/beta-gen-documentation and stuff in the Max6/examples folder. Those places and just messing around is where I've picked up my gen~ knowledge (plus some presentations from the Expo whose files are in the Toolbox on this site I think).