// ----
// ---- file   : tksampleedit.tks
// ---- author : Bastian Spiegel <bs@tkscript.de>
// ---- legal  : (c) 2009-2010 by Bastian Spiegel. 
// ----          Distributed under terms of the GNU LESSER GENERAL PUBLIC LICENSE (LGPL). See 
// ----          http://www.gnu.org/licenses/licenses.html#LGPL or COPYING for further information.
// ----
// ---- info   : 
// ----
// ---- created: 25Oct2009
// ---- changed: 11May2010, 20Jul2010, 25Feb2011, 04May2013, 12Feb2014, 23Feb2018, 24Feb2018
// ----          30Mar2018, 09Apr2018, 22Jun2018, 18Dec2018, 30Dec2018, 04May2019, 06May2019
// ----          09May2019
// ----
// ----
// ----

#ifndef __TKSAMPLEEDIT_H__
#define __TKSAMPLEEDIT_H__


#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define YAC_PRINTF defined
#define YAC_BIGSTRING
#include <yac.h>

#include "tks-opengl.h"


YG("tksampleedit");


// called by plugin init
extern void tksampleedit_rms_init(void);


/* @function tksampleedit_render_sample,FloatArray samples,int numCh,int sampleOffset,int numSamples,float w,float h,int renderStyle,int interpolStyle
Render a floating point waveform

@arg samples
@arg numCh
@arg frameOffset
@arg numFrames
@arg w
@arg h
@arg renderStyle
@arg interpolStyle
*/
YF void YAC_CALL tksampleedit_render_sample(YAC_Object *_data, sUI _numCh, sUI _frameOffset, sUI _numFrames, sF32 _w, sF32 _h, sF32 _min, sF32 _max, sSI _renderStyle, sSI _interpolStyle, sUI _baseColor);


/* @function tksampleedit_render_rms,FloatArray samples,int numCh,int sampleOffset,int numSamples,float w,float h
Render root mean square (RMS) of a floating point waveform 

*/
YF void YAC_CALL tksampleedit_render_rms(YAC_Object *_data, sUI _numCh, sUI _frameOffset, sUI _numFrames, sF32 _w, sF32 _h, sF32 _min, sF32 _max);


YF void YAC_CALL tksampleedit_render_mono_sample_1d_8bit(YAC_Object *_byteArray, sUI _width, YAC_Object *_fa, sUI _frameOffset, sUI _numFrames, sF32 _lumScl);


/* @function tksampleedit_calc_rms,FloatArray samples,int numCh:float
Calculate root mean square (RMS) of sample array.

@arg samples
@arg numCh
@return Root mean square
*/
YF sF32 YAC_CALL tksampleedit_calc_rms(YAC_Object *_fa, sUI _numCh);


YF void YAC_CALL tksampleedit_ringbuf_copy_samples(YAC_Object *_dst, sUI _dstNumCh, sUI _dstChOff, sUI _dstFrameOff, sUI _numFrames, sUI _numCh, YAC_Object *_src, sUI _srcNumCh, sUI _srcChOff, sUI _srcFrameOff);

YF void YAC_CALL tksampleedit_copy_looped_samples_mono(YAC_Object *_dst, sSI _dstFrameOff, YAC_Object *_src, sSI _srcFrameOff, sSI _loopStart, sSI _loopEnd, sUI _numFrames);

YF sF32 YAC_CALL tksampleedit_buf_get_max(YAC_Object *_src, sUI _numFrames, sUI _numCh, sUI _chOff);

// used by Audio.FillStreamBuffer()
YF void YAC_CALL tksampleedit_mix_stereo_input_buf(YAC_Object *_faOutput, YAC_Object *_faInput, YAC_Object *_faMixerLevels, sUI _numInputChannels, sUI _numOutputChannels, sUI _numFrames, sUI _outChOff, sUI _inChOff);

// used by Audio.ProcessSampleFragment()
YF void YAC_CALL tksampleedit_mixclip_stereo_mixbuf(YAC_Object *_faOutput, YAC_Object *_faMixBuf, sF32 _levelL, sF32 _levelR, sF32 _clipLevel, sUI _numOutputChannels, sUI _numFrames, sUI _outChOff);

// used by Eureka Replay/Track/Lane
//  (note) (outoff/inoff are #samples
//  (note) no type or bounds checks
YF void YAC_CALL tksampleedit_fill_mono(YAC_Object *_faOut, sUI _outOff, sF32 _val, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_mono_to_mono(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _numFrames);

YF void YAC_CALL tksampleedit_amp_mono_to_mono(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _numFrames, sF32 _amp);

YF void YAC_CALL tksampleedit_copy_mono_adv_to_mono(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_mono_adv_to_mono_adv(YAC_Object *_faOut, sUI _outOff, sUI _outAdv, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_mono_adv_to_mono_adv_ring(YAC_Object *_faOut, sUI _outOff, sUI _outAdv, sUI _outRingSize, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_mono_adv_ring_to_mono_adv(YAC_Object *_faOut, sUI _outOff, sUI _outAdv, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sUI _inRingSize, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_mono_to_dualmono(YAC_Object *_faOutL, YAC_Object *_faOutR, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_mono_adv_to_dualmono(YAC_Object *_faOutL, YAC_Object *_faOutR, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_mono_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_pan_mono_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_dualmono_to_dualmono(YAC_Object *_faOutL, YAC_Object *_faOutR, sUI _outOff, YAC_Object *_faInL, YAC_Object *_faInR, sUI _inOff, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_dualmono_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faInL, YAC_Object *_faInR, sUI _inOff, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_pan_dualmono_to_dualmono(YAC_Object *_faOutL, YAC_Object *_faOutR, sUI _outOff, YAC_Object *_faInL, YAC_Object *_faInR, sUI _inOff, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_pan_stereowidth_dualmono_to_dualmono(YAC_Object *_faOutL, YAC_Object *_faOutR, sUI _outOff, YAC_Object *_faInL, YAC_Object *_faInR, sUI _inOff, sF32 _lvlL, sF32 _lvlR, sF32 _stereoWidth, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_pan_dualmono_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faInL, YAC_Object *_faInR, sUI _inOff, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_stereo_to_dualmono(YAC_Object *_faOutL, YAC_Object *_faOutR, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_stereo_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_pan_stereo_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_pan_stereo_adv_to_stereo_adv(YAC_Object *_faOut, sUI _outOff, sUI _outAdv, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_copy_pan_tanh_dim_stereo_adv_to_stereo_adv(YAC_Object *_faOut, sUI _outOff, sUI _outAdv, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sF32 _lvlL, sF32 _lvlR, sF32 _dim, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_pan_mono_adv_to_dualmono(YAC_Object *_faOutL, YAC_Object *_faOutR, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_dualmono_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faInL, YAC_Object *_faInR, sUI _inOff, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_stereo_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_amp_mono_adv_to_mono_adv(YAC_Object *_faOut, sUI _outOff, sUI _outAdv, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sF32 _lvl, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_mono_to_mono(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _numFrames);

// (note) (numFrames-xfadeFrames) samples will be written to dstSmp
YF void YAC_CALL tksampleedit_xfade_mono(YAC_Object *_faOut, YAC_Object *_faIn, sUI _numFrames, sUI _xfadeFrames);

YF void YAC_CALL tksampleedit_add_pan_mono_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_pan_mono_adv_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_pan_mono_adv_to_stereo_adv(YAC_Object *_faOut, sUI _outOff, sUI _outAdv, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_pan_mono_to_stereo_adv(YAC_Object *_faOut, sUI _outOff, sUI _outAdv, YAC_Object *_faIn, sUI _inOff, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_pan_dualmono_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faInL, YAC_Object *_faInR, sUI _inOff, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_pan_dualmono_to_stereo_adv(YAC_Object *_faOut, sUI _outOff, sUI _outAdv, YAC_Object *_faInL, YAC_Object *_faInR, sUI _inOff, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_pan_stereo_to_dualmono(YAC_Object *_faOutL, YAC_Object *_faOutR, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_pan_stereo_to_stereo(YAC_Object *_faOut, sUI _outOff, YAC_Object *_faIn, sUI _inOff, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF void YAC_CALL tksampleedit_add_pan_stereo_adv_to_stereo_adv(YAC_Object *_faOut, sUI _outOff, sUI _outAdv, YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sF32 _lvlL, sF32 _lvlR, sUI _numFrames);

YF sF32 YAC_CALL tksampleedit_calc_peak_mono_adv(YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sUI _numFrames);

YF sF32 YAC_CALL tksampleedit_calc_rms_mono_adv(YAC_Object *_faIn, sUI _inOff, sUI _inAdv, sUI _numFrames);

YF void YAC_CALL tksampleedit_ms_convert_dualmono(YAC_Object *_faOutL, YAC_Object *_faOutR, sUI _outOff, YAC_Object *_faInL, YAC_Object *_faInR, sUI _inOff, sF32 _lvlOut, sUI _numFrames);

YF void YAC_CALL tksampleedit_monomix_restore_dualmono(YAC_Object *_faMonoMixOut, YAC_Object *_faRestoreOut, YAC_Object *_faInL, YAC_Object *_faInR, YAC_Object *_faRestoreIn, sUI _numFrames);


YF void YAC_CALL tksampleedit_autochop(YAC_Object *_retStartOffsets, YAC_Object *_retEndOffsets, YAC_Object *_smp, sUI _numChannels, sF32 _startThreshold, sF32 _startMinThreshold, sUI _startMinNumFramesBack, sF32 _endThreshold, sUI _endNumFramesThreshold, sF32 _endThresholdRestart, sUI _restartNumFramesThreshold);

YF sUI YAC_CALL tksampleedit_find_zerocrossing_near (YAC_Object *_fa, sUI _chOff, sUI _numChannels, sUI _off);


#endif // __TKSAMPLEEDIT_H__
