コード例 #1
0
        static void ExecuteTests(string faustPath)
        {
            string testPath = Path.Combine(faustPath, @"tests\impulse-tests");

            string dspPath = Path.Combine(testPath, @"dsp");
            string irPath  = Path.Combine(testPath, "reference");

            DSPCompiler compiler = new DSPCompiler(FaustDir);

            int numSucceeded = 0;
            int numFailed    = 0;

            foreach (string dspFile in Directory.GetFiles(dspPath, "*.dsp"))
            {
                string dspName = Path.GetFileName(dspFile);

                if (Array.Exists <string>(ExcludeDSP, element => element == dspName))
                {
                    Console.WriteLine(dspName + " - Excluded");

                    continue;
                }

                if (Verbose)
                {
                    Console.WriteLine("Compiling: " + dspFile);
                }

                IFaustDSP dsp = null;

                bool succeeded = false;

                try
                {
                    try
                    {
                        dsp = compiler.CompileDSP(dspFile);
                    }
                    catch (Exception ex)
                    {
                        if (Verbose)
                        {
                            Console.WriteLine("Dsp computation failed with: " + ex.ToString());
                        }
                    }

                    if (dsp != null)
                    {
                        string irFile = Path.Combine(irPath, Path.GetFileNameWithoutExtension(dspFile) + ".ir");

                        if (!File.Exists(irFile))
                        {
                            if (Verbose)
                            {
                                Console.WriteLine("Unable to find ir file: " + irFile);
                            }
                        }
                        else
                        {
                            double[][] irData = ReadIRFile(irFile);

                            if (irData == null)
                            {
                                if (Verbose)
                                {
                                    Console.WriteLine("Unable to read ir data from: " + irFile);
                                }
                            }
                            else
                            {
                                dsp.Init(44100);

                                int numFrames = irData[0].Length / 4;

                                if (Verbose)
                                {
                                    Console.WriteLine("IR files has " + dsp.GetNumInputs() + " inputs, " + dsp.GetNumOutputs() + " outputs, and " + numFrames + " frames");
                                }

                                double[][] inputData = new double[dsp.GetNumInputs()][];
                                for (int inputChannel = 0; inputChannel < dsp.GetNumInputs(); inputChannel++)
                                {
                                    inputData[inputChannel] = new double[ComputeBlockSize];
                                }

                                double[][] outputData = new double[dsp.GetNumOutputs()][];
                                for (int outputChannel = 0; outputChannel < dsp.GetNumOutputs(); outputChannel++)
                                {
                                    outputData[outputChannel] = new double[ComputeBlockSize];
                                }

                                double error     = 0;
                                double maxErrror = 0;

                                int computeRun   = 0;
                                int framesLeft   = numFrames;
                                int currentFrame = 0;

                                while (framesLeft > 0)
                                {
                                    if (computeRun == 0)
                                    {
                                        for (int inputChannel = 0; inputChannel < dsp.GetNumInputs(); inputChannel++)
                                        {
                                            inputData[inputChannel][0] = 1;
                                        }

                                        SetButtons(dsp.UIDefinition.RootElement, 1);
                                    }
                                    else if (computeRun == 1)
                                    {
                                        for (int inputChannel = 0; inputChannel < dsp.GetNumInputs(); inputChannel++)
                                        {
                                            inputData[inputChannel][0] = 0;
                                        }

                                        SetButtons(dsp.UIDefinition.RootElement, 0);
                                    }

                                    dsp.Compute(ComputeBlockSize, inputData, outputData);

                                    int blockSize = Math.Min(ComputeBlockSize, framesLeft);

                                    for (int sample = 0; sample < blockSize; sample++)
                                    {
                                        string compString = null;

                                        for (int outputChannel = 0; outputChannel < outputData.Length; outputChannel++)
                                        {
                                            double outputValue = outputData[outputChannel][sample];

                                            outputValue = Math.Round(outputValue * 1000000) / 1000000;

                                            double diff = irData[outputChannel][currentFrame] - outputValue;

                                            if (WriteOutput)
                                            {
                                                compString += irData[outputChannel][currentFrame] + "/" + outputValue + "  ";
                                            }

                                            if (Math.Abs(diff) > maxErrror)
                                            {
                                                maxErrror = Math.Abs(diff);
                                            }

                                            if (diff != 0)
                                            {
                                            }

                                            error += diff * diff;
                                        }

                                        currentFrame++;

                                        if (WriteOutput)
                                        {
                                            Console.WriteLine(compString);
                                        }
                                    }

                                    framesLeft -= blockSize;
                                    computeRun++;
                                }

                                error = Math.Sqrt(error / (double)(irData.Length * numFrames));

                                if (Verbose)
                                {
                                    Console.WriteLine("Root-mean-square error is: " + error.ToString("0.########") + " Max error is: " + maxErrror.ToString("0.########"));
                                    Console.WriteLine();
                                }
                                else
                                {
                                    succeeded = (maxErrror < 0.001);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (Verbose)
                    {
                        Console.WriteLine("Testing failed with: " + ex.ToString());
                    }
                    else
                    {
                        Console.WriteLine(Path.GetFileName(dspFile) + " - *** Failed");
                    }
                }

                Console.WriteLine(Path.GetFileName(dspFile) + " - " + (succeeded ? "Success" : "*** Failed"));

                if (succeeded)
                {
                    numSucceeded++;
                }
                else
                {
                    numFailed++;
                }
            }

            Console.WriteLine();
            Console.WriteLine(numSucceeded + " tests succeeded. " + numFailed + " tests failed");
        }
コード例 #2
0
ファイル: DSPCompiler.cs プロジェクト: grame-cncm/faust
        public IFaustDSP CompileDSP(string dspPath)
        {
            StringBuilder compilerOutput = new StringBuilder();

            using (Process process = new Process())
            {
                process.StartInfo.FileName               = Path.Combine(faustDir, @"build\bin\Release\faust.exe");
                process.StartInfo.Arguments              = "-lang csharp -a " + faustDir + @"\architecture\CSharpFaustClass.cs -double " + dspPath;
                process.StartInfo.CreateNoWindow         = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardInput  = true;
                process.StartInfo.UseShellExecute        = false;

                process.OutputDataReceived += (sender, args) => compilerOutput.AppendLine(args.Data);

                if (process.Start())
                {
                    process.BeginOutputReadLine();

                    process.WaitForExit();

                    List <MetadataReference> refs = new List <MetadataReference>();

                    Assembly executingAssembly = this.GetType().Assembly;

                    refs.Add(MetadataReference.CreateFromFile(executingAssembly.Location));

                    refs.Add(MetadataReference.CreateFromFile(typeof(Object).Assembly.Location));

                    foreach (AssemblyName assemblyName in executingAssembly.GetReferencedAssemblies())
                    {
                        refs.Add(MetadataReference.CreateFromFile(Assembly.Load(assemblyName).Location));
                    }

                    CSharpCompilationOptions options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Release,
                                                                                    assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default);

                    CSharpCompilation csharpCompilation = CSharpCompilation.Create("DynamicPlugin", new[] { CSharpSyntaxTree.ParseText(compilerOutput.ToString()) }, refs, options);

                    using (var memoryStream = new MemoryStream())
                    {
                        EmitResult result = csharpCompilation.Emit(memoryStream);

                        if (result.Success)
                        {
                            memoryStream.Seek(0, SeekOrigin.Begin);
                            Assembly assembly = Assembly.Load(memoryStream.ToArray());

                            Type dspType = assembly.GetType("mydsp");

                            if (dspType == null)
                            {
                                string typeStr = "Couldn't find type in assembly with types: ";

                                foreach (Type type in assembly.GetTypes())
                                {
                                    typeStr += type.FullName + ", ";
                                }

                                throw new Exception(typeStr);
                            }

                            IFaustDSP dspClass = Activator.CreateInstance(dspType) as IFaustDSP;

                            return(dspClass);
                        }
                        else
                        {
                            string errStr = null;

                            foreach (Diagnostic diag in result.Diagnostics)
                            {
                                errStr += diag.ToString();
                            }

                            throw new Exception(errStr);
                        }
                    }
                }
            }

            return(null);
        }