java音乐bpm,Java Tempo.setBpm方法代码示例

import com.leff.midi.event.meta.Tempo; //导入方法依赖的package包/类

private void createMidiFile(String filename) {

// 1. Create some MidiTracks

MidiTrack tempoTrack = new MidiTrack();

MidiTrack noteTrack = new MidiTrack();

// 2. Add events to the tracks

// 2a. Track 0 is typically the tempo map

TimeSignature ts = new TimeSignature();

ts.setTimeSignature(4, 4, TimeSignature.DEFAULT_METER,

TimeSignature.DEFAULT_DIVISION);

Tempo t = new Tempo();

t.setBpm(228);

tempoTrack.insertEvent(ts);

tempoTrack.insertEvent(t);

// 2b. Track 1 will have some notes in it

// for (int i = 0; i < 80; i++) {

final SheetMusic sheetMusic = mSheetMusicView.getSheetMusic();

final Staff staff = sheetMusic.getPart().getStaff();

int channel = 0;

int pitch = NoteScale.ToNumber(NoteScale.C, 5);

int velocity = 100;

for (MusicalFigure musicalFigure : staff.getMusicalFigures()) {

if (musicalFigure instanceof Measure) {

for (DurationFigure durationFigure : ((Measure) musicalFigure)

.getDurationFigures()) {

final Note note = (Note) durationFigure;

pitch = NoteScale.ToNumber(

Note.Step.getNoteScale(note.getStep()),

note.getOctave());

noteTrack.insertNote(channel, pitch + 1, velocity,

pitch + 1, 480 * durationFigure.getDuration());

}

}

}

// NoteOn on = new NoteOn(i * 480, channel, pitch, velocity);

// NoteOff off = new NoteOff(i * 480 + 120, channel, pitch, 0);

//

// noteTrack.insertEvent(on);

// noteTrack.insertEvent(off);

// There is also a utility function for notes that you should use

// instead of the above.

// noteTrack.insertNote(channel, pitch, velocity, i, 480 * 4);

// }

// It's best not to manually insert EndOfTrack events; MidiTrack will

// call closeTrack() on itself before writing itself to a file

// 3. Create a MidiFile with the tracks we created

ArrayList tracks = new ArrayList();

tracks.add(tempoTrack);

tracks.add(noteTrack);

MidiFile midi = new MidiFile(MidiFile.DEFAULT_RESOLUTION, tracks);

// 4. Write the MIDI data to a file

File output = new File(MusicFileManager.DEFAULT_STORAGE_DIRECTORY

+ filename);

try {

output.createNewFile();

//File output = File.createTempFile(filename, "", new File(

//MusicFileManager.DEFAULT_STORAGE_DIRECTORY));

midi.writeToFile(output);

} catch (IOException e) {

System.err.println(e);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值