public void CloseWindow(WINDOW_TYPE type)
        {
            if (WindowRoot == null || type == WINDOW_TYPE.NONE)
            {
                return;
            }

            var child = WindowRoot.transform.Find(WindowName[type]).gameObject;

            if (child == null)
            {
                return;
            }

            var windowClass = child.GetComponent(WindowClass[type]) as WindowBase;

            if (windowClass == null)
            {
                return;
            }

            if (!IsExecuteWindow)
            {
                switch (type)
                {
                default:
                    WillCloseWindow();
                    windowClass.Close(() =>
                    {
                        ClosedWindow();
                    });
                    break;
                }
            }
        }
Beispiel #2
0
    public DSP.Window.Type getWindowType(WINDOW_TYPE type)
    {
        switch (type)
        {
        case WINDOW_TYPE.Hamming: return(DSP.Window.Type.Hamming);

        default: return(DSP.Window.Type.Rectangular);
        }
    }
Beispiel #3
0
        public AddEditForm(string title, WINDOW_TYPE type)
        {
            InitializeComponent();
            this.Text = title;

            if (type == WINDOW_TYPE.EDIT_WINDOW)
            {
                textBoxSymbol.Enabled = false;
            }
        }
Beispiel #4
0
        public AddEditForm(string title, Position position, WINDOW_TYPE type)
        {
            InitializeComponent();

            this.Text                = title;
            this.m_position          = position;
            this.textBoxSymbol.Text  = m_position.Holding.Symbol.ToString();
            this.textBoxPrice.Text   = m_position.BuyPrice.ToString();
            this.textBoxShares.Text  = m_position.Shares.ToString();
            this.dateTimePicker.Text = m_position.BuyDate;

            if (type == WINDOW_TYPE.EDIT_WINDOW)
            {
                textBoxSymbol.Enabled = false;
            }
        }
        public Transform CreateWindow(WINDOW_TYPE type)
        {
            if (WindowRoot == null || type == WINDOW_TYPE.NONE)
            {
                return(null);
            }

            var window = Resources.Load(WINDOW_DIC + WindowName[type]) as GameObject;

            if (window != null)
            {
                var instantiateObject = Instantiate(window, WindowRoot.transform);
                instantiateObject.name = WindowName[type];
                return(instantiateObject.transform);
            }
            else
            {
                Debug.LogWarning("【WindowCreateError】" + WINDOW_DIC + WindowName[type] + "の取得に失敗しました。");
            }
            return(null);
        }
Beispiel #6
0
    public override float[] doFFT(float[] samples, WINDOW_TYPE window)
    {
        double[] dSamples = new double[samples.Length];
        for (int i = 0; i < samples.Length; i++)
        {
            dSamples[i] = (double)samples[i];
        }

        //Code from DSPLib documentation
        DSP.Window.Type dspWindow         = getWindowType(window);
        double[]        windowCoefs       = DSP.Window.Coefficients(dspWindow, (UInt32)this.binSize);
        double[]        windowInputData   = DSP.Math.Multiply(dSamples, windowCoefs);
        double          windowScaleFactor = DSP.Window.ScaleFactor.Signal(windowCoefs);

        System.Numerics.Complex[] complexSpectrum = fft.Execute(windowInputData);
        //Convert to magnitude and multiply by the window scale factor to get our binned array
        double[] fftSpectrum  = DSP.Math.Multiply(DSPLib.DSP.ConvertComplex.ToMagnitude(complexSpectrum), windowScaleFactor);
        float[]  fFFTSpectrum = new float[fftSpectrum.Length];
        for (int i = 0; i < fFFTSpectrum.Length; i++)
        {
            fFFTSpectrum[i] = (float)fftSpectrum[i];
        }
        return(fFFTSpectrum);
    }
Beispiel #7
0
 //Do fft on the samples with the given window
 //# of bins == samples.Length
 public abstract float[] doFFT(float[] samples, WINDOW_TYPE window);
        public void OpenWindow(WINDOW_TYPE type, bool isForce = false)
        {
            if (WindowRoot == null || type == WINDOW_TYPE.NONE)
            {
                return;
            }

            Transform childTransform;

            if (WindowRoot.transform.childCount > 0)
            {
                childTransform = WindowRoot.transform.Find(WindowName[type]);
                if (childTransform == null)
                {
                    childTransform = CreateWindow(type);
                    if (childTransform == null)
                    {
                        return;
                    }
                }
            }
            else
            {
                childTransform = CreateWindow(type);
                if (childTransform == null)
                {
                    return;
                }
            }

            var windowClass = childTransform.gameObject.GetComponent(WindowClass[type]) as WindowBase;

            if (windowClass == null)
            {
                return;
            }

            if (!IsExecuteWindow)
            {
                if (isForce)
                {
                    if (CurrentWindowType != WINDOW_TYPE.NONE)
                    {
                        CloseWindow(CurrentWindowType);
                    }
                }
                else if (CurrentWindowType != WINDOW_TYPE.NONE)
                {
                    return;
                }

                switch (type)
                {
                default:
                    WillOpenWindow(type);
                    windowClass.Open(() =>
                    {
                        OpenedWindow();
                    });
                    break;
                }
            }
        }
 private void WillOpenWindow(WINDOW_TYPE type)
 {
     IsExecuteWindow = true;
     NextWindowType  = type;
 }
Beispiel #10
0
    // float[] getCombinedSamplesWeb (ref float[] samples) {
    // }

    //The B(eat) G(enerating) A(lgorithim)
    //Background thread to create the beatmap
    void algorithim()
    {
        Debug.Log("algo");
        //float[] samples = //getCombinedSamples(ref song_info.samples, song_info.sampleCount, song_info.channels);

        float[] samples = song_info.samples;

        Debug.Log("Song samples coverted to mono");

        int finalArraySize = song_info.sampleCount / settings.n_bins;

        Debug.Log(finalArraySize);
        output.fftData         = new float[finalArraySize][];
        output.flux            = new float[finalArraySize];
        output.flux2           = new float[finalArraySize];
        output.peaks           = new float[finalArraySize];
        output.peaks2          = new float[finalArraySize];
        output.threshold       = new float[finalArraySize];
        output.peakThreshold   = new float[finalArraySize];
        output.drifts          = new float[finalArraySize];
        output.flySectionIndex = 0;
        FFTProvider fftProvider = new DSPLibFFTProvider(settings.n_bins);
        WINDOW_TYPE fftWindow   = WINDOW_TYPE.Hamming;

        Debug.Log("done allocating large amount of ram");

        //perform fft using N_BINS at a time
        for (int i = 0; i < finalArraySize; i++)
        {
            float[] currSamples = new float[settings.n_bins];
            int     startIndex  = (i * settings.n_bins);
            //Grab the current sample (length N_BINS) from the samples data
            for (int j = startIndex; j < startIndex + settings.n_bins; j++)
            {
                currSamples[j - startIndex] = samples[j];
            }

            Debug.Log("doFFT");
            output.fftData[i] = fftProvider.doFFT(currSamples, fftWindow);

            if (i != 0)
            {
                //Compute the spectral flux for the current spectrum
                float flux = 0;
                for (int j = 0; j < output.fftData[i].Length; j++)
                {
                    float currFlux = (output.fftData[i][j] - output.fftData[i - 1][j]);
                    //we only want 'rising' spectral flux - since we are detecting beat onset therefore we only care about rise in power
                    if (currFlux > 0)
                    {
                        flux += currFlux;
                    }
                }
                output.flux[i] = flux;
            }
            else
            {
                output.flux[0] = 0;
            }
        }

        //define a window size for the threshold. THRESHOLD_TIME is the length in time that the window should be.
        int thresholdWindowSize = Mathf.FloorToInt((settings.threshold_time / song_info.sampleLength) / settings.n_bins);

        Debug.Log("threshold: ");
        Debug.Log(thresholdWindowSize);

        //Compute threshold for each flux value
        for (int i = 0; i < output.flux.Length; i++)
        {
            float avg   = 0;
            float count = 0;
            for (int j = (i - thresholdWindowSize); j < (i + thresholdWindowSize); j++)
            {
                if (j < 0 || j >= output.flux.Length)
                {
                    continue;                                   //todo should be optimized
                }
                avg   += output.flux[j];
                count += 1;
            }
            if (count > 0)
            {
                output.threshold[i] = (avg / count) * settings.threshold_multiplier;
            }
            else
            {
                output.threshold[i] = 0f;
            }
        }

        //using the computed threshold, discard any flux values that are below/at the threshold (most likely not a beat as it is below avg)
        for (int i = 0; i < output.flux.Length; i++)
        {
            if (output.flux[i] <= output.threshold[i])
            {
                output.flux2[i] = 0f;
            }
            else
            {
                output.flux2[i] = output.flux[i]; //subtract avg so we see only the peak
            }
        }

        //Check for peaks: If curr value > next value this is a peak

        for (int i = 0; i < output.flux2.Length - 1; i++)
        {
            if (output.flux2[i] > output.flux2[i + 1])
            {
                output.peaks[i]    = output.flux2[i]; //Beat Detected
                output.totalPeaks += 1;
            }
            else
            {
                output.peaks[i] = 0f;
            }
        }

        //avg # of beats per second... multiply by 60 to get bpm
        output.totalPeaksOverTime = output.totalPeaks / song_info.length;

        Debug.Log("BPM: ");
        Debug.Log(output.totalPeaksOverTime * 60);

        //fly detection

        //define a window size for the threshold. THRESHOLD_TIME is the length in time that the window should be. for now use drift threshold time
        int flyThresholdWindowSize = Mathf.FloorToInt((settings.fly_threshold_time / song_info.sampleLength) / settings.n_bins) / 2;

        Debug.Log("threshold3: ");
        Debug.Log(flyThresholdWindowSize);

        //only look at the song between 30% and 70% duration,
        //and only find largest value.

        float largestDelta = 0;
        int   indexLargest = 0;

        Debug.Log("Detect fly");
        Debug.Log(Mathf.FloorToInt(.30f * output.peaks.Length));
        Debug.Log(Mathf.FloorToInt(.70f * output.peaks.Length));

        for (int i = Mathf.FloorToInt(.30f * output.peaks.Length); i < Mathf.FloorToInt(.70f * output.peaks.Length); i++)
        {
            float avgL   = 0;
            float avgR   = 0;
            float countL = 0;
            float countR = 0;

            for (int j = (i - flyThresholdWindowSize); j < (i + flyThresholdWindowSize); j++)
            {
                if (j < 0 || j >= output.peaks.Length)
                {
                    continue;                                    //todo should be optimized
                }
                if (j < i)
                {
                    avgL   += output.peaks[j];
                    countL += 1;
                }
                else
                {
                    avgR   += output.peaks[j];
                    countR += 1;
                }
            }
            if (countL > 0 && countR > 0)
            {
                float avg = (avgL / countL) - (avgR / countR); //we are looking for the biggest difference from left to right
                if (avg > largestDelta)
                {
                    largestDelta = avg;
                    indexLargest = i;
                }
            }
        }

        output.flySectionIndex = indexLargest;

        Debug.Log("Fly section: ");
        Debug.Log(output.flySectionIndex);

        //end fly detection

        //drift detection

        //define a window size for the threshold. THRESHOLD_TIME is the length in time that the window should be.
        int driftThresholdWindowSize = Mathf.FloorToInt((settings.drift_threshold_time / song_info.sampleLength) / settings.n_bins) / 2;

        Debug.Log("threshold2: ");
        Debug.Log(driftThresholdWindowSize);

        //Compute a threshold on avg # of peaks at a given time
        for (int i = 0; i < output.peaks.Length; i++)
        {
            float avg   = 0;
            float count = 0;
            for (int j = (i - driftThresholdWindowSize); j < (i + driftThresholdWindowSize); j++)
            {
                if (j < 0 || j >= output.peaks.Length)
                {
                    continue;                                    //todo should be optimized
                }
                avg   += output.peaks[j];
                count += 1;
            }
            if (count > 0)
            {
                output.peakThreshold[i] = (avg / count);
            }
            else
            {
                output.peakThreshold[i] = 0f;
            }
        }

        //select amount_drift_sections from output.peakThreshold, ensuring that each drift section is min_time_between_drift apart
        //remove all duplicates / close values next to a value, then store remaning values as a GreatestValueElement in a arraylist, then sort the list.

        List <GreatestValueElement> greatestValues = new List <GreatestValueElement>();

        Debug.Log("Start greatestvalues");
        int offset = 1;

        for (int i = 0; i < output.peakThreshold.Length; i += offset)
        {
            offset = 1;
            if ((i - driftThresholdWindowSize) <= 0 || (i + driftThresholdWindowSize) >= output.peakThreshold.Length) //throw out values near beginning and near end
            {
                continue;
            }
            while (((i + offset) < output.peakThreshold.Length) && Math.Abs(output.peakThreshold[i] - output.peakThreshold[i + offset]) <= delta)
            {
                output.peakThreshold[i + offset] = 0;
                offset += 1;
            }
            GreatestValueElement elem = new GreatestValueElement(i, output.peakThreshold[i]);
            greatestValues.Add(elem);
        }

        Debug.Log("Sort greatestvalues");

        //todo debug this...
        try {
            greatestValues.Sort((x, y) => y.value.CompareTo(x.value));
        }
        catch (Exception e) {
            Debug.Log("Could not sort greatestValues!");
            Debug.Log(e.ToString());
        }

        Debug.Log("Song length:");
        Debug.Log(song_info.length);

        int minLengthBetweenDrift = Mathf.FloorToInt((settings.min_drift_seperation_time / song_info.sampleLength) / settings.n_bins);
        int totalAllowableDrifts  = Mathf.FloorToInt(settings.drifts_per_minute * Mathf.FloorToInt(song_info.length / 60f));
        int warmUpTimeLength      = Mathf.FloorToInt((settings.warm_up_time / song_info.sampleLength) / settings.n_bins);

        Debug.Log("drifts per minute:");
        Debug.Log(settings.drifts_per_minute);

        Debug.Log("Drift sections: ");
        Debug.Log(totalAllowableDrifts);

        Debug.Log("Min length between drifts: ");
        Debug.Log(minLengthBetweenDrift);

        //select elements and make sure they are min_time_between_drift apart
        int numSelected = 0;
        List <GreatestValueElement> addedValues = new List <GreatestValueElement>();
        bool first = true;

        foreach (GreatestValueElement elem in greatestValues)
        {
            if (numSelected >= totalAllowableDrifts)
            {
                break;
            }
            if (first)
            {
                int offSetIndex = Math.Max(elem.index - driftThresholdWindowSize, 0);
                if (offSetIndex == 0)
                {
                    continue;
                }
                if (offSetIndex - warmUpTimeLength <= 0)
                {
                    continue;
                }
                output.drifts[offSetIndex] = elem.value;
                numSelected = 1;
                first       = false;
                addedValues.Add(elem);
            }
            else
            {
                bool flag = false;
                //We need to check if this elem is properly spaced at least min_time_between_drift apart from other drifts
                //And that the elem is not in the middle of a <fly> section or <time_after_fly> section
                foreach (GreatestValueElement added in addedValues)
                {
                    if (Math.Abs(elem.index - added.index) <= minLengthBetweenDrift ||
                        Math.Abs(elem.index - output.flySectionIndex) <= (flyThresholdWindowSize * 2.2))
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    int offSetIndex = Math.Max(elem.index - driftThresholdWindowSize, 0);
                    if (offSetIndex == 0)
                    {
                        continue;
                    }
                    if (offSetIndex - warmUpTimeLength <= 0)
                    {
                        continue;
                    }
                    output.drifts[offSetIndex] = elem.value;
                    numSelected += 1;
                    addedValues.Add(elem);
                }
            }
        }

        //end drift detection

        //Filter peaks to allowable min_time_between_peaks
        //Todo this is a bit naive should probably select highest peak or something (but will work for now)
        //int minLengthBetweenPeaks = Mathf.FloorToInt(Mathf.FloorToInt((settings.min_peak_seperation_time / sampleLength) / settings.n_bins) / 2);
        if (settings.min_peak_seperation_time > 0)
        {
            int minLengthBetweenPeaks = Mathf.FloorToInt((settings.min_peak_seperation_time / song_info.sampleLength) / settings.n_bins);
            for (int i = 0; i < output.peaks.Length; i++)
            {
                if (output.peaks[i] > 0)
                {
                    output.peaks2[i] = output.peaks[i];
                    i += minLengthBetweenPeaks - 1;
                }
            }
        }
        else
        {
            for (int i = 0; i < output.peaks.Length; i++)
            {
                output.peaks2[i] = output.peaks[i];
            }
        }

        BeatMap beatMap = makeBeatMap(driftThresholdWindowSize * 2, flyThresholdWindowSize * 2);

        beatMap.save(persistentDataPath);

        //todo deal with random stuff below
        //debug output

        Debug.Log("BGA Done");
        this.state = STATE.DONE;

        // using (StreamWriter file = new StreamWriter("output.txt"))
        // {
        //     for (int i=0; i < output.flux.Length; i++)
        //     {
        //         file.WriteLine(output.flux[i]);
        //     }
        // }
        // using (StreamWriter file = new StreamWriter("output2.txt"))
        // {
        //     for (int i = 0; i < output.threshold.Length; i++)
        //     {
        //         file.WriteLine(output.threshold[i]);
        //     }
        // }
        // using (StreamWriter file = new StreamWriter("output3.txt"))
        // {
        //     for (int i = 0; i < output.flux2.Length; i++)
        //     {
        //         file.WriteLine(output.flux2[i]);
        //     }
        // }
        // using (StreamWriter file = new StreamWriter("output4.txt"))
        // {
        //     for (int i = 0; i < output.peaks.Length; i++)
        //     {
        //         file.WriteLine(output.peaks[i]);
        //     }
        // }
        // using (StreamWriter file = new StreamWriter("output5.txt"))
        // {
        //     for (int i = 0; i < output.peaks2.Length; i++)
        //     {
        //         file.WriteLine(output.peaks2[i]);
        //     }
        // }
        // using (StreamWriter file = new StreamWriter("output6.txt"))
        // {
        //     for (int i = 0; i < output.peakThreshold.Length; i++)
        //     {
        //         file.WriteLine(output.peakThreshold[i]);
        //     }
        // }
        // using (StreamWriter file = new StreamWriter("output7.txt"))
        // {
        //     for (int i = 0; i < output.drifts.Length; i++)
        //     {
        //         file.WriteLine(output.drifts[i]);
        //     }
        // }
        // using (StreamWriter file = new StreamWriter("output8.txt"))
        // {
        //     Queue<LaneObject> laneObjects = beatMap.initLaneObjectQueue();
        //     foreach (LaneObject l in laneObjects) {
        //         file.WriteLine(l.sampleIndex + " " + l.lane + " " + l.time + " " + (l.type == LANE_OBJECT_TYPE.Beat ? "1" : "0"));
        //     }
        // }


        // Debug.Log("Output file saved");


        //frameScale = (int) (songLength / finalArraySize);
        //float sampleLength = song_info.length / (float)song_info.sampleCount;
        //Debug.Log(sampleLength);

        //Debug.Log(sampleLength * 1000);
        //Debug.Log(sampleLength * N_BINS); //length per reading

        //done = true;
    }