/*
ZeroModule
For Resound 2.2
Sean Luke
Jan 10 1995

A simple example module which zeroes out the selected sound, to get you
started using Resound modules.  This code is discussed in Resound's Help
facility, under the section "Building a Resound Module: A Walk-Though".
For a more sophisticated module, see the API/Math directory.

INTEL READY


Copyright (c) 1995 by Sean Luke

Permission to use, copy, modify, and distribute this material 
for any purpose and without fee is hereby granted.  SEAN LUKE 
MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
*/


	#import "ZeroModule.h"
	#import <soundkit/soundkit.h>
	#import <appkit/appkit.h>
	
	@implementation ZeroModule

	-init
		{
		id returnVal=[super init];
		
		ModuleMenuNode* zeroNode=[[ModuleMenuNode alloc] init];
		
		[TheModuleMenuNode setLength:1];
		[TheModuleMenuNode setSubmenu: 0 : zeroNode];
		
		[zeroNode setName: "Zero"];
		[zeroNode setReceiver: self];
		[zeroNode setMessage:@selector(zeroSound:)];
		[zeroNode setLength:0];
				
		return returnVal;
		}
		
	- zeroSound:sender
		{
		Sound* current=			[TheModuleController currentSound];
		SoundView* cview=	[TheModuleController currentSoundView];
		
		int dataFormat=			[current dataFormat];
		int channelCount=			[current channelCount];
		int sampleCount=			[current sampleCount];
		char* data;
		int x;
	
		int firstSample,sampleLength;
		
		[cview getSelection: &firstSample size: &sampleLength];
		
		if (current==NULL||!sampleLength)		// no sound
			{
			NXRunAlertPanel("No Sound", 
				"There is no sound with which to perform this operation." , 
				"Okay",NULL,NULL);
			return NULL;
			}
			
		[TheModuleController stop];
		[current compactSamples];

		data=(char*) [current data];

		SNDSwapSoundToHost
			((void*)data, (void*)data, sampleCount, channelCount, dataFormat);
			
		switch(dataFormat)
			{
			case SND_FORMAT_MULAW_8: 
			case SND_FORMAT_LINEAR_8:
				{
				char* d=(char*) data;
				for (x=firstSample;x<firstSample+sampleLength;x++) d[x]=0;
				break;
				}
			case SND_FORMAT_LINEAR_16:
				{
				signed short* d=(signed short*)data;
				for (x=firstSample;x<firstSample+sampleLength;x++) d[x]=0;
				break;
				}
			case SND_FORMAT_FLOAT: 			
				{
				float* d=(float*)data;
				for (x=firstSample;x<firstSample+sampleLength;x++) d[x]=0;
				break;
				}
			case SND_FORMAT_DOUBLE: 			
				{
				double* d=(double*)data;
				for (x=firstSample;x<firstSample+sampleLength;x++) d[x]=0;
				break;
				}
			default:
				NXRunAlertPanel("Invalid Format", 
					"This operation cannot be performed with "
					"this particular sound format." , 
					"Okay",NULL,NULL);
				break;
			}
		SNDSwapHostToSound
			((void*)data, (void*)data, sampleCount, channelCount, dataFormat);
		[TheModuleController soundTouched];
		return self;
		}	

	@end

