PURE DATA forum~

...that deal with pure data

You are not logged in.

#1 2007-09-14 12:38:38

hardoff
Member

what is an abstraction, and why use $0, $1 etc ??

this is a quick explanation of the role of abstractions in pd, and why it is useful to use $0, $1, etc.. inside these abstractions.




ok.   say you want to create a very simple sample player.   you'd do something like this.




[0]   <-- gui bang object #1                                 [table my-sample]
|
[openpanel]
|
[read -resize $1 my-sample(
|
[soundfiler]


[0]  <-- gui bang object #2 
|
[tabplay~ my-sample]
|\
|  \
|    \
|      \
[dac~]



so, you'd click the gui bang #1 to load your sample, and then click bang #2 to play it.  that should be pretty straight forward, yeah?


ok...so what if you want to make a copy of that patch so that you can run 2 sample players at once? 

one option is to copy the above patch, paste it, and then just change 'my-sample' to something else.  for example:  my-sample2

if you just want 2 sample players, then that is probably the easiest way to go.


but what if you want 4, or 10 players all running at the same time? it's a real pain in the arse to keep cutting and pasting and changing table names. ..that's where abstractions come in handy. 


so what you do is:  save your sample player as a patch.  lets call it 'sample-playa.pd'

for the moment, save this patch to your desktop, and close the patch.


now open a new (untitled) patch in pd, and try to create an object called [sample-playa]

any luck?  probably not, right?  this is because pd cannot find your patch to use it as an abstraction.  you have to put the patch somewhere for pd to find it.  there are a few ways to do this, but here is the one that doesn't involve changing pd's -path flagst:

find a spot on your disk and create a new folder, and then put your sample-playa.pd file inside that folder.   

now, go back to pd, open a new untitled patch, and just put any old object or message in it.  then, save that patch as 'multi-playa.pd' inside the same folder as your sample-playa.pd patch.

close multi-playa.pd , and then re-open it from the new folder. 


now, delete everything in multi-playa.pd  and try to create an object called 'sample-playa'

because multi-playa.pd and sample-playa.pd are in the same folder, pd can find sample-playa.pd and use it as an abstraction.  an abstraction acts just like a normal pd object.


ok..  so right click on your [sample-playa] abstraction, and choose 'open'.  you will see your original code. 


[0]   <-- gui bang object #1                                 [table my-sample]
|
[openpanel]
|
[read -resize $1 my-sample(
|
[soundfiler]


[0]  <-- gui bang object #2 
|
[tabplay~ my-sample]
|\
|  \
|    \
|      \
[dac~]


we're going to modify it, so that the gui bang objects will be 'graphed onto' the parent patch..  and now we will also add the $0 bits....


like so:


[0]     <-- bang #1.  right click for properties and set the send as $0-open               
[0]      <-- bang #2.  right click for properties and set the send as $0-play



[r  $0-open]                                                         [table $0-my-sample]
|
[openpanel]
|
|              [loadbang]
|              |
|              [f $0]
|              |
[pack s f]
|
[read -resize $1 $2-my-sample(
|
[soundfiler]


[r  $0-play] 
|
[tabplay~ $0-my-sample]
|\
|  \
|    \
|      \
[dac~]




now, right click somewhere on the blank canvas of this patch, and select 'properties'

there is a box titled 'graph on parent'... check this box and close properties.

there should now be a little red rectangle on your canvas.  put the two gui bangs inside this box. 

save this patch now, and close the window so you are back at your multi-playa.pd 'parent' patch.

there should now be an object called [sample-playa] with 2 bangs graphed onto it.  try to open a soundfile and play it using the bangs.   it should work fine.

now, make another object also called [sample-playa] and load a different soundfile.  now you have 2 versions of the same abstraction initiated in a parent patch.  you can make 4 or 5 or 500 objects called [sample-playa] and they will all work independantly.




so.. here's an explanation of what we just did:

anytime you make an object from an abstraction, it gets given a value $0.  this is usually a number higher than 1000.  for example, 1047

if you make another object from an abstraction, it will get a different value for $0.. for example 1088

no matter how many times you turn abstractions into objects, each one will get a different random $0 value. ..that's why we can use $0 in abstractions to make send/receive pairs unique to that instance of the abstraction.  we can also use $0 to make unique names for tables.  if your abstraction contains a table called [$0-mytable], then for one instance of the abstraction it might become [1066-mytable] and for another instance [1092-mytable] and so on.



hopefully that explains $0.





$1, $2 ..etc is a bit easier to understand.



make a new (untitled) patch and put the following inside:


[bang(
|
[f $1]
|
[print]


[bang(
|
[symbol $2]
|
[print]



save this patch in your folder as [dollars.pd]

open another patch from that same folder and create an object called [dollars 5 bananas] 


now, if you right click on that [dollars 5 bananas] object and open it, you can click on the bangs, and watch what shows up in pd's console. 

it will print out '5' if you click on the first bang, and 'symbol bananas' if you click on the second bang.



in this way you can add 'creation arguments' to your abstraction.  instead of a [bang( you can use [loadbang], and these arguments will be automatically passed on when an object is created from your abstraction. 

this is useful in making something like an ADSR abstraction, to set the initial values.


inside your abstraction you will have the objects:  [f $1]  [f $2]  [f $3] [f $4]  to pass the values for attack, decay, sustain and release.

when you make the abstraction into an object you can just type the name of the abstraction, followed by values for $1, $2...etc 

eg.    [my-adsr 50 200 20 500]

Offline

 

#2 2007-09-14 14:57:48

obiwannabe
Administrator

Re: what is an abstraction, and why use $0, $1 etc ??

Awesome! This reads really well.  Final example to explain $1,$2 is very elegant.Now we must sticky it. NIce work!


Use the Source.

Offline

 

#3 2007-09-14 15:24:54

hardoff
Member

Re: what is an abstraction, and why use $0, $1 etc ??

man.  you really do know jedi mind tricks! 

i was just thinking today...what this forum needs is 'stickies'


edit...oh..  it does have stickies.

Last edited by hardoff (2007-09-14 15:36:02)

Offline

 

#4 2007-09-14 16:36:02

nestor
Member

Re: what is an abstraction, and why use $0, $1 etc ??

Yeah, this is one of the cloistered secrets of pure data, finally revealed.  I like your writeup and it will help a lot of people out.  this stuff is is especially helpful when transitioning from "beginner" to "intermediate." 

Just to throw my pennies in.   I like to use $1, $2 etc as you said as well; to set the initial values for a nested delay, or envelopeparameters, or table length.  However I also find them invaluable in communication between sub-patches.  I like to break my patches us into modular components, as simple as possible.  Each component only does one thing, and all the control data is standardized and sent about using            [s $1-whatever].  One of my "performance" patches might look like:

[loader a]      [loader z]

[fazor a]        [badrummer z]

[looper a]      [looper z]

Offline

 

#5 2007-09-14 22:26:13

domien
Member

Re: what is an abstraction, and why use $0, $1 etc ??

Maybe something extra stuff that needs to be explained to make it more complete.

- the use of $ variables inside messages, because $0 can't be used in messages to reference $0 prefixed objects. This would of course need an explanation about the use of $ within messages.

In time i've noticed that the reason to prefix objects with $0 has not always so much to do with creating abstractions, but is to avoid duplicate names within different patches altogether. Once you start having a considerable amount of patches, chances are great you'll end up giving (gui) objects, send/returns... the same name. E.g. you often want to name a slider that controls a lowpass filter "lop" or "lp" or "lopFreq" or whatever. So even when you don't intend to create an abstraction out of something, it's often useful to use $0.

Last edited by domien (2007-09-15 14:34:30)

Offline

 

#6 2007-09-14 23:11:27

obiwannabe
Administrator

Re: what is an abstraction, and why use $0, $1 etc ??

This dual use of $args is very confusing to beginners. I would say separate any mention of message substitution $args completely from abstraction parameter use of $args and just make a brief mention that they have two uses. Frank has patiently explained this over and over yet it still comes up as a FAQ. There was some talk on the pd-list of changing this, but it will never happen for reasons of backward compatibility. I think in Max you use # for parameters, is that right?

On tutorial/howto subject: How about contributing that one to the pdwiki Hardoff? I think some of my tuts got tidied up and put in. Instead of long tutorials I might try contributing some shorter FAQ/WTF? I think your example above is a good model. One subject and a succinct focussed couple of hundred words to give just the facts. 

btw: If you chaps want a temporary home to host user written snippets like this I'd be happy to put them on OB1B and we can keep a running link list here until they are finalised into the wiki.

A really useful format is LaTeX, basically

\title{what is it}
\author{who wrote this stuff}
\begin{document}
\section{Section heading}
blah blah blah stuff
  \subsection{subsection heading}
   blah blah subsection of stuff
\end{document}

and these can be converted to HTML nicely


Use the Source.

Offline

 

#7 2007-09-15 02:42:52

brettb
Member

Re: what is an abstraction, and why use $0, $1 etc ??

This is great. thanks for taking time to explain all this. just one question to Nestor - when using $1, $2 etc at the beginning of an object name, such as $1-whatever, does the $1 in this case actually represent anything other than '1'? In other words, is naming the objects $1-whatever and $2-whatever any different from naming them whatever1 and whatever2?

Offline

 

#8 2007-09-15 06:07:32

obiwannabe
Administrator

Re: what is an abstraction, and why use $0, $1 etc ??

"In other words, is naming the objects $1-whatever and $2-whatever any different from naming them whatever1 and whatever2?"

Absolutley - 1-whatever will not work. $0-whatever works because $0 is special, it gets replaced bu a unique per patch/abstraction number. $n is the nth parameter to an abstraction.

sory jumping in, that was addressed to you Nestor, elaborate for brett with an example maybe, i need my bed.

Last edited by obiwannabe (2007-09-15 06:09:07)


Use the Source.

Offline

 

#9 2007-09-15 09:10:39

hardoff
Member

Re: what is an abstraction, and why use $0, $1 etc ??

brett, the answer to your question is at the end of what i wrote.

if u make an abstraction called [dollars]

then, for [dollars 5 bananas]   $1 = 5 and $2 = bananas

so $1-whatever = 5-whatever , and $2-whatever = bananas-whatever.

Offline

 

#10 2007-09-15 14:53:56

bz
Member

Re: what is an abstraction, and why use $0, $1 etc ??

Great!  I second that, I didn't understand this stuff well in the beginning. Coming from a MAX/Reaktor background I didn't get it.  But now I get it and like it more!  Thanks for the concise clarification.

Offline

 

#11 2007-09-17 20:32:21

don brown
Member

Re: what is an abstraction, and why use $0, $1 etc ??

thanks for this, I have been trying to figure this out with a modified sample/looper from the examples e.g: how to have 1 to 100 of these independently.
However, I must being doing something wrong in this example, because after I graph and add the $0....I can't load a sample, when I try I get "0-my-sample: no such table"
as far as I can tell I have followed your text to a tee, but I must be overlooking something simple.
thanks,
db

Offline

 

#12 2007-09-17 20:59:05

domien
Member

Re: what is an abstraction, and why use $0, $1 etc ??

I guess that's what i meant in my previous comment. When you try to use $0 objects within messages, you should "push" the $0 inside. For instance, assuming you have a table called $0-sampler1, the following won't work :

[bang]
|
[set $0-sampler1(
|
[tabread4~]

instead, you should do this :

[bang]
|
[pack $0]
|
[set $1-sampler1(
|
[tabread4~]

This way you push the $0 value (which is actually internally replaced by a number) inside the message. Remember that the $ variables inside messages are something completely different than the $ for objects parameters.

Last edited by domien (2007-09-17 21:00:35)

Offline

 

#13 2007-09-17 21:13:40

don brown
Member

Re: what is an abstraction, and why use $0, $1 etc ??

can you relate this to hardoffs sample, re; why no such table response when I am following his directions?
thanks..
db

Offline

 

#14 2007-09-17 21:46:00

hardoff
Member

Re: what is an abstraction, and why use $0, $1 etc ??

if you follow from my example like this, it will definitely work.....




[0]     <-- bang #1.  right click for properties and set the send as $0-open               
[0]      <-- bang #2.  right click for properties and set the send as $0-play



[r  $0-open]                              [table $0-my-sample]  <-- don't forget to make this!!
|
[openpanel]
|
|              [loadbang]
|              |
|              [f $0]
|              |
[pack s f]    <----  in this case, s = the filename from your sample, and f = $0
|
[read -resize $1 $2-my-sample(   <-- ie, read -resize (filename) $0-my-sample
|
[soundfiler]


[r  $0-play]
|
[tabplay~ $0-my-sample]
|\
|  \
|    \
|      \
[dac~]



if you still have trouble post your patch and we'll have a look at it and tell u where the glitch is.

Offline

 

#15 2007-09-18 03:56:50

don brown
Member

Re: what is an abstraction, and why use $0, $1 etc ??

it works now, I don't know what the problem is/was. I have been spending the last few hours trying to get a solid installation, so maybe this was part of it.
thanks.
db

Offline

 

#16 2007-10-16 07:24:40

brettb
Member

Re: what is an abstraction, and why use $0, $1 etc ??

before finding out about all this $1- and $0- stuff I'd already put together an abstraction for playing a drum sound inside a drum machine. because I knew I wanted several copies of this within the drum machine I assigned "d1" at the beginning of each send or receive object in the drum abstraction, and then did find/replace in a text editor to change it to "d2", "d3", "d4" etc to make several different copies.

Now that I realise you can just have one copy and use $1 and creation arguments to generate further copies, I have tried to use find/replace to replace "d1" with "$1-". Sure enough the replacement operation works, but when I try to open the amended abstraction again in pd, it just opens a blank canvas?!

In the text editor everything looks exactly the same except for the d1 has changed to $1- so I just can't fathom what the problem could be.

Anyone have any suggestions? 

I've attached the 2 versions for reference: 'drum1' is the original with "d1", and 'drum' is the amended version with "$1-" (except you won't be able to see anything with 'drum' unless you open it in text editor!)


Attachments:
Attachment Icon drum1.pd, Size: 6,539 bytes, Downloads: 19

Offline

 

#17 2007-10-16 07:26:35

brettb
Member

Re: what is an abstraction, and why use $0, $1 etc ??

...the other abstraction


Attachments:
Attachment Icon drum.pd, Size: 6,601 bytes, Downloads: 23

Offline

 

#18 2007-10-16 11:14:29

hardoff
Member

Re: what is an abstraction, and why use $0, $1 etc ??

you need a forwards slash before the $ sign if you go the textedit route

so $1-whatwhat -> \$1-whatwhat

attached fixed version seems to work fine:


Attachments:
Attachment Icon drum-fixed.pd, Size: 6,663 bytes, Downloads: 34

Offline

 

#19 2007-10-16 11:40:54

brettb
Member

Re: what is an abstraction, and why use $0, $1 etc ??

Thanks mate! If you got paid for all the help you give out, you wouldn't be hard off!!

the slash is specifically for $ signs is it? i presume to distinguish it from also being used in messages?

Offline

 

#20 2007-10-16 12:31:14

hardoff
Member

Re: what is an abstraction, and why use $0, $1 etc ??

i think it's because the $ symbol has another meaning in the software that pd runs on top of. 

i'm more than happy to help anyone here if i can.  certainly a much better use of my time than visiting music boards that are over-run with idiots being trolls to seek attention.

Offline

 

#21 2007-10-17 08:10:52

brettb
Member

Re: what is an abstraction, and why use $0, $1 etc ??

Is there any way to use $1 to select files for loading, for example:-

[atom]  (number box)
|
[pack $1]
|
[read -resize /Samples/DM/Boss_DR55/DR55_$1.wav drum1array(
|
[soundfiler]

This doesn't seem to work because $1 is not at the beginning of the file name, or because it is embedded in the filename.

I'm just thinking that, if only we could change a small part of the filename remotely like this, then we'd be able to load samples easily just by scrolling a number box or moving a slider (as long as the sample files are all named with numbers).

Offline

 

#22 2007-10-17 09:37:21

domien
Member

Re: what is an abstraction, and why use $0, $1 etc ??

It's better to use the [makefilename] object for this :

[atom]   (number box)
|
[makefilename /Samples/DM/Boss_DR55/DR55_%d.wav]
|
[read -resize $1 drum1array(
|
[soundfiler]

Offline

 

#23 2007-10-17 09:48:26

brettb
Member

Re: what is an abstraction, and why use $0, $1 etc ??

Great! Thanks alot Domien

Offline

 

#24 2010-10-09 16:10:25

konglom
Member

Re: what is an abstraction, and why use $0, $1 etc ??

hey guys,

thanks for that little tutorial.

i tried the example in a little sample looper, but i only get that error:

0-sample: no such table

can someone look inside my patch and tell me what is going wrong?


Attachments:
Attachment Icon looper_$$.pd, Size: 1,384 bytes, Downloads: 7

Offline

 

#25 2010-10-09 19:09:25

Simon2
Member

Re: what is an abstraction, and why use $0, $1 etc ??

you need to put [f $0] in an object box, not a message box [ ( . $0 messages don't work in message boxes. Run the output of [f $0] into the right inlet (the float inlet) of [pack s f] - then everything works.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson


pd.webring info