How to control the MIDI channel's volume

独自空忆成欢 提交于 2019-12-18 06:54:17

问题


I have this code:

Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
Instrument[] instrument = synthesizer.getDefaultSoundbank().getInstruments();
synthesizer.loadInstrument(instrument[29]);
MidiChannel[] channels = synthesizer.getChannels();
MidiChannel channel = channels[1];
channel.programChange(29);
channel.noteOn(noteNumber, 127);
Teszthang.sleep(2000);
channel.noteOff(noteNumber);

so this is an example, to play a sound in max volume (127) for 2 seconds. but i want to control the channel's volume, like after 2 seconds, the volume fade out in an another 2 seconds. How could I do that? I know these methods:

channel.controlChange(controller, value);
channel.setPolyPressure(noteNumber, pressure);

but these don't change any volume! I don't know how to use these methods. How could I change the channel's volume after the noteOn() while it has been playing?


回答1:


You can use CC 7 for setting channel volume.

channel.controlChange(7, value);

see: http://improv.sapp.org/doc/class/MidiOutput/controllers/controllers.html




回答2:


Sometimes you have some volume events in the midi file so you cannot change channel volume. After getting the sequence, remove these events :

Track[] tracks = sequence.getTracks();
for (Track track : tracks){
for(int i = 0; i < track.size(); i++){
    if(!track.remove(track.get(i))){
        System.out.println("MIDI Event not removed");
    }
}}


来源:https://stackoverflow.com/questions/8008286/how-to-control-the-midi-channels-volume

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!