Beispiel #1
0
    /// <summary>
    /// Get Settings from User Input or Config file
    /// </summary>
    /// <param name="bands">Specifies the desired height of the spectrogram (bands)</param>
    /// <param name="samplecount">Number of samples</param>
    /// <param name="samplerate">Sample rate</param>
    /// <param name="basefreq">Base frequency in Hertz</param>
    /// <param name="maxfreq">Maximum frequency in Hertz</param>
    /// <param name="pixpersec">Time resolution in Pixels Per Second</param>
    /// <param name="bandsperoctave">Frequency resolution in Bands Per Octave</param>
    /// <param name="Xsize">Specifies the desired width of the spectrogram</param>
    /// <param name="mode">0 = Analysis mode, 1 = Synthesis mode</param>
    public static void SettingsInput(ref int bands, ref int samplecount, ref int samplerate, ref double basefreq, ref double maxfreq, ref double pixpersec, ref double bandsperoctave, ref int Xsize, int mode)
    {
        /* mode :
         * 0 = Analysis mode
         * 1 = Synthesis mode
         */
        int    i = 0;
        double gf;
        double f;
        double trash;
        double ma;          // maximum allowed frequency
        int    unset   = 0; // count of unset interdependant settings
        int    set_min = 0;
        int    set_max = 0;
        int    set_bpo = 0;
        int    set_y   = 0;
        int    set_pps = 0;
        int    set_x   = 0;

                #if DEBUG
        Console.Write("settingsinput...\n");
                #endif

        // Path to the configuration file
        string     configFileName = "arss.conf";
        TextReader freqcfg        = null;
        if (File.Exists(configFileName))
        {
            freqcfg = new StreamReader(configFileName);
        }

        if (samplerate == 0)         // if we're in synthesis mode and that no samplerate has been defined yet
        {
            if (GlobalMembersUtil.quiet)
            {
                Console.Error.WriteLine("Please provide a sample rate for your output sound.\nUse --sample-rate (-r).\nExiting with error.\n");
                Environment.Exit(1);
            }
            //********Output settings querying********

            Console.Write("Sample rate [44100] : ");         // Query for a samplerate
            samplerate = (int)GlobalMembersUtil.GetFloat();
            if (samplerate == 0 || samplerate < -2147483647) // The -2147483647 check is used for the sake of compatibility with C90
            {
                samplerate = 44100;                          // Default value
            }
            //--------Output settings querying--------
        }

        if (basefreq != 0)         // count unset interdependant frequency-domain settings
        {
            set_min = 1;
        }

        if (maxfreq != 0)
        {
            set_max = 1;
        }

        if (bandsperoctave != 0)
        {
            set_bpo = 1;
        }

        if (bands != 0)
        {
            set_y = 1;
        }

        unset = set_min + set_max + set_bpo + set_y;

        if (unset == 4)         // if too many settings are set
        {
            if (mode == 0)
            {
                Console.Error.WriteLine("You have set one parameter too many.\nUnset either --min-freq (-min), --max-freq (-max), --bpo (-b)\nExiting with error.\n");
            }
            if (mode == 1)
            {
                Console.Error.WriteLine("You have set one parameter too many.\nUnset either --min-freq (-min), --max-freq (-max), --bpo (-b) or --height (-y)\nExiting with error.\n");
            }
            Environment.Exit(1);
        }

        if (pixpersec != 0)
        {
            set_pps = 1;
        }

        if (Xsize != 0)
        {
            set_x = 1;
        }

        if (set_x + set_pps == 2 && mode == 0)
        {
            Console.Error.WriteLine("You cannot define both the image width and the horizontal resolution.\nUnset either --pps (-p) or --width (-x)\nExiting with error.\n");
            Environment.Exit(1);
        }

        if (freqcfg != null)         // load settings from file if it exists
        {
            if (basefreq == 0)       // load values from it if they haven't been set yet
            {
                basefreq = double.Parse(freqcfg.ReadLine());
            }
            else
            {
                trash = double.Parse(freqcfg.ReadLine());
            }

            if (maxfreq == 0)
            {
                maxfreq = double.Parse(freqcfg.ReadLine());
            }
            else
            {
                trash = double.Parse(freqcfg.ReadLine());
            }

            if (bandsperoctave == 0)
            {
                bandsperoctave = double.Parse(freqcfg.ReadLine());
            }
            else
            {
                trash = double.Parse(freqcfg.ReadLine());
            }

            if (pixpersec == 0)
            {
                pixpersec = double.Parse(freqcfg.ReadLine());
            }
            else
            {
                trash = double.Parse(freqcfg.ReadLine());
            }
        }
        else
        {
            if (basefreq == 0)             // otherwise load default values
            {
                basefreq = 27.5;
            }
            if (maxfreq == 0)
            {
                maxfreq = 20000;
            }
            if (bandsperoctave == 0)
            {
                bandsperoctave = 12;
            }
            if (pixpersec == 0)
            {
                pixpersec = 150;
            }
        }
        if (freqcfg != null)
        {
            freqcfg.Close();
        }

        if (unset < 3 && set_min == 0)
        {
            if (GlobalMembersUtil.quiet)
            {
                Console.Error.WriteLine("Please define a minimum frequency.\nUse --min-freq (-min).\nExiting with error.\n");
                Environment.Exit(1);
            }
            Console.Write("Min. frequency (Hz) [{0:f3}]: ", basefreq);
            gf = GlobalMembersUtil.GetFloat();
            if (gf != 0)
            {
                basefreq = gf;
            }
            unset++;
            set_min = 1;
        }
        basefreq /= samplerate;         // turn basefreq from Hz to fractions of samplerate

        if (unset < 3 && set_bpo == 0)
        {
            if (GlobalMembersUtil.quiet)
            {
                Console.Error.WriteLine("Please define a bands per octave setting.\nUse --bpo (-b).\nExiting with error.\n");
                Environment.Exit(1);
            }
            Console.Write("Bands per octave [{0:f3}]: ", bandsperoctave);
            gf = GlobalMembersUtil.GetFloat();
            if (gf != 0)
            {
                bandsperoctave = gf;
            }
            unset++;
            set_bpo = 1;
        }

        if (unset < 3 && set_max == 0)
        {
            i = 0;
            do
            {
                i++;
                f = basefreq * Math.Pow(GlobalMembersDsp.LOGBASE, (i / bandsperoctave));
            }while (f < 0.5);

            ma = basefreq * Math.Pow(GlobalMembersDsp.LOGBASE, ((i - 2) / bandsperoctave)) * samplerate;           // max allowed frequency

            if (maxfreq > ma)
            {
                if (GlobalMembersUtil.FMod(ma, 1.0) == 0.0)
                {
                    maxfreq = ma;                     // replaces the "Upper frequency limit above Nyquist frequency" warning
                }
                else
                {
                    maxfreq = ma - GlobalMembersUtil.FMod(ma, 1.0);
                }
            }

            if (mode == 0)             // if we're in Analysis mode
            {
                if (GlobalMembersUtil.quiet)
                {
                    Console.Error.WriteLine("Please define a maximum frequency.\nUse --max-freq (-max).\nExiting with error.\n");
                    Environment.Exit(1);
                }
                Console.Write("Max. frequency (Hz) (up to {0:f3}) [{1:f3}]: ", ma, maxfreq);
                gf = GlobalMembersUtil.GetFloat();
                if (gf != 0)
                {
                    maxfreq = gf;
                }

                if (maxfreq > ma)
                {
                    if (GlobalMembersUtil.FMod(ma, 1.0) == 0.0)
                    {
                        maxfreq = ma;                         // replaces the "Upper frequency limit above Nyquist frequency" warning
                    }
                    else
                    {
                        maxfreq = ma - GlobalMembersUtil.FMod(ma, 1.0);
                    }
                }
            }

            unset++;
            set_max = 1;
        }

        if (set_min == 0)
        {
            basefreq = Math.Pow(GlobalMembersDsp.LOGBASE, (bands - 1) / bandsperoctave) * maxfreq;           // calculate the lower frequency in Hz
            Console.Write("Min. frequency : {0:f3} Hz\n", basefreq);
            basefreq /= samplerate;
        }

        if (set_max == 0)
        {
            maxfreq = Math.Pow(GlobalMembersDsp.LOGBASE, (bands - 1) / bandsperoctave) * (basefreq * samplerate);           // calculate the upper frequency in Hz
            Console.Write("Max. frequency : {0:f3} Hz\n", maxfreq);
        }

        if (set_y == 0)
        {
            bands = 1 + (int)GlobalMembersUtil.RoundOff(bandsperoctave * (GlobalMembersUtil.Log(maxfreq) - GlobalMembersUtil.Log(basefreq * samplerate)));
            Console.Write("Bands : {0:D}\n", bands);
        }

        if (set_bpo == 0)
        {
            if (GlobalMembersDsp.LOGBASE == 1.0)
            {
                bandsperoctave = maxfreq / samplerate;
            }
            else
            {
                bandsperoctave = (bands - 1) / (GlobalMembersUtil.Log(maxfreq) - GlobalMembersUtil.Log(basefreq * samplerate));
            }
            Console.Write("Bands per octave : {0:f3}\n", bandsperoctave);
        }

        if (set_x == 1 && mode == 0)                                              // If we're in Analysis mode and that X is set (by the user)
        {
            pixpersec = (double)Xsize * (double)samplerate / (double)samplecount; // calculate pixpersec
            Console.Write("Pixels per second : {0:f3}\n", pixpersec);
        }

        if ((mode == 0 && set_x == 0 && set_pps == 0) || (mode == 1 && set_pps == 0))         // If in Analysis mode none are set or pixpersec isn't set in Synthesis mode
        {
            if (GlobalMembersUtil.quiet)
            {
                Console.Error.WriteLine("Please define a pixels per second setting.\nUse --pps (-p).\nExiting with error.\n");
                Environment.Exit(1);
            }
            Console.Write("Pixels per second [{0:f3}]: ", pixpersec);
            gf = GlobalMembersUtil.GetFloat();
            if (gf != 0)
            {
                pixpersec = gf;
            }
        }

        basefreq *= samplerate;                                   // turn back to Hz just for the sake of writing to the file

        TextWriter freqcfgOut = new StreamWriter(configFileName); // saving settings to a file

        if (freqcfgOut == null)
        {
            Console.Error.WriteLine("Cannot write to configuration file");
            Environment.Exit(1);
        }
        freqcfgOut.WriteLine(basefreq);
        freqcfgOut.WriteLine(maxfreq);
        freqcfgOut.WriteLine(bandsperoctave);
        freqcfgOut.WriteLine(pixpersec);
        freqcfgOut.Close();

        basefreq  /= samplerate;        // basefreq is now in fraction of the sampling rate instead of Hz
        pixpersec /= samplerate;        // pixpersec is now in fraction of the sampling rate instead of Hz
    }