示例#1
0
        /// <summary>
        ///
        /// </summary>
        public void LoadCsv(string path)
        {
            var csv = new CsvImport().Load(path);

            for (int column = 0; column < csv.ColumCount; column++)
            {
                var wave = new Waveform();
                var row  = 0;

                for (row = 0; row < csv.RowCount; row++)
                {
                    var cell = csv.Table[row, column];

                    if (!string.IsNullOrEmpty(cell))
                    {
                        if (double.TryParse(cell, out double dummy) == false)
                        {
                            cell = cell.ToUpper();

                            if (cell.Contains("NAME"))
                            {
                                wave.Name = cell.Split('=')[1].Trim();
                            }

                            if (cell.Contains("DELTAX"))
                            {
                                wave.DeltaX = double.Parse(cell.Split('=')[1].Trim());
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                wave.YArray = new double[csv.RowCount - row];
                var yindex = 0;

                for (; row < csv.RowCount; row++)
                {
                    var cell = csv.Table[row, column];

                    if (string.IsNullOrEmpty(cell))
                    {
                        break;
                    }
                    else if (double.TryParse(cell, out double value) == true)
                    {
                        wave.YArray[yindex++] = value;
                    }
                    else
                    {
                        throw new InvalidCastException();
                    }
                }

                Waveforms.Add(wave);
            }
        }
示例#2
0
        public Note(NoteName name, NoteTypes type, Waveforms waveform, int octave)
        {
            this.name     = name;
            this.type     = type;
            this.waveform = waveform;
            this.octave   = octave;

            loadFrequencies();
        }
示例#3
0
文件: Form1.cs 项目: xlar54/sidmaker
        private void RenderNote(NoteTypes type, Waveforms waveform)
        {
            NoteName newNoteName = NoteName.E;
            int      octave      = 4;

            if (lastNote != null)
            {
                newNoteName = lastNote.name;
                octave      = lastNote.octave;
            }

            Note note = new Note(newNoteName, type, waveform, octave);

            song.AddNote(editVoice - 1, note);

            lastNote = note;
            selectedNote++;

            pnlSheet.Invalidate();

            Song.tempo = Convert.ToInt32(numTempo.Value);
            note       = new Note(newNoteName, NoteTypes.eighth, waveform, octave);
            Song.PlaySingleNote(note);
        }
示例#4
0
    public static double wave(double n, Waveforms waveform = Waveforms.SINE, double duty = 0.5, bool reflect = false, int auxData = 1)
    {
        // n %= 1.0;
        n = n - ((long)n);           //Truncate integral component

        // double sReflect = reflect? -1 : 1;
        double sReflect = Convert.ToInt32(reflect) * -2 + 1;

        switch (waveform)
        {
        case Waveforms.PULSE:
        case Waveforms.PULSE | Waveforms.USE_DUTY:
            // if (duty%1.0 == 0) duty = 0.5;  //Prevents silence from full duty wave cycles and allows sane duty defaults for other waveforms
            if ((n >= duty) ^ reflect)
            {
                return(1f);
            }
            return(-1f);

        case Waveforms.SINE:
            return(sint2(n) * sReflect);

        case Waveforms.SINE | Waveforms.USE_DUTY:
            return(n >= duty ? 0.0 : sint2(n * 1.0 / (duty + Double.Epsilon)) * sReflect);

        case Waveforms.ABSINE:
            return(Math.Abs(sint2(n)) * sReflect);

        case Waveforms.ABSINE | Waveforms.USE_DUTY:
            return(n >= duty? 0.0:  Math.Abs(sint2(n * 1.0 / (duty + Double.Epsilon))) * sReflect);

        case Waveforms.TRI:
            return(TRITABLE[(int)(Math.Abs(n) * 20)] * sReflect);

        case Waveforms.TRI | Waveforms.USE_DUTY:
            return(n >= duty? 0.0:  TRITABLE[(int)(Math.Abs(n) * 20)] * sReflect);

        case Waveforms.SAW:
            return((1.0 - (n * 2.0)) * sReflect);

        case Waveforms.SAW | Waveforms.USE_DUTY:
            // return (1.0f - (n * 2.0f)) * sReflect;
            // return (1.0 - (Math.Min((n/duty), 1.0) * 2.0 * duty)) * sReflect;
            duty += Single.Epsilon;
            // duty *= 2.0;   //Used when duty needs to be in the 0-0.5 range due to a 0.5 default
            return(1.0 - Math.Max(n + duty - 1.0, 0.0) * (1.0 / duty) * 2 * sReflect);

        case Waveforms.PINK:
            return(pinkr.GetNextValue());

        case Waveforms.PINK | Waveforms.USE_DUTY:
            return(n < duty?pinkr.GetNextValue() : 0.0);

        case Waveforms.BROWN:
            lastr += ThreadSafeRandom.NextDouble() * 0.2 - 0.1;
            lastr *= 0.99;
            return(lastr);

        case Waveforms.BROWN | Waveforms.USE_DUTY:
            if (n < duty)
            {
                lastr += ThreadSafeRandom.NextDouble() * 0.2 - 0.1;
                lastr *= 0.99;
                return(lastr);
            }
            else
            {
                return(0.0);
            }

        case Waveforms.WHITE:
            // noise_counter[auxData] += 0b1;
            // noise_counter[auxData] %= Convert.ToByte(auxData);
            auxData = 128 - auxData;
            noise_counter[auxData] += 1;
            noise_counter[auxData] -= (byte)(auxData & (((auxData - 1) - noise_counter[auxData]) >> 31));

            if (noise_counter[auxData] == 0)
            {
                lastNoiseValue[auxData] = ThreadSafeRandom.NextDouble() * 2.0 - 1.0;
            }
            return(lastNoiseValue[auxData]);

        case Waveforms.WHITE | Waveforms.USE_DUTY:
            auxData = 128 - auxData;
            noise_counter[auxData] += 1;
            noise_counter[auxData] -= (byte)(auxData & (((auxData - 1) - noise_counter[auxData]) >> 31));

            if (noise_counter[auxData] == 0)
            {
                lastNoiseValue[auxData] = ThreadSafeRandom.NextDouble() * 2.0 - 1.0;
            }
            return(n < duty? lastNoiseValue[auxData] : 0.0);

        default:
            return(0);                     //FIXME:  REROUTE CASES FOR FUNCTIONS NOT SUPPORTING DUTY CYCLE OR ELSE THIS WILL OCCUR
        }
    }
示例#5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        public string SaveToCsv(string path)
        {
            List <string> lines = new List <string>();

            foreach (Waveform wave in Waveforms)
            {
                List <string> properties = new List <string>();

                if (!string.IsNullOrWhiteSpace(wave.Name))
                {
                    properties.Add("NAME = " + wave.Name);
                }

                if (wave.DeltaX != 0)
                {
                    properties.Add("DELTAX = " + wave.DeltaX);
                }

                /* properties */
                int row = 0;
                for (; row < properties.Count; row++)
                {
                    if (lines.ElementAtOrDefault(row) == null)
                    {
                        lines.Add(AppConstants.CsvEscape + properties[row] + AppConstants.CsvEscape);
                        if (Waveforms.Last() == wave)
                        {
                            lines[row] += AppConstants.CsvSeparator;
                        }
                    }
                    else
                    {
                        lines[row] += AppConstants.CsvSeparator;
                        lines[row] += AppConstants.CsvEscape + properties[row].ToString() + AppConstants.CsvEscape;
                        if (Waveforms.Last() == wave)
                        {
                            lines[row] += AppConstants.CsvSeparator;
                        }
                    }
                }

                /* YArray */
                int data_index = 0;
                for (; data_index < wave.YArray.Length; row++, data_index++)
                {
                    if (lines.ElementAtOrDefault(row) == null)
                    {
                        lines.Add(AppConstants.CsvEscape + wave.YArray[data_index].ToString() + AppConstants.CsvEscape);
                        if (Waveforms.Last() == wave)
                        {
                            lines[row] += AppConstants.CsvSeparator;
                        }
                    }
                    else
                    {
                        lines[row] += AppConstants.CsvSeparator;
                        lines[row] += AppConstants.CsvEscape + wave.YArray[data_index].ToString() + AppConstants.CsvEscape;
                        if (Waveforms.Last() == wave)
                        {
                            lines[row] += AppConstants.CsvSeparator;
                        }
                    }
                }
            }

            if (!System.IO.File.Exists(path))
            {
                System.IO.File.WriteAllLines(path, lines);
            }
            else
            {
                string filename  = System.IO.Path.GetFileNameWithoutExtension(path);
                string extension = System.IO.Path.GetExtension(path);
                string directory = System.IO.Path.GetDirectoryName(path);
                for (int i = 1; i < 999; i++)
                {
                    var newName = filename + "_" + i.ToString("000");
                    var newPath = directory + newName + extension;

                    if (!System.IO.File.Exists(newPath))
                    {
                        System.IO.File.WriteAllLines(newPath, lines);
                        return(newPath);
                    }
                }
            }
            return(null);
        }
示例#6
0
 /// <summary>
 ///
 /// </summary>
 public void Clear()
 {
     Waveforms.Clear();
 }