
/* Generated by Interface Builder */

#import "StanController.h"
#import <appkit/appkit.h>
#import <musickit/musickit.h>

@implementation StanController

- init
{
	[super init];
	[NXApp loadNibSection:"StanControl.nib" owner:self withNames:NO];
	[thePanel makeKeyAndOrderFront:self];
	return self;
}

- closeUp
{
	[thePanel close];
	return self;
}

- setUp
{
	TWOPI = 8.0 * atan(1.0);
	I = [sInitValueFields doubleValueAt:0]; 
	theta = [sInitValueFields doubleValueAt:1]; 
	speed = [sSpeedField floatValue];
	duration = [sDurationField floatValue];
	K = [sKField doubleValue];
	[self setSoundMode:DSP];
	axisMode = [sAxisButtons selectedTag];
	return self;
}

- setSoundMode:(int)mode
{
	switch (soundMode = mode) {
		case DSP:
			[[sFreqBox setTitle:"Frequency (Hz)"] display];
			[sFreqRangeSlider setMinValue:0.0];
			[sFreqRangeSlider setMaxValue:4000.0];
			[sFreqRangeSlider setFloatValue:500.0];
			[sFreqRangeField setFloatValue:500.0];
			[sFreqCenterSlider setMinValue:0.0];
			[sFreqCenterSlider setMaxValue:4000.0];
			[sFreqCenterSlider setFloatValue:500.0];
			[sFreqCenterField setFloatValue:500.0];
			freqRange = [sFreqRangeField floatValue];
			freqCenter = [sFreqCenterField floatValue];
			[[sAmpBox setTitle:"Amplitude (dB)"] display];
			[sAmpRangeSlider setMinValue:0.0];
			[sAmpRangeSlider setMaxValue:60.0];
			[sAmpRangeSlider setFloatValue:30.0];
			[sAmpRangeField setFloatValue:30.0];
			[sAmpCenterSlider setMinValue:-60.0];
			[sAmpCenterSlider setMaxValue:0.0];
			[sAmpCenterSlider setFloatValue:-20.0];
			[sAmpCenterField setFloatValue:-20.0];
			ampRange = [sAmpRangeField floatValue];
			ampCenter = [sAmpCenterField floatValue];
			break;
		case MIDI:
			[[sFreqBox setTitle:"Key"] display];
			[sFreqRangeSlider setMinValue:0.0];
			[sFreqRangeSlider setMaxValue:127.0];
			[sFreqRangeSlider setFloatValue:64.0];
			[sFreqRangeField setFloatValue:64.0];
			[sFreqCenterSlider setMinValue:0.0];
			[sFreqCenterSlider setMaxValue:127.0];
			[sFreqCenterSlider setFloatValue:64.0];
			[sFreqCenterField setFloatValue:64.0];
			freqRange = [sFreqRangeField floatValue];
			freqCenter = [sFreqCenterField floatValue];
			[[sAmpBox setTitle:"Velocity"] display];
			[sAmpRangeSlider setMinValue:0.0];
			[sAmpRangeSlider setMaxValue:127.0];
			[sAmpRangeSlider setFloatValue:64.0];
			[sAmpRangeField setFloatValue:64.0];
			[sAmpCenterSlider setMinValue:0.0];
			[sAmpCenterSlider setMaxValue:127.0];
			[sAmpCenterSlider setFloatValue:64.0];
			[sAmpCenterField setFloatValue:64.0];
			ampRange = [sAmpRangeField floatValue];
			ampCenter = [sAmpCenterField floatValue];
			break;
		default:
			break;
	}
	return self;
}

- changeInitValue:sender
{
	I = [sender doubleValueAt:0]; 
	theta = [sender doubleValueAt:1];
    return self;
}

- changeDurationField:sender
{
	duration = [sender floatValue];
	[sDurationSlider setFloatValue:duration];
    return self;
}

- changeSpeedField:sender
{
	speed = [sender floatValue];
	if (speed < 0.01) {
		speed = 0.01;
		[sender setFloatValue:speed];
	}
	[sSpeedSlider setFloatValue:speed];
    return self;
}

- changeFreqCenterField:sender
{
	freqCenter = [sender floatValue];
	[sFreqCenterSlider setFloatValue:freqCenter];
    return self;
}

- changeKSlider:sender
{
	K = [sender doubleValue];
	[sKField setDoubleValue:K];
	KoverTWOPI = K/TWOPI;
    return self;
}

- changeFreqRangeField:sender
{
	freqRange = [sender floatValue];
	[sFreqRangeSlider setFloatValue:freqRange];
    return self;
}

- changeAmpCenterField:sender
{
	ampCenter = [sender floatValue];
	[sAmpCenterSlider setFloatValue:ampCenter];
    return self;
}

- changeKField:sender
{
	K = [sender doubleValue];
	[sKSlider setDoubleValue:K];
	KoverTWOPI = K/TWOPI;
    return self;
}

- changeAmpRangeField:sender
{
	ampRange = [sender floatValue];
	[sAmpRangeSlider setFloatValue:ampRange];
    return self;
}

- changeAmpRangeSlider:sender
{
	ampRange = [sender floatValue];
	[sAmpRangeField setFloatValue:ampRange];
    return self;
}

- changeFreqRangeSlider:sender
{
	freqRange = [sender floatValue];
	[sFreqRangeField setFloatValue:freqRange];
    return self;
}

- changeAmpCenterSlider:sender
{
	ampCenter = [sender floatValue];
	[sAmpCenterField setIntValue:ampCenter];
    return self;
}

- changeSpeedSlider:sender
{
	speed = [sender floatValue];
	[sSpeedField setFloatValue:speed];
    return self;
}

- changeDurationSlider:sender
{
	duration = [sender floatValue];
	[sDurationField setFloatValue:duration];
    return self;
}

- changeFreqCenterSlider:sender
{
	freqCenter = [sender floatValue];
	[sFreqCenterField setFloatValue:freqCenter];
    return self;
}

- changeAxis:sender
{
	axisMode = [sender selectedTag];
    return self;
}


- iterateFreq:(float *)freq Amp:(float *)amp Dur:(float *)dur Next:(float *)next 
	X:(float *)xpos Y:(float *)ypos
{
	while (I >= 1.0) I -= 1.0;
	while (I < 0.0) I += 1.0;
	while (theta >= 1.0) theta -= 1.0;
	while (theta < 0.0) theta += 1.0;

	*xpos = theta;
	*ypos = I;

	switch (axisMode) {
		case ITHETA:
			*freq = ((I - 0.5) * freqRange) + freqCenter;
			*amp = ((theta - 0.5) * ampRange) + ampCenter;
			break;
		case THETAI:
			*freq = ((theta - 0.5) * freqRange) + freqCenter;
			*amp = ((I - 0.5) * ampRange) + ampCenter;
			break;
	}
	*dur = duration;
	*next = speed;

	I += KoverTWOPI * sin (TWOPI * theta);
	theta += I;

	return self;
}

@end

