1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
import java.util.*;
import javax.sound.midi.*;
/* Un générateure de batukada aléatoire (percussions)
*
* 2004-2007 Sylvain Tournois - http://sylv.tournois.free.fr/
*/
public class RandomBatukada implements MetaEventListener {
private static final int NO_RYTHM = 0;
private static final int SILENCE = 1;
private static final int NO_RYTHM_2 = 2;
private static final int TRACK_LENGTH = 64;
private static final int MULTIPLICATOR = 9 * 5 * 7;
private static final int IC = 47;
private int[][][] instrumentsRythmes =
{ {{/*"Acoustic bass drum" */}, {}, {}, {0, 3, 6, 9, 14}},
{{/*"Bass drum 1" */}, {}, {}, {0, 4, 8, 12}},
{{/*"Side stick" */}, {}, {}, {2, 6, 13}},
{{/*"Acoustic snare" */}, {}, {}, {11}},
{{/*"Hand clap" */}, {}, {}, {4, 6, 9}},
{{/*"Electric snare" */}, {}, {}, {4, 12}},
{{/*"Low floor tom" */}, {}, {}, {0, 2, 13}},
{{/*"Closed hi-hat" */}, {}, {}, {0, 2, 4}},
{{/*"High floor tom" */}, {}, {}, {6, 10}},
{{/*"Pedal hi-hat" */}, {}, {}, {1, 5, 8}},
{{/*"Low tom" */}, {}, {}, {0, 12}},
{{/*"Open hi-hat" */}, {}, {}, {10}},
{{/*"Low-mid tom" */}, {}, {}, {0, 3, 13}},
{{/*"Hi-mid tom" */}, {}, {}, {1, 4, 12}},
{{/*"Crash cymbal 1" */}, {}, {}, {}},
{{/*"High tom" */}, {}, {}, {4, 10, 12, 14}},
{{/*"Ride cymbal 1" */}, {}, {}, {}},
{{/*"Chinese cymbal" */}, {}, {}, {}},
{{/*"Ride bell" */}, {}, {}, {0}},
{{/*"Tambourine" */}, {}, {}, {0, 2, 4, 6, 8, 9, 11, 12, 13, 14}},
{{/*"Splash cymbal" */}, {}, {}, {}},
{{/*"Cowbell" */}, {}, {}, {0}},
{{/*"Crash cymbal 2" */}, {}, {}, {}},
{{/*"Vibraslap" */}, {}, {}, {1}},
{{/*"Ride cymbal 2" */}, {}, {}, {}},
{{/*"Hi bongo" */}, {}, {}, {2, 4, 12, 14}},
{{/*"Low bongo" */}, {}, {}, {0, 8}},
{{/*"Mute hi conga" */}, {}, {}, {1}},
{{/*"Open hi conga" */}, {}, {}, {3}},
{{/*"Low conga" */}, {}, {}, {5}},
{{/*"High timbale" */}, {}, {}, {6}},
{{/*"Low timbale" */}, {}, {}, {10}},
{{/*"High agogo" */}, {}, {}, {0, 3, 6, 9, 12}},
{{/*"Low agogo" */}, {}, {}, {0, 2, 4, 6, 8, 10, 12, 14}},
{{/*"Cabasa" */}, {}, {}, {1, 5, 9, 13}},
{{/*"Maracas" */}, {}, {}, {7, 15}},
{{/*"Short whistle" */}, {}, {}, {10, 13}},
{{/*"Long whistle" */}, {}, {}, {0}},
{{/*"Short guiro" */}, {}, {}, {10, 13}},
{{/*"Long guiro" */}, {}, {}, {0}},
{{/*"Claves" */}, {}, {}, {0, 2, 6, 10, 12}},
{{/*"Hi wood block" */}, {}, {}, {0, 2, 12}},
{{/*"Low wood block" */}, {}, {}, {4, 6, 10}},
{{/*"Mute cuica" */}, {}, {}, {4}},
{{/*"Open cuica" */}, {}, {}, {13}},
{{/*"Mute triangle" */}, {}, {}, {0, 3, 6, 9}},
{{/*"Open triangle" */}, {}, {}, {12}} };
private String instruments[] =
{ "Acoustic bass drum",
"Bass drum 1",
"Side stick",
"Acoustic snare",
"Hand clap",
"Electric snare",
"Low floor tom",
"Closed hi-hat",
"High floor tom",
"Pedal hi-hat",
"Low tom",
"Open hi-hat",
"Low-mid tom",
"Hi-mid tom",
"Crash cymbal 1",
"High tom",
"Ride cymbal 1",
"Chinese cymbal",
"Ride bell",
"Tambourine",
"Splash cymbal",
"Cowbell",
"Crash cymbal 2",
"Vibraslap",
"Ride cymbal 2",
"Hi bongo",
"Low bongo",
"Mute hi conga",
"Open hi conga",
"Low conga",
"High timbale",
"Low timbale",
"High agogo",
"Low agogo",
"Cabasa",
"Maracas",
"Short whistle",
"Long whistle",
"Short guiro",
"Long guiro",
"Claves",
"Hi wood block",
"Low wood block",
"Mute cuica",
"Open cuica",
"Mute triangle",
"Open triangle" };
private static final int PROGRAM = 192;
private static final int NOTEON = 144;
private static final int NOTEOFF = 128;
private static final int velocity = 100;
private Vector data;
private Hashtable dataKeys;
private Sequencer sequencer;
private Sequence sequence;
private Track track;
private int tempo;
private boolean ready;
private boolean initOk;
private boolean midiExit;
private long midiExitDate;
private int[] lastPlayed;
private Random random;
public static void main(String[] args) {
RandomBatukada rb = new RandomBatukada();
rb.setTempo(109);
while (true)
rb.live();
}
public RandomBatukada() {
random = new Random();
ready = false;
midiExit = false;
initOk = false;
lastPlayed = new int[6];
for (int i = 0 ; i < 6 ; i++)
lastPlayed[i] = random.nextInt(instruments.length);
}
public void setTempo(int bpm) {
tempo = bpm;
}
public void play(String instrument, int time) {
play(instrument, time, time);
}
public void play(String instrument, int timeStart, int timeEnd) {
Data dt = (Data)dataKeys.get(instrument);
if (dt != null)
dt.play(timeStart, timeEnd);
}
public void silence(String instrument, int time) {
silence(instrument, time, time);
}
public void silence(String instrument, int timeStart, int timeEnd) {
Data dt = (Data)dataKeys.get(instrument);
if (dt != null)
dt.silence(timeStart, timeEnd);
}
public void playRythm(String instrument) {
Data dt = (Data)dataKeys.get(instrument);
if (dt != null)
dt.playRythm();
}
public void playRythm(String instrument, int r) {
Data dt = (Data)dataKeys.get(instrument);
if (dt != null)
dt.playRythm(r);
}
private void createEvent(int type, int chan, int num, long tick) {
ShortMessage message = new ShortMessage();
try {
message.setMessage(type, chan, num, velocity);
MidiEvent event = new MidiEvent( message, tick );
track.add(event);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void createSequence() {
try {
sequence = new Sequence(Sequence.PPQ, 4 * MULTIPLICATOR);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void initTrack() {
createSequence();
track = sequence.createTrack();
}
private void refreshTrack() {
initTrack();
createEvent(PROGRAM, 9, 1, 0);
for (int i = 0; i < data.size(); i++) {
Data d = (Data)data.get(i);
for (int j = 0; j < d.staff.length; j++)
if (d.staff[j]) {
createEvent(NOTEON, 9, d.id, j);
createEvent(NOTEOFF, 9, d.id, j + 1);
}
}
createEvent(PROGRAM, 9, 1, (TRACK_LENGTH * MULTIPLICATOR) - 1);
}
private void setSequencer() {
try {
sequencer.setSequence(sequence);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void startSequencer() {
refreshTrack();
setSequencer();
sequencer.start();
sequencer.setTempoInBPM(tempo);
}
private void initSequencer() {
sequencer = null;
try {
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.addMetaEventListener(this);
startSequencer();
}
catch (Exception e) {
sequencer = null;
e.printStackTrace();
}
}
private void createMidi() {
data = new Vector();
dataKeys = new Hashtable();
Data dt;
for (int i = 0, id = 35; i < instruments.length; i++, id++) {
dt = new Data(instruments[i], id);
data.add(dt);
dataKeys.put(instruments[i], dt);
}
for (int i = 0 ; i < 7 ; i++)
playRythm(instruments[random.nextInt(instruments.length)]);
for (int i = 0 ; i < 6 ; i++)
playRythm(instruments[lastPlayed[i]]);
setTempo(tempo);
initSequencer();
ready = sequencer != null;
}
public void meta(javax.sound.midi.MetaMessage metaMessage) {
if (metaMessage.getType() == 47) // 47 is end of track
startSequencer();
}
protected void finalize() throws Throwable {
close();
super.finalize();
}
public void close() {
if (sequencer != null)
sequencer.close();
}
public void live() {
if (ready) {
setTempo(tempo);
if (random.nextBoolean()) {
int n = random.nextInt(3) + 1;
for (int i = 0 ; i < 1 ; i++)
silence(instruments[random.nextInt(instruments.length)], 0, TRACK_LENGTH * MULTIPLICATOR - 1);
for (int i = 0 ; i < 2 ; i++) {
lastPlayed[i] = lastPlayed[i + 2];
lastPlayed[i + 2] = lastPlayed[i + 4];
lastPlayed[i + 4] = random.nextInt(instruments.length);
playRythm(instruments[lastPlayed[i + 4]]);
}
}
else {
for (int i = 0 ; i < 6 ; i++)
playRythm(instruments[lastPlayed[i]], NO_RYTHM_2);
for (int i = 0 ; i < 3 ; i++) {
lastPlayed[i] = lastPlayed[i + 3];
lastPlayed[i + 3] = random.nextInt(instruments.length);
playRythm(instruments[lastPlayed[i + 3]], NO_RYTHM_2);
}
}
}
else
if (!initOk) {
createMidi();
initOk = true;
}
if (midiExit) {
if (System.currentTimeMillis() > midiExitDate)
System.exit(0);
}
}
class Data {
String name;
int id;
boolean[] staff;
int[][] rythmes;
public Data(String name, int id) {
this.name = name;
this.id = id;
rythmes = instrumentsRythmes[id - 35];
staff = new boolean[TRACK_LENGTH * MULTIPLICATOR];
for (int i = 0; i < staff.length; i++)
staff[i] = false;
}
public void playRythm() {
playRythm(random.nextInt(rythmes.length - 2) + 2);
}
public void playRythm(final int r) {
switch(r) {
case NO_RYTHM : {
staff[random.nextInt(TRACK_LENGTH) * MULTIPLICATOR] = true;
staff[random.nextInt(TRACK_LENGTH) * MULTIPLICATOR] = false;
break;
}
case NO_RYTHM_2 : {
boolean okt = false;
boolean okf = false;
for (int i = 0 ; i < TRACK_LENGTH / 2 ; i++) {
okt |= staff[(i * 2 + 1) * MULTIPLICATOR];
okt |= staff[i * 2 * MULTIPLICATOR];
okf |= !staff[i * 2 * MULTIPLICATOR];
}
if (!okt) {
staff[random.nextInt(TRACK_LENGTH) * MULTIPLICATOR] = true;
break;
}
if (!okf) {
staff[random.nextInt(TRACK_LENGTH) * MULTIPLICATOR] = false;
break;
}
int i0 = random.nextInt(TRACK_LENGTH) * MULTIPLICATOR;
while (!staff[i0])
i0 = random.nextInt(TRACK_LENGTH) * MULTIPLICATOR;
int i1 = random.nextInt(TRACK_LENGTH / 2) * 2 * MULTIPLICATOR;
while (staff[i1])
i1 = random.nextInt(TRACK_LENGTH / 2) * 2 * MULTIPLICATOR;
if (i0 < i1) {
staff[i0] = false;
staff[i1] = true;
}
break;
}
case SILENCE : {
for (int i = 0 ; i < TRACK_LENGTH * MULTIPLICATOR ; i++)
staff[i] = false;
break;
}
default : {
for (int i = 0 ; i < TRACK_LENGTH * MULTIPLICATOR ; i++)
staff[i] = false;
for (int j = 0 ; j < TRACK_LENGTH / 16 ; j++)
for (int i = 0 ; i < rythmes[r].length ; i++)
staff[(rythmes[r][i % 16] + j * 16) * MULTIPLICATOR] = true;
break;
}
}
}
public void play(int timeStart, int timeEnd) {
for (int i = timeStart ; i <= timeEnd ; i++)
staff[i] = true;
}
public void silence(int timeStart, int timeEnd) {
for (int i = timeStart ; i <= timeEnd ; i++)
staff[i] = false;
}
}
} |