int effect_update(struct razer_fx_render_node *render)
{
	float percentage = daemon_get_parameter_float(daemon_effect_get_parameter_by_index(render->effect,0));
	int x,y;
	struct razer_rgb col;
	#ifdef USE_DEBUGGING
		printf(" (Bar.%d ## %%:%f)",render->id,percentage);
	#endif

	int xmax = (int)((21.0f / 100.0f) * percentage);
	for(x=0;x<render->device->columns_num;x++)
		for(y=0;y<render->device->rows_num;y++)
		{
			float dist = 0.0f;
			if(x-xmax <= 0)
				dist = 1.0f;
			else
			{
				dist = (21.0f / (x-xmax))/21.0f;
			}
			rgb_from_hue(dist,0.3f,0.0f,&col);
			rgb_mix_into(&render->output_frame->rows[y]->column[x],&render->input_frame->rows[y]->column[x],&col,render->opacity);//*render->opacity  //&render->second_input_frame->rows[y]->column[x]
			render->output_frame->update_mask |= 1<<y;
		}
	return(1);
}
char *daemon_parameter_to_json(struct razer_parameter *parameter)
{
	char *parameter_json = str_CreateEmpty();
	parameter_json = str_CatFree(parameter_json,"{\n \"key\": \"");
	parameter_json = str_CatFree(parameter_json,parameter->key);
	parameter_json = str_CatFree(parameter_json,"\",\n");
	parameter_json = str_CatFree(parameter_json," \"id\" : ");
	char *id_string = str_FromLong(parameter->id);
	parameter_json = str_CatFree(parameter_json,id_string);
	parameter_json = str_CatFree(parameter_json," ,\n");
	free(id_string);
	parameter_json = str_CatFree(parameter_json," \"type\" : ");
	char *type_string = str_FromLong(parameter->type);
	parameter_json = str_CatFree(parameter_json,type_string);
	parameter_json = str_CatFree(parameter_json," ,\n");
	free(type_string);
	parameter_json = str_CatFree(parameter_json," \"value\" : ");
	switch(parameter->type)
	{
		case RAZER_PARAMETER_TYPE_STRING:
			{
				parameter_json = str_CatFree(parameter_json,"\"");
				parameter_json = str_CatFree(parameter_json,daemon_get_parameter_string(parameter));
				parameter_json = str_CatFree(parameter_json,"\" ,\n");
			}
			break;
		case RAZER_PARAMETER_TYPE_INT:
			{
				char *int_string = str_FromLong(daemon_get_parameter_int(parameter));
				parameter_json = str_CatFree(parameter_json,int_string);
				free(int_string);
				parameter_json = str_CatFree(parameter_json," ,\n");
			}
			break;
		case RAZER_PARAMETER_TYPE_UINT:
			{
				char *int_string = str_FromLong(daemon_get_parameter_int(parameter));
				parameter_json = str_CatFree(parameter_json,int_string);
				free(int_string);
				parameter_json = str_CatFree(parameter_json," ,\n");
			}
			break;
		case RAZER_PARAMETER_TYPE_FLOAT:
			{
				char *float_string = str_FromDouble(daemon_get_parameter_float(parameter));
				parameter_json = str_CatFree(parameter_json,float_string);
				free(float_string);
				parameter_json = str_CatFree(parameter_json," ,\n");
			}
			break;
		case RAZER_PARAMETER_TYPE_RGB:
			{
				parameter_json = str_CatFree(parameter_json,"[");
				struct razer_rgb *color = daemon_get_parameter_rgb(parameter);
				char *r_string = str_FromLong(color->r);
				parameter_json = str_CatFree(parameter_json,r_string);
				parameter_json = str_CatFree(parameter_json," ,");
				free(r_string);
				char *g_string = str_FromLong(color->g);
				parameter_json = str_CatFree(parameter_json,g_string);
				parameter_json = str_CatFree(parameter_json," ,");
				free(g_string);
				char *b_string = str_FromLong(color->b);
				parameter_json = str_CatFree(parameter_json,b_string);
				free(b_string);
				parameter_json = str_CatFree(parameter_json,"] ,\n");
			}
			break;
		case RAZER_PARAMETER_TYPE_POS:
			{
				parameter_json = str_CatFree(parameter_json,"[");
				struct razer_pos *pos = daemon_get_parameter_pos(parameter);
				char *x_string = str_FromLong(pos->x);
				parameter_json = str_CatFree(parameter_json,x_string);
				parameter_json = str_CatFree(parameter_json," ,");
				free(x_string);
				char *y_string = str_FromLong(pos->y);
				parameter_json = str_CatFree(parameter_json,y_string);
				free(y_string);
				parameter_json = str_CatFree(parameter_json,"] ,\n");
			}
			break;
		case RAZER_PARAMETER_TYPE_RENDER_NODE:
			{
				struct razer_fx_render_node *render_node = daemon_get_parameter_render_node(parameter);
				char *render_node_string = str_FromLong(render_node->id);
				parameter_json = str_CatFree(parameter_json,render_node_string);
				free(render_node_string);
				parameter_json = str_CatFree(parameter_json," ,\n");
			}
			break;
		case RAZER_PARAMETER_TYPE_FLOAT_RANGE:
			{
				parameter_json = str_CatFree(parameter_json,"[");
				struct razer_float_range *range = daemon_get_parameter_float_range(parameter);
				char *min_string = str_FromDouble(range->min);
				parameter_json = str_CatFree(parameter_json,min_string);
				parameter_json = str_CatFree(parameter_json," ,");
				free(min_string);
				char *max_string = str_FromDouble(range->max);
				parameter_json = str_CatFree(parameter_json,max_string);
				free(max_string);
				parameter_json = str_CatFree(parameter_json,"] ,\n");
			}
			break;
		case RAZER_PARAMETER_TYPE_INT_RANGE:
			{
				parameter_json = str_CatFree(parameter_json,"[");
				struct razer_int_range *range = daemon_get_parameter_int_range(parameter);
				char *min_string = str_FromLong(range->min);
				parameter_json = str_CatFree(parameter_json,min_string);
				parameter_json = str_CatFree(parameter_json," ,");
				free(min_string);
				char *max_string = str_FromLong(range->max);
				parameter_json = str_CatFree(parameter_json,max_string);
				free(max_string);
				parameter_json = str_CatFree(parameter_json,"] ,\n");
			}
			break;
		case RAZER_PARAMETER_TYPE_UINT_RANGE:
			{
				parameter_json = str_CatFree(parameter_json,"[");
				struct razer_uint_range *range = daemon_get_parameter_uint_range(parameter);
				char *min_string = str_FromLong(range->min);
				parameter_json = str_CatFree(parameter_json,min_string);
				parameter_json = str_CatFree(parameter_json," ,");
				free(min_string);
				char *max_string = str_FromLong(range->max);
				parameter_json = str_CatFree(parameter_json,max_string);
				free(max_string);
				parameter_json = str_CatFree(parameter_json,"] ,\n");
			}
			break;
		case RAZER_PARAMETER_TYPE_RGB_RANGE:
			{
				parameter_json = str_CatFree(parameter_json,"[");
				struct razer_rgb_range *range = daemon_get_parameter_rgb_range(parameter);
				struct razer_rgb *min = range->min;
				parameter_json = str_CatFree(parameter_json,"{\n\"R\" : ");
				char *min_r_string = str_FromLong(min->r);
				parameter_json = str_CatFree(parameter_json,min_r_string);
				parameter_json = str_CatFree(parameter_json," ,\n\"G\" : ");
				free(min_r_string);
				char *min_g_string = str_FromLong(min->g);
				parameter_json = str_CatFree(parameter_json,min_g_string);
				parameter_json = str_CatFree(parameter_json," ,\n\"B\" : ");
				free(min_g_string);
				char *min_b_string = str_FromLong(min->b);
				parameter_json = str_CatFree(parameter_json,min_b_string);
				free(min_b_string);
				parameter_json = str_CatFree(parameter_json,"} ,\n");
				parameter_json = str_CatFree(parameter_json," ,");
				parameter_json = str_CatFree(parameter_json,"{");
				struct razer_rgb *max = range->max;
				parameter_json = str_CatFree(parameter_json,"{\n\"R\" : ");
				char *max_r_string = str_FromLong(max->r);
				parameter_json = str_CatFree(parameter_json,max_r_string);
				parameter_json = str_CatFree(parameter_json," ,\n\"G\" : ");
				free(max_r_string);
				char *max_g_string = str_FromLong(max->g);
				parameter_json = str_CatFree(parameter_json,max_g_string);
				parameter_json = str_CatFree(parameter_json," ,\n\"B\" : ");
				free(max_g_string);
				char *max_b_string = str_FromLong(max->b);
				parameter_json = str_CatFree(parameter_json,max_b_string);
				free(max_b_string);
				parameter_json = str_CatFree(parameter_json,"} ,\n");
				parameter_json = str_CatFree(parameter_json,"] ,\n");
			}
			break;
		case RAZER_PARAMETER_TYPE_POS_RANGE:
			{
				parameter_json = str_CatFree(parameter_json,"[");
				struct razer_pos_range *range = daemon_get_parameter_pos_range(parameter);
				struct razer_pos *min = range->min;
				parameter_json = str_CatFree(parameter_json,"{\n\"X\" : ");
				char *min_x_string = str_FromLong(min->x);
				parameter_json = str_CatFree(parameter_json,min_x_string);
				parameter_json = str_CatFree(parameter_json," ,\n\"Y\" : ");
				free(min_x_string);
				char *min_y_string = str_FromLong(min->y);
				parameter_json = str_CatFree(parameter_json,min_y_string);
				free(min_y_string);
				parameter_json = str_CatFree(parameter_json,"} ,\n");
				parameter_json = str_CatFree(parameter_json," ,");
				parameter_json = str_CatFree(parameter_json,"{");
				struct razer_pos *max = range->max;
				parameter_json = str_CatFree(parameter_json,"{\n\"X\" : ");
				char *max_x_string = str_FromLong(max->x);
				parameter_json = str_CatFree(parameter_json,max_x_string);
				parameter_json = str_CatFree(parameter_json," ,\n\"Y\" : ");
				free(max_x_string);
				char *max_y_string = str_FromLong(max->y);
				parameter_json = str_CatFree(parameter_json,max_y_string);
				free(max_y_string);
				parameter_json = str_CatFree(parameter_json,"} ,\n");
				parameter_json = str_CatFree(parameter_json,"] ,\n");
			}
			break;
		/*case RAZER_PARAMETER_TYPE_INT_ARRAY:
		case RAZER_PARAMETER_TYPE_UINT_ARRAY:
		case RAZER_PARAMETER_TYPE_FLOAT_ARRAY:
		case RAZER_PARAMETER_TYPE_RGB_ARRAY:
		case RAZER_PARAMETER_TYPE_POS_ARRAY:
		*/
	}

	parameter_json = str_CatFree(parameter_json," \"description\": \"");
	parameter_json = str_CatFree(parameter_json,parameter->description);
	parameter_json = str_CatFree(parameter_json,"\" },\n");
	return(parameter_json);
}
int effect_update(struct razer_fx_render_node *render)
{
	float magnitude = daemon_get_parameter_float(daemon_effect_get_parameter_by_index(render->effect,1));
	int x,y;
	struct razer_rgb col;
	#ifdef USE_DEBUGGING
		printf(" (Fft.%d ## %%:%f)",render->id);
	#endif

	unsigned long samples_left=0;
	while((samples_left=wav_samples_left(effect_input_file)))
	{
		unsigned int sample = read_wav_stereo_sample(effect_input_file);
		short high = sample >> 16;
		short low = sample & 0xFFFF;
		//add sample to fft buffer
		effect_fft_in[effect_fft_samples_used][0] = (double)high * effect_fft_hamming_buffer[effect_fft_samples_used];///(double)32768;//* windowHanning(step++, N);
  		effect_fft_in[effect_fft_samples_used++][1] = 0.0f;
		//enough samples gathered?
		if(effect_fft_samples_used==effect_fft_samples)
  		{
  			printf("Computing fft, still %d samples left\n",samples_left);
			//compute fft
    		effect_fft_plan = fftw_plan_dft_1d(effect_fft_samples, effect_fft_in, effect_fft_out, FFTW_FORWARD, FFTW_ESTIMATE);
    		fftw_execute(effect_fft_plan);
    		fftw_destroy_plan(effect_fft_plan);
    		effect_fft_samples_used = 0;
 		    double tmp_magnitude = sqrt(effect_fft_out[0][0]*effect_fft_out[0][0] + effect_fft_out[0][1]*effect_fft_out[0][1]);
		    tmp_magnitude = 10./log(10.) * log(tmp_magnitude + 1e-6);
    		printf("new fft mag db:%f\n",tmp_magnitude);
    		double sum = 0.0f;
    		for(int i=0;i<effect_fft_samples/2;i++)
    		{
	 		    double tmp_bin_magnitude = sqrt(effect_fft_out[i][0]*effect_fft_out[i][0] + effect_fft_out[i][1]*effect_fft_out[i][1]);
			    tmp_bin_magnitude = 10./log(10.) * log(tmp_bin_magnitude + 1e-6);
			    sum += tmp_bin_magnitude;
    		}
    		printf("sum:%f\n",sum/(effect_fft_samples/2));
    		magnitude = (float)tmp_magnitude - 50.0f;
    		break;
  		}
	}
	if(!samples_left)
	{
		#ifdef USE_DEBUGGING
			printf("no samples left to analyze, closing input file\n");
		#endif
		close_wav(effect_input_file);
		effect_input_file = NULL;
		return(0);
	}


	//set color to avg magnitude ,transformed to 0.0-1.0 space	

	//calculate hue from magnitude
	rgb_from_hue(magnitude/96,0.3f,0.0f,&col);

	for(x=0;x<22;x++)
		for(y=0;y<6;y++)
		{
			rgb_mix_into(&render->output_frame->rows[y].column[x],&render->input_frame->rows[y].column[x],&col,render->opacity);//*render->opacity  //&render->second_input_frame->rows[y].column[x]
			render->output_frame->update_mask |= 1<<y;
		}
	daemon_set_parameter_float(daemon_effect_get_parameter_by_index(render->effect,1),magnitude);	
	return(1);
}
struct razer_parameter *daemon_copy_parameter(struct razer_parameter *parameter)
{
	struct razer_parameter *copy = daemon_create_parameter();
	copy->id = parameter->id;
	copy->key = str_Copy(parameter->key);
	copy->description = str_Copy(parameter->description);
	copy->type = parameter->type;
	switch(parameter->type)
	{
		case RAZER_PARAMETER_TYPE_STRING:
			daemon_set_parameter_string(copy,str_Copy(daemon_get_parameter_string(parameter)));
			break;
		case RAZER_PARAMETER_TYPE_INT:
			daemon_set_parameter_int(copy,daemon_get_parameter_int(parameter));
			break;
		case RAZER_PARAMETER_TYPE_FLOAT:
			daemon_set_parameter_float(copy,daemon_get_parameter_float(parameter));
			break;
		case RAZER_PARAMETER_TYPE_RGB:
			{
				struct razer_rgb *color = rgb_copy(daemon_get_parameter_rgb(parameter));
				daemon_set_parameter_rgb(copy,color);
			}
			break;
		case RAZER_PARAMETER_TYPE_POS:
			{
				struct razer_pos *pos = razer_pos_copy(daemon_get_parameter_pos(parameter));
				daemon_set_parameter_pos(copy,pos);
			}
			break;
		case RAZER_PARAMETER_TYPE_RENDER_NODE:
			daemon_set_parameter_render_node(copy,daemon_get_parameter_render_node(parameter));
			break;
		case RAZER_PARAMETER_TYPE_FLOAT_RANGE:
			{
				struct razer_float_range *range = razer_float_range_copy(daemon_get_parameter_float_range(parameter));
				daemon_set_parameter_float_range(copy,range);
			}
			break;
		case RAZER_PARAMETER_TYPE_INT_RANGE:
			{
				struct razer_int_range *range = razer_int_range_copy(daemon_get_parameter_int_range(parameter));
				daemon_set_parameter_int_range(copy,range);
			}
			break;
		case RAZER_PARAMETER_TYPE_UINT_RANGE:
			{
				struct razer_uint_range *range = razer_uint_range_copy(daemon_get_parameter_uint_range(parameter));
				daemon_set_parameter_uint_range(copy,range);
			}
			break;
		case RAZER_PARAMETER_TYPE_RGB_RANGE:
			{
				struct razer_rgb_range *range = razer_rgb_range_copy(daemon_get_parameter_rgb_range(parameter));
				daemon_set_parameter_rgb_range(copy,range);
			}
			break;
		case RAZER_PARAMETER_TYPE_POS_RANGE:
			{
				struct razer_pos_range *range = razer_pos_range_copy(daemon_get_parameter_pos_range(parameter));
				daemon_set_parameter_pos_range(copy,range);
			}
			break;
		case RAZER_PARAMETER_TYPE_FLOAT_ARRAY:
			{
				struct razer_float_array *array = razer_float_array_copy(daemon_get_parameter_float_array(parameter));
				daemon_set_parameter_float_array(copy,array);
			}
			break;
		case RAZER_PARAMETER_TYPE_INT_ARRAY:
			{
				struct razer_int_array *array = razer_int_array_copy(daemon_get_parameter_int_array(parameter));
				daemon_set_parameter_int_array(copy,array);
			}
			break;
		case RAZER_PARAMETER_TYPE_UINT_ARRAY:
			{
				struct razer_uint_array *array = razer_uint_array_copy(daemon_get_parameter_uint_array(parameter));
				daemon_set_parameter_uint_array(copy,array);
			}
			break;
		case RAZER_PARAMETER_TYPE_RGB_ARRAY:
			{
				struct razer_rgb_array *array = razer_rgb_array_copy(daemon_get_parameter_rgb_array(parameter));
				daemon_set_parameter_rgb_array(copy,array);
			}
			break;
		case RAZER_PARAMETER_TYPE_POS_ARRAY:
			{
				struct razer_pos_array *array = razer_pos_array_copy(daemon_get_parameter_pos_array(parameter));
				daemon_set_parameter_pos_array(copy,array);
			}
			break;
	}
	
	return(copy);
}