Exemple #1
0
        void ReadWave()
        {
            WaveStream reader;

            if (fileLocation.Text.Contains(".mp3"))
            {
                reader = new Mp3FileReader(fileLocation.Text);
                reader = WaveFormatConversionStream.CreatePcmStream(reader);
            }
            else
            {
                reader = new WaveFileReader(fileLocation.Text);
            }
            ISampleProvider provider = new Pcm16BitToSampleProvider(reader);

            float[] buffer = new float[bufferLength];
            byte[]  test   = new byte[bufferLength];
            notes      = new Dictionary <string, Note>();
            sampleRate = reader.WaveFormat.SampleRate * 4;
            //provider.Read(buffer, 0, buffer.Length);

            while (provider.Read(buffer, 0, buffer.Length) != 0)
            {
                detectPitch(buffer);
            }
            Note[] key = new Note[12];
            notes.Values.CopyTo(key, 0);

            Array.Sort(key);
            Note[] tonal = new Note[7];
            for (int i = 0; i < 7; i++)
            {
                tonal[i] = key[i];
            }
            Tonality            tonality    = new Tonality(tonal);
            TonalityCoincidence coincidence = new TonalityCoincidence();

            coincidence.calculateCoincidence(tonality);
            Tonality[] tonalities = new Tonality[24];
            coincidence.coincidence.Keys.CopyTo(tonalities, 0);
            System.Windows.Controls.Label label;
            for (int i = 0; i < coincidence.coincidence.Values.Count; i++)
            {
                label            = (System.Windows.Controls.Label)A.FindName(tonalities[i].name);
                label.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#" + Convert.ToInt32((255 * (1 - coincidence.coincidence[tonalities[i]]))).ToString("X2") + Convert.ToInt32((255 * coincidence.coincidence[tonalities[i]])).ToString("X2") + "00"));
                Console.WriteLine(tonalities[i].name + " - > " + coincidence.coincidence[tonalities[i]] * 100 + "%");
            }

            foreach (var j in key)
            {
                Console.WriteLine(j.name + " - > " + j.magnitude);
            }
        }
        public void calculateCoincidence(Tonality tonality)
        {
            int coinc;

            for (int i = 0; i < tonalities.Length; i++)
            {
                coinc = 0;

                for (int j = 0; j < tonality.notes.Length; j++)
                {
                    if (tonality.notes.Contains(tonalities[i].notes[j]))
                    {
                        coinc++;
                    }
                }

                coincidence.Add(tonalities[i], (float)coinc / 7f);
            }
        }
        public TonalityCoincidence()
        {
            coincidence = new Dictionary <Tonality, float>();

            bool[] major = new bool[12] {
                true, false, true, false, true, true,
                false, true, false, true, false, true
            };

            bool[] minor = new bool[12] {
                true, false, true, true, false, true,
                false, true, true, false, true, false
            };

            tonalities = new Tonality[24];

            for (int i = 0; i < tonalities.Length / 2; i++)
            {
                tonalities[i * 2]       = new Tonality(Tonality.HarmonicType.Major, major, i);
                tonalities[(i * 2) + 1] = new Tonality(Tonality.HarmonicType.Minor, minor, i);
            }
        }