private void Raise_NotesDetected(Note[] notes)
        {
            EventHandler<NotesDetectedEventArgs> h = NotesDetected;
            if (h == null) return;

            h(this, new NotesDetectedEventArgs(notes));
        }
Beispiel #2
0
        private Feedback GetPitchFeedback(Note goalNote, Note closestNote)
        {
            if (goalNote.Semitone == closestNote.Semitone)
                return new GoodPitchFeedback();

            int diff = closestNote.Semitone - goalNote.Semitone;
            return new BadPitchFeedback(diff);
        }
Beispiel #3
0
 private Note FindClosestNote(Note goalNote, IList<Note> playedNotes)
 {
     Note closestNote = playedNotes[0];
     double minDiff = Math.Abs((closestNote.startTime - goalNote.startTime).TotalSeconds);
     foreach (Note playedNote in playedNotes) {
         double diff = Math.Abs((closestNote.startTime - goalNote.startTime).TotalSeconds);
         if (diff < minDiff) {
             minDiff = diff;
             closestNote = playedNote;
         }
     }
     return closestNote;
 }
        public void NoteThread()
        {
            while (_detecting) {

                i++;
                if (i > 12)
                    i = -12;

                Note note = new Note() { frequency = 440 + i, startTime = DateTime.Now, endTime = DateTime.Now + TimeSpan.FromSeconds(0.1) };

                Raise_NotesDetected(new Note[1] { note });

                Thread.Sleep(50);
            }
        }
Beispiel #5
0
        public void NoteThread()
        {
            while (_detecting) {
                if (i > 12)
                    i = 0;

                //int semitone = i + 48;
                int semitone = i + 48;
                i++;

                //Note randomNote = new Note() { Semitone = semitone, startTime = DateTime.Now, endTime = DateTime.Now + TimeSpan.FromSeconds(0.1) };
                //_noteQueue.Enqueue(randomNote);

                Note note = new Note() { Semitone = semitone, startTime = DateTime.Now, endTime = DateTime.Now + TimeSpan.FromSeconds(0.1) };
                //_noteQueue.TryDequeue(out note);

                Raise_NotesDetected(new Note[1] { note });

                Thread.Sleep(200);
            }
        }
        private void SimpleDetectNotesImproved()
        {
            while (!_stopDetecting)
            {
                FFTPower fftCalc;
                if (!_fftSource.FFTQueue.TryPeek(out fftCalc))
                    continue;

                if (powerBins == null)
                    powerBins = new double[AudioCapture.AudioCaptureSettings.BufferSize * AudioCapture.AudioCaptureSettings.BufferModifier];

                freq = 0;
                max = 0;
                index = 0;
                _relativePower = 0;

                if (xorPowerBins == null)
                    xorPowerBins = new double[_xorPowerBins.Length];

                currentTotalPower = powerBins.Sum();
                //Buffer.BlockCopy(_xorPowerBins, 0, xorPowerBins, 0, _xorPowerBins.Length * sizeof(double));

                Array.Clear(xorPowerBins, 0, xorPowerBins.Length);
                for (int i = 0; i < ((_sampleRate / 2) / _step); i++)//(fftCalc.PowerBins.Length / 2); i++)
                {
                    if ((i == 0) || ((i * _step) < 75))
                        powerBins[i] = 0;
                    else
                        powerBins[i] = (fftCalc.PowerBins[i] * (1 - xorPowerBins[i]));

                    if (powerBins[i] < _noiseFloor)
                    {
                        powerBins[i] = 0;
                    }

                    _relativePower = powerBins[Math.Max(i - 1, 0)] + powerBins[Math.Max(i + 1, 0)] + powerBins[Math.Max(i, 0)];

                    if ((max < _relativePower))
                    {
                        max = _relativePower;
                        index = i;
                    }
                    else if (max > _relativePower)
                    {
                        if (index > 1)
                        {
                            for (int j = index * 2; (j < (((_sampleRate / 2) / _step) - 2)) || (j < index * 6); j = j + index)
                            {
                                //xorPowerBins[j - 2] = 1;
                                xorPowerBins[j - 1] = 1;
                                xorPowerBins[j] = 1;
                                xorPowerBins[j + 1] = 1;
                                //xorPowerBins[j + 2] = 1;
                            }
                        }
                    }
                    //powerBins[i] = powerBins[i] / fftCalc.PowerBins.Max() * 1000;
                }

                //_noiseFloor = 0.7 * _lastMax;

                //freq = DetectFrequency2(powerBins, index);
                freq = DetectFrequency_Mirrored(fftCalc.PowerBins, index - _subPeaks, index + _subPeaks);
                //System.Diagnostics.Debug.Write(freq + "\n");

                Note[] notes = new Note[1] { new Note() };
                notes[0].startTime = DateTime.Now;
                notes[0].frequency = freq;

                //Console.WriteLine(notes[0].closestRealNoteFrequency);

                NoteQueue.Enqueue(notes);
                //if (NoteQueue.Count > _maxNoteQueueSize)
                //{
                //    Note[] dqnotes;
                //    NoteQueue.TryDequeue(out dqnotes);
                //}
            }
        }
        private void SimpleDetectNotes()
        {
            while (!_stopDetecting)
            {
                FFTPower fftCalc;
                if (!_fftSource.FFTQueue.TryPeek(out fftCalc))
                    continue;

                double[] powerBins = new double[AudioCapture.AudioCaptureSettings.BufferSize * AudioCapture.AudioCaptureSettings.BufferModifier];
                fftCalc.PowerBins.CopyTo(powerBins, 0);
                double freq = 0;

                //Start Note Detection
                double currentTotalPower = powerBins.Sum();
                //if (currentTotalPower < _previousTotalPower) // if our power is less than thy previous power by at least MAX_POWER_DECAY, ignore the rest
                //{
                //    freq = 0;
                //}/
                //else
                //{
                double max = 0;
                int index = 0;
                for (int i = 0; i < powerBins.Length / 2; i++)
                {
                    powerBins[i] = powerBins[i] * _xorPowerBins[i];
                    if (powerBins[i] < _noiseFloor)
                        powerBins[i] = 0;
                    if ((max < powerBins[i]) && (i > 75))
                    {
                        max = powerBins[i];
                        index = i;
                    }
                    powerBins[powerBins.Length - 1 - i] = 0;
                }

                index = index;
                max = max;

                freq = DetectFrequency_Mirrored(powerBins);

                //}
                System.Diagnostics.Debug.Write(freq + "\n");
                _previousTotalPower = currentTotalPower;

                Note[] notes = new Note[1] { new Note() };
                notes[0].startTime = DateTime.Now;
                notes[0].frequency = freq;

                //Console.WriteLine(notes[0].closestRealNoteFrequency);
                NoteQueue.Enqueue(notes);
                if (NoteQueue.Count > _maxNoteQueueSize)
                {
                    Note[] dqnotes;
                    NoteQueue.TryDequeue(out dqnotes);
                }
            }
        }
        private void MultiImpulseDetectNotes()
        {
            while (!_stopDetecting)
            {
                FFTPower fftCalc;
                if (!_fftSource.FFTQueue.TryPeek(out fftCalc))
                    continue;

                if (xorPowerBins == null)
                    xorPowerBins = new double[_xorPowerBins.Length];

                if (powerBins == null)
                    powerBins = new double[AudioCapture.AudioCaptureSettings.BufferSize * AudioCapture.AudioCaptureSettings.BufferModifier];

                if (freqArray == null)
                    freqArray = new double[6];

                if (indexArray == null)
                    indexArray = new int[6];

                if (maxArray == null)
                    maxArray = new double[6];

                if (_powerBinsDelta == null)
                    _powerBinsDelta = new double[powerBins.Length];

                if (_powerBinsOld == null)
                    _powerBinsOld = new double[powerBins.Length];

                if (impulseArray == null)
                    impulseArray = new double[6];

                _relativePower = 0;

                Array.Clear(xorPowerBins, 0, xorPowerBins.Length);

                for (int p = 0; p < 6; p++)
                {
                    maxArray[p] = 0;
                    freqArray[p] = 0;
                    indexArray[p] = 0;
                    impulseArray[p] = 0;

                    for (int i = (int)(stringFreq[p, 0] / _step); i < (stringFreq[p, 1] / _step); i++)
                    {
                        powerBins[i] = (fftCalc.PowerBins[i] * (1 - xorPowerBins[i]));
                        powerBins[i + 1] = (fftCalc.PowerBins[i + 1] * (1 - xorPowerBins[i + 1]));
                        _powerBinsDelta[i] = (fftCalc.PowerBins[i] - _powerBinsOld[i]) * (1 - xorPowerBins[i]);

                        if (powerBins[i] < _noiseFloor)
                        {
                            powerBins[i] = 0;
                            powerBins[i + 1] = 0;
                            _powerBinsDelta[i] = 0;
                        }

                        _relativePower = powerBins[Math.Max(i - 1, 0)] + powerBins[Math.Max(i + 1, 0)] + powerBins[Math.Max(i, 0)];

                        if ((maxArray[p] < _relativePower))
                        {
                            maxArray[p] = _relativePower;
                            indexArray[p] = i;
                            if (_powerBinsDelta[i] > 0)
                                impulseArray[p] = _powerBinsDelta[i];
                            //xorPowerBins[i - 2] = 1;
                            //xorPowerBins[i - 1] = 1;
                            //xorPowerBins[i] = 1;
                            //xorPowerBins[i + 1] = 1;
                            //xorPowerBins[i + 2] = 1;
                        }
                        else if (maxArray[p] > _relativePower)
                        {
                            if (indexArray[p] > 1)
                            {
                                for (int j = indexArray[p]; (j < (((_sampleRate / 2) / _step) - 2)) && (j < indexArray[p] * 6); j = j + indexArray[p])
                                {
                                    xorPowerBins[j - 2] = 1;
                                    xorPowerBins[j - 1] = 1;
                                    xorPowerBins[j] = 1;
                                    xorPowerBins[j + 1] = 1;
                                    xorPowerBins[j + 2] = 1;
                                }
                            }
                            stringFreq[Math.Min((p + 1), 5), 0] = (index * _step);
                            break;
                        }
                    }
                }

                for (int j = 0; j < 6; j++)
                {
                    if (j == 0)
                        _impulseCutoff = _lowImpulseCutoff;
                    else
                        _impulseCutoff = _highImpulseCutoff;
                    if (impulseArray[j] > _lowImpulseCutoff)
                    {
                        Note[] notes = new Note[6];
                        for (int i = 0; i < 6; i++)
                        {
                            freqArray[i] = DetectFrequency_Mirrored(fftCalc.PowerBins, indexArray[i] - _subPeaks, indexArray[i] + _subPeaks);
                            notes[i] = new Note();
                            notes[i].startTime = DateTime.Now;
                            notes[i].frequency = freqArray[i];
                        }
                        Raise_NotesDetected(notes);
                        break;
                    }
                }

                Buffer.BlockCopy(fftCalc.PowerBins, 0, _powerBinsOld, 0, _powerBinsOld.Length * sizeof(double));
                //NoteQueue.Enqueue(notes);
            }
        }
        private void MultiDetectNotes()
        {
            while (!_stopDetecting)
            {
                FFTPower fftCalc;
                if (!_fftSource.FFTQueue.TryPeek(out fftCalc))
                    continue;

                if (xorPowerBins == null)
                    xorPowerBins = new double[_xorPowerBins.Length];

                if (powerBins == null)
                    powerBins = new double[AudioCapture.AudioCaptureSettings.BufferSize * AudioCapture.AudioCaptureSettings.BufferModifier];

                if (freqArray == null)
                    freqArray = new double[6];

                if (indexArray == null)
                    indexArray = new int[6];

                if (maxArray == null)
                    maxArray = new double[6];

                _relativePower = 0;

                Array.Clear(xorPowerBins, 0, xorPowerBins.Length);

                for (int p = 0; p < 6; p++)
                {
                    maxArray[p] = 0;
                    freqArray[p] = 0;
                    indexArray[p] = 0;

                    for (int i = (int)(stringFreq[p, 0] / _step); i < (stringFreq[p, 1] / _step); i++)
                    {
                        powerBins[i] = (fftCalc.PowerBins[i] * (1 - xorPowerBins[i]));
                        powerBins[i + 1] = (fftCalc.PowerBins[i + 1] * (1 - xorPowerBins[i + 1]));

                        if (powerBins[i] < _noiseFloor)
                        {
                            powerBins[i] = 0;
                            powerBins[i + 1] = 0;
                        }

                        _relativePower = powerBins[Math.Max(i - 1, 0)] + powerBins[Math.Max(i + 1, 0)] + powerBins[Math.Max(i, 0)];

                        if ((maxArray[p] < _relativePower))
                        {
                            maxArray[p] = _relativePower;
                            indexArray[p] = i;

                            //xorPowerBins[i - 2] = 1;
                            //xorPowerBins[i - 1] = 1;
                            //xorPowerBins[i] = 1;
                            //xorPowerBins[i + 1] = 1;
                            //xorPowerBins[i + 2] = 1;
                        }
                        else if (maxArray[p] > _relativePower)
                        {
                            if (indexArray[p] > 1)
                            {
                                for (int j = indexArray[p]; (j < (((_sampleRate / 2) / _step) - 2)) && (j < indexArray[p] * 6); j = j + indexArray[p])
                                {
                                    xorPowerBins[j - 2] = 1;
                                    xorPowerBins[j - 1] = 1;
                                    xorPowerBins[j] = 1;
                                    xorPowerBins[j + 1] = 1;
                                    xorPowerBins[j + 2] = 1;
                                }
                            }
                            break;
                        }
                    }
                }

                Note[] notes = new Note[6];
                for (int i = 0; i < 6; i++)
                {
                    freqArray[i] = DetectFrequency_Mirrored(fftCalc.PowerBins, indexArray[i] - _subPeaks, indexArray[i] + _subPeaks);
                    notes[i] = new Note();
                    notes[i].startTime = DateTime.Now;
                    notes[i].frequency = freqArray[i];
                }
                Raise_NotesDetected(notes);
                //NoteQueue.Enqueue(notes);
            }
        }
        private void ImpulseDetectNotes()
        {
            while (!_stopDetecting)
            {
                FFTPower fftCalc;
                if (!_fftSource.FFTQueue.TryPeek(out fftCalc))
                    continue;

                if (powerBins == null)
                    powerBins = new double[AudioCapture.AudioCaptureSettings.BufferSize * AudioCapture.AudioCaptureSettings.BufferModifier];

                if (_powerBinsDelta == null)
                    _powerBinsDelta = new double[powerBins.Length];

                if (_powerBinsOld == null)
                    _powerBinsOld = new double[powerBins.Length];

                if (xorPowerBins == null)
                    xorPowerBins = new double[_xorPowerBins.Length];

                Array.Clear(xorPowerBins, 0, xorPowerBins.Length);

                freq = 0;
                max = 0;
                index = 0;
                _relativePower = 0;
                impulse = 0;

                for (int i = (int)(stringFreq[0, 0] / _step); i < (stringFreq[5, 1] / _step); i++)
                {
                    powerBins[i] = (fftCalc.PowerBins[i] * (1 - xorPowerBins[i]));
                    powerBins[i + 1] = (fftCalc.PowerBins[i + 1] * (1 - xorPowerBins[i + 1]));
                    _powerBinsDelta[i] = (fftCalc.PowerBins[i] - _powerBinsOld[i]) * (1 - xorPowerBins[i]);

                    if (powerBins[i] < _noiseFloor)
                    {
                        powerBins[i] = 0;
                        powerBins[i + 1] = 0;
                        _powerBinsDelta[i] = 0;
                    }

                    _relativePower = powerBins[Math.Max(i - 1, 0)] + powerBins[Math.Max(i + 1, 0)] + powerBins[Math.Max(i, 0)];

                    if ((max < _relativePower))
                    {
                        max = _relativePower;
                        index = i;
                        if (_powerBinsDelta[i] > 0)
                            impulse = _powerBinsDelta[i];
                    }
                    else if (max > _relativePower)
                    {
                        if (index > 1)
                        {
                            for (int j = index; (j < (((_sampleRate / 2) / _step) - 2)) && (j < index * 6); j = j + index)
                            {
                                xorPowerBins[j - 2] = 1;
                                xorPowerBins[j - 1] = 1;
                                xorPowerBins[j] = 1;
                                xorPowerBins[j + 1] = 1;
                                xorPowerBins[j + 2] = 1;
                            }
                        }
                        break;
                    }
                }

                Buffer.BlockCopy(fftCalc.PowerBins, 0, _powerBinsOld, 0, _powerBinsOld.Length * sizeof(double));

                _impulseCutoff = _lowImpulseCutoff;
                //_impulseCutoff = _highImpulseCutoff;
                if (impulse> _lowImpulseCutoff)
                {
                    Note[] notes = new Note[6];
                    for (int i = 0; i < 6; i++)
                    {
                        freq = DetectFrequency_Mirrored(fftCalc.PowerBins, index - _subPeaks, index + _subPeaks);
                        notes[i] = new Note();
                        notes[i].startTime = DateTime.Now;
                        notes[i].frequency = freq;
                    }
                    Raise_NotesDetected(notes);
                }
            }
        }
        private void RunFreeform(string _chord)
        {
            // Fix this too
            int index;
            Note[] prevNote = new Note[3];
            Note[] curNote;
            int noteCount = 0;

            while (_runningChordFreeform)
            {
                if (!chordSource.ChordQueue.TryPeek(out curNote))
                    continue;
                noteCount = 0;
                for (int j = 0; j < ChordDictionary.ChordList.Count; j++)
                {

                    for (int i = 0; i < curNote.Count(); i++)
                    {
                        bool test = (ChordDictionary.ChordList[j].Notes.Where(n => n.ClosestRealNoteFrequency == curNote[i].ClosestRealNoteFrequency).Count() > 0);
                        if (test == true)
                            noteCount++;
                    }

                    if ((noteCount / ChordDictionary.ChordList[j].Notes.Count) >= 0.5)
                    {
                        _chord = ChordDictionary.ChordList[j].CharValue.ToString();
                        break;
                    }
                    else
                    {
                        _chord = "";
                    }
                }

                if (_chord == "")
                    continue;

                Application.Current.Dispatcher.Invoke(
                    DispatcherPriority.Render,
                    new Action<string>(updateStaff),
                    _chord);
            }
        }
Beispiel #12
0
 public void SetNote(Note note)
 {
     StartTime = note.startTime;
     EndTime = note.endTime;
     Frequency = note.frequency;
     ClosestRealNoteFrequency = note.frequency;
 }
Beispiel #13
0
        private Feedback GetTimingFeedback(Note goalNote, Note closestNote)
        {
            double diff = (closestNote.startTime - goalNote.startTime).TotalSeconds;
            double absDiff = Math.Abs(diff);

            string slowOrFastStr = diff > 0 ? "slow" : "fast"; // user is slow if difference is positive (played after goal note)

            if (absDiff < _goodTiming) {
                return new GoodTimingFeedback(diff) { Note = closestNote };
            } else if (absDiff < _mediumTiming) {
                return new MediumTimingFeedback(diff) { Note = closestNote };
            } else {
                return new BadTimingFeedback(diff) { Note = closestNote };
            }
        }