Beginner question regarding clocks

    Nov 02 2011 | 6:08 pm
    Had a look on the forum and I've searched through the manual but cant really find an answer to this.
    I've been trying to get my head around building externals by just looking at small aspects of it at a time-so basically I started off with bangs in and out, ints in and out etc etc.
    Got to looking at clocks and I was wondering if its a bad idea to create them as needed(ie create a clock when a bang is received) rather than in the object creation method? Does this result in loads of clocks running even after they've been called?
    I've tried implementing a couple of different ways of calling "freeobject" after the clocks have been used but this just gives me the "freeobject: 18452218: bad object" error followed most likely by a crash, so I'm just calling it as the cleanup method now.
    So what i have is:
    void timing4_tick(t_timing4 *x)
    	setclock_fdelay(0, x->m_clock, x->intervalForDelay);
    This is called every time the object gets a bang in.
    void makenewclock(t_timing4 *x)
    	x->m_clock = clock_new(x, (method)outputAfterDelay);
    This is how I'm calling clock_new.
    And my cleanup method is:
    void timing4_free_clocks (t_timing4 *x)
    	freeobject((t_object *)x->m_clock);
    Basically I want to be able to create the clocks dynamically so I dont have to have loads defined at the start, and I'm wondering whether this is the way to go about it or whether I'm missing something simple(like does the fact that they're all being called as "m_clock" mean I'm actually just using the same clock multiple times?). I tried using "schedule_delay" but it gives me the same error message as above if its deleted and it doesnt seem to have the same accuracy in timing anyway. Hopefully this makes sense....

    • Nov 02 2011 | 6:19 pm
      Oh I realise that this is an old way of writing the code but the only tutorials that I came across were all written this way so it was only after I'd gotten my head around some of the basics that I noticed the code for the examples with the newer SDK were different.....
    • Nov 03 2011 | 11:37 am
      Well at first i'd be looking to make something like "pipe" yea. As i said I'm basically just trying to get my head around it bit by bit at the moment, so I used a clock to create a "delay" object, with the clock created inside the initialization method, which takes in floats and ints as well as bangs but as I'm sure you realise that means that anytime a new input is received the delayline is cleared so I was trying the above method to get something which is closer to pipe. It definitely works more like i'd like it too(in that the delayline isnt cleared) but i'm just not sure I'm doing it the right way.
      Eventually I'd like to use the technique to create something to control a drum machine which receives midinotes and then varies their outputs by delaying them by different amounts.
      But first I'd just like to have a better understanding of what i'm doing and make something like a pipe object.
      .....hmm seem to have repeated myself a bit there....
    • Nov 03 2011 | 1:45 pm
      Hmm while i think of it, in relation to your suggestion about using a growing array/linked list-how would this solve the problem of needing to give variable names to each of the clocks? Surely this would still require naming all of your clocks at the start of the program or am I missing something?...
    • Nov 04 2011 | 12:51 pm
      Hey, Oh ok sorry, misunderstood you, well I sorted the freeobject crash by only calling freeobject as the cleanup function. But if i'm only using one clock when would I be calling thex->m_clock = clock_new(x, (method)outputAfterDelay); to create the clock? In the documentation it says to create the clock in the instance creation function, but if I do this it just works like "delay" and clears the queue each time. Maybe I'm wrong but it seems like when I've created the clock in the instance creation function anytime I call it after that it forgets any previous calls. Yet if I call it each time that I input a bang/int/float I get separate outputs for each input... I can see a couple of ways I could use arrays/lists to record the incoming data and then call it again on output I just dont get how I'm calling the clock so as my clock_delay calls my output function once for every input.
    • Nov 04 2011 | 5:34 pm
      I have done a pipe-like thing recently, and my solution has been as follows:
      - every instance of my object has an unique id - I create a new clock at every new incoming message. I don't pass my actual object to the clock; instead, I create a new object (it doesn't have to be a t_object) containing the data I want to pass, a pointer to the clock itself, a pointer to the master object and the id of the master object - in the task method, I check if the associated master object is a valid one, and if its id matches my ephemeral object's (never underestimate what can happen to your object's memory after it's freed!). If so, I output the data from the master object's outlet. - eventually I free the clock. This looks like the most dangerous part (are we sure that no-one is going to need it anymore?) but so fare I didn't have any problems...
      hth! aa
    • Nov 04 2011 | 8:01 pm
      Yea I really wasnt expecting it to be so tough.... naive to say the least :) If you could make a prototype that would be amazing!!!
      Thanks for the explanation andrea agostini although I must admit its a bit over my head at the moment... I've got my attempt working in so far as it delays all of the incoming data individually but i still get crashes if i try to delete/cut the object while it still has data to output. Seems that the output function is still being called despite the fact that the clock has been unset and freed... Anyway I'll try see if I can implement something similar to what you've described
    • Nov 06 2011 | 9:54 pm
      Cheers man, I'll take a look at that and let you know how I get on