我尝试创建一个方法,在卷数组中搜索最大值,并返回音符数组相应位置的值。
另外,另外,我怎么能修改它以便它只播放最响亮的音符
import arb.soundcipher.*;
SoundCipher midi;
String[] note = {"C", "C#", "D", "D#", "E", "F", "F#",
"G", "G#", "A", "A#", "B"};
float[] volume = {80, 100, 75, 43, 40, 81, 100,
60, 90, 30, 75, 52};
float[] duration = {1.3, 2, 0.5, 3, 0.9, 1, 0.25,
0.6, 1.5, 3, 1.25, 2};
void setup() {
size(200,200);
midi = new SoundCipher(this);
int i = (int) random(note.length);
midi.playNote(MIDIValue(note[i]),volume[i],duration[i]);
}
float MIDIValue(String aNote) {
float noteMIDIValue = 0.0;
int i;
for (i =0; i < note.length && !note[i].equals(aNote) ; i++);
if(i < note.length) {
noteMIDIValue = i+60;
}
return noteMIDIValue;
}
答案 0 :(得分:0)
遍历volume
数组并跟踪最大音量和id
(索引)。最后,返回与最大元素ID(note
)对应的node[id]
。
方法实施......
String findLargestVolumeNote() {
int maxId = 0;
float maxVol = volume[0];
// searching array for loudest volume
for(int i = 0; i < volume.length; i++) {
if(volume[i] > maxVol) {
maxId = i;
maxVol = volume[i];
}
}
return note[maxId]; // returning corrsponding note
}