gpt4 book ai didi

java - 你如何在java中使用一个音序器实现多个节奏

转载 作者:行者123 更新时间:2023-11-30 11:08:39 25 4
gpt4 key购买 nike

我一直在使用 Java 开发节拍器音乐应用程序,但偶然发现了一个复杂问题。考虑以下摘自的代码:https://community.oracle.com/thread/2135272 .这里的实现类似于我为获得多个节奏所做的。我遇到的唯一问题是它让事情变得有点困惑,我希望也许有人可以提供一种替代方法来最好通过使用一个音序器来获得多个速度。

import java.util.*;
import javax.swing.*;
import javax.sound.midi.*;

public class MetronomeTestApplet extends JApplet
{
public void init()
{
MetronomeTest metTest = new MetronomeTest();
metTest.play();
}
}

class MetronomeTest
{
Sequencer sequencer1=null;
Sequencer sequencer2=null;

Sequence sequence1=null;
Sequence sequence2=null;

Track track1=null;
Track track2=null;

MidiEvent newEvent=null;

int volume = 90;
int tempo = 100;

public MetronomeTest()
{
try
{ // SEQUENCER 1
sequencer1 = MidiSystem.getSequencer();
sequencer1.open();
sequence1 = new Sequence(Sequence.PPQ, 16);
track1 = sequence1.createTrack();

ShortMessage instrumentMsg = new ShortMessage();
instrumentMsg.setMessage(ShortMessage.PROGRAM_CHANGE, 0, 115, 0); // 115 == woodblock
track1.add(new MidiEvent(instrumentMsg, 0));

// SEQUENCER 2
sequencer2 = MidiSystem.getSequencer();
sequencer2.open();
sequence2 = new Sequence(Sequence.PPQ, 16);
track2 = sequence2.createTrack();

instrumentMsg = new ShortMessage();
instrumentMsg.setMessage(ShortMessage.PROGRAM_CHANGE, 0, 117, 0); // 117 == melodic tom
track2.add(new MidiEvent(instrumentMsg, 0));
}
catch (Exception e) { e.printStackTrace(); }
}

public void destroy()
{
sequencer1.stop();
sequencer1.close();

sequencer2.stop();
sequencer2.close();
}

public void play()
{
try
{
// LOAD SEQUENCE 1
for (int i=0; i<4; i++)
{
ShortMessage noteOnMsg = new ShortMessage();
noteOnMsg.setMessage(ShortMessage.NOTE_ON, 0, 60, volume);
track1.add(new MidiEvent(noteOnMsg, 0 + 16*i));

ShortMessage noteOffMsg = new ShortMessage();
noteOffMsg.setMessage(ShortMessage.NOTE_OFF, 0, 60, volume);
track1.add(new MidiEvent(noteOffMsg, 16 + 16*i));
}

// LOAD SEQUENCE 2
for (int i=0; i<8; i++)
{
ShortMessage noteOnMsg = new ShortMessage();
noteOnMsg.setMessage(ShortMessage.NOTE_ON, 0, 60, volume);
track2.add(new MidiEvent(noteOnMsg, 0 + 8*i));

ShortMessage noteOffMsg = new ShortMessage();
noteOffMsg.setMessage(ShortMessage.NOTE_OFF, 0, 60, volume);
track2.add(new MidiEvent(noteOffMsg, 8 + 8*i));
}

// LOAD AND START SEQUENCERS
sequencer1.setSequence(sequence1);
sequencer1.setTempoInBPM(tempo);
sequencer1.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);

sequencer2.setSequence(sequence2);
sequencer2.setTempoInBPM(tempo);
sequencer2.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);

sequencer1.start();
sequencer2.start();
}
catch (Exception e) { e.printStackTrace(); }
}
}

最佳答案

documentation说:

A sequence can contain events that encapsulate standard MIDI tempo-change messages. When the sequencer processes such an event, it changes the speed of playback to reflect the indicated tempo.

关于java - 你如何在java中使用一个音序器实现多个节奏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28457721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com