public static byte[] CreateTone(MusicNote note, TimeSpan duration, PCMModifiers mods)
 {
     mods.Init();
     using (MemoryStream stream = new MemoryStream()) {
         BinaryWriter writer = new BinaryWriter(stream);
         WriteNote(writer,
                   new MidiNote()
         {
             Note     = note,
             Duration = duration,
         }, mods);
         return(stream.ToArray());
     }
 }
        public static byte[] ConvertMidi(Sequence sequence, PCMModifiers mods)
        {
            mods.Init();
            var notes  = GetNotes(sequence);
            int length = (int)Math.Round(notes.LastOrDefault().End.TotalSeconds *mods.SampleRate);

            byte[] data = new byte[length];
            using (MemoryStream stream = new MemoryStream(data)) {
                BinaryWriter writer = new BinaryWriter(stream);
                foreach (MidiNote note in notes)
                {
                    WriteNote(writer, note, mods);
                }
                return(stream.ToArray());
            }
        }
        private static void WriteNote(BinaryWriter writer, MidiNote note, PCMModifiers mods)
        {
            // Square wave
            MusicNote musicNote = note.Note;

            musicNote.Octave += mods.OctaveOffset;
            musicNote.Octave  = Math.Max(mods.MaxOctave, musicNote.Octave);
            int max   = (int)Math.Round(sbyte.MaxValue * note.Note.Volume * mods.Volume);
            int min   = (int)Math.Round(sbyte.MinValue * note.Note.Volume * mods.Volume);
            int range = max - min;
            //int halfRange = range / 2;
            float freq   = note.Note.Frequency;
            float period = mods.SampleRate / freq;
            int   start  = (int)Math.Round(note.Start.TotalSeconds * mods.SampleRate);
            int   length = (int)Math.Round(note.Duration.TotalSeconds * mods.SampleRate);

            writer.BaseStream.Position = start;
            bool up   = true;
            int  inc  = (int)Math.Round(period / 2);
            int  next = inc;

            for (int i = 0; i < length; i++)
            {
                int   sample;
                float dif = next - i;
                if (dif <= 0)
                {
                    up    = !up;
                    next += inc;
                    //dif++;
                    dif++;
                }
                if (dif >= 1)
                {
                    if (up)
                    {
                        sample = max;
                    }
                    else
                    {
                        sample = min;
                    }
                }
                else
                {
                    if (up != (dif >= 0.5f))
                    {
                        sample = max;
                    }
                    else
                    {
                        sample = min;
                    }

                    /*if (up)
                     *      sample = (int) (max - range * dif);
                     * else
                     *      sample = (int) (min + range * dif);*/
                }
                writer.Write(unchecked ((byte)(sample & 0xFF)));
            }
        }
        public static byte[] ConvertMidi(string file, PCMModifiers mods)
        {
            Sequence sequence = new Sequence(file);

            return(ConvertMidi(sequence, mods));
        }