/* update control state for effects processors.  this is called once per envelope */
        /* tick, and constitutes the first half of the control-update cycle. */
        /* returns true if successful, or false if it failed. */
        public static SynthErrorCodes UpdateStateTrackEffectGenerator(
            TrackEffectGenRec Generator,
            SynthParamRec SynthParams)
        {
            if (Generator.List != null)
            {
                AccentRec Accents = new AccentRec();
                InitializeAccent(
                    ref Accents,
                    Generator.Accents0.Current,
                    Generator.Accents1.Current,
                    Generator.Accents2.Current,
                    Generator.Accents3.Current,
                    Generator.Accents4.Current,
                    Generator.Accents5.Current,
                    Generator.Accents6.Current,
                    Generator.Accents7.Current);

                OneEffectRec Scan = Generator.List;
                while (Scan != null)
                {
                    SynthErrorCodes error = Scan.u.TrackUpdateState(
                        ref Accents,
                        SynthParams);
                    if (error != SynthErrorCodes.eSynthDone)
                    {
                        return(error);
                    }
                    Scan = Scan.Next;
                }
            }

            return(SynthErrorCodes.eSynthDone);
        }
        /* apply effects to data generated during this envelope clock cycle.  this is the */
        /* second half of the control-update cycle. */
        public static SynthErrorCodes ApplyTrackEffectGenerator(
            TrackEffectGenRec Generator,
            float[] workspace,
            int nActualFrames,
            int lOffset,
            int rOffset,
            SynthParamRec SynthParams)
        {
            OneEffectRec Scan = Generator.List;

            if (Scan != null)
            {
                /* auto-quiescence prepass -- if input exceeds level, then enable */
                if (Generator.AutoQuiescence)
                {
                    AutoQuiescenceDetector(
                        Generator,
                        workspace,
                        lOffset,
                        rOffset,
                        nActualFrames,
                        true /*examining input data*/);
                }

                if (Generator.Enable)
                {
                    while (Scan != null)
                    {
                        SynthErrorCodes error = Scan.u.Apply(
                            workspace,
                            lOffset,
                            rOffset,
                            nActualFrames,
                            SynthParams);
                        if (error != SynthErrorCodes.eSynthDone)
                        {
                            return(error);
                        }
                        Scan = Scan.Next;
                    }

                    /* auto-quiescence postpass -- if effects were applied, then */
                    /* examine the output */
                    if (Generator.AutoQuiescence)
                    {
                        AutoQuiescenceDetector(
                            Generator,
                            workspace,
                            lOffset,
                            rOffset,
                            nActualFrames,
                            false /*examining output data*/);
                    }
                }
            }

            return(SynthErrorCodes.eSynthDone);
        }
        /* finalize before termination */
        public static void FinalizeTrackEffectGenerator(
            TrackEffectGenRec Generator,
            SynthParamRec SynthParams,
            bool writeOutputLogs)
        {
            OneEffectRec Scan = Generator.List;

            if (Scan != null)
            {
                if (Generator.Enable)
                {
                    while (Scan != null)
                    {
                        Scan.u.Finalize(
                            SynthParams,
                            writeOutputLogs);
                        Scan = Scan.Next;
                    }
                }
            }
        }
        /* create a new track effect generator */
        public static SynthErrorCodes NewTrackEffectGenerator(
            EffectSpecListRec SpecList,
            SynthParamRec SynthParams,
            out TrackEffectGenRec GeneratorOut)
        {
            GeneratorOut = null;

            TrackEffectGenRec Generator = new TrackEffectGenRec();

            Generator.Enable         = true;
            Generator.AutoQuiescence = EffectSpecListIsAutoQuiescenceEnabled(SpecList);
            if (Generator.AutoQuiescence)
            {
                Generator.Enable    = false; /* start with it off in this case */
                Generator.GateLevel = (float)(Math.Pow(
                                                  2,
                                                  EffectSpecListGetAutoQuiescenceDecibels(SpecList) * (1 / -6.0205999132796239))
                                              / SynthParams.fOverallVolumeScaling);
                Generator.WindowDuration = (int)(SynthParams.dEnvelopeRate
                                                 * EffectSpecListGetAutoQuiescenceWindowDuration(SpecList));
                Generator.CurrentWindowDuration = 0;
            }

            /* this is the current envelope update index for removing things from the */
            /* scanning gap list (i.e. the back edge of the scanning gap) */
            /* by setting this negative, we cause the scanning gap to open. */
            Generator.ExecutionIndex = -SynthParams.iScanningGapWidthInEnvelopeTicks;

            /* initialize accent trackers */
            InitAccentTracker(ref Generator.Accents0);
            InitAccentTracker(ref Generator.Accents1);
            InitAccentTracker(ref Generator.Accents2);
            InitAccentTracker(ref Generator.Accents3);
            InitAccentTracker(ref Generator.Accents4);
            InitAccentTracker(ref Generator.Accents5);
            InitAccentTracker(ref Generator.Accents6);
            InitAccentTracker(ref Generator.Accents7);

            /* build list of thingers */
            OneEffectRec Appender = null;
            int          l        = GetEffectSpecListLength(SpecList);

            for (int i = 0; i < l; i += 1)
            {
                /* see if effect is enabled */
                if (IsEffectFromEffectSpecListEnabled(SpecList, i))
                {
                    OneEffectRec Effect = new OneEffectRec();

                    /* link */
                    Effect.Next = null;
                    if (Appender == null)
                    {
                        Generator.List = Effect;
                    }
                    else
                    {
                        Appender.Next = Effect;
                    }
                    Appender = Effect;

                    /* fill in fields */
                    EffectTypes Type = GetEffectSpecListElementType(SpecList, i);
                    switch (Type)
                    {
                    default:
                        Debug.Assert(false);
                        throw new ArgumentException();

                    case EffectTypes.eDelayEffect:
                        Effect.u = DelayUnifiedRec.NewTrackDelayLineProcessor(
                            GetDelayEffectFromEffectSpecList(SpecList, i),
                            SynthParams);
                        break;

                    case EffectTypes.eNLProcEffect:
                        Effect.u = NLProcUnifiedRec.NewTrackNLProcProcessor(
                            GetNLProcEffectFromEffectSpecList(SpecList, i),
                            SynthParams);
                        break;

                    case EffectTypes.eFilterEffect:
                        Effect.u = FilterArrayRec.NewTrackFilterArrayProcessor(
                            GetFilterEffectFromEffectSpecList(SpecList, i),
                            SynthParams);
                        break;

                    case EffectTypes.eAnalyzerEffect:
                        Effect.u = AnalyzerRec.NewAnalyzer(
                            GetAnalyzerEffectFromEffectSpecList(SpecList, i),
                            SynthParams);
                        break;

                    case EffectTypes.eHistogramEffect:
                        Effect.u = HistogramRec.NewHistogram(
                            GetHistogramEffectFromEffectSpecList(SpecList, i),
                            SynthParams);
                        break;

                    case EffectTypes.eResamplerEffect:
                        Effect.u = ResamplerRec.NewResampler(
                            GetResamplerEffectFromEffectSpecList(SpecList, i),
                            SynthParams);
                        break;

                    case EffectTypes.eCompressorEffect:
                        Effect.u = CompressorRec.NewTrackCompressor(
                            GetCompressorEffectFromEffectSpecList(SpecList, i),
                            SynthParams);
                        break;

                    case EffectTypes.eVocoderEffect:
                        Effect.u = VocoderRec.NewTrackVocoder(
                            GetVocoderEffectFromEffectSpecList(SpecList, i),
                            SynthParams);
                        break;

                    case EffectTypes.eIdealLowpassEffect:
                        Effect.u = IdealLPRec.NewIdealLP(
                            GetIdealLPEffectFromEffectSpecList(SpecList, i),
                            SynthParams);
                        break;

                    case EffectTypes.eConvolverEffect:
                    {
                        ConvolverRec    ConvolverEffect;
                        SynthErrorCodes Result = ConvolverRec.NewConvolver(
                            GetConvolverEffectFromEffectSpecList(SpecList, i),
                            SynthParams,
                            out ConvolverEffect);
                        if (Result != SynthErrorCodes.eSynthDone)
                        {
                            return(Result);
                        }
                        Effect.u = ConvolverEffect;
                    }
                    break;

                    case EffectTypes.eUserEffect:
                    {
                        UserEffectProcRec userEffect;
                        SynthErrorCodes   error = UserEffectProcRec.NewTrackUserEffectProc(
                            GetUserEffectFromEffectSpecList(SpecList, i),
                            SynthParams,
                            out userEffect);
                        if (error != SynthErrorCodes.eSynthDone)
                        {
                            return(error);
                        }
                        Effect.u = userEffect;
                    }
                    break;

                    case EffectTypes.ePluggableEffect:
                    {
                        PluggableSpec Spec = GetPluggableEffectFromEffectSpecList(SpecList, i);
                        Debug.Assert(Spec is PluggableTrackSpec);
                        PluggableTrackEffectTemplate Template = new PluggableTrackEffectTemplate(
                            (PluggableTrackSpec)Spec,
                            SynthParams);
                        ITrackEffect    effect;
                        SynthErrorCodes error = Template.Create(
                            SynthParams,
                            out effect);
                        if (error != SynthErrorCodes.eSynthDone)
                        {
                            return(error);
                        }
                        Effect.u = effect;
                    }
                    break;
                    }
                }
            }

            GeneratorOut = Generator;
            return(SynthErrorCodes.eSynthDone);
        }