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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100307.html;106,k100308.html;107,k100310.html;108,k100312.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100307.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100308.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100310.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100312.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">Kapitel 49 - Sound
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id049002"></a>
<h2>49.2 <a name="ixa103535">Gesampelter Sound</a></h2>
<hr>
<ul>
<li><a href="k100309.html#sectlevel2id049002">49.2 Gesampelter Sound</a>
<ul>
<li><a href="k100309.html#sectlevel3id049002001">49.2.1 Was ist Sampling?</a>
<li><a href="k100309.html#sectlevel3id049002002">49.2.2 Das Sampling-API</a>
<ul>
<li><a href="k100309.html#sectlevel4id049002002001">Zugriff auf Audio-Dateien</a>
<li><a href="k100309.html#sectlevel4id049002002002">Audio-Geräte beschaffen</a>
<li><a href="k100309.html#sectlevel4id049002002003">Die Steuerelemente eines Audio-Geräts</a>
<li><a href="k100309.html#sectlevel4id049002002004">Der Clip</a>
</ul>
<li><a href="k100309.html#sectlevel3id049002003">49.2.3 Abspielen einer Sample-Datei</a>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="sectlevel3id049002001"></a>
<h3>49.2.1 Was ist <a name="ixa103536">Sampling</a>? </h3>
<p>
Das Sound-API macht eine sehr grundlegende Unterscheidung zwischen
<i>gesampeltem Sound</i> und <i>Midi-Sound</i>. Beim Sampling, das
wir in diesem Abschnitt behandeln wollen, wird ein Audio-Signal in
viele kleine Stücke zerlegt, deren Amplitude in sehr kurzen Abständen
mit Hilfe eines Analog-Digital-Konverters gemessen wird:
<p>
<a name="imageid049001"></a>
<img src="images/Sample1.gif">
<p>
<p><i>
Abbildung 49.1: Samplen eines Audio-Signals</i></p>
<p>
Die Frequenz, mit der die Abtastung geschieht, bezeichnet man als
<i>Sampling Rate</i>, und sie sollte mindestens doppelt so hoch sein
wie die größte aufzuzeichnende Frequenz. Bei Audio-CDs
beträgt sie 44100 Hz, und die Auflösung des A/D-Konverters
beträgt 16 Bit. Speichert man die so entstehenden Amplitudenwerte
fortlaufend ab, so kann man bei Kenntnis der Sampling-Rate daraus
später das Originalsignal näherungsweise rekonstruieren.
Sind Sampling-Rate und Auflösung hoch genug, kann das menschliche
Ohr keinen Unterschied zwischen gesampeltem und Originalsignal feststellen:
<p>
<a name="imageid049002"></a>
<img src="images/Sample2.gif">
<p>
<p><i>
Abbildung 49.2: Ein gesampeltes Audio-Signal</i></p>
<!-- Section -->
<a name="sectlevel3id049002002"></a>
<h3>49.2.2 Das Sampling-API </h3>
<p>
Das Sound-API macht keine Annahmen über vorhandene Hardware oder
angeschlossene Geräte. Statt dessen stellt es Methoden zur Verfügung,
mit denen die vorhandene Hardware zur Laufzeit ermittelt und Objekte
zum Zugriff darauf beschafft werden können. Im Falle von gesampeltem
Sound dient dazu die Klasse <a name="ixa103537"><a href="index_a.html#ixb102658"><font color=#000080><tt>AudioSystem</tt></font></a></a>
aus dem Paket <a name="ixa103538"><a href="index_j.html#ixb102659"><font color=#000080><tt>javax.sound.sampled</tt></font></a></a>.
Sie besitzt eine Reihe von statischen Methoden, mit denen die grundlegenden
Hilfsmittel für den Umgang mit gesampeltem Sound beschafft werden
können:
<ul>
<li>Die Klasse <a name="ixa103539"><a href="index_a.html#ixb102660"><font color=#000080><tt>AudioFormat</tt></font></a></a>
beschreibt das Format von gesampeltem Sound. Sie enthält unter
anderem Informationen über das Kodierungsverfahren, die Anzahl
der Kanäle, die Sampling Rate oder die Auflösung der einzelnen
Samples.
<li>Die Klasse <a name="ixa103540"><a href="index_a.html#ixb102661"><font color=#000080><tt>AudioFileFormat</tt></font></a></a>
beschreibt das Format von Dateien, die gesampelten Sound enthalten.
Im wesentlichen wird dazu ein eingebettetes <a href="index_a.html#ixb102660"><font color=#000080><tt>AudioFormat</tt></font></a>-Objekt
verwendet, das mit einigen Zusatzinformationen versehen ist. Die wichtigsten
Dateiformate, die seit dem JDK 1.3 standardmäßig unterstützt
werden, sind <a name="ixa103541"><i>wav</i></a>, <a name="ixa103542"><i>aiff</i></a>
und <a name="ixa103543"><i>au</i></a>.
<li>Eine weitere wichtige Abstraktion ist die der <a name="ixa103544"><a href="index_l.html#ixb102662"><font color=#000080><tt>Line</tt></font></a></a>.
Sie repräsentiert die verschiedenen, miteinander verbundenen
Elemente der Audio-Pipeline, mit denen die Sound-Daten erzeugt, transportiert
oder modifiziert werden. Die abstrakten Eigenschaften einer <a href="index_l.html#ixb102662"><font color=#000080><tt>Line</tt></font></a>
sind, dass sie entweder geöffnet oder geschlossen ist und dass
sie eine Reihe von <a name="ixa103545"><a href="index_c.html#ixb102663"><font color=#000080><tt>Control</tt></font></a></a>-Objekten
haben kann, um etwa die Lautstärke, das Stereo-Panorama oder
den Hall zu verändern. Zudem sendet eine <a href="index_l.html#ixb102662"><font color=#000080><tt>Line</tt></font></a>
bei wichtigen Statusänderungen Events an registrierte Listener.
<li>Wichtige Subinterfaces von <a href="index_l.html#ixb102662"><font color=#000080><tt>Line</tt></font></a>
sind <a name="ixa103546"><a href="index_p.html#ixb102664"><font color=#000080><tt>Port</tt></font></a></a>
und <a name="ixa103547"><a href="index_m.html#ixb102665"><font color=#000080><tt>Mixer</tt></font></a></a>.
Ein <a href="index_p.html#ixb102664"><font color=#000080><tt>Port</tt></font></a>
ist ein <i>Endpunkt</i> der Audio-Pipeline, also etwa ein Mikrophon
oder CD-Laufwerk auf der Eingabeseite oder ein Kopfhörer- oder
Verstärkerausgang auf der Ausgabeseite. Ein <a href="index_m.html#ixb102665"><font color=#000080><tt>Mixer</tt></font></a>
ist ein Audio-Element mit mehreren Ein-/Ausgabeleitungen. Er dient
typischerweise dazu, verschiedene Eingangssignale getrennt aufzubereiten
und zu einem Summensignal zusammenzumischen. Ein <a href="index_m.html#ixb102665"><font color=#000080><tt>Mixer</tt></font></a>
kann auch Veränderungen an den Eingangssignalen vornehmen, etwa
einen Halleffekt hinzufügen oder mit Hilfe eines Equalizers den
Klang verändern.
<li>Mit <a name="ixa103548"><a href="index_d.html#ixb102666"><font color=#000080><tt>DataLine</tt></font></a></a>
kommt das dritte Subinterface von <a href="index_l.html#ixb102662"><font color=#000080><tt>Line</tt></font></a>
ins Spiel. Eine <a href="index_d.html#ixb102666"><font color=#000080><tt>DataLine</tt></font></a>
ist das Vaterinterface für alle Lines, die explizit mit Datenströmen
umgehen. Eine <a name="ixa103549"><a href="index_s.html#ixb102667"><font color=#000080><tt>SourceDataLine</tt></font></a></a>
versorgt einen <a href="index_m.html#ixb102665"><font color=#000080><tt>Mixer</tt></font></a>
mit Eingabedaten, und eine <a name="ixa103550"><a href="index_t.html#ixb102668"><font color=#000080><tt>TargetDataLine</tt></font></a></a>
empfängt Daten von ihm. Ein <a name="ixa103551"><a href="index_c.html#ixb102669"><font color=#000080><tt>Clip</tt></font></a></a>
ist eine spezielle Datenquelle, die vor der Weitergabe alle Daten
in einem Stück liest und fortan im Hauptspeicher hält. Ein
<a href="index_c.html#ixb102669"><font color=#000080><tt>Clip</tt></font></a>
kann dadurch wahlfrei auf die Daten zugreifen und beispielsweise nur
einen Teil des Signals wiedergeben oder Ausschnitte beliebig oft wiederholen.
Allerdings kommt er nur für relativ kleine Datenmengen in Betracht.
</ul>
<!-- Section -->
<a name="sectlevel4id049002002001"></a>
<h4>Zugriff auf Audio-Dateien </h4>
<p>
Die Klasse <a href="index_a.html#ixb102658"><font color=#000080><tt>AudioSystem</tt></font></a>
stellt einige Methoden zum Zugriff auf Dateien mit gesampeltem Sound
zur Verfügung:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public static AudioFileFormat getAudioFileFormat(File file)
throws UnsupportedAudioFileException, IOException
public static AudioFileFormat getAudioFileFormat(InputStream stream)
throws UnsupportedAudioFileException, IOException
public static AudioFileFormat getAudioFileFormat(URL url)
throws UnsupportedAudioFileException, IOException
public static AudioInputStream getAudioInputStream(File file)
throws UnsupportedAudioFileException, IOException
public static AudioInputStream getAudioInputStream(InputStream stream)
throws UnsupportedAudioFileException, IOException
public static AudioInputStream getAudioInputStream(URL url)
throws UnsupportedAudioFileException, IOException
public static boolean isConversionSupported(
AudioFormat targetFormat,
AudioFormat sourceFormat
)
public static AudioInputStream getAudioInputStream(
AudioFormat targetFormat,
AudioInputStream sourceStream
)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/sound/sampled/AudioSystem.html" onClick="this.href=getApiDoc('javax.sound.sampled.AudioSystem')"><font color="#660066" size=-1>javax.sound.sampled.AudioSystem</font></a></td>
</tr>
</table>
<p>
Mit <a name="ixa103552"><a href="index_g.html#ixb102670"><font color=#000080><tt>getAudioFileFormat</tt></font></a></a>
kann das <a href="index_a.html#ixb102661"><font color=#000080><tt>AudioFileFormat</tt></font></a>
einer Sound-Datei ermittelt werden. Die Eingabedatei kann entweder
als <a href="index_f.html#ixb100973"><font color=#000080><tt>File</tt></font></a>,
<a href="index_i.html#ixb100642"><font color=#000080><tt>InputStream</tt></font></a>
oder <a href="index_u.html#ixb102196"><font color=#000080><tt>URL</tt></font></a>
übergeben werden. Mit <a name="ixa103553"><a href="index_g.html#ixb102671"><font color=#000080><tt>getAudioInputStream</tt></font></a></a>
kann ein <a name="ixa103554"><a href="index_a.html#ixb102672"><font color=#000080><tt>AudioInputStream</tt></font></a></a>
beschafft werden, mit dem der Inhalt der Sound-Datei gelesen werden
kann. Auch hier darf der Parameter wahlweise vom Typ <a href="index_f.html#ixb100973"><font color=#000080><tt>File</tt></font></a>,
<a href="index_i.html#ixb100642"><font color=#000080><tt>InputStream</tt></font></a>
oder <a href="index_u.html#ixb102196"><font color=#000080><tt>URL</tt></font></a>
sein.
<p>
Die Klasse <a href="index_a.html#ixb102658"><font color=#000080><tt>AudioSystem</tt></font></a>
unterstützt sogar Konvertierungen zwischen verschiedenen Formaten.
Ob eine bestimmte Konvertierung verfügbar ist, kann mit <a name="ixa103555"><a href="index_i.html#ixb102673"><font color=#000080><tt>isConversionSupported</tt></font></a></a>
abgefragt werden. Wird <a href="index_g.html#ixb102671"><font color=#000080><tt>getAudioInputStream</tt></font></a>
mit einem Zielformat und einem anderen <a href="index_a.html#ixb102672"><font color=#000080><tt>AudioInputStream</tt></font></a>
als Parameter aufgerufen, führt die Methode die Konvertierung
durch und liefert einen <a href="index_a.html#ixb102672"><font color=#000080><tt>AudioInputStream</tt></font></a>,
der das gewünschte Zielformat hat.
<!-- Section -->
<a name="sectlevel4id049002002002"></a>
<h4>Audio-Geräte beschaffen </h4>
<p>
Die Klasse <a href="index_a.html#ixb102658"><font color=#000080><tt>AudioSystem</tt></font></a>
stellt auch Methoden zum Zugriff auf Audio-Geräte zur Verfügung:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public static Mixer.Info[] getMixerInfo()
public static Mixer getMixer(Mixer.Info info)
public static Line getLine(Line.Info info)
throws LineUnavailableException
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/sound/sampled/AudioSystem.html" onClick="this.href=getApiDoc('javax.sound.sampled.AudioSystem')"><font color="#660066" size=-1>javax.sound.sampled.AudioSystem</font></a></td>
</tr>
</table>
<p>
Um herauszufinden, welche <a href="index_m.html#ixb102665"><font color=#000080><tt>Mixer</tt></font></a>
verfügbar sind, muss zunächst <a name="ixa103556"><a href="index_g.html#ixb102674"><font color=#000080><tt>getMixerInfo</tt></font></a></a>
aufgerufen werden. Der Rückgabewert ist ein Array mit <a name="ixa103557"><a href="index_m.html#ixb102675"><font color=#000080><tt>Mixer.Info</tt></font></a></a>-Objekten,
das je vorhandenem <a href="index_m.html#ixb102665"><font color=#000080><tt>Mixer</tt></font></a>
ein Element enthält. Durch Übergabe eines <a href="index_m.html#ixb102675"><font color=#000080><tt>Mixer.Info</tt></font></a>-Objekts
an <a name="ixa103558"><a href="index_g.html#ixb102676"><font color=#000080><tt>getMixer</tt></font></a></a>
kann der zugehörige <a href="index_m.html#ixb102665"><font color=#000080><tt>Mixer</tt></font></a>
beschafft werden. Man kann allerdings auch ohne explizite Verwendung
eines Mixers gesampelten Sound ausgegeben, wenn mit <a name="ixa103559"><a href="index_g.html#ixb102677"><font color=#000080><tt>getLine</tt></font></a></a>
direkt ein geeignetes Audio-Gerät beschafft wird. Dazu muss ein
<a name="ixa103560"><a href="index_l.html#ixb102678"><font color=#000080><tt>Line.Info</tt></font></a></a>-Objekt
übergeben werden, das dessen Eigenschaften beschreibt. Wir werden
in <a href="k100309.html#playsamplefile">Listing 49.1</a> zeigen,
wie das <a href="index_l.html#ixb102678"><font color=#000080><tt>Line.Info</tt></font></a>-Objekt
konstruiert werden muss, um einen <a href="index_c.html#ixb102669"><font color=#000080><tt>Clip</tt></font></a>
zu erzeugen.
<!-- Section -->
<a name="sectlevel4id049002002003"></a>
<h4>Die Steuerelemente eines Audio-Geräts </h4>
<p>
Wir hatten eingangs erwähnt, dass eine <a href="index_l.html#ixb102662"><font color=#000080><tt>Line</tt></font></a>
eine Reihe von Steuerlementen zur Verfügung stellt, mit denen
Parameter wie Lautstärke, Stereo-Panorama oder Hall eingestellt
werden können. Auf diese kann mit folgenden Methoden zugegriffen
werden:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public Control[] getControls()
public boolean isControlSupported(Control.Type control)
public Control getControl(Control.Type control)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/sound/sampled/Line.html" onClick="this.href=getApiDoc('javax.sound.sampled.Line')"><font color="#660066" size=-1>javax.sound.sampled.Line</font></a></td>
</tr>
</table>
<p>
<a name="ixa103561"><a href="index_g.html#ixb102679"><font color=#000080><tt>getControls</tt></font></a></a>
liefert ein Array aller verfügbaren Steuerelemente, die durch
Instanzen der Klasse <a name="ixa103562"><a href="index_c.html#ixb102663"><font color=#000080><tt>Control</tt></font></a></a>
repräsentiert werden. Mit <a name="ixa103563"><a href="index_i.html#ixb102680"><font color=#000080><tt>isControlSupported</tt></font></a></a>
kann durch Übergabe eines <a name="ixa103564"><a href="index_c.html#ixb102681"><font color=#000080><tt>Control.Type</tt></font></a></a>-Objekts
festgestellt werden, ob ein bestimmter Typ vorhanden ist. Mit <a name="ixa103565"><a href="index_g.html#ixb102682"><font color=#000080><tt>getControl</tt></font></a></a>
kann dieser schließlich beschafft werden.
<p>
<a href="index_c.html#ixb102663"><font color=#000080><tt>Control</tt></font></a>
ist die Basisklasse einer ganzen Gruppe von Kontrollelementen, die
sich durch den Datentyp des zu verändernden Parameters unterscheiden.
Es gibt die Unterklassen <a name="ixa103566"><a href="index_b.html#ixb102683"><font color=#000080><tt>BooleanControl</tt></font></a></a>,
<a name="ixa103567"><a href="index_e.html#ixb102684"><font color=#000080><tt>EnumControl</tt></font></a></a>,
<a name="ixa103568"><a href="index_f.html#ixb102685"><font color=#000080><tt>FloatControl</tt></font></a></a>
und <a name="ixa103569"><a href="index_c.html#ixb102686"><font color=#000080><tt>CompoundControl</tt></font></a></a>.
Ein <a href="index_f.html#ixb102685"><font color=#000080><tt>FloatControl</tt></font></a>
beispielsweise dient zur Veränderung eines Fließkommawerts
(wie etwa der Lautstärke), während ein <a href="index_b.html#ixb102683"><font color=#000080><tt>BooleanControl</tt></font></a>
einen An-/Aus-Wert verändern kann (beispielsweise die Stummschaltung
eines Elements). Die wichtigsten Methoden der Klasse <a href="index_f.html#ixb102685"><font color=#000080><tt>FloatControl</tt></font></a>
sind:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public float getMaximum()
public float getMinimum()
public float getValue()
public void setValue(float newValue)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/sound/sampled/FloatControl.html" onClick="this.href=getApiDoc('javax.sound.sampled.FloatControl')"><font color="#660066" size=-1>javax.sound.sampled.FloatControl</font></a></td>
</tr>
</table>
<p>
Mit <a name="ixa103570"><a href="index_g.html#ixb101687"><font color=#000080><tt>getMinimum</tt></font></a></a>
und <a name="ixa103571"><a href="index_g.html#ixb101688"><font color=#000080><tt>getMaximum</tt></font></a></a>
können der kleinste und größte einstellbare Wert abgefragt
werden, mit <a name="ixa103572"><a href="index_g.html#ixb100752"><font color=#000080><tt>getValue</tt></font></a></a>
der aktuelle. Mit <a name="ixa103573"><a href="index_s.html#ixb101686"><font color=#000080><tt>setValue</tt></font></a></a>
kann der Wert des Controls verändert werden.
<p>
Die <font color="#000077"><tt>Type</tt></font>-Klassen der einzelnen
Steuerelemente besitzen jeweils eine Reihe von vordefinierten Konstanten,
die an <a href="index_g.html#ixb102682"><font color=#000080><tt>getControl</tt></font></a>
übergeben werden können. Für <a name="ixa103574"><a href="index_f.html#ixb102687"><font color=#000080><tt>FloatControl.TYPE</tt></font></a></a>
sind das beispielsweise <a name="ixa103575"><a href="index_m.html#ixb102688"><font color=#000080><tt>MASTER_GAIN</tt></font></a></a>
zur Einstellung der Lautstärke oder <a name="ixa103576"><a href="index_p.html#ixb102689"><font color=#000080><tt>PAN</tt></font></a></a>
zur Veränderung des Stereo-Panoramas.
<p>
Ein Steuerelement des Typs <a href="index_m.html#ixb102688"><font color=#000080><tt>MASTER_GAIN</tt></font></a>
bestimmt die Gesamtverstärkung, die das Audio-Element dem Ursprungssignal
hinzufügt. Ihr Wert wird in <a name="ixa103577"><i>Dezibel</i></a>
(<a name="ixa103578"><i>dB</i></a>) angegeben, wobei positive Werte
eine Verstärkung und negative eine Abschwächung des Eingangssignals
anzeigen. Das Dezibel ist eine logarithmische Maßeinheit, die
den Verstärkungsfaktor durch die Formel 10<sup>dB/20</sup> ausdrückt.
20 dB entsprechen also einer 10-fachen Verstärkung, -40 dB einer
100-fachen Abschwächung. 0 dB bedeutet, dass die Stärke
des Ausgangs- im Verhältnis zum Eingangssignal unverändert
bleibt.
<p>
Ein Steuerelement des Typs <a href="index_p.html#ixb102689"><font color=#000080><tt>PAN</tt></font></a>
bestimmt die Lage des Audio-Signals im Stereo-Panorama. Es kann Werte
von -1.0 (ganz links) bis +1.0 (ganz rechts) annehmen. Ein Wert von
0 legt das Signal in die Mitte.
<!-- Section -->
<a name="sectlevel4id049002002004"></a>
<h4>Der Clip </h4>
<p>
Ein <a href="index_c.html#ixb102669"><font color=#000080><tt>Clip</tt></font></a>
ist eine besondere Form einer <a href="index_d.html#ixb102666"><font color=#000080><tt>DataLine</tt></font></a>,
der alle Audiodaten komplett im Hauptspeicher hält. Wie jede
<a href="index_l.html#ixb102662"><font color=#000080><tt>Line</tt></font></a>
muss er vor der Verwendung durch Aufruf von <a name="ixa103579"><a href="index_o.html#ixb101348"><font color=#000080><tt>open</tt></font></a></a>
geöffnet werden und nach Gebrauch mit <a name="ixa103580"><a href="index_c.html#ixb100957"><font color=#000080><tt>close</tt></font></a></a>
geschlossen werden. Zum Abspielen stellt er folgende Methoden zur
Verfügung:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void start()
public void stop()
public boolean isRunning()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/sound/sampled/Clip.html" onClick="this.href=getApiDoc('javax.sound.sampled.Clip')"><font color="#660066" size=-1>javax.sound.sampled.Clip</font></a></td>
</tr>
</table>
<p>
Ein Aufruf von <a name="ixa103581"><a href="index_s.html#ixb101110"><font color=#000080><tt>start</tt></font></a></a>
startet das Abspielen des Clips, mit <a name="ixa103582"><a href="index_s.html#ixb101115"><font color=#000080><tt>stop</tt></font></a></a>
wird es angehalten. Mit <a name="ixa103583"><a href="index_i.html#ixb102692"><font color=#000080><tt>isRunning</tt></font></a></a>
kann überprüft werden, ob die Wiedergabe noch läuft
oder bereits beendet wurde.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Die Methode <a href="index_s.html#ixb101110"><font color=#000080><tt>start</tt></font></a>
ist nicht <i>modal</i>, d.h. sie wartet beim Aufruf nicht, bis der
Clip vollständig abgespielt ist. Sie initiiert lediglich den
Abspielvorgang und kehrt dann sofort zum Aufrufer zurück. Falls
dieser auf das Ende warten will, muss er entweder durch wiederholten
Aufruf von <a href="index_i.html#ixb102692"><font color=#000080><tt>isRunning</tt></font></a>
den Status des Clips abfragen oder sich bei diesem als Listener registrieren
und auf das <font color="#000077"><tt>STOP</tt></font>-Signal warten.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel3id049002003"></a>
<h3>49.2.3 Abspielen einer Sample-Datei </h3>
<p>
Nach den Erklärungen der vorangegangenen Abschnitte wollen wir
nun das Beispielprogramm zum Abspielen einer Sample-Datei vorstellen:
<a name="playsamplefile"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing4901.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.io.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> javax.sound.sampled.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing4901
<font color="#555555">007 </font>{
<font color="#555555">008 </font> <font color="#0000AA">private</font> <font color="#0000AA">static</font> <font color="#006699">void</font> playSampleFile(String name, <font color="#006699">float</font> pan, <font color="#006699">float</font> gain)
<font color="#555555">009 </font> <font color="#0000AA">throws</font> Exception
<font color="#555555">010 </font> {
<font color="#555555">011 </font> <font color="#00AA00">//AudioInputStream öffnen</font>
<font color="#555555">012 </font> AudioInputStream ais = AudioSystem.getAudioInputStream(
<font color="#555555">013 </font> <font color="#0000AA">new</font> File(name)
<font color="#555555">014 </font> );
<font color="#555555">015 </font> AudioFormat format = ais.getFormat();
<font color="#555555">016 </font> <font color="#00AA00">//ALAW/ULAW samples in PCM konvertieren</font>
<font color="#555555">017 </font> <font color="#0000AA">if</font> ((format.getEncoding() == AudioFormat.Encoding.ULAW) ||
<font color="#555555">018 </font> (format.getEncoding() == AudioFormat.Encoding.ALAW))
<font color="#555555">019 </font> {
<font color="#555555">020 </font> AudioFormat tmp = <font color="#0000AA">new</font> AudioFormat(
<font color="#555555">021 </font> AudioFormat.Encoding.PCM_SIGNED,
<font color="#555555">022 </font> format.getSampleRate(),
<font color="#555555">023 </font> format.getSampleSizeInBits() * 2,
<font color="#555555">024 </font> format.getChannels(),
<font color="#555555">025 </font> format.getFrameSize() * 2,
<font color="#555555">026 </font> format.getFrameRate(),
<font color="#555555">027 </font> <font color="#006699">true</font>
<font color="#555555">028 </font> );
<font color="#555555">029 </font> ais = AudioSystem.getAudioInputStream(tmp, ais);
<font color="#555555">030 </font> format = tmp;
<font color="#555555">031 </font> }
<font color="#555555">032 </font> <font color="#00AA00">//Clip erzeugen und öffnen</font>
<font color="#555555">033 </font> DataLine.Info info = <font color="#0000AA">new</font> DataLine.Info(
<font color="#555555">034 </font> Clip.<font color="#0000AA">class</font>,
<font color="#555555">035 </font> format,
<font color="#555555">036 </font> ((<font color="#006699">int</font>) ais.getFrameLength() * format.getFrameSize())
<font color="#555555">037 </font> );
<font color="#555555">038 </font> Clip clip = (Clip)AudioSystem.getLine(info); <a name="playsamplefile.a"></a>
<font color="#555555">039 </font> clip.open(ais);
<font color="#555555">040 </font> <font color="#00AA00">//PAN einstellen</font>
<font color="#555555">041 </font> FloatControl panControl = (FloatControl)clip.getControl(
<font color="#555555">042 </font> FloatControl.Type.PAN
<font color="#555555">043 </font> );
<font color="#555555">044 </font> panControl.setValue(pan);
<font color="#555555">045 </font> <font color="#00AA00">//MASTER_GAIN einstellen</font>
<font color="#555555">046 </font> FloatControl gainControl = (FloatControl)clip.getControl(
<font color="#555555">047 </font> FloatControl.Type.MASTER_GAIN
<font color="#555555">048 </font> );
<font color="#555555">049 </font> gainControl.setValue(gain);
<font color="#555555">050 </font> <font color="#00AA00">//Clip abspielen</font>
<font color="#555555">051 </font> clip.start(); <a name="playsamplefile.b"></a>
<font color="#555555">052 </font> <font color="#0000AA">while</font> (<font color="#006699">true</font>) {
<font color="#555555">053 </font> <font color="#0000AA">try</font> {
<font color="#555555">054 </font> Thread.sleep(100);
<font color="#555555">055 </font> } <font color="#0000AA">catch</font> (Exception e) {
<font color="#555555">056 </font> <font color="#00AA00">//nothing</font>
<font color="#555555">057 </font> }
<font color="#555555">058 </font> <font color="#0000AA">if</font> (!clip.isRunning()) {
<font color="#555555">059 </font> <font color="#0000AA">break</font>;
<font color="#555555">060 </font> }
<font color="#555555">061 </font> }
<font color="#555555">062 </font> clip.stop();
<font color="#555555">063 </font> clip.close();
<font color="#555555">064 </font> }
<font color="#555555">065 </font>
<font color="#555555">066 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">067 </font> {
<font color="#555555">068 </font> <font color="#0000AA">try</font> {
<font color="#555555">069 </font> playSampleFile(
<font color="#555555">070 </font> args[0],
<font color="#555555">071 </font> Float.parseFloat(args[1]),
<font color="#555555">072 </font> Float.parseFloat(args[2])
<font color="#555555">073 </font> );
<font color="#555555">074 </font> } <font color="#0000AA">catch</font> (Exception e) {
<font color="#555555">075 </font> e.printStackTrace();
<font color="#555555">076 </font> System.exit(1);
<font color="#555555">077 </font> }
<font color="#555555">078 </font> System.exit(0);
<font color="#555555">079 </font> }
<font color="#555555">080 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing4901.java"><font color="#000055" size=-1>Listing4901.java</font></a></td>
</tr>
</table>
<i>
Listing 49.1: Abspielen einer Sample-Datei</i></p>
<p>
Die Methode <font color="#000077"><tt>playSampleFile</tt></font> öffnet
zunächst einen <a href="index_a.html#ixb102672"><font color=#000080><tt>AudioInputStream</tt></font></a>
und bestimmt dessen <a href="index_a.html#ixb102660"><font color=#000080><tt>AudioFormat</tt></font></a>.
Ist dessen Kodierung <a name="ixa103584"><a href="index_u.html#ixb102693"><font color=#000080><tt>ULAW</tt></font></a></a>
oder <a name="ixa103585"><a href="index_a.html#ixb102694"><font color=#000080><tt>ALAW</tt></font></a></a>,
konvertiert es den Stream in das <a name="ixa103586"><a href="index_p.html#ixb102695"><font color=#000080><tt>PCM</tt></font></a></a>-Format,
denn die anderen beiden Formate werden standardmäßig nicht
unterstützt. Anschließend wird ein <a name="ixa103587"><a href="index_d.html#ixb102696"><font color=#000080><tt>DataLine.Info</tt></font></a></a>-Objekt
instanziert und in <a href="k100309.html#playsamplefile.a">Zeile 038</a>
ein <a href="index_c.html#ixb102669"><font color=#000080><tt>Clip</tt></font></a>
damit erzeugt. Dieser wird geöffnet und <a href="index_m.html#ixb102688"><font color=#000080><tt>MASTER_GAIN</tt></font></a>
und <a href="index_p.html#ixb102689"><font color=#000080><tt>PAN</tt></font></a>
werden auf die als Methodenparameter übergebenen Werte eingestellt.
In <a href="k100309.html#playsamplefile.b">Zeile 051</a> wird der
<a href="index_c.html#ixb102669"><font color=#000080><tt>Clip</tt></font></a>
gestartet und anschließend gewartet, bis er vollständig
abgespielt ist.
<p>
Das Programm wird mit dem Namen der Datei und den Werten für
<a href="index_p.html#ixb102689"><font color=#000080><tt>PAN</tt></font></a>
und <a href="index_m.html#ixb102688"><font color=#000080><tt>MASTER_GAIN</tt></font></a>
als Kommandozeilenparameter aufgerufen. Es kann zum Abspielen von
<i>wav</i>-, <i>aiff</i>- und <i>au</i>-Dateien verwendet werden.
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100307.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100308.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100310.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100312.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">© 1998, 2007 Guido Krüger & Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>
|