/************************************************************************
 *	TITLE:			JAZENV.C					*
 *	PROJECT:		JAGUAR/JAZ				*
 *	FUNCTION:		JAZ ENVELOPE EDIT MODULE				*
 *	PROGRAMMER:		Andreas Binner			*
 *	DATE:			6/95					*
 *									*
 *              COPYRIGHT 1993,1994 Atari U.S. Corporation              *
 *          UNATHORIZED REPRODUCTION, ADAPTATION, DISTRIBUTION,         *
 *          PERFORMANCE OR DISPLAY OF THIS COMPUTER PROGRAM OR          *
 *        THE ASSOCIATED AUDIOVISUAL WORK IS STRICTLY PROHIBITED.       *
 *                            ALL RIGHTS RESERVED.                      *
 *									*
 ************************************************************************/

#include <stdarg.h>
#include "portab.h"
#include "slibc.h"
#include "glib.h"
#include "gemlib.h"
#include "jazobj.h"
#include "ptchptch.h"
#include "jazsyn.h"
#include "dos.h"
#include "jazutil.h"
#include "jazenv.h"
#include "algobj.h"

#define ENV_BOX	0
#define ENV_TITLE 1
#define ENV_ENV 2
#define ENV_POINT 3
#define ENV_AMPL 4
#define ENV_TIME 5
#define ENV_LOOP 6
#define ENV_BAR1 7
#define ENV_ADD 8
#define ENV_DELETE 9
#define ENV_BAR 10
#define ENV_OK 11
#define ENV_CANCEL 12

#define VOL_BOX	0
#define VOL_TITLE	1
#define VOL_DELAY	2
#define VOL_PAN	3
#define VOL_VOL	4
#define VOL_BAR	5
#define VOL_OK	6

typedef struct
{
	LONG type;
	LONG r_flags;
	LONG wave_poi1;	
	LONG pitch1;
	LONG loc1;
	LONG wave_poi2;	
	LONG pitch2;
	LONG loc2;
	ENVPARA env[3];
	
	LONG filter_table,delay,pan,volume;
} PARAM3;

VOID handle_modifier(OBJECT *tree,WORD obj,POPUPINFO *info);
VOID adjust_volume(OBJECT *tree,WORD obj,POPUPINFO *info);
VOID handle_env(OBJECT *tree,WORD obj,POPUPINFO *info);
VOID env_change_point(OBJECT *tree,WORD obj,SLIDEINFO *info);
VOID env_change_value(OBJECT *tree,WORD obj,SLIDEINFO *info);
WORD env_button_cancel(OBJECT *tree,WORD obj,WORD d1,WORD d2,WORD d3);
WORD env_button_ok(OBJECT *tree,WORD obj,WORD d1,WORD d2,WORD d3);
WORD env_button_add(OBJECT *tree,WORD obj,WORD d1,WORD d2,WORD d3);
WORD env_button_delete(OBJECT *tree,WORD obj,WORD d1,WORD d2,WORD d3);
VOID setup_env(ENVINFO *info);
VOID delay_change(OBJECT *tree,WORD obj,SLIDEINFO *info);
VOID pan_change(OBJECT *tree,WORD obj,SLIDEINFO *info);
VOID vol_change(OBJECT *tree,WORD obj,SLIDEINFO *info);
WORD vol_button_ok(OBJECT *tree,WORD obj,WORD d1,WORD d2,WORD d3);

PARAM3 old_patch;
EXTERN PARAM3 *curr_patch; 
EXTERN WORD scrn_w, scrn_h;
EXTERN BYTE *samplenameslist;

EXTERN OBJECT algtree[];

OBJECT envtree[20],voltree[10];
WORD type,max_points;
LONG zero=0L;

VOID handle_modifier(OBJECT *tree,WORD obj,POPUPINFO *info)
{
	
	v_dark_tree(algtree);	
	type=info->index;
	if (!type)
	{
		switch(obj)
		{
			case ALG_FILTERENV:
			case ALG_OSC1ENV:
				handle_constvalue(tree,obj,3);
			break;
		
			case ALG_DISTENV:
				handle_constvalue(tree,obj,4);
			break;

			case ALG_ATTNENV:
				handle_constvalue(tree,obj,2);
			break;
			
			default:
				return;
			break;
		}
		objc_draw(algtree, 0, MAXDEPTH, 0, 0, scrn_w, scrn_h);
		return;
	}	
	memcpy((BYTE *)&old_patch,(BYTE *) curr_patch, (LONG)sizeof(PARAM3));

	if (!(info->user_defined&0x8000))
	{
		type=info->user_defined-2;
		max_points=32;
		if (dspenv2temp(curr_patch->env[type].point, type)!=FAILURE)
		{
			envtree[ENV_ENV].ob_spec.env->patch_env=&curr_patch->env[type];
			calc_envelope(envtree[ENV_ENV].ob_spec.env,0);
			setup_env(envtree[ENV_ENV].ob_spec.env);
			handle_env(tree,obj,info);
		}
	}
	else
	{

/* Call ROM-Wave edit page for LFO
*/
   
		handle_rom(tree,obj,info,info->user_defined&0x7fff);
	}

	objc_draw(algtree, 0, MAXDEPTH, 0, 0, scrn_w, scrn_h);
}


VOID build_jazenv(VOID)
{
	objc_xadd(voltree, NIL, VOL_BOX, 
		G_BOX, -1, -1, 
		117, 20, 150, 160,
		IS3DOBJ|NONE, NORMAL,
		0);	
		objc_xadd(voltree, VOL_BOX, VOL_TITLE, 
			G_XBOXTEXT, -1, -1, 
			0, 0, 112, 14, 
			IS3DOBJ|NONE, NORMAL,
			T_OB_LATTACH, VOL_BOX, 6,		
			T_OB_RATTACH, VOL_BOX, 6,		
			T_OB_UATTACH, VOL_BOX, 2,
			T_BX_TEXT, "MIXER PARAMETERS",
			T_BX_FONTID, FNT_SMALL,
			T_BX_ALIGN, TE_CNTR,
			0);
		objc_xadd(voltree, VOL_BOX, VOL_DELAY,
			G_XREALSLIDER, -1, -1,
			0, 6, 40, 96,
			JOYPAD|SELECTABLE, NORMAL,
			T_OB_UATTACH, VOL_TITLE, 10,		
			T_OB_LATTACH, VOL_BOX, 10,		
			T_OB_DOWN, VOL_BAR,
			T_OB_RIGHT, VOL_PAN,
			T_SLIDE_SLNAME, "DELAY",
			T_SLIDE_MIN, 0L,
			T_SLIDE_MAX, 0x7fL,
			T_SLIDE_CALLBK, delay_change,
			0);
		objc_xadd(voltree, VOL_BOX, VOL_PAN,
			G_XREALSLIDER, -1, -1,
			0, 6, 40, 96,
			JOYPAD|SELECTABLE, NORMAL,
			T_OB_UATTACH, VOL_TITLE, 10,		
			T_OB_LATTACH, VOL_DELAY, 6,		
			T_OB_LEFT, VOL_DELAY,
			T_OB_RIGHT, VOL_VOL,
			T_OB_DOWN, VOL_BAR,
			T_SLIDE_SLNAME, "PAN",
			T_SLIDE_MIN, 0L,
			T_SLIDE_MAX, 0x7fL,
			T_SLIDE_CALLBK, pan_change,
			0);
		objc_xadd(voltree, VOL_BOX, VOL_VOL,
			G_XREALSLIDER, -1, -1,
			0, 6, 40, 96,
			JOYPAD|SELECTABLE, NORMAL,
			T_OB_UATTACH, VOL_TITLE, 10,		
			T_OB_LATTACH, VOL_PAN, 6,		
			T_OB_LEFT, VOL_PAN,
			T_OB_DOWN, VOL_BAR,
			T_SLIDE_SLNAME, "VOLUME",
			T_SLIDE_MIN, 0L,
			T_SLIDE_MAX, 0x7fffL,
			T_SLIDE_CALLBK, vol_change,
			0);

		objc_xadd(voltree, VOL_BOX, VOL_BAR, 
			G_XBAR, -1, -1, 
			6, 0, scrn_w-12, 18,
			IS3DOBJ|JOYPAD|SELECTABLE, NORMAL,
			T_OB_UP, VOL_PAN,
			T_OB_UATTACH, VOL_PAN, 6,
			T_OB_LATTACH, VOL_BOX, 6,
			T_OB_RATTACH, VOL_BOX, 6,
			T_BA_NUM, 1,
			T_BA_FIRST, 1,
			0);
			objc_xadd(voltree, VOL_BAR, VOL_OK, 
				G_XBUTTON, -1, -1, 
				0, 2, 70, 12,
				ABUTTON|IS3DOBJ|SELECTABLE, NORMAL,
				T_BT_TEXT, "OK",
				T_BT_BGCOLOR, 0,
				T_BT_TXTCOLOR, 1,
				T_BT_FONTID, 1,
				T_BT_SMCHAR, '1',
				T_BT_CALLBK, vol_button_ok,
				T_OB_LATTACH, VOL_BAR, 20,
				T_OB_RATTACH, VOL_BAR, 20,
				T_OB_UATTACH, VOL_BAR, 1,
				0);


	objc_xadd(envtree, NIL, ENV_BOX, 
		G_BOX, -1, -1, 
		10, 10, scrn_w - 20, scrn_h - 20,
		IS3DOBJ|NONE, NORMAL,
		0);	

		objc_xadd(envtree, ENV_BOX, ENV_TITLE, 
			G_XBOXTEXT, -1, -1, 
			0, 0, scrn_w-12, 14, 
			IS3DOBJ|NONE, NORMAL,
			T_OB_LATTACH, ENV_BOX, 6,		
			T_OB_RATTACH, ENV_BOX, 6,		
			T_OB_UATTACH, ENV_BOX, 2,
			T_BX_TEXT, "ENVELOPE EDITOR",
			T_BX_FONTID, FNT_SMALL,
			T_BX_ALIGN, TE_CNTR,
			0);

		objc_xadd(envtree, ENV_BOX, ENV_ENV, 
			G_XENVELOPE, -1, -1, 
			0, 0, 256, 100,
			NONE, NORMAL,
			T_OB_UATTACH, ENV_TITLE, 6,		
			T_OB_LATTACH, ENV_BOX, 6,		
			0);
		envtree[ENV_ENV].ob_spec.env->p_obj=ENV_POINT;
		envtree[ENV_ENV].ob_spec.env->t_obj=ENV_TIME;
		envtree[ENV_ENV].ob_spec.env->a_obj=ENV_AMPL;
		
		objc_xadd(envtree, ENV_BOX, ENV_POINT,
			G_XINTSLIDER, -1, -1,
			0, 6, 65, 24,
			JOYPAD|SELECTABLE, NORMAL,
			T_OB_DOWN, ENV_AMPL,
			T_OB_UATTACH, ENV_TITLE, 6,		
			T_OB_LATTACH, ENV_ENV, 10,
			T_INT_SLNAME, "POINT",
			T_INT_MAXINT, (LONG)(envtree[ENV_ENV].ob_spec.env->points+1),
			T_INT_INTSPACES, 6,
			T_INT_FONTID, 0,
			T_INT_CALLBK, env_change_point,
			0);

		objc_xadd(envtree, ENV_BOX, ENV_AMPL,
			G_XINTSLIDER, -1, -1,
			0, 6, 65, 24,
			JOYPAD|SELECTABLE, NORMAL,
			T_OB_DOWN, ENV_TIME,
			T_OB_UP, ENV_POINT,
			T_OB_UATTACH, ENV_POINT, 6,		
			T_OB_LATTACH, ENV_ENV, 10,
			T_INT_SLNAME, "AMPL",
			T_INT_MAXINT, 32767L,
			T_INT_INTSPACES, 6,
			T_INT_FONTID, 0,
			T_INT_CALLBK, env_change_value,
			0);
		objc_xadd(envtree, ENV_BOX, ENV_TIME,
			G_XINTSLIDER, -1, -1,
			0, 6, 65, 24,
			JOYPAD|SELECTABLE, NORMAL,
			T_OB_UP, ENV_AMPL,
			T_OB_DOWN, ENV_LOOP,
			T_OB_UATTACH, ENV_AMPL, 6,		
			T_OB_LATTACH, ENV_ENV, 10,
			T_INT_SLNAME, "TIME IN MS",
			T_INT_MAXINT, 32767L,
			T_INT_INTSPACES, 6,
			T_INT_FONTID, 0,
			T_INT_CALLBK, env_change_value,
			0);
		objc_xadd(envtree, ENV_BOX, ENV_LOOP,
			G_XINTSLIDER, -1, -1,
			0, 6, 65, 24,
			JOYPAD|SELECTABLE, NORMAL,
			T_OB_UP, ENV_TIME,
			T_OB_DOWN, ENV_BAR1,
			T_OB_UATTACH, ENV_TIME, 6,		
			T_OB_LATTACH, ENV_ENV, 10,
			T_INT_SLNAME, "LOOP TO",
			T_INT_MAXINT, (LONG)(envtree[ENV_ENV].ob_spec.env->points-1),
			T_INT_INTSPACES, 6,
			T_INT_FONTID, 0,
			T_INT_CALLBK, env_change_value,
			0);

		objc_xadd(envtree, ENV_BOX, ENV_BAR1, 
			G_XBAR, -1, -1, 
			6, 0, 256, 16,
			IS3DOBJ|JOYPAD|SELECTABLE, NORMAL,
			T_OB_UP, ENV_LOOP,
			T_OB_DOWN, ENV_BAR,
			T_OB_UATTACH, ENV_ENV, 4,
			T_OB_LATTACH, ENV_BOX, 6,
			T_BA_NUM, 2,
			T_BA_FIRST, 1,
			0);

		objc_xadd(envtree, ENV_BAR1, ENV_ADD, 
			G_XBUTTON, -1, -1, 
			0, 2, 70, 9,
			ABUTTON|IS3DOBJ|SELECTABLE, NORMAL,
			T_BT_TEXT, "ADD POINT",
			T_BT_BGCOLOR, 0,
			T_BT_TXTCOLOR, 1,
			T_BT_FONTID, 0,
			T_BT_SMCHAR, '1',
			T_BT_CALLBK, env_button_add,
			T_OB_LATTACH, ENV_BAR1, 4,
			T_OB_UATTACH, ENV_BAR1, 1,
			0);

		objc_xadd(envtree, ENV_BAR1, ENV_DELETE, 
			G_XBUTTON, -1, -1, 
			0, 2, 70, 9,
			ABUTTON|IS3DOBJ|SELECTABLE, NORMAL,
			T_BT_TEXT, "DEL POINT",
			T_BT_BGCOLOR, 0,
			T_BT_TXTCOLOR, 1,
			T_BT_FONTID, 0,
			T_BT_SMCHAR, '2',
			T_BT_CALLBK, env_button_delete,
			T_OB_LATTACH, ENV_ADD, 6,
			T_OB_UATTACH, ENV_BAR1, 1,
			0);


		objc_xadd(envtree, ENV_BOX, ENV_BAR, 
			G_XBAR, -1, -1, 
			6, 0, scrn_w-12, 18,
			IS3DOBJ|JOYPAD|SELECTABLE, NORMAL,
			T_OB_UP, ENV_BAR1,
			T_OB_UATTACH, ENV_BAR1, 6,
			T_OB_LATTACH, ENV_BOX, 6,
			T_OB_RATTACH, ENV_BOX, 6,
			T_BA_NUM, 2,
			T_BA_FIRST, 1,
			0);
			objc_xadd(envtree, ENV_BAR, ENV_CANCEL, 
				G_XBUTTON, -1, -1, 
				0, 2, 70, 12,
				ABUTTON|IS3DOBJ|SELECTABLE, NORMAL,
				T_BT_TEXT, "Cancel",
				T_BT_BGCOLOR, 0,
				T_BT_TXTCOLOR, 1,
				T_BT_FONTID, 1,
				T_BT_SMCHAR, '1',
				T_BT_CALLBK, env_button_cancel,
				T_OB_LATTACH, ENV_BAR, 6,
				T_OB_UATTACH, ENV_BAR, 1,
				0);
			objc_xadd(envtree, ENV_BAR, ENV_OK, 
				G_XBUTTON, -1, -1, 
				0, 2, 70, 12,
				ABUTTON|IS3DOBJ|SELECTABLE, NORMAL,
				T_BT_TEXT, "OK",
				T_BT_BGCOLOR, 0,
				T_BT_TXTCOLOR, 1,
				T_BT_FONTID, 1,
				T_BT_SMCHAR, '2',
				T_BT_CALLBK, env_button_ok,
				T_OB_LATTACH, ENV_CANCEL, 6,
				T_OB_UATTACH, ENV_BAR, 1,
				0);
}

VOID handle_env(OBJECT *tree,WORD obj,POPUPINFO *info)
{

	objc_draw(envtree, 0, MAXDEPTH, 0, 0, scrn_w, scrn_h);
	form_xdo(envtree, ENV_BAR);

}


VOID env_change_point(OBJECT *tree,WORD obj,SLIDEINFO *info)
{
	envtree[ENV_ENV].ob_spec.env->curr_point=envtree[ENV_POINT].ob_spec.intslider->intval;
	setup_env(envtree[ENV_ENV].ob_spec.env);
	objc_draw(envtree, ENV_ENV, MAXDEPTH, 0, 0, scrn_w, scrn_h);
}

VOID env_change_value(OBJECT *tree,WORD obj,SLIDEINFO *info)
{
	WORD p;

	p=envtree[ENV_POINT].ob_spec.intslider->intval;
	envtree[ENV_ENV].ob_spec.env->ampl[p]=(envtree[ENV_AMPL].ob_spec.intslider->intval<<15);
	envtree[ENV_ENV].ob_spec.env->time[p]=(envtree[ENV_TIME].ob_spec.intslider->intval<<15);
	envtree[ENV_ENV].ob_spec.env->loop=envtree[ENV_LOOP].ob_spec.intslider->intval;

	if (p>0)
		if (envtree[ENV_ENV].ob_spec.env->time[p]<envtree[ENV_ENV].ob_spec.env->time[p-1])
			envtree[ENV_ENV].ob_spec.env->time[p]=envtree[ENV_ENV].ob_spec.env->time[p-1];
	calc_envelope(envtree[ENV_ENV].ob_spec.env,1);
	objc_draw(envtree, ENV_ENV, MAXDEPTH, 0, 0, scrn_w, scrn_h);
}


WORD env_button_cancel(OBJECT *tree,WORD obj,WORD d1,WORD d2,WORD d3)
{
/*	REG LONG *d,*s,c;
	
	d=(LONG *)curr_patch;
	s=(LONG *)&old_patch;
	c=21L;
	do
	{
		*d++=*s++;
	} while (--c);*/

	memcpy((BYTE *)curr_patch,(BYTE *)&old_patch,84L);
	cancel_tempenv();
	return -1;
}

WORD env_button_ok(OBJECT *tree,WORD obj,WORD d1,WORD d2,WORD d3)
{
	temp2dspenv(2 * (envtree[ENV_ENV].ob_spec.env->points - 1),
			    curr_patch->env[type].curr,
			    curr_patch->env[type].release);
	return -1;
}

LONG labs(LONG v)
{
	if (v<0L)
		return (-v);
	else
		return (v);
}


VOID setup_env(ENVINFO *info)
{
	WORD p,anz;
	ENVPARA *env;
	
	env=info->patch_env;
	p=envtree[ENV_POINT].ob_spec.intslider->intval;
	anz=envtree[ENV_ENV].ob_spec.env->points;

	envtree[ENV_LOOP].ob_spec.intslider->intval=(LONG)info->loop;
			
	envtree[ENV_AMPL].ob_spec.intslider->intval=
					envtree[ENV_ENV].ob_spec.env->ampl[p]>>15;
	objc_draw(envtree, ENV_AMPL, MAXDEPTH, 0, 0, scrn_w, scrn_h);
	envtree[ENV_TIME].ob_spec.intslider->intval=
					envtree[ENV_ENV].ob_spec.env->time[p]>>15;

	if (p<anz-1)
	{
		envtree[ENV_TIME].ob_spec.intslider->maxint=envtree[ENV_ENV].ob_spec.env->time[p+1]>>15;
	}
	else
	{
		envtree[ENV_TIME].ob_spec.intslider->maxint=32767L;
	}

	objc_draw(envtree, ENV_TIME, MAXDEPTH, 0, 0, scrn_w, scrn_h);

	if (anz==max_points)
		envtree[ENV_ADD].ob_state|=DISABLED;
	else
		envtree[ENV_ADD].ob_state&=~DISABLED;

	if (!p || p==anz-1 || anz<=3)
		envtree[ENV_DELETE].ob_state|=DISABLED;
	else
		envtree[ENV_DELETE].ob_state&=~DISABLED;

	objc_draw(envtree, ENV_ADD, MAXDEPTH, 0, 0, scrn_w, scrn_h);
	objc_draw(envtree, ENV_DELETE, MAXDEPTH, 0, 0, scrn_w, scrn_h);
}


VOID calc_envelope(ENVINFO *info,WORD f)
{
	LONG old,new,slope,dest,j,t;
	LONG *poi;
	ENVPARA *para;
	
	para=info->patch_env;
	if (!f)
	{
		old=info->ampl[0]=para->curr;
		t=info->time[0]=0L;

		j=1;
		poi=para->point;

		for(;;)
		{
			slope=(*poi++);
			if (!slope) slope=0x8000L;
			dest=(*poi++);
			if (dest>=0x40000000L)
			{
				info->loop=((WORD)(dest&0xffff)-1)/2;
				break;
			}
			
			new=info->ampl[j]=dest;		
			t+=labs(new-old)/(labs(slope)>>15)/3L;
			info->time[j++]=t;

			old=new;
		}
		slope=para->release;
		if (!slope) slope=0x8000L;

		new=info->ampl[j]=0L;		
		t+=labs(new-old)/(labs(slope)>>15)/3L;
		info->time[j++]=t;

		info->points=j;

/* FOR NOW !!!!!!!!!!!!!*/
/*		max_points=j;*/
		
		envtree[ENV_POINT].ob_spec.intslider->maxint=j-1;
		envtree[ENV_LOOP].ob_spec.intslider->maxint=j-1;
	}
	else
	{
		old=para->curr=info->ampl[0];
		poi=para->point;
	
		for (j=1;j<info->points-1;j++)
		{
			new=info->ampl[j];
			t=info->time[j]-info->time[j-1];
			if (!t)	t=1L;
							
			slope=((new-old)/3L/t)<<15;		
			if (!slope)
				slope=((new-old+0x8000)/3L/t)<<15;		
			*poi++=slope;
			*poi++=info->ampl[j];
			old=new;
		}
		*poi++=zero;
		*poi=0x40000000+2L*(LONG)info->loop+1L;
		t=info->time[j]-info->time[j-1];
		if (t<0xa0000L)	t=0xa0000L;
		para->release=(-new/3L/t)<<15;
	}

}

WORD env_button_add(OBJECT *tree,WORD obj,WORD d1,WORD d2,WORD d3)
{
	WORD p,anz,i;

	p=envtree[ENV_POINT].ob_spec.intslider->intval;
	anz=envtree[ENV_ENV].ob_spec.env->points;

	if (anz<max_points)
	{
		anz++;	
		if (!p)
			p++;
				
		for (i=max_points-1;i>p;i--)
		{
			envtree[ENV_ENV].ob_spec.env->time[i]=envtree[ENV_ENV].ob_spec.env->time[i-1];
			envtree[ENV_ENV].ob_spec.env->ampl[i]=envtree[ENV_ENV].ob_spec.env->ampl[i-1];
		}
		
		
		envtree[ENV_ENV].ob_spec.env->time[p]=(envtree[ENV_ENV].ob_spec.env->time[p+1]-
											   envtree[ENV_ENV].ob_spec.env->time[p-1])/2+		
											   envtree[ENV_ENV].ob_spec.env->time[p-1];
		envtree[ENV_ENV].ob_spec.env->ampl[p]=(envtree[ENV_ENV].ob_spec.env->ampl[p+1]-
											   envtree[ENV_ENV].ob_spec.env->ampl[p-1])/2+		
											   envtree[ENV_ENV].ob_spec.env->ampl[p-1];
											   
		envtree[ENV_ENV].ob_spec.env->points=anz;				
		envtree[ENV_POINT].ob_spec.intslider->maxint=anz-1;
		envtree[ENV_LOOP].ob_spec.intslider->maxint=anz-1;
		calc_envelope(envtree[ENV_ENV].ob_spec.env,1);
/*		objc_draw(envtree, ENV_TIME, MAXDEPTH, 0, 0, scrn_w, scrn_h);
		objc_draw(envtree, ENV_AMPL, MAXDEPTH, 0, 0, scrn_w, scrn_h);*/
		objc_draw(envtree, ENV_ENV, MAXDEPTH, 0, 0, scrn_w, scrn_h);
		setup_env(envtree[ENV_ENV].ob_spec.env);
	}
}

WORD env_button_delete(OBJECT *tree,WORD obj,WORD d1,WORD d2,WORD d3)
{
	WORD p,anz,i;
	
	p=envtree[ENV_POINT].ob_spec.intslider->intval;
	anz=envtree[ENV_ENV].ob_spec.env->points;
	if (p && p<anz-1)
	{
		anz--;	
		for (i=p;i<max_points-1;i++)
		{
			envtree[ENV_ENV].ob_spec.env->time[i]=envtree[ENV_ENV].ob_spec.env->time[i+1];
			envtree[ENV_ENV].ob_spec.env->ampl[i]=envtree[ENV_ENV].ob_spec.env->ampl[i+1];
		}

		envtree[ENV_ENV].ob_spec.env->points=anz;				
		envtree[ENV_POINT].ob_spec.intslider->maxint=anz-1;
		envtree[ENV_LOOP].ob_spec.intslider->maxint=anz-1;
		calc_envelope(envtree[ENV_ENV].ob_spec.env,1);
/*		objc_draw(envtree, ENV_TIME, MAXDEPTH, 0, 0, scrn_w, scrn_h);
		objc_draw(envtree, ENV_AMPL, MAXDEPTH, 0, 0, scrn_w, scrn_h);*/
		objc_draw(envtree, ENV_ENV, MAXDEPTH, 0, 0, scrn_w, scrn_h);
		setup_env(envtree[ENV_ENV].ob_spec.env);
	}
}

VOID adjust_volume(OBJECT *tree,WORD obj,POPUPINFO *info)
{

	v_dark_tree(algtree);	

	voltree[VOL_DELAY].ob_spec.slider->curr=curr_patch->delay;
	voltree[VOL_PAN].ob_spec.slider->curr=curr_patch->pan;
	voltree[VOL_VOL].ob_spec.slider->curr=curr_patch->volume&0xffff;
	
	objc_draw(voltree, 0, MAXDEPTH, 0, 0, scrn_w, scrn_h);
	form_xdo(voltree, ENV_BAR);

	objc_draw(algtree, 0, MAXDEPTH, 0, 0, scrn_w, scrn_h);

}

VOID delay_change(OBJECT *tree,WORD obj,SLIDEINFO *info)
{
	curr_patch->delay=info->curr;
}

VOID pan_change(OBJECT *tree,WORD obj,SLIDEINFO *info)
{
	curr_patch->pan=info->curr;
}

VOID vol_change(OBJECT *tree,WORD obj,SLIDEINFO *info)
{
	curr_patch->volume=info->curr;
}

WORD vol_button_ok(OBJECT *tree,WORD obj,WORD d1,WORD d2,WORD d3)
{
	return -1;
}
