⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spectrumtimeanalyzer.java

📁 mp3播放功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */
    private void drawVUMeter(Graphics pGrp, float[] pLeft, float[] pRight, float pFrrh)
    {
        if (displayMode == DISPLAY_MODE_OFF) return;
        float wLeft = 0.0f;
        float wRight = 0.0f;
        float wSadfrr = (vuDecay * pFrrh);
        for (int a = 0; a < pLeft.length; a++)
        {
            wLeft += Math.abs(pLeft[a]);
            wRight += Math.abs(pRight[a]);
        }
        wLeft = ((wLeft * 2.0f) / (float) pLeft.length);
        wRight = ((wRight * 2.0f) / (float) pRight.length);
        if (wLeft > 1.0f)
        {
            wLeft = 1.0f;
        }
        if (wRight > 1.0f)
        {
            wRight = 1.0f;
        }
        //      vuAverage += ( ( wLeft + wRight ) / 2.0f );
        //      vuSamples++;
        //
        //      if ( vuSamples > 128 ) {
        //          vuSamples /= 2.0f;
        //          vuAverage /= 2.0f;
        //      }
        if (wLeft >= (oldLeft - wSadfrr))
        {
            oldLeft = wLeft;
        }
        else
        {
            oldLeft -= wSadfrr;
            if (oldLeft < 0)
            {
                oldLeft = 0;
            }
        }
        if (wRight >= (oldRight - wSadfrr))
        {
            oldRight = wRight;
        }
        else
        {
            oldRight -= wSadfrr;
            if (oldRight < 0)
            {
                oldRight = 0;
            }
        }
        int wHeight = (height >> 1) - 24;
        drawVolumeMeterBar(pGrp, 16, 16, (int) (oldLeft * (float) (width - 32)), wHeight);
        //      drawVolumeMeterBar( pGrp, 16, wHeight + 22, (int)( ( vuAverage / vuSamples ) * (float)( width - 32 ) ), 4 );
        drawVolumeMeterBar(pGrp, 16, wHeight + 32, (int) (oldRight * (float) (width - 32)), wHeight);
        //      pGrp.fillRect( 16, 16, (int)( oldLeft  * (float)( width - 32 ) ), wHeight );
        //      pGrp.fillRect( 16, 64, (int)( oldRight * (float)( width - 32 ) ), wHeight );
    }

    /**
     * Method to draw Spectrum Analyser Bar.
     * 
     * @param pGraphics
     * @param pX
     * @param pY
     * @param pWidth
     * @param pHeight
     * @param band
     */
    private void drawSpectrumAnalyserBar(Graphics pGraphics, int pX, int pY, int pWidth, int pHeight, int band)
    {
        float c = 0;
        for (int a = pY; a >= pY - pHeight; a -= barOffset)
        {
            c += saColorScale;
            if (c < spectrumAnalyserColors.length)
            {
                pGraphics.setColor(spectrumAnalyserColors[(int) c]);
            }
            pGraphics.fillRect(pX, a, pWidth, 1);
        }
        if ((peakColor != null) && (peaksEnabled == true))
        {
            pGraphics.setColor(peakColor);
            if (pHeight > peaks[band])
            {
                peaks[band] = pHeight;
                peaksDelay[band] = peakDelay;
            }
            else
            {
                peaksDelay[band]--;
                if (peaksDelay[band] < 0) peaks[band]--;
                if (peaks[band] < 0) peaks[band] = 0;
            }
            pGraphics.fillRect(pX, pY - peaks[band], pWidth, 1);
        }
    }

    /**
     * Method to draw Volume Meter Bar.
     * 
     * @param pGraphics
     * @param pX
     * @param pY
     * @param pWidth
     * @param pHeight
     */
    private void drawVolumeMeterBar(Graphics pGraphics, int pX, int pY, int pWidth, int pHeight)
    {
        float c = 0;
        for (int a = pX; a <= pX + pWidth; a += 2)
        {
            c += vuColorScale;
            if (c < 256.0f)
            {
                pGraphics.setColor(spectrumAnalyserColors[(int) c]);
            }
            pGraphics.fillRect(a, pY, 1, pHeight);
        }
    }

    /**
     * 
     * @return an Image
     */
    private synchronized Image getDoubleBuffer()
    {
        if (bi == null || (bi.getWidth(null) != getSize().width || bi.getHeight(null) != getSize().height))
        {
            width = getSize().width;
            height = getSize().height;
            height_2 = height >> 1;
            computeColorScale();
            bi = getGraphicsConfiguration().createCompatibleVolatileImage(width, height);
        }
        return bi;
    }

    /**
     * Get default Spectrum Analyzer Colors.
     * 
     * @return Returns default Spectrum Analyzer Colors
     */
    public static Color[] getDefaultSpectrumAnalyserColors()
    {
        Color[] wColors = new Color[256];
        for (int a = 0; a < 128; a++)
        {
            wColors[a] = new Color(0, (a >> 1) + 192, 0);
        }
        for (int a = 0; a < 64; a++)
        {
            wColors[a + 128] = new Color(a << 2, 255, 0);
        }
        for (int a = 0; a < 64; a++)
        {
            wColors[a + 192] = new Color(255, 255 - (a << 2), 0);
        }
        return wColors;
    }

    /**
     * Get the current display mode.
     * 
     * @return Returns the current display mode, DISPLAY_MODE_SCOPE or DISPLAY_MODE_SPECTRUM_ANALYSER.
     */
    public int getDisplayMode()
    {
        return displayMode;
    }

    /**
     * Get the current number of bands displayed by the Spectrum Analyzer.
     * 
     * @return Returns the current number of bands displayed by the spectrum analyser.
     */
    public int getSpectrumAnalyserBandCount()
    {
        return saBands;
    }

    /**
     * Get the decay rate of the Spectrum Analyzer's bands.
     * 
     * @return Returns the decay rate of the spectrum analyser's bands.
     */
    public float getSpectrumAnalyserDecay()
    {
        return saDecay;
    }

    /**
     * Get the color the scope is rendered in.
     * 
     * @return Returns the color the scope is rendered in.
     */
    public Color getScopeColor()
    {
        return scopeColor;
    }

    /**
     * Get the color scale used to render the Spectrum Analyzer bars.
     * @return Returns the color scale used to render the spectrum analyser bars.
     */
    public Color[] getSpectrumAnalyserColors()
    {
        return spectrumAnalyserColors;
    }

    /**
     * Method is used to initialize the Spectrum Time Analyzer's state
     *
     */
    private void initialize()
    {
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        prepareDisplayToggleListener();
        setSpectrumAnalyserBandCount(DEFAULT_SPECTRUM_ANALYSER_BAND_COUNT);
        setSpectrumAnalyserFFTSampleSize(DEFAULT_SPECTRUM_ANALYSER_FFT_SAMPLE_SIZE);
    }

    /**
     * 
     * @return Returns 'true' if "Frames Per Second" are being calculated and displayed.
     */
    public boolean isShowingFPS()
    {
        return showFPS;
    }

    /**
     * Paint the Component.When dspStarted is true,draw the peaks
     */
    public void paintComponent(Graphics pGraphics)
    {
        if (displayMode == DISPLAY_MODE_OFF) return;
        if (dspStarted)
        {
            pGraphics.drawImage(getDoubleBuffer(), 0, 0, null);    
        }
        else
        {
            super.paintComponent(pGraphics);
        }
    }

    /**
     * Set Prepare Toggle Listener for displaying peaks 
     *
     */
    private void prepareDisplayToggleListener()
    {
        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent pEvent)
            {
                if (pEvent.getButton() == MouseEvent.BUTTON1)
                {
                    if (displayMode + 1 > 1)
                    {
                        displayMode = 0;
                    }
                    else
                    {
                        displayMode++;
                    }
                }
            }
        });
    }

    /**
     * @see kj.dsp.KJDigitalSignalProcessor#process(float[], float[], float)
     */
    public synchronized void process(float[] pLeft, float[] pRight, float pFrameRateRatioHint)
    {
        if (displayMode == DISPLAY_MODE_OFF) return;
        Graphics wGrp = getDoubleBuffer().getGraphics();
        wGrp.setColor(getBackground());
        wGrp.fillRect(0, 0, getSize().width, getSize().height);
        switch (displayMode)
        {
            case DISPLAY_MODE_SCOPE:
                drawScope(wGrp, stereoMerge(pLeft, pRight));
                break;
            case DISPLAY_MODE_SPECTRUM_ANALYSER:
                drawSpectrumAnalyser(wGrp, stereoMerge(pLeft, pRight), pFrameRateRatioHint);
                break;
            case DISPLAY_MODE_OFF:
                drawVUMeter(wGrp, pLeft, pRight, pFrameRateRatioHint);
                break;
        }
        // -- Show FPS if necessary.
        if (showFPS)
        {
            // -- Calculate FPS.
            if (System.currentTimeMillis() >= lfu + 1000)
            {
                lfu = System.currentTimeMillis();
                fps = fc;
                fc = 0;
            }
            fc++;
            wGrp.setColor(Color.yellow);
            wGrp.drawString("FPS: " + fps + " (FRRH: " + pFrameRateRatioHint + ")", 0, height - 1);
        }
        if (getGraphics() != null) getGraphics().drawImage(getDoubleBuffer(), 0, 0, null);
        //      repaint();
        //      try {
        //          EventQueue.invokeLater( new AWTPaintSynchronizer() );
        //      } catch ( Exception pEx ) {
        //          // -- Ignore exception.
        //          pEx.printStackTrace();
        //      }
    }

    /**
     * Sets the current display mode.
     *
     * @param pMode Must be either DISPLAY_MODE_SCOPE or DISPLAY_MODE_SPECTRUM_ANALYSER.
     */
    public synchronized void setDisplayMode(int pMode)
    {
        displayMode = pMode;
    }

    /**
     * Sets the color of the scope.
     *
     * @param pColor
     */
    public synchronized void setScopeColor(Color pColor)
    {
        scopeColor = pColor;
    }

    /**
     * When 'true' is passed as a parameter, will overlay the "Frames Per Seconds"
     * achieved by the component.
     *
     * @param pState
     */
    public synchronized void setShowFPS(boolean pState)
    {
        showFPS = pState;
    }

    /**
     * Sets the numbers of bands rendered by the spectrum analyser.
     *
     * @param pCount Cannot be more than half the "FFT sample size".
     */
    public synchronized void setSpectrumAnalyserBandCount(int pCount)
    {
        saBands = pCount;
        peaks = new int[saBands];
        peaksDelay = new int[saBands];
        computeSAMultiplier();
    }

    /**
     * Sets the spectrum analyser band decay rate.
     *
     * @param pDecay Must be a number between 0.0 and 1.0 exclusive.
     */
    public synchronized void setSpectrumAnalyserDecay(float pDecay)
    {
        if ((pDecay >= MIN_SPECTRUM_ANALYSER_DECAY) && (pDecay <= MAX_SPECTRUM_ANALYSER_DECAY))
        {
            saDecay = pDecay;
        }
        else saDecay = DEFAULT_SPECTRUM_ANALYSER_DECAY;
    }

    /**
     * Sets the spectrum analyser color scale.
     *
     * @param pColors Any amount of colors may be used. Must not be null.
     */
    public synchronized void setSpectrumAnalyserColors(Color[] pColors)
    {
        spectrumAnalyserColors = pColors;
        computeColorScale();
    } 

    /**
     * Sets the FFT sample size to be just for calculating the spectrum analyser
     * values. The default is 512.
     *
     * @param pSize Cannot be more than the size of the sample provided by the DSP.
     */
    public synchronized void setSpectrumAnalyserFFTSampleSize(int pSize)
    {
        saFFTSampleSize = pSize;
        fft = new KJFFT(saFFTSampleSize);
        old_FFT = new float[saFFTSampleSize];
        computeSAMultiplier();
    }

    /**
     * As a param for drawScope
     * 
     * @param pLeft
     * @param pRight
     * @return
     */
    private float[] stereoMerge(float[] pLeft, float[] pRight)
    {
        for (int a = 0; a < pLeft.length; a++)
        {
            pLeft[a] = (pLeft[a] + pRight[a]) / 2.0f;
        }
        return pLeft;
    }

    /*public void update(Graphics pGraphics)
    {
        // -- Prevent AWT from clearing background.
        paint(pGraphics);
    }*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -