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
  Rob Adventures in Sound
Please visit the chat
 Forum index » DIY Hardware and Software » ChucK programming language
A SndBuf query that has had me tearing out clumps of hair
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [7 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Tue May 15, 2007 2:28 pm    Post subject: A SndBuf query that has had me tearing out clumps of hair Reply with quote  Mark this post and the followings unread

Hi guys,

I was wondering how to do the following but cant get my head around it. I've tried all the different ctrl loops and that hasnt worked. Maybe someone can shed some light on this for me?

Here it goes..

I'm currently trying to play back from a Buffer (SndBuf) but I only want to play a certain number of samples even though the duration may change. How would I go about doing this?

E.g.

Code:


SndBuf Buffer1 => Gain Gain => dac;
"Beat1(165).wav" => Buffer1.read;

3:second => now;

(after the Buffer1.pos has reached say a 1000 I would like it to mute or stop playing back but still have the time passing.)



The problem is, I cant figure out how to do something mid durartion and I know I could do this explicitly a sample at a time and then change the gain to 0 after 1000 but I wish to work in seconds and find a way of updating the gain at the point when Buffer1.pos >= 1000, which would be during those 3 seconds.

Many thanks for your help with this head-scratcher.

Rhys
Back to top
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Wed May 16, 2007 3:02 am    Post subject: Reply with quote  Mark this post and the followings unread

Hi Dr!

Let me get this straight. The buffer needs to stop once it reaches the 1000th sample but that might take a arbitrary amount of time because playback-speed might not be 1?

this would be a obvious way;

Code:
SndBuf Buffer1 => Gain Gain => dac;
"Beat1(165).wav" => Buffer1.read;

fun void stopper()
    {
    while ( Buffer1.pos() < 1000 ) samp => now;
    0 => Buffer1.gain;
    }

//make our "stopper" run paralel to whatever else happens
spork ~ stopper();

3::second => now;
//do something else


It's quite cpu intensive though because we'll need to run that check every sample.

Far more eficient and a bit smarter would be;

Code:
SndBuf Buffer1 => Gain Gain => dac;
"Beat1(165).wav" => Buffer1.read;

//calulate how long it takes the buffer to play 1000 samples
1000::samp / Buffer1.rate() => dur foo;

//wait for that amount of time
foo => now;

0 => Buffer1.gain();

//now let the remainder of time pass
(3::second - foo )=> now;
//do something else


Much more clever and far cheaper. If many other things need to happen at the same time you could also make a sporked function out of that method. Yet another way of doing it would be using a envelope (like in a sampler), doing it that way the note would be cut off without needing any extra code but of cource it would in turn need a envelope Ugen which is likely more expensive.

Is that what you meant?

_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Wed May 16, 2007 3:07 am    Post subject: Reply with quote  Mark this post and the followings unread

Yes!

I was just about to post that I have solved it with this code:

Code:


fun void cutter()
{
   while (CurrentTime <DurationOfOneBar> time later;
   0 => int y;
   while (now < later)
   {
      if (y <CrotchetB1> Buffer1.gain;
         // <<<"GAIN 1">>>;
      }
      if (y > CrotchetB1)
      {
         0 => Buffer1.gain;
         // <<<"GAIN 0">>>;
      }   
      1::samp => now;
      y + 1 => y;   
   }
   0 => Buffer1.pos;
   CurrentTime + CutTime => CurrentTime;
   }   
}



BUT..... thankyou very much for giving me some alterantive code to play with n I hope that what I have written above is a sensible way of doing it and not too processor intensive!

Rhys
Back to top
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Wed May 16, 2007 3:21 am    Post subject: Reply with quote  Mark this post and the followings unread

Either you made a typo in the third line (starting with "while") or -more likely- you forgot to turn off HTML. I recomend chuckists turn off HTML in posts in their profile on this site because otherwise the board gets very confused by all of our triangular brackets. We're working on that but anti-spammer measures have a higher priority.

Right now that third line makes no sense to me at all and I fear ChucK will agree on that.

Also; generally it's best to try to avoid loops that contain "1::samp => now" wherever possible because those are hard on the cpu. Constructions like that will work for small programs but as soon as the program becomes larger and more involved you'll run into problems.

_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Wed May 16, 2007 3:48 am    Post subject: Reply with quote  Mark this post and the followings unread

Yeah the code didnt come out the same as I had written it. I will try and implement your way of passing time forward by the amount required as opposed to every sample and let you know how I get on.

Thanks again

Rhys
Back to top
View user's profile Send private message
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Wed May 16, 2007 4:36 am    Post subject: Reply with quote  Mark this post and the followings unread

SOLVED!!!!

Code:


fun void cutter()
{
   while (CurrentTime < DurationOfOneBar)
   {
      now + CutTime::second => time later;
      while (now < later)
      {
         if (CrotchetB1::samp > CutTime::second)
         {
            1 => Buffer1.gain;
            CutTime::second => now;      
         }
         if (CrotchetB1::samp < CutTime::second)
         {
            1 => Buffer1.gain;
            CrotchetB1::samp => now;
         }
         if (now < later)
         {   
            0 => Buffer1.gain;
            CutTime::second - CrotchetB1::samp => now;
         }
      }
      0 => Buffer1.pos;
      CurrentTime + CutTime => CurrentTime;
   }   
}



This code will now allow me take any beat/loop (of any time signature and BPM )and play any cut from it back at any requested new BPM ....along with the rest of the code not shown here. Finally!!!

Thanks very much for all your help Kassen

Rhys
Back to top
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Wed May 16, 2007 6:48 am    Post subject: Reply with quote  Mark this post and the followings unread

Great, keep on ChucKing!
_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [7 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 » ChucK programming language
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
AmbiphonicDSP

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