Esempio n. 1
0
            /* shared initialization */
            private static SynthErrorCodes UserEffectSharedInit(
                UserEffectProcRec Proc,
                UserEffectSpecRec Template,
                SynthParamRec SynthParams)
            {
                Proc.disableOversampling = GetUserEffectSpecNoOversampling(Template);

                double sr = SynthParams.dSamplingRate;

                if (!((SynthParams.iOversampling == 1) || !Proc.disableOversampling))
                {
                    sr /= SynthParams.iOversampling;
                }

                /* init func */
                {
                    string FuncName = GetUserEffectSpecInitFuncName(Template);
                    if (FuncName != null)
                    {
                        FuncCodeRec FuncCode = SynthParams.CodeCenter.ObtainFunctionHandle(FuncName);
                        if (FuncCode == null)
                        {
                            // Function missing; should have been found by CheckUnreferencedThings
                            Debug.Assert(false);
                            throw new ArgumentException();
                        }

                        DataTypes[] argsTypes;
                        DataTypes   returnType;
                        UserEffectGetInitSignature(Template, out argsTypes, out returnType);
                        FunctionSignature expectedSignature = new FunctionSignature(argsTypes, returnType);
                        FunctionSignature actualSignature   = new FunctionSignature(
                            FuncCode.GetFunctionParameterTypeList(),
                            FuncCode.GetFunctionReturnType());
                        if (!FunctionSignature.Equals(expectedSignature, actualSignature))
                        {
                            // Function type mismatch; should have been found by CheckUnreferencedThings
                            Debug.Assert(false);
                            throw new ArgumentException();
                        }

                        Proc.InitFunc = FuncCode.GetFunctionPcode();
                    }
                }

                /* data func */
                {
                    DataTypes[] argsTypes;
                    DataTypes   returnType;
                    UserEffectGetDataSignature(Template, out argsTypes, out returnType);
                    FunctionSignature expectedSignature = new FunctionSignature(argsTypes, returnType);

                    foreach (string FuncName in GetUserEffectSpecProcessDataFuncNames(Template))
                    {
                        FuncCodeRec FuncCode = SynthParams.CodeCenter.ObtainFunctionHandle(FuncName);
                        if (FuncCode == null)
                        {
                            // Function missing; should have been found by CheckUnreferencedThings
                            Debug.Assert(false);
                            throw new ArgumentException();
                        }

                        FunctionSignature actualSignature = new FunctionSignature(
                            FuncCode.GetFunctionParameterTypeList(),
                            FuncCode.GetFunctionReturnType());
                        if (FunctionSignature.Equals(expectedSignature, actualSignature))
                        {
                            Proc.DataFunc = FuncCode.GetFunctionPcode();
                            break;
                        }
                    }
                    if (Proc.DataFunc == null)
                    {
                        // None matched -- should have been found by CheckUnreferencedThings
                        Debug.Assert(false);
                        throw new ArgumentException();
                    }
                }

                Proc.paramCount           = GetUserEffectSpecParamCount(Template);
                Proc.paramResults         = new double[Proc.paramCount];
                Proc.paramResultsPrevious = new double[Proc.paramCount];

                // create state objects
                DataTypes[] stateTypes = UserEffectGetWorkspaceTypes(Template);
                Proc.userState = new ArrayHandle[stateTypes.Length];
                for (int i = 0; i < Proc.userState.Length; i++)
                {
                    switch (stateTypes[i])
                    {
                    default:
                        Debug.Assert(false);
                        throw new ArgumentException();

                    case DataTypes.eArrayOfInteger:
                        Proc.userState[i] = new ArrayHandleInt32(new int[0]);
                        break;

                    case DataTypes.eArrayOfFloat:
                        Proc.userState[i] = new ArrayHandleFloat(new float[0]);
                        break;

                    case DataTypes.eArrayOfDouble:
                        Proc.userState[i] = new ArrayHandleDouble(new double[0]);
                        break;
                    }
                }

                Proc.smoothingBuffers = new SmoothingEntry[Template.Items.Length];
                for (int i = 0; i < Template.Items.Length; i++)
                {
                    if (Template.Items[i].Smoothed)
                    {
                        float[] vector = new float[SynthParams.nAllocatedPointsOneChannel];
                        Proc.smoothingBuffers[i].vector      = vector;
                        Proc.smoothingBuffers[i].arrayHandle = new ArrayHandleFloat(vector);
                    }
                }

                /* initialize user state */
                if (Proc.InitFunc != null)
                {
                    int argCount = 1                       /*retval*/
                                   + 1                     /*t*/
                                   + 1                     /*bpm*/
                                   + 1                     /*samplingRate*/
                                   + 1                     /*maxSampleCount*/
                                   + Proc.userState.Length /*user state arrays*/
                                   + 1 /*retaddr*/;
                    SynthParams.FormulaEvalContext.EmptyParamStackEnsureCapacity(argCount);

                    StackElement[] StackBase;
                    int            StackNumElements;
                    SynthParams.FormulaEvalContext.GetRawStack(out StackBase, out StackNumElements);

                    StackBase[StackNumElements++].Data.Double = SynthParams.dElapsedTimeInSeconds;       /* t */

                    StackBase[StackNumElements++].Data.Double = SynthParams.dCurrentBeatsPerMinute;      /* bpm */

                    StackBase[StackNumElements++].Data.Double = sr;                                      /* samplingRate */

                    StackBase[StackNumElements++].Data.Integer = SynthParams.nAllocatedPointsOneChannel; /* maxSampleCount */

                    for (int i = 0; i < Proc.userState.Length; i++)
                    {
                        StackBase[StackNumElements++].reference.arrayHandleGeneric = Proc.userState[i]; // user state
                    }

                    StackNumElements++; /* return address placeholder */

                    SynthParams.FormulaEvalContext.UpdateRawStack(StackBase, StackNumElements);

                    EvalErrInfoRec ErrorInfo;
                    EvalErrors     Error = PcodeSystem.EvaluatePcode(
                        SynthParams.FormulaEvalContext,
                        Proc.InitFunc,
                        SynthParams.CodeCenter,
                        out ErrorInfo,
                        PcodeExternsNull.Default,
                        ref SynthParams.pcodeThreadContext);
                    if (Error != EvalErrors.eEvalNoError)
                    {
                        SynthParams.ErrorInfo.ErrorEx           = SynthErrorSubCodes.eSynthErrorExUserEffectFunctionEvalError;
                        SynthParams.ErrorInfo.UserEvalErrorCode = Error;
                        SynthParams.ErrorInfo.UserEvalErrorInfo = ErrorInfo;
                        return(SynthErrorCodes.eSynthErrorEx);
                    }
                    Debug.Assert(SynthParams.FormulaEvalContext.GetStackNumElements() == 1); // return value

                    SynthParams.FormulaEvalContext.Clear();
                }

                // initialize sample data in/out staging areas
                Proc.leftWorkspace        = new float[SynthParams.nAllocatedPointsOneChannel];
                Proc.leftWorkspaceHandle  = new ArrayHandleFloat(null);
                Proc.rightWorkspace       = new float[SynthParams.nAllocatedPointsOneChannel];
                Proc.rightWorkspaceHandle = new ArrayHandleFloat(null);

                return(SynthErrorCodes.eSynthDone);
            }
        /* check user effect */
        public static SynthErrorCodes CheckUserEffectForUnreferencedSamples(
            UserEffectSpecRec UserEffect,
            CheckUnrefParamRec Param)
        {
            /* init func */
            {
                string FuncName = GetUserEffectSpecInitFuncName(UserEffect);
                if (FuncName != null) /* optional */
                {
                    FuncCodeRec FuncCode = Param.CodeCenter.ObtainFunctionHandle(FuncName);
                    if (FuncCode == null)
                    {
                        Param.ErrorInfo.ErrorEx      = SynthErrorSubCodes.eSynthErrorExUndefinedFunction;
                        Param.ErrorInfo.FunctionName = FuncName;
                        return(SynthErrorCodes.eSynthErrorEx);
                    }

                    DataTypes[] argsTypes;
                    DataTypes   returnType;
                    UserEffectGetInitSignature(UserEffect, out argsTypes, out returnType);
                    FunctionSignature expectedSignature = new FunctionSignature(argsTypes, returnType);
                    FunctionSignature actualSignature   = new FunctionSignature(
                        FuncCode.GetFunctionParameterTypeList(),
                        FuncCode.GetFunctionReturnType());
                    if (!FunctionSignature.Equals(expectedSignature, actualSignature))
                    {
                        Param.ErrorInfo.ErrorEx      = SynthErrorSubCodes.eSynthErrorExTypeMismatchFunction;
                        Param.ErrorInfo.FunctionName = FuncName;
                        Param.ErrorInfo.ExtraInfo    = String.Format(
                            "{0}{0}Expected:{0}{1}{0}{0}Actual:{0}{2}",
                            Environment.NewLine,
                            expectedSignature,
                            actualSignature);
                        return(SynthErrorCodes.eSynthErrorEx);
                    }
                }
            }

            /* data func */
            {
                DataTypes[] argsTypes;
                DataTypes   returnType;
                UserEffectGetDataSignature(UserEffect, out argsTypes, out returnType);
                FunctionSignature expectedSignature = new FunctionSignature(argsTypes, returnType);

                bool matched = false;
                List <KeyValuePair <string, FunctionSignature> > actualSignatures = new List <KeyValuePair <string, FunctionSignature> >();
                foreach (string FuncName in GetUserEffectSpecProcessDataFuncNames(UserEffect))
                {
                    FuncCodeRec FuncCode = Param.CodeCenter.ObtainFunctionHandle(FuncName);
                    if (FuncCode == null)
                    {
                        Param.ErrorInfo.ErrorEx      = SynthErrorSubCodes.eSynthErrorExUndefinedFunction;
                        Param.ErrorInfo.FunctionName = FuncName;
                        return(SynthErrorCodes.eSynthErrorEx);
                    }

                    FunctionSignature actualSignature = new FunctionSignature(
                        FuncCode.GetFunctionParameterTypeList(),
                        FuncCode.GetFunctionReturnType());
                    actualSignatures.Add(new KeyValuePair <string, FunctionSignature>(FuncName, actualSignature));
                    if (FunctionSignature.Equals(expectedSignature, actualSignature))
                    {
                        matched = true;
                        break;
                    }
                }
                if (!matched)
                {
                    StringBuilder extraInfo = new StringBuilder();
                    extraInfo.AppendLine();
                    extraInfo.AppendLine();
                    extraInfo.AppendLine(String.Format("Expected - {0}", expectedSignature));
                    foreach (KeyValuePair <string, FunctionSignature> actualSignature in actualSignatures)
                    {
                        extraInfo.AppendLine();
                        extraInfo.AppendLine(String.Format("Actual - {0}{1}", actualSignature.Key, actualSignature.Value));
                    }
                    Param.ErrorInfo.ErrorEx   = SynthErrorSubCodes.eSynthErrorExTypeMismatchFunctionMultiple;
                    Param.ErrorInfo.ExtraInfo = extraInfo.ToString();
                    return(SynthErrorCodes.eSynthErrorEx);
                }
            }

            return(SynthErrorCodes.eSynthDone);
        }