Audio input

AudioIn.ar(channel, mul, add)

Stereo Input - first 2 inputs of the soundcard

AudioIn.ar([1,2])

e.g. Patch to take stereo audio input, control its volume, and output it again

(
SynthDef("audioin", { arg vol=0;
var input;
input = AudioIn.ar([1,2]);
Out.ar(0, input*vol);
}).writeDefFile;
)

(
var s, w, volslider;
s = Server.local;
s.boot;
s.sendSynthDef("audioin");
w = SCWindow("Audio In", Rect(100, 800, 50, 500));
w.front;

volslider = SCSlider(w, Rect(10, 50, 30, 400));
volslider.value = 0;
volslider.action = { s.sendMsg("/n_set", 1000, "vol", volslider.value); };
)

s.sendMsg("/s_new", "audioin", 1000, 1, 0);
s.sendMsg("/n_free", 1000);
s.quit;

*****************************
Using soundfiles as input

In most cases you will create a buffer into which you will read the contents of a soundfile. You need to make sure that the buffer has the relevant number of channels and samples. For example, a 2 second CD quality stereo file will need a 2 channel buffer of size 2*44100 samples - the file has a sample rate of 44100 samples per second.

Last week we used Buffer.alloc to create a new buffer. To read a soundfile into a buffer automatically we can use Buffer.read instead. This will allocate and fill the buffer at the same time.

Buffer.read(server, path, startFrame, numFrames, bufStartFrame, leaveOpen, completionMessage);

		path - path of the soundfile
		startFrame - where in the soundfile to start
		numFrames - how many frames to read or -1 to read as much of the file
			as will fit in the Buffer
			if leaving the file open for DiskIn usage, use -1 to fill the whole buffer
		bufStartFrame - which frame in the buffer to start reading into
		leaveOpen -
			for DiskIn you will want this to be true, the buffer will be
			used for streaming the soundfile from disk.
			the buffer must have been allocated with a multiple of
			(2 * synth block size).  most commonly 32768 frames.
			The class method .cueSoundFile simplifies this.
		completionMessage
			osc message or function that returns an osc message

If you have a longer soundfile which won't fit into a buffer (a buffer is a space in memory, so the buffer size will be limited by the amount of physical memory you have installed) then you can use Buffer.cueSoundFile instead in conjunction with DiskIn.

Buffer.cueSoundFile(path,fileStartFrame, bufferSize,completionMessage);

		Cue a soundfile into the buffer for use with DiskIn
		path - path of the soundfile
		fileStartFrame - where in the soundfile to start
		bufferSize - the default is 32768, suitable for most situations.
		completionMessage
			osc message or function that returns an osc message

and then

DiskIn.ar(numchannels, buffernumber);

OSC equivalents: /b_allocRead to allocate a buffer and read a soundfile at the same time and
/b_read to read a soundfile into an existing buffer.

/b_allocRead				allocate buffer space and read a sound file.
	int - buffer number
	string - path name of a sound file.
	int - starting frame in file (optional. default = 0)
	int - number of frames to read (optional. default = 0, see below)
	bytes - an OSC message to execute upon completion. (optional)
	
Allocates buffer to number of channels of file and number of samples requested, or fewer if sound file is smaller than requested. Reads sound file data from the given starting frame in the file. If the number of frames argument is less than or equal to zero, the entire file is read. 
Asynchronous. Replies to sender with /done when complete.

/b_read					read sound file data into an existing buffer.
	int - buffer number
	string - path name of a sound file.
	int - starting frame in file (optional. default = 0)
	int - number of frames to read (optional. default = -1, see below)
	int - starting frame in buffer (optional. default = 0)
	int - leave file open (optional. default = 0)
	bytes - an OSC message to execute upon completion. (optional)
	
Reads sound file data from the given starting frame in the file and writes it to the given starting frame in the buffer. If number of frames is less than zero, the entire file is read. 
If reading a file to be used by DiskIn ugen then you will want to set "leave file open" to one, otherwise set it to zero.
Asynchronous. Replies to sender with /done when complete.

**********************
Playing back from a buffer

This can be done using PlayBuf. You need to specify the following:

numChannels - number of channels that the buffer will be.
			this must be a fixed integer. The architechture of the SynthDef
			cannot change after it is compiled.
			warning: if you supply a bufnum of a buffer that has a different
			numChannels then you have specified to the PlayBuf, it will
			fail silently.
	bufnum - the index of the buffer to use
	rate   - 1.0 is normal, 2.0 is one octave up, 0.5 is one octave down
			-1.0 is backwards normal rate ... etc.
	trigger - a trigger causes a jump to the startPos. 
		A trigger occurs when a signal changes from <= 0 to > 0.
	startPos - sample frame to start playback.
	loop       - 1 means true, 0 means false.  this is modulate-able.
	
(
SynthDef("loop", { arg trig=1, rate=1;
var input, b, s;
s = Server.local;
b = Buffer.read(s, "sounds/loop1.wav", 0, -1);
Out.ar(0, PlayBuf.ar(2, b.bufnum, rate, trig));
}).writeDefFile;
s.sendSynthDef("loop");
)	
s.boot;
s.sendMsg("/s_new", "loop", 1000, 1, 0);

s.sendMsg("/n_set", 1000, "trig", 0);
s.sendMsg("/n_set", 1000, "trig", 1, "rate", 0.5);

s.sendMsg("/n_free", 1000);
b.free;
s.quit;

************************
Writing audio to a buffer and saving it to file

To do this you need to first allocate a buffer (make sure it is big enough) and then write to it using BufWr

e.g. BufWr.ar(input, bufnum, phase, loop) - generally you will just need to supply the input and bufnum arguments.

To write the contents of a buffer to a file, then use Buffer.write:

Buffer.write( path,headerFormat,sampleFormat,numFrames,startFrame,leaveOpen,
										completionMessage);

		Write the contents of the buffer to a file.
		set leaveOpen to true for use with DiskOut
	
		see SoundFile for headerFormat and sampleFormat
		
For longer files which won't fit into a buffer, you would use DiskOut as well as the above.

************************
Getting soundfile information

The SoundFile ugen can be used to find out some useful things about the files we are using - such as length, soundfile type, number of channels etc. We can then take this information to make sure we create suitable buffers etc. Check out the SoundFile help file for more info. Generally you will use

(
s = SoundFile.new;
s.openRead("sounds/loop1.wav");
s.inspect;
s.close;
)