Spray - Audio file segmenting 
          
        
        This tutorial shows how the audio can be used in a creative way. 
          The audio file is granulated and the grains are spread across the stereo 
          spectrum in a steady sweeping pattern controlled by a sine function.
        The output file will sound similar this:
         
          
        Click here to view source.
        Lets have a closer look.  
        
          
             
               import jm.JMC; import jm.music.data.*; import jm.util.*; import jm.audio.*; import jm.audio.synth.*;   | 
            
          
        
        
        The import statements must bring in the jMusic audio classes as well as 
        the music classes.
 
        
          
             
               public class Spray implements JMC { 	 	public static void main(String[] args) { 	    new Spray(); 	} 	 	public Spray() { 		Score score = new Score(); 		Part p = new Part("Spray", 0, 0); 		Phrase phr = new Phrase(); 		String fileName = "Welcome.au";
  		int numb = (int)(Math.random() * 10) + 1; 		File f = new File(fileName); 		double fileSize = (double)(f.length() - 32)/44100/2/2; 		
  		Instrument[] ensemble = new Instrument[1]; 		ensemble[0] = new SimpleSampleInst(fileName, FRQ[C4]);
  		double st = 0.0;
  		double dur = 0.1;
  		double pan = 0.0;
 
  		double panSpeed = 4.0;  | 
            
          
        
        
        The attributes are declared in this next section, and the file size is 
        calculated. 
        Note that the formula assumes a stereo file at a 44100 sample rate.
 
        
          
             
                		for(int i=0; i<100; i++) {
 
  			pan = Math.sin((0.01 * panSpeed * (double)i)%3.14); 			System.out.println((0.01 * panSpeed * (double)i)%3.14);
  			Note n = new Note(C4 + (int)(Math.random() * 7), dur,  				(int)(Math.random() * 100 + 27)); 			n.setPan(pan); 			n.setDuration(dur * 1.5);
  			n.setSampleStartTime(st);
  			phr.addNote(n);
  			st += dur;
 
  			if ((st + dur) > fileSize) st = 0.0; 		}
   
               | 
            
          
        
        
        100 notes are created and added to the phrase. 
        On each iteration the panSpeed value is checked to see if the panning 
        position should be updated or not. 
        Each note has a random pitch with in an octave, a random dynamic, and 
        the duration is slightly longer than the rhythmValue to compensate
        for shortening due to resampling for notes above C4. 
        
        The SampleStartTime is the duration through 
          the sound file that the note should start reading.
          Error checking is done to make sure the next note will not read past 
          the end of the file.
        To provide variety in the panning, the speed 
          is shifted up and down by step. 
         
        
          
             
                		p.addPhrase(phr); 		score.addPart(p);
  		View.print(score); 		View.show(score);
  		Write.au(score, "TestSpray.au", ensemble); 	}
  }   | 
            
          
        
        
        The phrase is packed into a score in the normal way, and the score is 
        then written to an AU file. 
        During this processes each note in turn is rendered. With 100 notes this 
        can take a little while.