[jsyn] Polyphony - bug in program

jsyn at music.columbia.edu jsyn at music.columbia.edu
Thu Dec 8 12:35:36 EST 2005


Hi Ross-c,

This one's tricky.  If you remove the declaration of "output" in SlowSine then 
it should work. Here's why.

You are getting a NullPointerException in:

    return addVoiceToMix( SS );

That connects the output of the SynthCircuit to a BusWriter. It uses the 
"output" member predefined for SynthCircuit.

You declared a second "output" that is different than the one declared in 
SynthCircuit and used by addVoiceToMix( SS ). It is dangerous that Java let's 
you do that because you now have two output members with different scopes.

Ouch. My brain hurts.

Phil Burk

jsyn at music.columbia.edu wrote:
> Hi. No matter how carefully I look, I cannot see any bugs in the
> following program. It's supposed to be a simple example of polyphony
> using a BussedVoiceAllocator. But when I try and run it, it crashes with
> a null pointer exception in "makeVoice()". The "SlowSine" object is
> created, but when return is called the null pointer exception is thrown.
> 
> I'm stumped. Any ideas?
> 
> Thanks,
> 
> Ross-c
> 
> /* Polyphonic.java - run with a single argument e.g. java Polyphonic 10
> */
> 
> import com.softsynth.jsyn.*;
> import com.softsynth.jsyn.util.*;
> import com.softsynth.jsyn.circuits.*;
> 
> public class Polyphonic
> {
>   public static void main( String Args[] )
>   {
>     Synth.startEngine( 0 );
> 
>     LineOut LO = new LineOut();
> 
>     int maxVoices = Integer.parseInt( Args[0] );
> 
>     PolySine PS = new PolySine( maxVoices );
> 
>     PS.getOutput().connect( 0, LO.input, 0 );
>     PS.getOutput().connect( 0, LO.input, 1 );
> 
>     LO.start();
> 
>     int currentTick = 0;
> 
>     while( currentTick < 20000 )
>     {
>       SlowSine SS = (SlowSine) PS.allocate();
> 
>       if ( SS != null )
>       {
>         SS.amplitude.set( 1.0 / maxVoices );
>         SS.frequency.set( randomFrequency() );
> 
>         SS.start( currentTick );
>         SS.stop( currentTick + makeTime( 2 ) );
> 
>       }
> 
>       currentTick = currentTick + 30;
>       Synth.sleepUntilTick( currentTick );
>     }
> 
>     LO.stop();
>     PS.stop();
> 
>     Synth.stopEngine();
>   }
> 
>   private static int makeTime( double t )
>   {
>     return (int) (Synth.getTickRate() * t );
>   }
> 
>   private static double randomFrequency()
>   {
>     double p = Math.random();
> 
>     if ( p < 0.3 )
>     {
>       return 440.0;
>     }
>     else if ( p < 0.5 )
>     {
>       return 523.2511;
>     }
>     else if ( p < 0.7 )
>     {
>       return 587.32953;
>     }
>     else if ( p < 0.9 )
>     {
>       return 659.25511;
>     }
>     else
>     {
>       return 698.4564;
>     }
>   }
> }
> 
> class SlowSine extends SynthCircuit
> {
>   public SynthOutput output;
>   public SynthInput frequency;
>   public SynthInput amplitude;
> 
>   private SynthEnvelope env;
>   private EnvelopePlayer envPlayer;
> 
>   public SlowSine()
>   {
>     SineOscillator SO = new SineOscillator();
>     add( SO );
> 
> System.out.println( "I am creating a sine voice" );
> 
>     addPort( output = SO.output );
>     addPort( frequency = SO.frequency );
>     addPort( amplitude = SO.amplitude );
> 
>     double d[] =
>     {
>        0.7, 1,
>        0.6, 1,
>        0.7, 0
>     };
> 
>     env = new SynthEnvelope( d );
> 
>     envPlayer = new EnvelopePlayer();
>     add( envPlayer );
> 
>     envPlayer.output.connect( SO.amplitude );
>   }
> 
>   public void start()
>   {
>     super.start();
>     envPlayer.envelopePort.queue( env );
>   }
> }
> 
> class PolySine extends BussedVoiceAllocator
> {
>   public PolySine( int maxVoices )
>   {
>     super( maxVoices );
>   }
> 
>   public SynthCircuit makeVoice()
>   {
> 
> System.out.println( "I am making a voice" );
>     SlowSine SS = new SlowSine();
> System.out.println( "SS is  " + SS );
>     return addVoiceToMix( SS );
>   }
> }
> 
> 
> 
> _______________________________________________
> 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
> 
> 
> 

-- 
Thank you,
Phil Burk
-----------------------------------------------------------------
SoftSynth, Audio Research and Development, http://www.softsynth.com/
75 Pleasant Lane, San Rafael, CA, 94901 USA,   Phone/FAX: 1-415-453-4320
This communication may contain commercial content and is intended to comply
with all applicable laws governing email communications. To stop receiving
messages from us, simply reply to us indicating that preference.
------------------------------------------------------------------



More information about the JSyn mailing list