rukano

music, technology, and more…

Archive for the ‘code’ tag

SuperCollider on Twitter (or how I learned to love the sc-tweets)

without comments

Just a quick one.
Dan Stowell curated an album made out of compositions that aren’t longer than 140 charactes in SuperCollider code (sc140). The pieces aren’t only great, the code is pretty ingenious. A lot of recursions, syntax shortcuts and tricks to generate a great sound or piece. The album and code are free to download and view, but if you want more of these tweet that didn’t make it to the album,there are lots of code snippets that are also really good. One can find them on twitter by #sc or #supercollider or searching for ‘play{‘ or ‘.play‘. There’s a Yahoo Pipe to facilitate this search, but not all the ‘sc-tweets‘ are there. Another library of sc tweets is the swiki. Great stuff, great sounds, nice code, nice ideas… just digg into it. If you are into SuperCollider, there’s a lot one can learn from these pieces.

  • Twitter
  • Facebook
  • Google Reader
  • Share/Bookmark

Written by rukano

November 2nd, 2009 at 1:09 am

Posted in programming

Tagged with , , , ,

Tricky Random Numbers in a Range

without comments

I’m working on a learn project to know more about Objective C, the Cocoa Framework and just for fun. I think only from the praxis I’ll be able to solve little problems that one does not have in the Theory.

Yesterday was the problem: how can I generate random numbers? and this within a range? well for the C/C++ people this wouldn’t be a problem. In fact one can use the same  functions as in C/C++ but I thought there would be a more ‘cocoay’ version for that, like:

[NSNumber randomIntFrom:(int)a to:(int)b ];

Well, there is not such a thing. However, I knew I was able with a tutorial I made a couple of months ago, that random numbers was possible and with a very easy way, rather than calling random() or rand() an dividing and scaling, etc… I looked at an old ScreenSaver Tutorial and I found the answer there. I still was a little bit ‘disappointed’ there wasn’t like an NSValue or NSNumber that does this job, but well, the ScreenSaver framework has a fix for that…


#import <ScreenSaver/ScreenSaver.h>

SSRandomIntBetween(0, 100);

SSRandomFloatBetween(0.0, 1.0);


Well that was the kind of easy function I was looking for, but I couldn’t expect it to be in the ScreenSaver Framework!

Anyway it’s just a C function call that looks like this:


static __inline__ int SSRandomIntBetween(int a, int b)

{

int range = b – a < 0 ? b – a – 1 : b – a + 1;

int value = (int)(range * ((float) random() / (float) RAND_MAX));

return value == range ? a : a + value;

}


I think I’ll stay with the ScreenSaver solution, even if it isn’t beautiful to call a ScreenSaver function in your app, but well… it’s a lot more understandable and they made a much better and elegant solution than my first try. I guess one could copy also the code in that function and make it your own… but the ScreenSaver Framework has everybody so… why not use it? At least for little projects should do the job.

  • Twitter
  • Facebook
  • Google Reader
  • Share/Bookmark

Written by rukano

October 12th, 2009 at 11:02 am

Posted in programming

Tagged with , , , ,