Example #1
0
    private void Update()
    {
        if (MicrophoneFeed.MicLoudness < MicrophoneFeed.MicSensitivity)
        {
            leftHand.GetComponent <SkinnedMeshRenderer>().material.color  = Color.white;
            rightHand.GetComponent <SkinnedMeshRenderer>().material.color = Color.white;
            //im.color = Color.black;
            return;
        }

        float newHue  = PitchUtils.mapNoteToHue(PitchDetection.s.m_noteIndex);
        float prevHue = hue;

        Color prevRbg = Color.HSVToRGB(prevHue, saturation, value);
        Color newRbg  = Color.HSVToRGB(newHue, saturation, value);

        leftHand.GetComponent <SkinnedMeshRenderer>().material.color  = newRbg;
        rightHand.GetComponent <SkinnedMeshRenderer>().material.color = newRbg;

        im.color = newRbg;

        float h;
        float s;
        float v;

        Color.RGBToHSV(newRbg, out h, out s, out v);

        hue = h;
    }
Example #2
0
    private void setColor(int index)
    {
        Color color = PitchUtils.mapNoteToRBG(index);

        rend.material.color  = color;
        particles.startColor = color;
    }
Example #3
0
 private void freeMode()
 {
     PitchUtils.initalizeNoteFrequencies(60, Spawn.s.mode); //TODO: check if it works without this in vr
     setText("Sing and the music will follow you!");
     PitchDetection.s.generateMusic = true;
     Spawn.s.spawn = false;
     Invoke("goAwayText", 3);
 }
Example #4
0
        private void btnPlot_Click(object sender, EventArgs e)
        {
            int minFreq  = (int)nudMinFreq.Value;
            int maxFreq  = (int)nudMaxFreq.Value;
            int stepFreq = (int)nudFreqStep.Value;

            int samplingRate  = (int)nudSamplingRate.Value;
            int pcmDataLength = (int)nudSampleCount.Value / 2;

            float[] pcm = new float[samplingRate];

            var zcr        = new ZCR(samplingRate, cbParallel.Checked);
            var autocorrel = new AutocorrelationDetector(samplingRate, cbParallel.Checked);
            var tracker    = new PitchTracker();

            tracker.SampleRate = samplingRate;
            var maxLikehood = new MaximumLikehoodDetector(samplingRate, cbParallel.Checked);

            var zcrPairList           = new PointPairList();
            var autocorrelPairList    = new PointPairList();
            var autocorrelNewPairList = new PointPairList();
            var maxLikehoodPairList   = new PointPairList();

            for (int i = minFreq; i < maxFreq; i += stepFreq)
            {
                var     wave = PitchUtils.CreateSineWave(pcm, samplingRate, samplingRate, i, 1.0f, 0 /*, (float)i / 10, 0.05f*/);
                float[] fft  = new FFT(WindowType.Rectangle, cbParallel.Checked).Calculate(pcm);

                zcrPairList.Add(i, Math.Abs(i - zcr.Detect(pcm, fft)));
                autocorrelPairList.Add(i, Math.Abs(i - autocorrel.Detect(pcm, fft)));
                tracker.ProcessBuffer(pcm);
                autocorrelNewPairList.Add(i, Math.Abs(i - tracker.CurrentPitchRecord.Pitch));
                maxLikehoodPairList.Add(i, Math.Abs(i - maxLikehood.Detect(pcm, fft)));
            }

            zedGraphControl1.GraphPane.Title.Text = "Абсолютная ошибка";
            var graphPane = zedGraphControl1.GraphPane;

            graphPane.CurveList.Clear();
            graphPane.XAxis.Title.Text = "Частота (Hz)";
            graphPane.YAxis.Title.Text = "Ошибка (Hz)";

            graphPane.GraphObjList.Clear();
            graphPane.AddCurve("Пересечение с нулем", zcrPairList, System.Drawing.Color.Sienna, SymbolType.None);

            graphPane.GraphObjList.Clear();
            graphPane.AddCurve("Автокорреляция", autocorrelPairList, System.Drawing.Color.DarkGreen, SymbolType.None);

            graphPane.GraphObjList.Clear();
            graphPane.AddCurve("Автокорреляция модифицированная", autocorrelNewPairList, System.Drawing.Color.OrangeRed, SymbolType.None);

            graphPane.GraphObjList.Clear();
            graphPane.AddCurve("Гармоническое перемножение спектров", maxLikehoodPairList, System.Drawing.Color.SkyBlue, SymbolType.None);

            graphPane.AxisChange();
            zedGraphControl1.Refresh();
        }
Example #5
0
    void Update()
    {
        if (useMicrophone != prevUseMicrophone)
        {
            prevUseMicrophone = useMicrophone;
            if (useMicrophone)
            {
                foreach (string m in Microphone.devices)
                {
                    device = m;
                    break;
                }

                source   = GetComponent <AudioSource>();
                prevClip = source.clip;
                source.Stop();
                source.clip = Microphone.Start(null, true, 1, AudioSettings.outputSampleRate);
                source.Play();

                int dspBufferSize, dspNumBuffers;
                AudioSettings.GetDSPBufferSize(out dspBufferSize, out dspNumBuffers);

                source.timeSamples = (Microphone.GetPosition(device) + AudioSettings.outputSampleRate - 3 * dspBufferSize * dspNumBuffers) % AudioSettings.outputSampleRate;
            }
            else
            {
                Microphone.End(device);
                source.clip = prevClip;
                source.Play();
            }
        }

        MicLoudness = LevelMax();

        if (MicLoudness > volumeBuffer)
        {
            volumeBuffer   = MicLoudness;
            bufferDecrease = 0.0005f;
        }
        if (MicLoudness < volumeBuffer)
        {
            volumeBuffer   -= bufferDecrease;
            bufferDecrease *= 1.2f;
        }

        if (volumeBuffer > highestVolume)
        {
            highestVolume = volumeBuffer;
        }

        //Debug.Log(volumeBuffer);

        float scale = PitchUtils.map(volumeBuffer, 0, highestVolume, 0.75f, 1.5f);

        hand1.localScale = new Vector3(scale, scale, scale);
        hand2.localScale = new Vector3(scale, scale, scale);
    }
Example #6
0
 public void setNoteIndex(int index)
 {
     if (!PitchUtils.isInRange(index, 0, 11))
     {
         Debug.Log("Invalid Note Index");
         return;
     }
     noteIndex = index;
     setColor(index);
     setTone(index);
 }
Example #7
0
    IEnumerator Tutorial()
    {
        MicrophoneFeed.MicSensitivity = 0.01f;
        tutorialImage.gameObject.SetActive(true);

        setText("Welcome to Melodia!");
        //textAnim.SetTrigger("fadeIn");

        yield return(new WaitForSeconds(5));

        //textAnim.SetTrigger("transition");
        //yield return new WaitForSeconds(0.5f);
        setText("When you sing, your hands change color. Try it!");

        while (!gotFirstNote)
        {
            yield return(new WaitForSeconds(0.001f));                  // wait to get first note
        }
        setText("Nice job!");
        //Debug.Log("First note: " + firstNote);

        PitchUtils.initalizeNoteFrequencies(firstNote, Spawn.s.mode);

        yield return(new WaitForSeconds(1));

        // Make orb appear and stop near them
        GameObject       sphere = spawn.spawnNote(0);
        MoveTowardPlayer m      = sphere.GetComponent <MoveTowardPlayer>();

        yield return(new WaitForSeconds(2f));

        m.go = false;
        //textAnim.SetTrigger("transition");
        //yield return new WaitForSeconds(0.5f);
        setText("It's an orb! Listen and match the color, then touch it with your hand.");

        // Once they hit it, start the game (called in onCollisionEnter of orb)
    }
Example #8
0
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            int   samplingRate = Convert.ToInt32(nudSamplingRate.Value);
            float freq         = (float)nudFreq.Value;

            int pcmDataLength = (int)nudSampleCount.Value / 2;

            float[] pcm  = new float[pcmDataLength];
            var     wave = PitchUtils.CreateSineWave(pcm, pcmDataLength, samplingRate, freq, 1.0f, 0);

            var stopwatch = new Stopwatch();

            float[] fft         = new FFT(WindowType.Rectangle, cbParallel.Checked).Calculate(pcm);
            var     zcrDetector = new ZCR(samplingRate, cbParallel.Checked);

            stopwatch.Restart();
            float zcr = zcrDetector.Detect(pcm, fft);

            stopwatch.Stop();
            tbZCR.Text     = zcr.ToString();
            tbZCRTime.Text = stopwatch.ElapsedTicks.ToString();

            var autocorrelDetector = new AutocorrelationDetector(samplingRate, cbParallel.Checked);

            stopwatch.Restart();
            float autocorrel = autocorrelDetector.Detect(pcm, fft);

            stopwatch.Stop();
            tbAutocorrel.Text          = autocorrel.ToString();
            tbAutocorrelationTime.Text = stopwatch.ElapsedTicks.ToString();

            var tracker = new PitchTracker();

            tracker.SampleRate = samplingRate;
            stopwatch.Restart();
            tracker.ProcessBuffer(pcm);
            float autocorrelNew = tracker.CurrentPitchRecord.Pitch;

            stopwatch.Stop();
            tbAutocorrelNew.Text          = autocorrelNew.ToString();
            tbAutocorrelationNewTime.Text = stopwatch.ElapsedTicks.ToString();

            var hpsDetector = new HPS(samplingRate, cbParallel.Checked, 1);

            stopwatch.Restart();
            float hps = hpsDetector.Detect(pcm, fft);

            stopwatch.Stop();
            tbHPS.Text     = hps.ToString();
            tbHPSTime.Text = stopwatch.ElapsedTicks.ToString();

            var maxLikehoodDetector = new MaximumLikehoodDetector(samplingRate, cbParallel.Checked);

            stopwatch.Restart();
            fft = new FFT(WindowType.Rectangle, cbParallel.Checked).Calculate(pcm);
            float maxLikehood = maxLikehoodDetector.Detect(pcm, fft);

            stopwatch.Stop();
            tbMaxLikehood.Text     = maxLikehood.ToString();
            tbMaxLikehoodTime.Text = stopwatch.ElapsedTicks.ToString();
        }
Example #9
0
        private void btnPlotTimes_Click(object sender, EventArgs e)
        {
            int minSamples  = (int)nudMinSamples.Value;
            int maxSamples  = (int)nudMaxSamples.Value;
            int samplesStep = (int)nudSamplesStep.Value;

            var zcrPairList           = new PointPairList();
            var autocorrelPairList    = new PointPairList();
            var autocorrelNewPairList = new PointPairList();
            var maxLikehoodPairList   = new PointPairList();

            for (int i = minSamples; i < maxSamples; i += samplesStep)
            {
                float[] pcm  = new float[i];
                var     wave = PitchUtils.CreateSineWave(pcm, i, i, 400, 1.0f, 0);
                float[] fft  = new FFT(WindowType.Rectangle, cbParallel.Checked).Calculate(pcm);

                var zcr        = new ZCR(i, cbParallel.Checked);
                var autocorrel = new AutocorrelationDetector(i, cbParallel.Checked);
                var tracker    = new PitchTracker();
                tracker.SampleRate = i;
                var maxLikehood = new MaximumLikehoodDetector(i, cbParallel.Checked);

                var stopwatch = new Stopwatch();
                stopwatch.Restart();
                zcr.Detect(pcm, fft);
                stopwatch.Stop();
                zcrPairList.Add(i, stopwatch.ElapsedMilliseconds);

                stopwatch.Restart();
                autocorrel.Detect(pcm, fft);
                stopwatch.Stop();
                autocorrelPairList.Add(i, stopwatch.ElapsedMilliseconds);

                stopwatch.Restart();
                tracker.ProcessBuffer(pcm);
                stopwatch.Stop();
                autocorrelNewPairList.Add(i, stopwatch.ElapsedMilliseconds);

                stopwatch.Restart();
                fft = new FFT(WindowType.Rectangle, cbParallel.Checked).Calculate(pcm);
                maxLikehood.Detect(pcm, fft);
                stopwatch.Stop();
                maxLikehoodPairList.Add(i, stopwatch.ElapsedMilliseconds);
            }

            zedGraphControl2.GraphPane.Title.Text = "Время вычисления";
            var graphPane = zedGraphControl2.GraphPane;

            graphPane.CurveList.Clear();
            graphPane.XAxis.Title.Text = "Размер фрагмента (кол-во отсчетов)";
            graphPane.YAxis.Title.Text = "Время вычисления (мсек)";

            graphPane.GraphObjList.Clear();
            graphPane.AddCurve("Пересечение с нулем", zcrPairList, System.Drawing.Color.Sienna, SymbolType.None);

            graphPane.GraphObjList.Clear();
            graphPane.AddCurve("Автокорреляция", autocorrelPairList, System.Drawing.Color.DarkGreen, SymbolType.None);

            graphPane.GraphObjList.Clear();
            graphPane.AddCurve("Автокорреляция модифицированная", autocorrelNewPairList, System.Drawing.Color.OrangeRed, SymbolType.None);

            graphPane.GraphObjList.Clear();
            graphPane.AddCurve("Гармоническое перемножение спектров", maxLikehoodPairList, System.Drawing.Color.SkyBlue, SymbolType.None);

            graphPane.AxisChange();
            zedGraphControl2.Refresh();
        }
Example #10
0
    void FixedUpdate()
    {
        if (!spawnSixteenths)
        {
            return;
        }
        // we have reached a sixteenth
        if (nextSixteenthCounter >= sixteenthsPerSecond)
        {
            //Debug.Log("Sixteenth");
            nextSixteenthCounter = 0;
            int rand3 = Random.Range(0, 10);
            if (rand3 < frequencyOfArpeggios)
            {
                MidiMusicManager.s.arpeggiate();
            }
        }
        nextSixteenthCounter += Time.deltaTime;

        if (!spawn)
        {
            return;
        }

        beatsPerSecond      = 60f / tempoBPM; // calculated here so we can change the tempo in the middle of the song
        sixteenthsPerSecond = 15f / tempoBPM;
        int rand = Random.Range(1, 100);

        // we have reached a beat
        if (nextBeatCounter >= beatsPerSecond)
        {
            //Debug.Log("Beat!");
            if (rand < activityLevel || firstBeat)
            {
                firstBeat = false;
                //spawn
                int rand2 = Random.Range(0, melodyChaos + ((prevNote == 5) ? 2 : 0)); // 5 is more likely to lead to the tonic
                                                                                      // (adding a larger number makes a tonic transition more likely)
                int note;
                if (rand2 < melodyChaos)
                {
                    note = PitchUtils.getRandomNoteIndex(mode);
                }
                else if (rand2 > 10)
                {
                    note = 0; // going back to the tonic is nice
                }
                else
                {
                    note = PitchUtils.getCloseNoteIndex(mode, prevNote);
                }

                spawnNote(note);
                //Debug.Log("Note: " + note);

                // note is in 0-12 range
                changeChord(note);

                prevNote = note;
            }

            // reset beat counter
            nextBeatCounter = 0;
        }

        // based on frame rate (even in fixed update?) - is this bad? seems pretty constant
        nextBeatCounter += Time.deltaTime; // set to Time.time for a joyful sound
    }