Leskos
5/9/2016 - 10:59 PM

OSCaudioAnalysis.scd


// START AUDIO ANALYSIS
(
// Global variables
b = Buffer.alloc( s, 1024, 1 );
~oscFps        = 120;
~oscOutput     = NetAddr( "127.0.0.1", 1234 );
~oscPaths      = [ "/amp",
	           "/freq",
	           "/hasFreq",
	           "/loudness",
	           "/peak",
	           "/mfcc/1",
	           "/mfcc/2",
	           "/mfcc/3",
	           "/mfcc/4",
	           "/mfcc/5",
	           "/mfcc/6",
	           "/mfcc/7",
	           "/mfcc/8",
	           "/mfcc/9",
	           "/mfcc/10",
	           "/mfcc/11",
	           "/mfcc/12",
	           "/mfcc/13",
	           "/onsets"
];

// Define the audio analysis synth
SynthDef.new( \audioAnalysis,
	{
		var input, oscTrigger, oscTriggerMfcc;
		var amp, freq, hasFreq;
		var fft, loudness;
		var peak;
		var mfccArray;

		input            = SoundIn.ar;                        // Main audio input
		oscTrigger       = Impulse.kr( ~oscFps );             // The pulse that triggers sending audio OSC
		oscTriggerMfcc   = Impulse.kr( ~oscFps, phase:0.5 );  // The pulse that triggers sending mfcc OSC

		// Analysis
		amp            = Amplitude.kr( input, mul:1000);                     // Calculate Amplitude
		#freq, hasFreq = Pitch.kr( input, clar:1, ampThreshold:0.0005 );     // Calculate Pitch
		loudness       = Loudness.kr( FFT( b, input), smask:0.25, tmask:1 ); // Calculate Perceptual loudness
		peak           = PeakFollower.kr( input );                           // Calculate Peak
		mfccArray      = MFCC.kr( FFT( b, input) );                          // Calculate Mel Frequency Cepstral Coefficients


		// OSC triggering
		SendTrig.kr( in:oscTrigger, id:0,  value:amp      );
		SendTrig.kr( in:oscTrigger, id:1,  value:freq     );
		SendTrig.kr( in:oscTrigger, id:2,  value:hasFreq  );
		SendTrig.kr( in:oscTrigger, id:3,  value:loudness );
		SendTrig.kr( in:oscTrigger, id:4,  value:peak     );

		SendTrig.kr( in:oscTriggerMfcc, id:5,  value:mfccArray[0]  );
		SendTrig.kr( in:oscTriggerMfcc, id:6,  value:mfccArray[1]  );
		SendTrig.kr( in:oscTriggerMfcc, id:7,  value:mfccArray[2]  );
		SendTrig.kr( in:oscTriggerMfcc, id:8,  value:mfccArray[3]  );
		SendTrig.kr( in:oscTriggerMfcc, id:9,  value:mfccArray[4]  );
		SendTrig.kr( in:oscTriggerMfcc, id:10, value:mfccArray[5]  );
		SendTrig.kr( in:oscTriggerMfcc, id:11, value:mfccArray[6]  );
		SendTrig.kr( in:oscTriggerMfcc, id:12, value:mfccArray[7]  );
		SendTrig.kr( in:oscTriggerMfcc, id:13, value:mfccArray[8]  );
		SendTrig.kr( in:oscTriggerMfcc, id:14, value:mfccArray[9]  );
		SendTrig.kr( in:oscTriggerMfcc, id:15, value:mfccArray[10] );
		SendTrig.kr( in:oscTriggerMfcc, id:16, value:mfccArray[11] );
		SendTrig.kr( in:oscTriggerMfcc, id:17, value:mfccArray[12] );
}).add;

// Start the audio analysis synth
~audioAnalysis = Synth(\audioAnalysis);

// send OSC output
OSCdef( \OSCsender,
	{
		arg msg, time;
		var msgIndex, msgValue;

		msgIndex = msg[2];
		msgValue = msg[3];

		~oscOutput.sendMsg( ~oscPaths[msgIndex], msgValue );
	},
	"/tr");
)


// STOP AUDIO ANALYSIS
(
~audioAnalysis.free;
b.free;
)