electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Articles  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links  |  Store
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
Live streaming at radio.electro-music.com

  host / artist show at your time
  Faux Pas Quartet and friends Music From Last Thursday
Please visit the chat
 Forum index » DIY Hardware and Software » Supercollider
Using a loop inside a SynthDef
Post new topic   Reply to topic Moderators: v-un-v
Page 1 of 1 [3 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
nelsen



Joined: Jun 25, 2009
Posts: 2
Location: London, UK

PostPosted: Thu Jun 25, 2009 12:14 am    Post subject: Using a loop inside a SynthDef Reply with quote  Mark this post and the followings unread

I'm trying to create an echo Synthdef which produces a specifiable number of echos of the input. I've tried it like this:

Code:

(
SynthDef(\echo, {
   arg in=0, out=0, period=0.25, feedback=0.5, repeats=4;
   var source, output;
   
   source = SoundIn.ar(in, 1);
   output = source;

   repeats.do({
      arg i;
      var j=i+1;

      output = output + DelayN.ar(source, j*period, j*period, feedback**j);
   });
   
   Out.ar(out, output);
}).send(s);
)


However, I only get one echo of the input signal. If I add,
Code:
repeats = 4;
before
Code:
repeats.do...
it works, although then the number of repeats is fixed.

I guess the problem is that the loop just runs once on creation of the synthdef, but I can't think of a better way of doing this!

Thanks,

John
Back to top
View user's profile Send private message
dewdrop_world



Joined: Aug 28, 2006
Posts: 858
Location: Guangzhou, China
Audio files: 4

PostPosted: Thu Jun 25, 2009 6:14 am    Post subject: Reply with quote  Mark this post and the followings unread

You can't use a synthdef argument to control the number of repeats.

A synthdef is a fixed structure, but you're asking it to change its structure (increase the number of UGens) when you create a new Synth. That's not possible.

The loop runs just once because the server (which runs the synth) does not know ANYTHING about language flow-of-control constructs.

Easiest way is to create a function that generates a synthdef.

Code:
(
f = { |repeats = 4|
   SynthDef(\echo ++ repeats, {
      arg in=0, out=0, period=0.25, feedback=0.5;
      var source, output, delayed;

      source = SoundIn.ar(in, 1);
      output = source;


      repeats.do({
         arg i;
         var j=i+1;

         output = output + DelayN.ar(source, j*period, j*period, feedback**j);
      });

      Out.ar(out, output);
   }).send(s);
};
)

f.value(4);   // creates "echo4"
f.value(3);   // creates "echo3"


Also, this small change will save some memory:

Code:
(
f = { |repeats = 4|
   SynthDef(\echo ++ repeats, {
      arg in=0, out=0, period=0.25, feedback=0.5;
      var source, output;

      source = SoundIn.ar(in, 1);
      output = source;
      delayed = source;

      repeats.do({
         arg i;
         var j=i+1;

         delayed = DelayN.ar(delayed, period, period);
         output = output + (delayed * (feedback**j));
      });

      Out.ar(out, output);
   }).send(s);
};
)


hjh

_________________
ddw online: http://www.dewdrop-world.net
sc3 online: http://supercollider.sourceforge.net
Back to top
View user's profile Send private message Visit poster's website AIM Address
nelsen



Joined: Jun 25, 2009
Posts: 2
Location: London, UK

PostPosted: Thu Jun 25, 2009 3:48 pm    Post subject: Reply with quote  Mark this post and the followings unread

Awesome, thank you!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Moderators: v-un-v
Page 1 of 1 [3 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » Supercollider
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
emSynth

Please support our site. If you click through and buy from
our affiliate partners, we earn a small commission.


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use