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
  EdisonRex Edison's Electronic Review
Please visit the chat
 Forum index » DIY Hardware and Software » ChucK programming language
new class creation question
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [16 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
dws



Joined: Feb 05, 2007
Posts: 6
Location: Toronto, Canada

PostPosted: Mon Feb 05, 2007 9:07 am    Post subject: new class creation question Reply with quote  Mark this post and the followings unread

Hi,

I am trying to create a new Event class and need to refer to an instance of that class internally (i.e. within the class definition). me only refers to a shred. Any idea how to do this?

thanks,
don
Back to top
View user's profile Send private message
spencer



Joined: Aug 16, 2006
Posts: 53
Location: northern california

PostPosted: Mon Feb 05, 2007 3:19 pm    Post subject: Reply with quote  Mark this post and the followings unread

Hi Don,
You can do this with the this keyword. Here is a useless, contrived example:
Code:
class A
{
    0 => int value;

    fun int getValue()
    {
        return this.value;
    }

    fun void printValue()
    {
        <<< this.getValue() >>>;
    }
}


hope this helps,
spencer
Back to top
View user's profile Send private message
dws



Joined: Feb 05, 2007
Posts: 6
Location: Toronto, Canada

PostPosted: Tue Feb 06, 2007 6:18 pm    Post subject: Reply with quote  Mark this post and the followings unread

Perfect. Thanks, I'll report back when I have it working...

don

spencer wrote:
Hi Don,
You can do this with the this keyword. Here is a useless, contrived example:
Code:
class A
{
    0 => int value;

    fun int getValue()
    {
        return this.value;
    }

    fun void printValue()
    {
        <<<this>>>;
    }
}


hope this helps,
spencer
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: Thu Feb 08, 2007 8:01 am    Post subject: Reply with quote  Mark this post and the followings unread

Erm, DWG did you notice there's a rather big difference between Spencer's post and your quote? I think Spencer edited a mistake he made after it got quoted?

Anyway, I thought I'd point that out before there is confusion. Wecome on board, BTW!

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



Joined: Aug 16, 2006
Posts: 53
Location: northern california

PostPosted: Thu Feb 08, 2007 10:22 am    Post subject: Reply with quote  Mark this post and the followings unread

Kassen wrote:
Erm, DWG did you notice there's a rather big difference between Spencer's post and your quote? I think Spencer edited a mistake he made after it got quoted?

Anyway, I thought I'd point that out before there is confusion. Wecome on board, BTW!

Hmm, no, no edits from me. Looks like a case of the forum's misguided HTML parsing... my guess is he copied+pasted it correctly but when it was "converted" to proper HTML after posting, the .getValue() was chopped off.

spencer
Back to top
View user's profile Send private message
dws



Joined: Feb 05, 2007
Posts: 6
Location: Toronto, Canada

PostPosted: Wed Feb 14, 2007 10:03 am    Post subject: new class creation continued Reply with quote  Mark this post and the followings unread

ok here is what i am trying to do... i want to create a public class with a method called playnote(pitch, velocity, duration, channel) that will handle playing midi notes. then i want to call it from another chuck file. the problem is that when i call the method, more than once (two play two notes at once), the second (and subsequent) notes don't play until the previous one is finished. i assume that i am a bit confused about chuck time. here is what i have. i have attached the two files as well. any pointers would be greatly appreciated.

don

class creation file:

Code:
public class Makemidinote
{
   MidiOut mout;
   MidiMsg msg;
   int pitch;
   int velocity;
   int duration;
   int channel;
   0 => int device;

   // try to open MIDI port (see chuck --probe for available devices)
   if( !mout.open( device ) ) me.exit();

   // print out device that was opened
   <<<MIDI> ", mout.name() >>>;

   public void playnote(int p, int v, int d, int c)
   {
      p => pitch;
      v => velocity;
      d => duration;
      c => channel;

      // do midi out (on/off)
      144 + (channel - 1) => msg.data1;
      pitch => msg.data2;
      velocity => msg.data3;
      mout.send(msg);
      128 + (channel - 1) => msg.data1;
      pitch => msg.data2;
      velocity => msg.data3;
      <<<"on: ", this, pitch, velocity, duration, channel>>>;
      me.yield();
      duration::ms => now;
      me.yield();
      mout.send(msg);
      <<<"off: ",pitch, velocity, duration, channel>>>;
   }
}

other file that calls function:

Code:
Makemidinote n;

n.playnote(59, 80, 4000, 1);
n.playnote(69, 80, 2000, 1);
n.playnote(49, 80, 1000, 1);
1::second => now;
n.playnote(61, 80, 4000, 1);



test.ck
 Description:

Download
 Filename:  test.ck
 Filesize:  151 Bytes
 Downloaded:  157 Time(s)


Makemidinote.ck
 Description:

Download
 Filename:  Makemidinote.ck
 Filesize:  848 Bytes
 Downloaded:  157 Time(s)

Back to top
View user's profile Send private message
spencer



Joined: Aug 16, 2006
Posts: 53
Location: northern california

PostPosted: Wed Feb 14, 2007 11:06 am    Post subject: Reply with quote  Mark this post and the followings unread

Don,
Quick note-you might want to use the "Disable HTML in this post" option when posting ChucK code. It looks like part of your ChucK program was erroneously deleted by the forum software as it tried to parse it as HTML when you posted it. See this thread for more info:
http://electro-music.com/forum/topic-14150.html
(Its also possible to disable HTML in posts by default through your profile.)

The reason why notes aren't playing simutaneously is because the playnote function does duration::ms => now; This means that the function will take a total of duration milliseconds to return. So the first n.playnote() in your test program will take 4000 ms, and then after that the next next n.playnote() will take 2000 ms, etc., in sequence. One really easy solution would be to spork each function call in a new shred. E.g.
Code:
Makemidinote n;

spork ~ n.playnote(59, 80, 4000, 1);
spork ~ n.playnote(69, 80, 2000, 1);
spork ~ n.playnote(49, 80, 1000, 1);
1::second => now;
spork ~ n.playnote(61, 80, 4000, 1);

This will spork a new shred for each note, so that groups of notes will play in parallel, rather than in sequence. However this creates a problem. Each of these shreds is going to be making modifications to the
Code:
   MidiMsg msg;
   int pitch;
   int velocity;
   int duration;
   int channel;

part of n in parallel. This means that the values of these variables could be constantly changing as each new shred goes through the playnote function, and the variables could contain incorrect values when you create your MIDI msg. So the solution to this would be to move all of these variables into the body of the playnote() function, assuming you don't need them in any other functions in the Makemidinote class. In fact you could probably even remove the pitch, velocity, duration, and channel variables as they seem to be redundant in the code you posted, and just use the p, v, d, and c variables.

hope this helps!
spencer
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 Feb 14, 2007 11:39 am    Post subject: Reply with quote  Mark this post and the followings unread

spencer wrote:
Don,
Quick note-you might want to use the "Disable HTML in this post" option when posting ChucK code.


Yet better; I found that you can disable HTML for all posts you make by setting a single checkbox in your profile.

I did this myself as well; it's just too easy to forget about the silly little checkbox every time. Site-admin Mosc is looking into improving the behaviour of the "code" tag to stop the board from trying to parse HTML.

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



Joined: Feb 05, 2007
Posts: 6
Location: Toronto, Canada

PostPosted: Wed Feb 14, 2007 11:47 am    Post subject: almost there - notes don't stop Reply with quote  Mark this post and the followings unread

thanks for your help on this.

things are working simultaneously now but notes don't end. my little test file plays the first three notes (on only) then seems to stop.

Code:
public class Makemidinote
{
   MidiOut mout;
   0 => int device;

   // try to open MIDI port (see chuck --probe for available devices)
   if( !mout.open( device ) ) me.exit();

   // print out device that was opened
   <<< "MIDI device:", mout.num(), " -> ", mout.name() >>>;

   public void playnote(int p, int v, int d, int c)
   {
      MidiMsg msg;

      // do midi out (on/off)
      144 + (c - 1) => msg.data1;
      p => msg.data2;
      v => msg.data3;
      mout.send(msg);
      128 + (c - 1) => msg.data1;
      p => msg.data2;
      v => msg.data3;
      <<<"on: ", this, p, v, d, c>>>;
      me.yield();
      d::ms => now;
      me.yield();
      mout.send(msg);
      <<<"off: ", p, v, d, c>>>;
   }
}


Code:
Makemidinote n;

spork ~ n.playnote(59, 80, 4000, 1);
spork ~ n.playnote(69, 80, 2000, 1);
spork ~ n.playnote(49, 80, 1000, 1);

1::second => now;
spork ~ n.playnote(61, 80, 4000, 1);
Back to top
View user's profile Send private message
spencer



Joined: Aug 16, 2006
Posts: 53
Location: northern california

PostPosted: Wed Feb 14, 2007 12:32 pm    Post subject: Reply with quote  Mark this post and the followings unread

don,

Code:
   public void playnote(int p, int v, int d, int c)
   {
      MidiMsg msg;

      // do midi out (on/off)
      144 + (c - 1) => msg.data1;
      p => msg.data2;
      v => msg.data3;
      mout.send(msg);
      128 + (c - 1) => msg.data1;
      p => msg.data2;
      v => msg.data3;
      <<<"on: ", this, p, v, d, c>>>;
      me.yield();
      d::ms => now;
      me.yield();
Should msg.data1 be set to a note off message here?
Code:
      mout.send(msg);
      <<<"off: ", p, v, d, c>>>;
   }


spencer
Back to top
View user's profile Send private message
dws



Joined: Feb 05, 2007
Posts: 6
Location: Toronto, Canada

PostPosted: Wed Feb 14, 2007 12:48 pm    Post subject: Reply with quote  Mark this post and the followings unread

i had the text output line in the wrong spot. here is a new version, functionally equivalent. same issue.

don

Code:
public void playnote(int p, int v, int d, int c)
   {
      MidiMsg msg;

      // do midi out (on/off)
      144 + (c - 1) => msg.data1;
      p => msg.data2;
      v => msg.data3;
      mout.send(msg);
      <<<"on: ", this, p, v, d, c>>>;
      me.yield();
      d::ms => now;
      me.yield();
      0 => msg.data3;
      mout.send(msg);
      <<<"off: ", p, v, d, c>>>;
   }
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 Feb 14, 2007 1:09 pm    Post subject: Reply with quote  Mark this post and the followings unread

That's weird. That looks quite right to me, asuming it's sporked as a new shred and not just called as a function.

Chances are I missed something but the one thing I see to comment on is that you don't need to yield if you advance time right below it. Advancing time already yields *and* advances time in itself.

Advancing time time basically says "I don't need it anymore right now but I'd like to have the CPU back in this amount of time to do something again". Yielding expresses that it's now ok or (more likely) nesiscary for other shreds to execute but that you'd like to use the cpu after those shreds do their thing yet before any time (from a outside persective) passes again.

As a side note; I don't think I can find the "this" keyword in the manual and I'm not aware of it being documented anywhere? I think I'll add it to the "manual erata" WIKI page.

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



Joined: Aug 16, 2006
Posts: 53
Location: northern california

PostPosted: Wed Feb 14, 2007 1:28 pm    Post subject: Reply with quote  Mark this post and the followings unread

Uh I thought I just posted something but somehow it disappeared. whoops.

Anyways it looks to me like the playnote() function is sending two noteOn messages instead of a noteOn followed by a noteOff. Shouldnt there be a 0x80 + ( c - 1 ) => msg.data1; before the second mout.send(); to make the message a noteOff before sending it?

yeah i cant recall learning about "this" except via the mailing list, so that might be a good thing to have documented.

spencer
Back to top
View user's profile Send private message
dws



Joined: Feb 05, 2007
Posts: 6
Location: Toronto, Canada

PostPosted: Wed Feb 14, 2007 1:44 pm    Post subject: Reply with quote  Mark this post and the followings unread

you can send a note off using note on with velocity 0. they are equivalent.

there are a couple of references to this in the chuck manual. pages 31, 81. but no documentation of it's (or should i say this's) use.

don
Back to top
View user's profile Send private message
spencer



Joined: Aug 16, 2006
Posts: 53
Location: northern california

PostPosted: Wed Feb 14, 2007 1:49 pm    Post subject: Reply with quote  Mark this post and the followings unread

oh yeah... whoops...

spencer
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 Feb 14, 2007 2:28 pm    Post subject: Reply with quote  Mark this post and the followings unread

dws wrote:
you can send a note off using note on with velocity 0. they are equivalent.


Indeed and this is very convenient if within the curent scope there's only one note at a time like here. I'm not sure what's wrong but as a overall structure for a implemention it looks quite sound to me right now.

Quote:

there are a couple of references to this in the chuck manual. pages 31, 81. but no documentation of it's (or should i say this's) use.


I looked those up and you are right, but those references are a bit... erm.... thin (to say the least). It does make one wonder what "pure" does. The "pure" keyword gets mentioned on page 31 as well and that's the only reference to it. "const" is another mysterious word in that list.

More whoopses, but exciting ones, at least.

_________________
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 [16 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
e-m mkii

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