ChucK/sequencer.ck

From CSWiki
Revision as of 19:02, 11 March 2006 by Ssalazar (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Fairly simple sequencer. To use, first add sequencer.ck to you virtual machine. Then, from add any ChucK program, make the following function call:

"/var/samples/kickdrum.wav" => string filename;
4 => int beats_per_measure;
[1.0, 0 ] => float volume_vector[];
sequencer.sequence( filename, beats_per_measure, volume_vector );

where filename is a string containing the path to the sample you want to play, beats_per_measure is the number times that sample will play during each measure, and volume_vector is a vector of floats used to determine the volume of each beat.

sequencer.ck:

// sequencer.ck
// sequencer class for sequencing arbitrary samples 
// author: spencer salazar
// Feel free to use/modify/distribute as you see fit.  

public class sequencer
{
	static float measure_length;
	static Event new_measure;
	static int on;
	
	fun static void start()
	{
		while( on )
		{
			new_measure.broadcast();
			measure_length::second => now;
		}
	}
	
	fun static void stop()
	{
		0 => on;
	}

	fun static void set_measure_length( float new_ml )
	{
		// wait until the end of the measure
		new_measure => now;
		new_ml => measure_length;
	}

	fun static void sequence( string filename, float bpmeasure, float volumev[] )
	{
		new_measure => now;
	
		sndbuf buf => gain g => dac;
		filename => buf.read;
		.5 => g.gain;

		0 => int i;
		0 => int j;

		while( on )
		{		
			for( 0 => i; i < bpmeasure; i++ )
			{
				if( volumev[j] > 0 )
				/* only play the sample if its meant to be heard */
				{
					0 => buf.pos;
					volumev[j] => buf.gain;
				}
				
				j++;
				if( j >= volumev.cap() )
					0 => j;
				(measure_length/bpmeasure)::second => now;
			}
			//new_measure => now;
		}
	}

}

Event e @=> sequencer.new_measure;
1.5 => sequencer.measure_length;
1 => sequencer.on;

sequencer.start();