Пример #1
0
        public List <Note> Detection()
        {
            List <Note> noteList = new List <Note>();

            //Console.WriteLine("Press any key to convert to .WAV file...");
            //Console.ReadLine();

            int i;
            //Open MP3 file and convert to WAV file.
            WaveOutEvent  waveOut   = new WaveOutEvent();
            Mp3FileReader mp3Reader = new Mp3FileReader(@"C:\Users\Fazle\source\repos\Practice\Media\twinkle.mp3");

            toWAV(mp3Reader);
            //Console.WriteLine(".WAV file created! Press any key to continue...");
            //Console.ReadLine();
            StereoToMono(@"C:\Users\Fazle\source\repos\Practice\Media\current.wav");

            int sampleRate;

            double[] data = null, R = null;
            ReadWavFile(out sampleRate);
            openWav(@"C:\Users\Fazle\source\repos\Practice\Media\currentMono.wav", out data, out R);
            //Console.WriteLine(data.Length);


            Progress <string> progress = new Progress <string>();

            OnsetDetector detector = new OnsetDetector(DetectorOptions.Default, progress);
            List <Onset>  list     = detector.Detect(@"C:\Users\Fazle\source\repos\Practice\Media\currentMono.wav");

            /*Console.WriteLine("Time    Amp");
             * for (i = 0; i < list.Count; i++)
             * {
             *  Console.Write(list[i].OnsetTime);
             *  Console.Write("   ");
             *  Console.Write(list[i].OnsetAmplitude);
             *  Console.Write("   ");
             *  Console.WriteLine();
             * }
             * Console.WriteLine();*/

            int    startPos = 0, stopPos = 0, flag = 0;
            double freq = 0, duration = 0;

            double[] temp;
            double[] result;
            CSCore.Utils.Complex[] complex;

            for (i = 0; i < list.Count - 1; i++)
            {
                startPos = (int)Math.Ceiling(list[i].OnsetTime * sampleRate);
                stopPos  = (int)Math.Floor(list[i + 1].OnsetTime * sampleRate);
                temp     = new double[stopPos - startPos];
                result   = new double[stopPos - startPos];
                complex  = new CSCore.Utils.Complex[stopPos - startPos];

                Array.ConstrainedCopy(data, startPos, temp, 0, temp.Length);

                for (int j = 0; j < complex.Length; j++)
                {
                    complex[j] = new Complex((float)temp[j]);
                    //Console.WriteLine(complex[j]);
                }
                //Console.ReadLine();
                Note note = new Note();
                note.Frequency = CalculateFFT(result, complex, flag);
                note.Name      = UtilMethods.FreqToNote(note.Frequency);
                note.Duration  = list[i + 1].OnsetTime - list[i].OnsetTime;
                noteList.Add(note);
            }

            //Last Note
            startPos = (int)Math.Floor(list[i].OnsetTime * sampleRate);
            stopPos  = data.Length - 1;
            temp     = new double[stopPos - startPos];
            result   = new double[stopPos - startPos];
            complex  = new CSCore.Utils.Complex[stopPos - startPos];
            Array.ConstrainedCopy(data, startPos, temp, 0, temp.Length);
            for (int j = 0; j < complex.Length; j++)
            {
                complex[j] = new Complex((float)temp[j]);
            }
            flag = 1;

            Note lastNote = new Note();

            lastNote.Frequency = CalculateFFT(result, complex, flag);
            lastNote.Name      = UtilMethods.FreqToNote(lastNote.Frequency);
            lastNote.Duration  = (data.Length / sampleRate) - list[i].OnsetTime;
            noteList.Add(lastNote);

            //Console.WriteLine("Press any key to terminate.");
            //Console.ReadLine();
            mp3Reader.Dispose();

            return(noteList);
        }