当前位置: 首页 > 工具软件 > Tempo > 使用案例 >

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

锺离嘉容
2023-12-01

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

}

}

 类似资料: