public SampleSet LoadSampleSetFromComposition(Composition comp)
        {
            SampleSet set = new SampleSet();

            if (comp.Tracks.Count < 2)
                return set;

            var mainSeq = comp.Tracks[0].GetMainSequence() as MelodySequence;
            var mainNotes = mainSeq.ToArray();

            for (int track = 1; track < comp.Tracks.Count; track++)
            {
                Dictionary<Note, int> frequencies = new Dictionary<Note, int>();

                if (comp.Tracks[track].Instrument != instrument)
                {
                   // Console.WriteLine("\tSkipping instrument {0}", comp.Tracks[track].Instrument);
                    continue;
                }

                var seq = comp.Tracks[track].GetMainSequence() as MelodySequence;
                var notes = seq.ToArray();

                var max = Math.Min(mainNotes.Length, notes.Length);

                if(seq.TotalNoteDuration() < seq.TotalRestDuration())
                {
                    continue;
                }

                Console.WriteLine("\tAdding instrument {0}", comp.Tracks[track].Instrument);

                for (int j = 0; j < max; j++ )
                {
                    if (!frequencies.ContainsKey(notes[j]))
                        frequencies[notes[j]] = 1;
                    else frequencies[notes[j]] += 1;
                }

               /* // Filtering
                for (int j = 0; j < max; j++)
                {
                    double normalizedFrequency = frequencies[notes[j]] / (double)max;
              //      if (normalizedFrequency < filterThreshold)
              //          continue;

                    if (notes[j].Velocity > 0)
                        notes[j].Velocity = 127;

                    if (!noteHashes.ContainsKey(notes[j]))
                        noteHashes[notes[j]] = hash++;
                }*/

                int mainTrackTime = 0;
                int accompTrackTime = 0;
                int incr = 0;
                for (int j = 0; j < max; j++)
                {
                    // make sure to use closest note
                    if (j + incr >= max)
                        break;
                 /*   mainTrackTime += mainNotes[j].Duration;
                    accompTrackTime += notes[j + incr].Duration;
                    while(accompTrackTime < mainTrackTime)
                    {
                        incr++;
                        if (j + incr + 1 > max)
                            break;
                        accompTrackTime += notes[j + incr].Duration;
                    }*/

                    if (j + incr > max - 1)
                        break;

            /*                    if (!noteHashes.ContainsKey(notes[j + incr]))
                        continue;

                    // caching notes
                    if (noteHashes[notes[j + incr]] > MAX_OUTPUTS - 1)
                        continue;
                    */

                    Sample s = GetSample(mainNotes[j],notes[j+incr]);

                    set.add(s);
                }

            }

            return set;
        }
        public Note[] GenerateMelody(MelodySequence inputSeq)
        {
            SampleSet set = new SampleSet();
            var mainNotes = inputSeq.ToArray();

            for (int j = 0; j < mainNotes.Length && j < 200; j++)
            {
                Sample s = GetSample(mainNotes[j],null);

                set.add(s);
            }

            List<Note> notes = new List<Note>();
            var reverseHashes = Utils.ReverseDictionary(noteHashes);
            int mainTime = 0;
            int accompTime = 0;
            foreach (Sample sample in set)
            {
                var res = ComputeNetworkOutput(sample);
                //var maxIndex = GetMaxIndex(res);
                Note outputNote = null;
                if(res[0] <= 0.05)
                    outputNote = new Note(-1, (int)(res[1] * 64.0),0);
                else
                    outputNote = new Note((NoteNames)(res[0] * 12), 4, (Durations)(res[1] * 64.0));
                outputNote.StandardizeDuration();
                notes.Add(outputNote);
             /*   if (reverseHashes.ContainsKey(maxIndex))
                {
                    outputNote = reverseHashes[maxIndex];
                    notes.Add(outputNote);
                    /*accompTime += outputNote.Duration;
                    if (mainTime > accompTime)
                    {
                        notes.Add(new Note(-1, mainTime - accompTime, 0));
                        accompTime += mainTime - accompTime;
                    }
                    mainTime += (int)(sample.getInput()[1] * 64.0);*/
                //}
            }

            return notes.ToArray();
        }