在java中更改midi音量

ztyzrc3y  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(377)

首先,更改音量是可行的,但我在更改音量时遇到了一些问题,因此1)更改音量(如设置为0(无音量))后,您仍能听到部分歌曲的默认音量值2)更改音量后,恢复到默认音量值

public static void setVolume(double value) {
    System.out.println();
    int CHANGE_VOLUME = 7;
    midivol =(value);
    try {
        if (synthesizer.getDefaultSoundbank() == null) {
            System.out.println(444);
                ShortMessage volumeMessage = new ShortMessage();
                for (int i = 0; i < 16; i++) {
                    volumeMessage.setMessage(ShortMessage.CONTROL_CHANGE, i, CHANGE_VOLUME,(int)(value * 127.0));
                    volumeMessage.setMessage(ShortMessage.CONTROL_CHANGE, i, 39, (int)(value * 127.0));
                    MidiSystem.getReceiver().send(volumeMessage, -1);
                }
        } else {  
             MidiChannel[] channels = synthesizer.getChannels();

              for( int c = 0; c < channels.length; c++ ) {
                 if(channels[c] != null)   channels[c].controlChange( 7, (int)( value*127.0) );
              }
        }
        music.setSequence(sequence);
    } catch (Exception e) {
            e.printStackTrace();
        }
}

播放midi:

private void playMidi(String location) {
    double gain =Slider.musicvolume;
    music = null;
    //synthesizer = null;
    sequence = null;
    File midiFile = new File(location);
    try {
    sequence = MidiSystem.getSequence(midiFile);
        music = MidiSystem.getSequencer(false);
        music.open();
        music.setSequence(sequence);
    } catch (Exception e) {
        System.err.println("Problem loading MIDI file.");
        e.printStackTrace();
        return;
    }
    if (music instanceof Synthesizer) {
        synthesizer = (Synthesizer) music;
    } else {
        try {
            synthesizer = MidiSystem.getSynthesizer();
            synthesizer.open();
            if (synthesizer.getDefaultSoundbank() == null) {
                music.getTransmitter().setReceiver(MidiSystem.getReceiver());
            } else {
                music.getTransmitter().setReceiver(synthesizer.getReceiver());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
      MidiChannel[] channels = synthesizer.getChannels();
     for (int i = 0; i < channels.length; i++) {
        channels[i].controlChange(7, (int) (gain * 127.0));
      }
     try {
        music.setSequence(sequence);
    } catch (InvalidMidiDataException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    music.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
    music.start();

}
yk9xbfzb

yk9xbfzb1#

volumeMessage.setMessage(ShortMessage.CONTROL_CHANGE, i, CHANGE_VOLUME,(int)(value * 127.0));
volumeMessage.setMessage(ShortMessage.CONTROL_CHANGE, i, 39, (int)(value * 127.0));
MidiSystem.getReceiver().send(volumeMessage, -1);

此代码发送一条消息。该消息的内容是第二个 setMessage 调用,因此卷msb根本没有更改(这是计算lsb的错误方法。)
控制器7确实是针对音量的,但是对于如何解释这些值没有确切的标准。显然,当合成器接收到的音量为零时,它不会自动静音。
许多midi文件对controller7做了自己的更改。如果你的合成器支持主音量,你应该改为发送。

相关问题