[jsyn] Basic Soundtoy in JSyn

jsyn at music.columbia.edu jsyn at music.columbia.edu
Mon Nov 6 09:53:43 EST 2006


(disclaimer: message is pre-coffee)

I guess it depends on the target audience, but if you're going for  
simple, this seems to be overkill, and confusing. Making a thread,  
sleeping it to animate, etc... this is advanced stuff. I know what  
I'm doing and I don't feel invited to play with this code!

And it bothers me that the size of the square is just an animation,  
and not actually bound to the SynthEnvelope. It's a convincing  
animation, but if students are going to be poring over the code, you  
might as well make the animation meaningful.

AND, while I'm throwing my criticism around :p, your coding style is  
less than conventional (who cares... except you're teaching a class).  
Indentation, capital letters for variables, mixing AWT and swing, etc.

Solution? Look at processing [processing.org] for multimedia stuff.  
It's essentially a simple IDE  with a very neat class that extends  
Applet and gives you a TON of [fun]ctions in the main namespace. And,  
you can use jsyn stuff as you normally would. Here's the equivalent  
processing code to draw your box:

int size=0;

void setup() {
   size(200,200);
   rectMode(CENTER);
   stroke(255);
   fill(0);
}

void draw() {
   background(0);
   rect(width/2, height/2, size, size);
   size++;
}

void mousePressed() {
   size=0;
}

It took less than 2 minutes to write, and should be self-explanatory.  
It took me longer than that to understand what was going on with your  
code!

good morning,
kevin

On Nov 6, 2006, at 6:19 AM, jsyn at music.columbia.edu wrote:

> On Mon, 2006-11-06 at 08:55 +0000, jsyn at music.columbia.edu wrote:
>> I need something to introduce "SoundToys" in JSyn to my class.  
>> What I'm
>> looking for is the simplest possible program with
>> animation/interaction/jsyn sound. Any comments on the following  
>> program
>> as an example of such?
>>
>> Cheers,
>>
>> Ross-c
>
> I attached the wrong program. The previous program had graphics,  
> but no
> JSyn sound. Not that the sound is very complex. I attach the proper
> program here.
>
> Cheers,
>
> Ross-c
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import com.softsynth.jsyn.*;
>
> /*
>  * MovingGraphicsJSyn shows how to use some graphic operations to draw
>  * stuff, how to have a thread running, and how to link all this to
> JSyn.
>  * First we have a JFrame.
>  *
>  */
>
> public class MovingGraphicsJSyn extends JFrame
> {
>   // Constructor of our MovingGraphicsJSyn class (Frame)
>
>   public MovingGraphicsJSyn()
>   {
>     setBounds( 200, 200, 300, 300 );	// Size and position
>
>     Container C = getContentPane();	// get access to the 'content  
> pane'
> 					// where we can put "stuff"
>
>     C.setLayout( new BorderLayout() );	// Set layout manager
>
> 					// Add the actual graphics screen
>     C.add( new MovingGraphicsScreenJSyn(), BorderLayout.CENTER );
>
> 					// Add a window listener in case
> 					// anyone tries to close the window
>     addWindowListener(
>
> 	new WindowAdapter()
> 	{
> 	  public void windowClosing( WindowEvent we )
> 	  {
>  	    Synth.stopEngine();
> 	    System.exit( -1 );
> 	  }
> 	}
>     );
>
> 					// Set the title
>     setTitle( "Moving Graphics Program" );
>   }
>
>   /*
>    * This main method makes our JFrame extending class into an
> application
>    * that can be executed. Note that we only create an "instance"  
> of our
>    * "MovingGraphicsJSyn" class, and then make it visible.
>    *
>    */
>
>   public static void main( String Args[] )
>   {
>     Synth.startEngine( 0 );	// start JSyn. As early as possible
>
>     MovingGraphicsJSyn SG = new MovingGraphicsJSyn();
>     SG.setVisible( true );
>   }
> }
>
> /*
>  * Another class in the same file!. Note that this one isn't "public",
> so
>  * we don't need to declare it in its own file. We generally only do
> this
>  * when the class won't be used anywhere else except in this file.
>  *
>  * This class extends JPanel as it will be a flat graphics panel
>  *
>  * This class "implements Runnable" as it can be a thread.
>  *
>  */
>
> class MovingGraphicsScreenJSyn extends JPanel implements Runnable
> {
>   private int size;	// The size of the rectangle. This is fixed in  
> this
> 			// application, but will change in the next
>
>   /* JSyn objects */
>
>   private SawtoothOscillator Saw1;
>   private SawtoothOscillator Saw2;
>   private AddUnit Mixer;
>   private Filter_LowPass FLP;
>   private EnvelopePlayer EP;
>   private SynthEnvelope ShortEnv;
>   private LineOut LO;
>
>   /*
>    * Constructor. More to do now as we create the JSyn synth
>    */
>
>   public MovingGraphicsScreenJSyn()
>   {
>     size = 1;
>
>     Thread T = new Thread( this );
>     T.start();
>
>     addMouseListener(
> 	new MouseAdapter()
> 	{
> 	  public void mousePressed( MouseEvent me )
> 	  {
> 	    mouseDown();
> 	  }
> 	}
>     );
>
>     Saw1 = new SawtoothOscillator();
>     Saw1.frequency.set( 55 );
>     Saw2 = new SawtoothOscillator();
>     Saw2.frequency.set( 54.75 );
>
>     Mixer = new AddUnit();
>     Saw1.output.connect( Mixer.inputA );
>     Saw2.output.connect( Mixer.inputB );
>
>     FLP = new Filter_LowPass();
>     Mixer.output.connect( FLP.input );
>     FLP.Q.set( 4 );
>
>     LineOut LO = new LineOut();
>     FLP.output.connect( 0, LO.input, 0 );
>     FLP.output.connect( 0, LO.input, 1 );
>
>     EP = new EnvelopePlayer();
>     EP.output.connect( FLP.frequency );
>
>     double data[] =
>     {
>       0.1, 5000,
>       4, 500
>     };
>
>     ShortEnv = new SynthEnvelope( data );
>
>     Saw1.start(); Saw2.start(); Mixer.start(); FLP.start(); LO.start 
> ();
>     EP.start();
>
>     EP.envelopePort.queue( ShortEnv );
>   }
>
>   // When the mouse is clicked, make the size of the rectangle 1  
> again.
>
>   private void mouseDown()
>   {
>     size = 1;
>     EP.envelopePort.clear();
>     EP.envelopePort.queue( ShortEnv );
>   }
>
>   /*
>    * GUI components in Swing all have a "paintComponent" method. When
>    * the component is "exposed" on the screen or is uncovered when you
>    * move another window away from being on top of it, or etc, then  
> this
>    * method is called to paint the contents of the window.
>    *
>    */
>
>   public void paintComponent( Graphics g )
>   {
>     Dimension Size = getSize();		// Get size of component. May change
> 					// if the window is resized etc.
>
>     /*
>      * The "Graphics" object represents the area we should draw to. By
>      * calling methods, we can draw stuff onto the are of the screen
>      * represented by our JPanel extending class.
>      *
>      */
>
>     g.setColor( Color.black );		// Set colour of draw operations to
> 					// black	
>
> 					// Fill entire panel with black
>     g.fillRect( 0, 0, Size.width, Size.height );
>
>     g.setColor( Color.white );		// Set colour of draw operations to
> 					// be white
>
>     int middleX = Size.width / 2;	// Find "address" of middle pixel on
>     int middleY = Size.height / 2;	// screen, and store (x,y) coord in
> 					// two new integer variables.
>
> 					// Draw white "rectangle" on screen,
> 					// centered around middle pixel. Note
> 					// role of "size" in controlling the
> 					// size of the rectangle.
>
>     g.drawRect( middleX - size/2, middleY - size/2, size, size );
>   }
>
>   // The run method is called when the "thread" starts. The thread  
> lasts
>   // as long as this method is running.
>
>   public void run()
>   {
>     while( true ) // Loop forever!
>     {
>       try
>       {
>         Thread.sleep( 25 );	// Try to sleep for 25 milliseconds.
>       }
>       catch( InterruptedException ie )
>       {
>         ie.printStackTrace();	// Print out some debugging  
> information if
> 				// anything goes wrong.
>       }
>
>       size = size + 1;		// increase the size of the square
>
>       repaint();		// say that the screen needs repainting
>     }
>   }
> }
>
>
> _______________________________________________
> JSyn mailing list
> JSyn at music.columbia.edu
> To change digest mode or to make other administrative changes visit:
> http://music.columbia.edu/mailman/listinfo/jsyn



More information about the JSyn mailing list