— Scott McNealy, Sun Microsystems Chairman, President and ceo


The Message says what to do, the



Download 40,57 Mb.
Pdf ko'rish
bet323/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   319   320   321   322   323   324   325   326   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

The Message says what to do, the 
MidiEvent says when to do it.


you are here
4
exception 
handling
345
b.setMessage(128, 1, 44, 100);
MidiEvent noteOff = new MidiEvent(b, 
3
);
Now that you know what’s in a Midi message, you can start experimenting. You 
can change the note that’s played, how long the note is held, add more notes, 
and even change the instrument.
Change a message
Change the note
Try a number between 0 and 127 in the note 
on and note off messages.
1
a.setMessage(144, 1, 
20
, 100);
Change the duration of the note
Change the note off event (not the 
message) so 
that it happens at an earlier or later beat.
2
1
0
2 3 4
5 6 7
8
127
fi rst.setMessage(
192
, 1, 
102
, 0);
Change the instrument
Add a new message, BEFORE the note-playing message, 
that sets the instrument in channel 1 to something other 
than the default piano. The change-instrument message 
is ‘192’, and the third argument represents the actual 
instrument (try a number between 0 and 127)
3
change-instrument message
in channel 1 (musician 1)
to instrument 102


346
 
chapter 11
This version still plays just a single note, but you get to use command-line argu-
ments to change the instrument and note. Experiment by passing in two int values 
from 0 to 127. The first int sets the instrument, the second int sets the note to play.
Version 2:
Using command-line args to experiment with sounds
import javax.sound.midi.*;
public class MiniMusicCmdLine { // this is the first one
public static void main(String[] args) {
MiniMusicCmdLine mini = new MiniMusicCmdLine();
if (args.length < 2) {
System.out.println(“Don’t forget the instrument and note args”);
} else {
int instrument = Integer.parseInt(args[0]);
int note = Integer.parseInt(args[1]);
mini.play(instrument, note);
}

// close main
public void play(int instrument, int note) {
try {
Sequencer player = MidiSystem.getSequencer();
player.open();
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
MidiEvent event = null;
ShortMessage first = new ShortMessage();
first.setMessage(192, 1, instrument, 0);
MidiEvent changeInstrument = new MidiEvent(first, 1); 
track.add(changeInstrument);
ShortMessage a = new ShortMessage();
a.setMessage(144, 1, note, 100);
MidiEvent noteOn = new MidiEvent(a, 1); 
track.add(noteOn);
ShortMessage b = new ShortMessage();
b.setMessage(128, 1, note, 100);
MidiEvent noteOff = new MidiEvent(b, 16); 
track.add(noteOff);
player.setSequence(seq); 
player.start();
} catch (Exception ex) {ex.printStackTrace();}

// close play

// close class

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   319   320   321   322   323   324   325   326   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish