简体   繁体   English

更改 java 中的 MIDI 音量

[英]changing midi volume in java

first of all changing volume is working but i got some problems while changing the volume so 1) after changing the volume like setting it to 0 (no volume) you still hearing parts of the song in the default value of the volume 2)after song changed the volume getting back to the default value of the volume首先,改变音量是有效的,但我在改变音量时遇到了一些问题,所以 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();
        }
}

playing 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();

}
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);

This code sends one message.此代码发送一条消息。 The contents of that message are the values set by the second setMessage call, so the volume MSB is not changed at all.该消息的内容是由第二个setMessage调用设置的值,因此音量 MSB 根本没有改变。 (And this is the wrong way of computing the LSB.) (这是计算 LSB 的错误方法。)

Controller 7 is indeed for volume, but there is no exact standard for how the values are to be interpreted. Controller 7 确实是体积,但是对于如何解释这些值没有确切的标准。 Apparently, your synthesizer does not mute itself when it receives a volume of zero.显然,您的合成器在收到零音量时不会自行静音。

Many MIDI files do their own changes to controller 7. If your synthesizer supports Master Volume , you should send that instead.许多 MIDI 文件对 controller 进行了自己的更改 7. 如果您的合成器支持Master Volume ,您应该发送它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM