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

📄 musiceffects.java

📁 j2me源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *
 * Copyright (c) 2007, Sun Microsystems, Inc.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of Sun Microsystems nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.io.*;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.amms.*;
import javax.microedition.amms.control.*;
import javax.microedition.amms.control.audioeffect.*;

public class MusicEffects extends MIDlet {

    // UI stuff {

    private final static int MAX_GAUGE_VALUE = 25;

    // Commands and listeners

    Command exitCommand = new Command("Exit", Command.EXIT, 1);
    Command setupItemCommand = new Command("Setup", Command.ITEM, 2);
    Command finishSetupCommand = new Command("Done", Command.BACK, 1);

    public class ExitCommandListener implements CommandListener {
        public void commandAction(Command c, Displayable d) {
            if (c == null)
                return;
            if (c == exitCommand) {
                destroyApp(true);
                notifyDestroyed();
            }
        }
    }

    public class SetupItemCommandListener implements ItemCommandListener {

        private Form setupForm;    

        public SetupItemCommandListener(Form form) {
            setupForm = form;
        }

        public void commandAction(Command c, Item item) {
            if (c == null)
                return;
            if (c == setupItemCommand)
                Display.getDisplay(MusicEffects.this).setCurrent(setupForm);
        }
    }

    public class FinishSetupCommandListener implements CommandListener {
        public void commandAction(Command c, Displayable d) {
            if (c == null)
                return;
            if (c == finishSetupCommand) {
                Display.getDisplay(MusicEffects.this).setCurrent(mainForm);
                mainForm.updateControls(equalizerForm.getMode());
            }
        }
    }

    FinishSetupCommandListener finishSetupCommandListener = new FinishSetupCommandListener();

    
    // equalizerForm stuff

    public class EqualizerForm extends Form implements ItemStateListener {

        public final static int MODE_BASS_TREBLE = 0;
        public final static int MODE_PRESET = 1;
        public final static int MODE_BANDS = 2;

        int mode = MODE_BASS_TREBLE;
        
        ChoiceGroup modeChoice = new ChoiceGroup("Equalizer mode", Choice.EXCLUSIVE, new String[] {"Simple (bass/treble)", "Preset", "Custom"}, null);
        ChoiceGroup presetChoice = new ChoiceGroup("Preset", Choice.EXCLUSIVE);
        Gauge [] bandGauges;

        int minLevel = -1;
        int maxLevel = 1;

        public EqualizerForm() {
            super("Advanced Equalizer Setup");

            if (equalizerControl != null) {
                String [] presets = equalizerControl.getPresetNames();
                if (presets != null && presets.length != 0)
                    for (int i = 0; i < presets.length; ++i)
                        presetChoice.append(presets[i], null);
                else
                    presetChoice.setLabel("No Presets...");

                minLevel = equalizerControl.getMinBandLevel();
                maxLevel = equalizerControl.getMaxBandLevel();
                if (maxLevel <= minLevel)
                    maxLevel = minLevel + 1;
                
                int num = equalizerControl.getNumberOfBands();

                bandGauges = new Gauge [num];

                for (int i = 0; i < num; ++i) {
                    int level = (equalizerControl.getBandLevel(i) - minLevel) * MAX_GAUGE_VALUE / (maxLevel - minLevel);
                    bandGauges[i] = new Gauge(String.valueOf(equalizerControl.getCenterFreq(i) / 1000) + " hz", true, MAX_GAUGE_VALUE, level);
                    bandGauges[i].setLayout(Item.LAYOUT_EXPAND);
                }
            }

            append(modeChoice);
            
            setItemStateListener(this);
            addCommand(finishSetupCommand);
            setCommandListener(finishSetupCommandListener);
        }

        public int getMode() { return mode; }

        public void setMode(int newMode) {
            if (mode == newMode)
                return;
            
            deleteAll();

            append(modeChoice);

            switch (newMode) {
            case MODE_BASS_TREBLE: break;
            case MODE_PRESET: append(presetChoice); break;
            case MODE_BANDS:
               for (int i = 0; i < bandGauges.length; ++i)
                   append(bandGauges[i]);
               break;
            }
            
            mode = newMode;
        }

        public void itemStateChanged(Item item) {
            if (item == null)
                return;
            else if (item == modeChoice)
                setMode(modeChoice.getSelectedIndex());
            else if (item == presetChoice)
                equalizerControl.setPreset(presetChoice.getString(presetChoice.getSelectedIndex()));
            else
                for (int i = 0; i < bandGauges.length; ++i)
                    if (item == bandGauges[i]) {
                        equalizerControl.setBandLevel(minLevel + bandGauges[i].getValue() * (maxLevel - minLevel) / MAX_GAUGE_VALUE, i);
                        break;
                    }
        }
    }


    // reverbForm stuff

    public class ReverbForm extends Form implements ItemStateListener {
        public final static int MIN_TIME = 1;
        public final static int MAX_TIME = 10000;
        public final static int MIN_LEVEL = -1200;
        public final static int MAX_LEVEL = 0;

        Gauge timeGauge = new Gauge("Time", true, MAX_GAUGE_VALUE, 0);
        Gauge levelGauge = new Gauge("Level", true, MAX_GAUGE_VALUE, 0);
        Gauge roomLevelGauge = new Gauge("Room level", true, MAX_GAUGE_VALUE, 0);
        ChoiceGroup presetChoice = new ChoiceGroup("Reverb Preset", Choice.EXCLUSIVE);

        public ReverbForm() {
            super("Reverb Setup");

            timeGauge.setLayout(Item.LAYOUT_EXPAND);
            levelGauge.setLayout(Item.LAYOUT_EXPAND);
            roomLevelGauge.setLayout(Item.LAYOUT_EXPAND);

            try {
                timeGauge .setValue((reverbControl.getReverbTime() - MIN_TIME ) * MAX_GAUGE_VALUE / (MAX_TIME - MIN_TIME ));
                levelGauge.setValue((reverbControl.getReverbLevel() - MIN_LEVEL) * MAX_GAUGE_VALUE / (MAX_LEVEL - MIN_LEVEL));
                roomLevelGauge.setValue((reverbSourceControl.getRoomLevel() - MIN_LEVEL) * MAX_GAUGE_VALUE / (MAX_LEVEL - MIN_LEVEL));
            } catch (Exception e) {}

            presetChoice.append("TURN OFF", null);

            String preset = reverbControl.getPreset();
            String [] presets = reverbControl.getPresetNames();
            for (int i = 0; i < presets.length; ++i) {
                presetChoice.append(presets[i], null);
                if (presets[i].equals(preset) && reverbControl.isEnabled())
                    presetChoice.setSelectedIndex(i + 1, true);
            }

            append(timeGauge);
            append(levelGauge);
            append(roomLevelGauge);
            append(presetChoice);

            setItemStateListener(this);
            addCommand(finishSetupCommand);
            setCommandListener(finishSetupCommandListener);
        }

        public void itemStateChanged(Item item) {
            if (item == null)
                return;
            else if (item == timeGauge)
                try {
                    reverbControl.setReverbTime(MIN_TIME + timeGauge.getValue() * (MAX_TIME - MIN_TIME ) / MAX_GAUGE_VALUE);
                } catch (Exception e) {
                    System.out.println(e);
                }
            else if (item == levelGauge)
                try {
                    reverbControl.setReverbLevel(MIN_LEVEL + levelGauge.getValue() * (MAX_LEVEL - MIN_LEVEL) / MAX_GAUGE_VALUE);
                } catch (Exception e) {
                    System.out.println(e);
                }
            else if (item == roomLevelGauge)
                try {
                    reverbSourceControl.setRoomLevel(MIN_LEVEL + roomLevelGauge.getValue() * (MAX_LEVEL - MIN_LEVEL) / MAX_GAUGE_VALUE);
                } catch (Exception e) {
                    System.out.println(e);
                }
            else if (item == presetChoice) {
                if (0 == presetChoice.getSelectedIndex())
                    reverbControl.setEnabled(false);
                else {
                    if (!reverbControl.isEnabled())
                        reverbControl.setEnabled(true);
                    reverbControl.setPreset(presetChoice.getString(presetChoice.getSelectedIndex()));
                }
            }
        }
    }


    // chorusForm stuff

    public class ChorusForm extends Form implements ItemStateListener {

        ChoiceGroup presetChoice = new ChoiceGroup("Chorus mode", Choice.EXCLUSIVE);
        Gauge wetLevelGauge = new Gauge("Wet level", true, MAX_GAUGE_VALUE, 0);
        Gauge modulationRateGauge = new Gauge("Modulation rate", true, MAX_GAUGE_VALUE, 0);
        Gauge modulationDepthGauge = new Gauge("Modulation depth", true, MAX_GAUGE_VALUE, 0);
        Gauge averageDelayGauge = new Gauge("Average delay", true, MAX_GAUGE_VALUE, 0);

        int minModulationRate = 0;
        int maxModulationRate = 1000;
        int maxModulationDepth = 100;
        int maxAverageDelay = 1000;

        public ChorusForm() {
            super("Chorus Setup");

            minModulationRate  = chorusControl.getMinModulationRate();
            maxModulationRate  = chorusControl.getMaxModulationRate();
            maxModulationDepth = chorusControl.getMaxModulationDepth();
            maxAverageDelay    = chorusControl.getMaxAverageDelay();

            presetChoice.append("OFF", null);
            String [] presets = chorusControl.getPresetNames();
            if (presets != null && presets.length != 0) {
                String preset = chorusControl.getPreset();
                for (int i = 0; i < presets.length; ++i) {
                    presetChoice.append(presets[i], null);
                    if (presets[i].equals(preset))
                        presetChoice.setSelectedIndex(i + 1, true);
                }
            }
            append(presetChoice);

            updateControls();

            wetLevelGauge       .setLayout(Item.LAYOUT_EXPAND);
            modulationRateGauge .setLayout(Item.LAYOUT_EXPAND);
            modulationDepthGauge.setLayout(Item.LAYOUT_EXPAND);
            averageDelayGauge   .setLayout(Item.LAYOUT_EXPAND);

            append(wetLevelGauge);
            append(modulationRateGauge);
            append(modulationDepthGauge);
            append(averageDelayGauge);

            setItemStateListener(this);

⌨️ 快捷键说明

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