Пример #1
0
        private void Eval()
        {
            if (!mainWindow.MakeUpToDate())
            {
                return;
            }

            int ErrorLineNumberCompilation;
            DataTypes ReturnType;
            PcodeRec FuncCode;
            Compiler.ASTExpression AST;
            CompileErrors CompileError = Compiler.CompileSpecialFunction(
                mainWindow.Document.CodeCenter,
                new FunctionParamRec[]
                {
                    new FunctionParamRec("frames", DataTypes.eInteger),
                    new FunctionParamRec("tables", DataTypes.eInteger),
                    new FunctionParamRec("data", DataTypes.eArrayOfFloat),
                },
                out ErrorLineNumberCompilation,
                out ReturnType,
                textBoxFormula.Text,
                false/*suppressCILEmission*/,
                out FuncCode,
                out AST);
            if (CompileError != CompileErrors.eCompileNoError)
            {
                textBoxFormula.Focus();
                textBoxFormula.SetSelectionLine(ErrorLineNumberCompilation - 1);
                textBoxFormula.ScrollToSelection();
                LiteralBuildErrorInfo errorInfo = new LiteralBuildErrorInfo(Compiler.GetCompileErrorString(CompileError), ErrorLineNumberCompilation);
                MessageBox.Show(errorInfo.CompositeErrorMessage, "Error", MessageBoxButtons.OK);
                return;
            }

            using (ParamStackRec ParamList = new ParamStackRec())
            {
                int numFrames = waveTableObject.WaveTableData.NumFrames;
                int numTables = waveTableObject.WaveTableData.NumTables;
                float[] vector = new float[numFrames * numTables];

                ArrayHandleFloat dataHandle = new ArrayHandleFloat(vector);

                int initialCapacity = 1/*frames*/ + 1/*tables*/ + 1/*data*/ + 1/*retaddr*/;
                ParamList.EmptyParamStackEnsureCapacity(initialCapacity);

                ParamList.AddIntegerToStack(numFrames);
                ParamList.AddIntegerToStack(numTables);
                ParamList.AddArrayToStack(dataHandle);
                ParamList.AddIntegerToStack(0); /* return address placeholder */

                for (int i = 0; i < numTables; i++)
                {
                    WaveTableStorageRec.Table table = waveTableObject.WaveTableData.ListOfTables[i];
                    for (int j = 0; j < numFrames; j++)
                    {
                        vector[i * numFrames + j] = table[j];
                    }
                }

                CodeCenterRec CodeCenter = mainWindow.Document.CodeCenter;
                EvalErrInfoRec ErrorInfo;
                EvalErrors EvaluationError = PcodeSystem.EvaluatePcodeThread.EvaluatePcode(
                    ParamList,
                    FuncCode,
                    CodeCenter,
                    out ErrorInfo,
                    new PcodeExterns(mainWindow));
                if (EvaluationError != EvalErrors.eEvalNoError)
                {
                    PcodeEvaluationErrorInfo errorInfo = new PcodeEvaluationErrorInfo(
                        EvaluationError,
                        ErrorInfo,
                        FuncCode,
                        CodeCenter);
                    MessageBox.Show(errorInfo.CompositeErrorMessage, "Error", MessageBoxButtons.OK);
                    return;
                }
                Debug.Assert(ParamList.GetStackNumElements() == initialCapacity); // args - retaddr + return value
#if DEBUG
                ParamList.Elements[2].AssertFloatArray();
#endif
                dataHandle = ParamList.Elements[2].reference.arrayHandleFloat;

                WaveTableStorageRec NewTable = new WaveTableStorageRec(numTables, numFrames, waveTableObject.WaveTableData.NumBits);
                float[] NewData = dataHandle.floats;
                if (NewData.Length != numTables * numFrames)
                {
                    PcodeEvaluationErrorInfo errorInfo = new PcodeEvaluationErrorInfo(
                        "<anonymous>",
                        PcodeSystem.GetPcodeErrorMessage(EvalErrors.eEvalArrayWrongDimensions),
                        1);
                    MessageBox.Show(errorInfo.CompositeErrorMessage, "Error", MessageBoxButtons.OK);
                    return;
                }
                SampConv.QuantizeAndClampVector(NewData, NewTable.NumBits);
                for (int i = 0; i < numTables; i++)
                {
                    WaveTableStorageRec.Table table = NewTable.ListOfTables[i];
                    for (int j = 0; j < numFrames; j++)
                    {
                        table[j] = NewData[i * numFrames + j];
                    }
                }

                undo.Push(waveTableObject.WaveTableData);
                redo.Clear();
                waveTableObject.WaveTableData = NewTable;
            }
        }
Пример #2
0
        private ArrayHandleFloat LoadSampleHelper(
            ChannelType channel,
            string type,
            string name)
        {
            if (!String.Equals(type, String.Empty) &&
                !String.Equals(type, "WAV") &&
                !String.Equals(type, "AIFF"))
            {
                throw new EvalErrorException(EvalErrors.eEvalUnableToImportFile);
            }

            // SECURITY: ensure navigation to user-provided path does not escape from the permitted root directories

            string path = null;

            if (mainWindow.SavePath != null)
            {
                string path2;
                if (SecurePathCombine(Path.GetDirectoryName(mainWindow.SavePath), name, out path2) &&
                    File.Exists(path2))
                {
                    path = path2;
                }
            }
            if (path == null)
            {
                string path2;
                if (SecurePathCombine(Program.GetSettingsDirectory(false /*create*/, true /*roaming*/), name, out path2) &&
                    File.Exists(path2))
                {
                    path = path2;
                }
            }
            if (path == null)
            {
                throw new EvalErrorException(EvalErrors.eEvalUnableToImportFile);
            }

            ArrayHandleFloat array = null;

            try
            {
                using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.BufferSize))
                {
                    using (AudioFileReader reader = Program.TryGetAudioReader(stream))
                    {
                        if (reader == null)
                        {
                            throw new NullReferenceException(); // neither AIFF nor WAV
                        }

                        if ((reader.NumChannels == NumChannelsType.eSampleMono)
                            != (channel == ChannelType.eMonoChannel))
                        {
                            throw new ArgumentException(); // file does not have specified channel
                        }

                        array = new ArrayHandleFloat(new float[reader.NumFrames]);
                        float[] buffer = new float[4096];
                        int     c;
                        int     p = 0;
                        while ((c = reader.ReadPoints(buffer, 0, buffer.Length)) != 0)
                        {
                            switch (channel)
                            {
                            default:
                                Debug.Assert(false);
                                throw new ArgumentException();

                            case ChannelType.eLeftChannel:
                                for (int i = 0; i < c; i += 2)
                                {
                                    array.floats[p++] = buffer[i + 0];
                                }
                                break;

                            case ChannelType.eRightChannel:
                                for (int i = 0; i < c; i += 2)
                                {
                                    array.floats[p++] = buffer[i + 1];
                                }
                                break;

                            case ChannelType.eMonoChannel:
                                for (int i = 0; i < c; i++)
                                {
                                    array.floats[p++] = buffer[i];
                                }
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw new EvalErrorException(EvalErrors.eEvalUnableToImportFile);
            }
            return(array);
        }
Пример #3
0
        public override bool EnsureBuilt(
            bool force,
            PcodeSystem.IEvaluationContext pcodeEnvironment,
            BuildFailedCallback failedCallback)
        {
            if (!force && (SampleData != null))
            {
                return(true);
            }
            SampleData = null;

            PcodeRec FuncCode;

            if (!BuildCode(failedCallback, out FuncCode))
            {
                return(false);
            }

            using (ParamStackRec ParamList = new ParamStackRec())
            {
                ArrayHandleFloat dataHandleLeft  = new ArrayHandleFloat(new float[0]);
                ArrayHandleFloat dataHandleRight = new ArrayHandleFloat(new float[0]);

                int initialCapacity =
                    1 /*loopstart1*/ + 1 /*loopstart2*/ + 1 /*loopstart3*/ +
                    1 /*loopend1*/ + 1 /*loopend2*/ + 1 /*loopend3*/ +
                    1 /*origin*/ + 1 /*samplingrate*/ + 1 /*naturalfrequency*/ +
                    (NumChannels == NumChannelsType.eSampleStereo ? 2 : 1) /*data or leftdata/rightdata */ +
                    1 /*retaddr*/;
                ParamList.EmptyParamStackEnsureCapacity(initialCapacity);

                ParamList.AddIntegerToStack(LoopStart1);
                ParamList.AddIntegerToStack(LoopStart2);
                ParamList.AddIntegerToStack(LoopStart3);
                ParamList.AddIntegerToStack(LoopEnd1);
                ParamList.AddIntegerToStack(LoopEnd2);
                ParamList.AddIntegerToStack(LoopEnd3);
                ParamList.AddIntegerToStack(Origin);
                ParamList.AddIntegerToStack(SamplingRate);
                ParamList.AddDoubleToStack(NaturalFrequency);
                if (NumChannels == NumChannelsType.eSampleStereo)
                {
                    ParamList.AddArrayToStack(dataHandleLeft);
                    ParamList.AddArrayToStack(dataHandleRight);
                }
                else
                {
                    ParamList.AddArrayToStack(dataHandleLeft);
                }
                ParamList.AddIntegerToStack(0); /* return address placeholder */

                CodeCenterRec  CodeCenter = ((Document)Parent).CodeCenter;
                EvalErrInfoRec ErrorInfo;
                EvalErrors     EvaluationError = PcodeSystem.EvaluatePcodeThread.EvaluatePcode(
                    ParamList,
                    FuncCode,
                    CodeCenter,
                    out ErrorInfo,
                    pcodeEnvironment);
                if (EvaluationError != EvalErrors.eEvalNoError)
                {
                    failedCallback(
                        this,
                        new PcodeEvaluationErrorInfo(
                            EvaluationError,
                            ErrorInfo,
                            FuncCode,
                            CodeCenter));
                    return(false);
                }
                Debug.Assert(ParamList.GetStackNumElements() == initialCapacity); // args - retaddr + return value
#if DEBUG
                ParamList.Elements[9].AssertFloatArray();
#endif
                dataHandleLeft = ParamList.Elements[9].reference.arrayHandleFloat;
                if (NumChannels == NumChannelsType.eSampleStereo)
                {
#if DEBUG
                    ParamList.Elements[10].AssertFloatArray();
#endif
                    dataHandleRight = ParamList.Elements[10].reference.arrayHandleFloat;
                }

                if (NumChannels == NumChannelsType.eSampleStereo)
                {
                    float[] Left  = dataHandleLeft.floats;
                    float[] Right = dataHandleRight.floats;
                    if (Left.Length != Right.Length)
                    {
                        failedCallback(
                            this,
                            new PcodeEvaluationErrorInfo(
                                "<anonymous>",
                                "Left and Right algorithmic sample arrays are not the same size.",
                                1));
                        return(false);
                    }

                    SampleData = new SampleStorageActualRec(Left.Length, NumBitsType.Max, NumChannels);
                    for (int i = 0; i < Left.Length; i++)
                    {
                        SampleData[2 * i + 0] = Left[i];
                        SampleData[2 * i + 1] = Right[i];
                    }
                }
                else
                {
                    float[] Mono = dataHandleLeft.floats;

                    SampleData = new SampleStorageActualRec(Mono.Length, NumBitsType.Max, NumChannels);
                    for (int i = 0; i < Mono.Length; i++)
                    {
                        SampleData[i] = Mono[i];
                    }
                }
            }

            return(true);
        }
Пример #4
0
        public override bool EnsureBuilt(
            bool force,
            PcodeSystem.IEvaluationContext pcodeEnvironment,
            BuildFailedCallback failedCallback)
        {
            if (!force && (WaveTableData != null))
            {
                return(true);
            }
            WaveTableData = null;

            PcodeRec FuncCode;

            if (!BuildCode(failedCallback, out FuncCode))
            {
                return(false);
            }

            using (ParamStackRec ParamList = new ParamStackRec())
            {
                ArrayHandleFloat dataHandle = new ArrayHandleFloat(new float[NumFrames * NumTables]);

                int initialCapacity = 1 /*frames*/ + 1 /*tables*/ + 1 /*data*/ + 1 /*retaddr*/;
                ParamList.EmptyParamStackEnsureCapacity(initialCapacity);

                ParamList.AddIntegerToStack(NumFrames);
                ParamList.AddIntegerToStack(NumTables);
                ParamList.AddArrayToStack(dataHandle);
                ParamList.AddIntegerToStack(0); /* return address placeholder */

                CodeCenterRec  CodeCenter = ((Document)Parent).CodeCenter;
                EvalErrInfoRec ErrorInfo;
                EvalErrors     EvaluationError = PcodeSystem.EvaluatePcodeThread.EvaluatePcode(
                    ParamList,
                    FuncCode,
                    CodeCenter,
                    out ErrorInfo,
                    pcodeEnvironment);
                if (EvaluationError != EvalErrors.eEvalNoError)
                {
                    failedCallback(
                        this,
                        new PcodeEvaluationErrorInfo(
                            EvaluationError,
                            ErrorInfo,
                            FuncCode,
                            CodeCenter));
                    return(false);
                }
                Debug.Assert(ParamList.GetStackNumElements() == initialCapacity); // args - retaddr + return value
#if DEBUG
                ParamList.Elements[2].AssertFloatArray();
#endif
                dataHandle = ParamList.Elements[2].reference.arrayHandleFloat;

                WaveTableData = new WaveTableStorageRec(NumTables, NumFrames, NumBitsType.eSample24bit);
                float[] NewData = dataHandle.floats;
                if (NewData.Length != NumTables * NumFrames)
                {
                    failedCallback(
                        this,
                        new PcodeEvaluationErrorInfo(
                            "<anonymous>",
                            PcodeSystem.GetPcodeErrorMessage(EvalErrors.eEvalArrayWrongDimensions),
                            1));
                    return(false);
                }
                for (int i = 0; i < NumTables; i++)
                {
                    WaveTableStorageRec.Table table = WaveTableData.ListOfTables[i];
                    for (int j = 0; j < NumFrames; j++)
                    {
                        table[j] = NewData[i * NumFrames + j];
                    }
                }
            }

            return(true);
        }
Пример #5
0
 public void AddArrayToStack(ArrayHandleFloat value)
 {
     EnsureStackSize1();
     ElementArray[NumElements].reference.arrayHandleFloat = value;
     NumElements++;
 }