Source

OpenBSD-FAQ / faq13.tex

  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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
\section{How do I configure my audio device?}

The devices in OpenBSD that are related to audio are: \texttt{/dev/audio}, \texttt{/dev/sound}, \texttt{/dev/audioctl} and \texttt{/dev/mixer}. For a good overview of the audio driver layer, please read the \texttt{audio(4)} manual page.

All supported audio drivers are already included in the GENERIC kernel so there is no need for extra configuration or installation of drivers. To find out about options for your specific sound chip, you must find out which sound chip you have. Supported chips may be found on the hardware compatibility page for your platform. When you already have OpenBSD running, look for a sound driver in the output of the \texttt{dmesg(8)} command, and read its manual page to find more specific information like options and other details about the driver. An example of an audio chip in a dmesg output is:

\begin{alltt}auich0 at pci0 dev 31 function 5 "Intel 82801BA AC97" rev 0x04: irq 10, ICH2 AC97
ac97: codec id 0x41445360 (Analog Devices AD1885)
ac97: codec features headphone, Analog Devices Phat Stereo
audio0 at auich0\end{alltt}

OpenBSD base provides two tools for monitoring and configuring audio devices. \texttt{audioctl(1)} is used for the audio processing parameters, such as encoding, sample rate and number of channels, while \texttt{mixerctl(1)} is used for the mixing parameters, such as channel source, gain level and mute.

The following command uses \texttt{audioctl(1)} to display the default processing parameters of an audio device.

\begin{alltt}\$ \textbf{audioctl -f /dev/audio}
\ldots{}\end{alltt}

Note that \texttt{-f /dev/audio} was used explicitly. Opening \texttt{/dev/audio} causes the audio device to reset to the default parameters, which is what we wanted to see.

\texttt{audioctl(1)} is also quite useful for exploring the capabilities of an audio device. For example, to see if the device supports some common sample rates, you could simply try setting the playback rate:

\begin{alltt}\$ \textbf{audioctl play.rate=48000}
play.rate: -\textgreater{} 48000
\$ \textbf{audioctl play.rate=44100}
play.rate: -\textgreater{} 44100
\$ \textbf{audioctl play.rate=22050}
audioctl: set failed: Invalid argument
\$ \textbf{audioctl play.rate=8000}
audioctl: set failed: Invalid argument
$\end{alltt}

This device supports 48000 and 44100 Hz playback rates, but not 22050 or 8000. Note that if a sample rate is not supported, there is not always an error message, but the returned sample rate is not the one that was desired.

\begin{alltt}\$ \textbf{audioctl play.rate=48000}
play.rate: -\textgreater{} 48000
\$ \textbf{audioctl play.rate=44100}
play.rate: -\textgreater{} 48000
\$ \textbf{audioctl play.rate=22050}
play.rate: -\textgreater{} 48000
\$ \textbf{audioctl play.rate=8000}
play.rate: -\textgreater{} 48000
\$\end{alltt}

This device supports 48000 Hz playback only.

Audio hardware is usually capable of at least some minimal mixing. Running \texttt{mixerctl(1)} with no arguments will list the device's mixer controls and current settings.

\begin{alltt}\$ \textbf{mixerctl}
\ldots{}\end{alltt}

Some devices have only a handful of controls, some have a hundred or more. Note that not every option of every audio chip necessarily reaches the outside world. So there may be, for example, more outputs listed than are physically available on a sound card or motherboard.

There are a few controls that are common to many devices:

\begin{itemize}
\item \texttt{outputs.master} controls the playback output level
\item \texttt{inputs.dac} controls the level from the DAC (digital to analog converter), used when playing an audio file
\item \texttt{record.source} controls what inputs are mixed into the ADC (analog to digital converter), used when recording
\item \texttt{record.volume} or \texttt{record.record} controls the input level of the ADC
\end{itemize}

The controls of an audio device may be labeled differently. For instance, there might not be an \texttt{outputs.master} as above, but there is an \texttt{outputs.outputs} which does the same thing. Usually the controls have a meaningful label, but sometimes one must simply try different settings to see what effect each control has.

Some devices use what is known as EAPD, which stands for external amplifier power down. However, this is just another on/off switch. It is probably refered to as "power down" because it is often used for power saving, which means this type of control is more often found in laptops. Sometimes it is necessary to set controls with eapd or extamp in their name to on to get an output signal.

As a basic example of common mixerctl usage, to set the volume of the left and right channels to 200, you would issue

\begin{alltt}\$ \textbf{mixerctl outputs.master=200,200}
outputs.master: 255,255 -\textgreater{} 207,207\end{alltt}

Notice how the value becomes 207. The reason for this is that this audio device has an AC'97 codec, which uses only 5 bits for volume control, leading to only 32 possible values. Other hardware could have different resolution.

To unmute the master channel, you would do

\begin{alltt}\$ \textbf{mixerctl outputs.master.mute=off}
outputs.master.mute: on -\textgreater{} off\end{alltt}

To make the changes take affect on each reboot, edit \textit{/etc/mixerctl.conf}, for example:

\begin{alltt}\$ \textbf{cat /etc/mixerctl.conf}
outputs.master=200,200
outputs.master.mute=off
outputs.headphones=160,160
outputs.headphones.mute=off\end{alltt}

\section{Playing different kinds of audio}

\subsection*{Digitized audio}

\subsubsection*{Lossless audio formats (AU, PCM, WAV, FLAC, TTA)}

Some of the lossless audio formats may be played without the need for third party software, provided they contain the uncompressed digital samples in chunks of bytes. These formats include Sun audio (AU), raw PCM files (without headers), and RIFF WAV.

OpenBSD comes with \texttt{aucat(1)}, a program for recording and playing uncompressed audio. The following example will play a WAV file.

\begin{alltt}\$ \textbf{aucat -i filename.wav}\end{alltt}

\texttt{aucat(1)} supports both headerless and WAV audio files with the -i option. aucat also plays Sun audio files where the audio data is encoded as 8 kHz monaural mulaw, which is the most common encoding for this type of audio file.

It is also possible to play uncompressed audio data by passing it directly to the audio device. To do this, you need to know its main parameters: encoding type, number of channels, sample rate, bits per sample. If you don't know this, you might find out with the \texttt{file(1)} utility:

\begin{alltt}\$ \textbf{file music.au}
music.au:  Sun/NeXT audio data: 16-bit linear PCM, stereo, 44100 Hz
\$ \textbf{file music.wav}
music.wav: Microsoft RIFF, WAVE audio data, 16 bit, stereo 44100 Hz\end{alltt}

The only remaining things to know about these example files is that they use little-endian byte ordering and signed linear quantization. You could figure this out by reading the header with \textbf{hexdump(1)}. If you are using a headerless (raw) file, there is no way to know the parameters beforehand. Set the following parameters accordingly using \textbf{audioctl(1)}.

\begin{alltt}play.encoding=slinear\_le
play.rate=44100
play.channels=2
play.precision=16\end{alltt}

Next, pass the audio file to the sound device:

\begin{alltt}\$ \textbf{cat music.au \textgreater{} /dev/sound}\end{alltt}

If you applied the correct settings, you should be hearing what you expected.

Note: Always use \texttt{/dev/sound}, not \texttt{/dev/audio}, if you want the settings you applied with audioctl to stay in place.

There are, of course, other utilities you can use to play these files. Such as XMMS which is available in packages and ports and can play numerous other audio formats.

Apart from the above, there are audio formats which use lossless data compression. Examples are the Free Lossless Audio Codec (FLAC) and TTA. The FLAC implementation has been ported to OpenBSD and may be found under \texttt{audio/flac} in packages and ports.

\subsubsection*{Audio formats using lossy compression (Ogg Vorbis, MP3, WMA, AAC)}

Lossy compression methods are often used for audio or other media files. The idea is that an amount of data is thrown away during compression, but in such a way that the compressed result is still very usable and has a good enough quality to be played. The advantage is that these techniques enable much higher compression ratios, resulting in reduced disk space and bandwidth requirements.

A good example is the free, open and unpatented Ogg Vorbis format. To play Ogg Vorbis files, you can use the ogg123 utility, which is bundled in the \texttt{audio/vorbis-tools} package. For example:

\begin{alltt}\$ \texttt{ogg123 music.ogg}

Audio Device:   Sun audio driver output

Playing: music.ogg
Ogg Vorbis stream: 2 channel, 44100 Hz
Time: 00:02.95 \[02:21.45\] of 02:24.40  (133.1 kbps)  Output Buffer  87.5\%\end{alltt}

Of course, Ogg Vorbis plugins exist for many other audio software.

Another example is the very popular MPEG-1 Audio Layer 3 (MP3) encoding, which has, however, its share of licensing and patent issues. Many tools can play MP3 files, just have a look through the \texttt{audio} section of the packages and ports system and pick one you like.

How about the proprietary Windows Media Audio (WMA) format? Files of this type can be played using \texttt{x11/mplayer} which uses the FFmpeg framework.

A good starting point to learn more about different audio file formats is this Wikipedia article: Audio file formats.

\subsection*{Synthesized sound}

\subsubsection*{MIDI}

The Musical Instrument Digital Interface (MIDI) protocol, is handled by MIDI devices. If you don't have a MIDI synthesizer, but you wish to play a standard MIDI file (SMF), you can use software to render MIDI data, generating audio files. By default, the \texttt{audio/timidity} port renders MIDI files and play them on the audio device:

\begin{alltt}\$ \texttt{timidity file.mid}\end{alltt}

\subsection*{MOD}

A Soundtracker module is a binary format that mixes audio samples with sequencing orders, making it possible to play rather long pieces of digital music with reasonably good quality.

The easiest way to play your favorite MOD files on OpenBSD is probably to use the XMMS software, available through packages and ports. You should install the \texttt{-mikmod} subpackage for XMMS to let it use the MikMod sound library, which supports the MOD, S3M, IT and XM module formats.

You will also find a number of so-called "trackers" in the audio section of the packages and ports collection, e.g. tracker, soundtracker. With these trackers you can not only play but also generate your own modules. Note, however, that not every tracker format is supported by the tools in the ports tree. You are always welcome to submit a port of your favorite tracker software.

\section{How can I play audio CDs in OpenBSD?}

It is possible to play audio CDs by either having the CD drive play the disc and send analog audio to the sound card, or by reading the audio data and sending the digital samples to the sound card over the PCI bus.

To play an audio CD using the analog output of your CD-ROM drive, you can

\begin{itemize}
\item Use the headphones output, usually at the front side of the drive.
\item Connect the audio output at the back side of the drive to your audio card. Yes, this is a supplementary cable next to the data (SATA/IDE/SCSI) and power cables.
\end{itemize}

A nice command line utility called \texttt{cdio(1)}, has been included in the base system. Called without parameters, it will enter interactive mode. If you want to play the CD right away, just enter

\begin{alltt}\$ \textbf{cdio play}\end{alltt}

This will play from the first CD-ROM drive, \texttt{cd0}, by default. Note that the user running cdio should have permissions to read the CD-ROM device (e.g. \texttt{/dev/rcd0c}). As this device is only readable by root or the operator group by default, for convenience you may want to add the user to the operator group by adjusting this group's line in \texttt{/etc/group}. Alternatively, you can modify the file permissions of the device as needed.

Note that you may need to unmute the CD input of the mixer. Just like the outputs, the actual name of this input may vary between systems, but you will be using a command like:

\begin{alltt}\$ \textbf{mixerctl inputs.cd.mute=off}\end{alltt}

It is also possible that there is no analog audio connection between your CD drive and audio device. In this case you could use \texttt{cdio}'s \texttt{cdplay} command to send the CD audio data to the sound card through the PCI bus.

\begin{alltt}\$ \textbf{cdio cdplay}\end{alltt}

If you prefer a beautiful GUI, there are plenty of X11-based CD players in the packages and ports collection. Just have a look in the \texttt{audio} section.

\section{Can I use OpenBSD to record audio samples?}

Yes. Most devices support recording. \texttt{aucat(1)} comes with OpenBSD and can be used for recording.

\begin{alltt}\$ \textbf{aucat -o file.wav}\end{alltt}

The above command will start the recording of a file in WAV format. Press \[CTRL\]-C to finish the recording. The file will contain signed 16-bit stereo samples, sampled at 44.1 kHz. Other sample formats, sample rates and number of channels can be recorded. See the manual for more details.

Use aucat to play the file back:

\begin{alltt}\$ \textbf{aucat -i file.wav}\end{alltt}

If recording seemed to work, but playback of the recording was silent or not what was expected, the mixer probably needs some configuration. Make sure that you select the right device to record from and that the source is unmuted. You can set the necessary parameters using \texttt{mixerctl(1)}. For example:

\begin{alltt}inputs.mic.mute=off
inputs.mic.preamp=on
inputs.mic.source=mic0
record.source=mic
record.volume=255,255
record.volume.mute=off
record.mic=255
record.mic.mute=off\end{alltt}

These are settings for recording from a microphone. Pre-amplifying has been enabled, otherwise the recorded sound can be pretty silent on some systems. However, pre-amplifying can also be quite loud on other systems.

\section{How do I setup an audio server?}

\subsection*{Do I need an audio server?}

The \texttt{aucat(1)} utility can be used as an audio server, which acts as a layer between the audio(4) driver and audio applications. It aims to:

\begin{itemize}
\item Overcome incompatibilities between hardware and applications. For instance the application may not support the encodings or sample rates of the hardware; if so, the server can do the necessary conversions on the fly.
\item Allow multiple applications to use the hardware concurrently. For instance, to use a background music player while running an application playing sounds, or to simultaneously use the front speakers for music and the headphone socket for telephony is another example.
\end{itemize}

If applications you use are compatible with your hardware and you don't plan to run multiple applications concurently, then you don't need an audio server.

\subsection*{How do I setup \texttt{aucat(1)}?}

There's no configuration file and, in most cases, no tweaking is needed. Typing:

\begin{alltt}\$ \textbf{aucat -l}\end{alltt}

will start the server on the default audio device (the one the \texttt{/dev/audio} symlink points to) running at 44.1kHz and using two channels (stereo). This means that applications using stereo at 44.1kHz will run optimally, i.e. without triggering conversion code. If the device doesn't support those parameters, \texttt{aucat(1)} will automatically pick another set of parameters.

If you start aucat as root, it will increase its priority, to decrease the probability of buffer underruns or overruns. It can be started at system boot by appending:

\begin{alltt}\textbf{aucat\_flags=""}\end{alltt}

to \texttt{/etc/rc.conf.local}.

\subsection*{What latency do I need?}

The latency is the time between when a program takes the decision to play a sample and when the user hears the sample. Since audio data is always buffered, this delay is proportional to the audio buffer size. The following values are recommended:

\begin{itemize}
\item Real-time synthesizers: 5ms. This is the time it takes between hitting a key on your MIDI keyboard and actually hearing the note. Roughly, 5ms corresponds to the time it takes for the sound to propagate 1.75m.
\item Games: 50ms. This is the time between when you see an event and you hear the corresponding sound.
\item Movie players and alike: 500ms and more. Such applications ``know'' the sound to play in advance, and send audio data in such a way that it is played simultaneously with the corresponding picture.
\end{itemize}

The smaller audio buffers are (to achieve low latency), the larger the probability to overrun/underrun is. Buffer overruns/underruns result in ``stuttering'' of the sound.

In server mode, \texttt{aucat(1)} imposes a minimum latency on all audio applications, and the default latency is around 250ms. If you plan to use applications that require a lower latency, use the ``\texttt{-b}'' option to select the desired latency (expressed in number of frames). For instance, at 44100 samples/second, 50ms latency corresponds to:

44100 samples/second x 0.050 seconds = 2205 samples

then run \texttt{aucat(1)} as follows:

\begin{alltt}\$ \textbf{aucat -b 2205 -l}\end{alltt}

\subsection*{Does low latency improve audio-video synchronization?}

Synchronizing audio to video doesn't require low latency. Synchronization problems are often caused by the software itself (poor implementation, bugs, \ldots{}). Forcing the application to use smaller buffers (by starting aucat(1) in low latency mode) may hide the actual problem in some cases and give the feeling that the software works better, but obviously the right thing to do is to start searching for the corresponding bug.

\section{What can I do if I have audio problems?}

If you do not hear anything when playing audio, it's possible there is a mixer control turned to low or simply muted. See section 13.1 - How do I configure my audio device for configuring the mixer. Please unmute \textbf{all} inputs and outputs before reporting a problem.

If sound is distorted, it could be that your sound card only supports a single or limited set of sample rates or encodings. See section 13.1 - How do I configure my audio device for examples of determining what parameters your audio device supports.

If your device only supports unusual encodings or only one or a few sample rates and applications you use do not perform the necessary format conversions, consider using \texttt{aucat(1)} as audio server. See section 13.5 - How do I setup an audio server?

If you are still experiencing trouble, here are some things to consider:

\begin{itemize}
\item Some old ISA cards have particular quirks:
\subitem Some need to be configured with a different I/O address and IRQ value to avoid conflicts with other hardware. You can easily try different combinations using the User Kernel Configuration (UKC).
\subitem It is possible that a less than optimal driver attaches to the sound device, and that you can get better results using another driver. This is not the easiest thing to spot, but take a closer look at your dmesg(8) output, and find the lines where audio drivers attach. If you see more than one sound driver attaching (or trying to), test them one at a time by disabling some and leaving one enabled using the User Kernel Configuration (UKC).
\item Find information about your sound device. Use the documentation, or use an internet search engine to find its specifications. For \texttt{ac97(4)} and \texttt{azalia(4)} devices, look for documentation for both the controller and the codec. They may actually help you find the source of the problem.
\end{itemize}


If you believe your device should be working, but for whatever reason isn't, then it's time for a little debugging. The following steps can determine if data is being processed by the DAC.

\begin{alltt}\$ \textbf{cat \textgreater{} /dev/audio \textless{} /dev/zero \&}
\[1\] 9926
\$ \textbf{audioctl play.\{seek,samples,errors\}}
play.seek=48000
play.samples=3312000
play.errors=0
\$ \textbf{audioctl play.\{seek,samples,errors\}}
play.seek=57600
play.samples=7065600
play.errors=0
\$ \textbf{audioctl play.\{seek,samples,errors\}}
play.seek=48000
play.samples=9379200
play.errors=0
\$ \textbf{kill \%1}
\$ \textbf{fg \%1}
cat > /dev/audio \textless{} /dev/zero
Terminated\end{alltt}

Here we see that the processed data count \texttt{play.samples} increases each time we check, so data is flowing. We also see that the device is keeping enough data buffered \texttt{play.seek} that the device has not underrun any samples \texttt{play.errors}. That's good too.

Note that even if you had speakers plugged in when running the above test, you should not have heard anything. The test sends zeros to the device, which is silence for all currently supported default encodings.

Since we know the device can process data, it's a good idea to check the mixer settings again. Make sure all outputs and all inputs are unmuted and are at a reasonable level.

If at this point you are still having problems, it's probably time to file a bug report. Besides the normal bug report information such as a full dmesg and description of the problem, please also include the default output of \texttt{mixerctl -v} and the output of the above test for DAC processing.

\section{How do I use my MIDI instruments?}

The Musical Instrument Digital Interface (MIDI) protocol provides a standardized and efficient means to represent musical performance information as electronic data. A MIDI data contain only instructions needed by a synthesizer to play the sounds, rather than the sounds. More information: Tutorial on MIDI and Music Synthesis.

To play MIDI data, a synthesizer connected to a MIDI port of the machine is required. Similarly, to record a MIDI data a MIDI instrument is required (eg. a MIDI keyboard). Certain sound cards contain embedded MIDI synthesizers that are attached as MIDI ports. Advanced MIDI instruments may contain multiple subparts (synthesizers, keyboards, control surfaces, etc...), they appear as multiple MIDI ports on OpenBSD.

When you already have OpenBSD running, look for MIDI ports in the output of the dmesg(8) command. An example of MIDI ports in a dmesg output is:

\begin{alltt}umidi0 at uhub2 port 2 configuration 1 interface 0 "Roland Roland XV-2020" rev 1.10/1.00 addr 2
midi0 at umidi0: \textless{}USB MIDI I/F\textgreater{}
umidi1 at uhub1 port 2 configuration 1 interface 1 "Evolution Electronics Ltd. USB Keystation 61es" rev 1.00/1.25 addr 3
midi1 at umidi1: \textless{}USB MIDI I/F\textgreater{}
\end{alltt}

It shows three MIDI ports, corresponding to:

\begin{itemize}
\item \texttt{/dev/rmidi0} - synthesizer connected by USB
\item \texttt{/dev/rmidi1} - a MIDI master keyboard
\end{itemize}

These devices are known by \texttt{sndio(7)} as \texttt{rmidi:0} and \texttt{rmidi:1}. To test your MIDI keyboard, you can use the \texttt{hexdump(1)} utility to display MIDI data you're playing on it:

\begin{alltt}\$ \textbf{midicat -q rmidi:1 -o - | hexdump -e '1/1 "\%02x\textbackslash{}n"'}
90
3c
71
\ldots{}\end{alltt}

The output of the keyboard can be connected to the input of the synthesizer, as follows:

\begin{alltt}\$ \textbf{midicat -q rmidi:0 -q rmidi:1}\end{alltt}

Now you can hear on the synthesizer what you're playing on the MIDI keyboard. Refer to the \texttt{midicat(1)} manual page for further information.

The main utility to play standard MIDI files is \texttt{midiplay(1)}. Playing a standard MIDI file, in this example through the synthesizer, is as easy as:

\begin{alltt}\$ \textbf{midiplay -f rmidi:0 file.mid}\end{alltt}

To record MIDI files, you can use the \texttt{smfrec} utility bundled in the \texttt{audio/midish} port, for instance:

\begin{alltt}\$ \textbf{smfrec -d rmidi:0 -i rmidi:1 file.mid}\end{alltt}

will record what is played on the keyboard (\texttt{rmidi:1}) while sending it in real-time on the synthesizer (\texttt{rmidi:0}) so you can hear what you're playing. More complicated operations like editing, routing, mixing and transforming MIDI data, can be achieved using the rmidish utility bundled in the \texttt{audio/midish} port.

\section{Tell me about Ogg Vorbis and MP3 encoding?}

These formats were already mentioned in Playing different kinds of audio. In this section we will give a brief introduction about encoding such files. If you are interested in learning how these audio compression codecs work, further reading may be found through these Wikipedia articles about Vorbis and MP3.

\subsection*{Ogg Vorbis}

Encoding raw, WAV or AIFF format audio to Ogg Vorbis may be done with the \textbf{oggenc} utility, contained in the \texttt{audio/vorbis-tools} package, which is available through OpenBSD's packages and ports system.

Say you have a number of WAV files ready to encode, for example your favorite album you just extracted from its CD. To encode all these files using an approximate bit rate of 192 kbps, you could issue a command like

\begin{alltt}\$ \textbf{oggenc *.wav -b 192}\end{alltt}

When finished, this will give you a set of .ogg files in the current directory. More extensive examples, as well as encoding options, can be found in the oggenc manual page.

\subsection*{MPEG-1 Audio Layer 3 (MP3)}

If for some reason you want to use the MP3 format, you can use "Lame ain't an MP3 encoder" (LAME), an educational tool to be used for learning about MP3 encoding. Lame is included in the OpenBSD ports tree. Note that due to MP3 patents, you will not find this package on the official CD sets.

Below is a simple example of encoding a WAV file with a bit rate of 192 kbps:

\begin{alltt}\$ \textbf{lame -b 192 track01.wav track01.mp3}\end{alltt}

For all options and details, please consult the manual page that comes with lame.

\section{How can I playback video DVDs in OpenBSD?}

OpenBSD supports DVD media through the ISO 9660 filesystem which is also used on CD-ROMs, and, since OpenBSD 3.8, also through the newer Universal Disk Format (UDF) filesystem which is found on some DVDs. However, almost all DVD-Video and DVD-ROM discs use the UDF bridge format, which is a combination of the DVD MicroUDF (subset of UDF 1.02) and ISO 9660 filesystems. It is used for backward compatibility reasons.

As most computers with DVD-ROM drives use software decoding, it is recommended to have at least a 350-MHz Pentium II or equivalent CPU to have good quality playback.

Some popular media players, supporting DVD playback, have been ported to OpenBSD. Examples are ogle, mplayer, xine, and kaffeine. Please read the installation instructions that come with these packages, because these tools may need further setup. With these utilities, it is possible to playback the DVD by directly accessing the raw device. Of course, it is also possible to mount a DVD first using \texttt{mount\_cd9660(8)}, and play the files on this or any other mounted filesystem.

\textbf{Notes:}
\begin{itemize}
\item Nearly all DVDs you buy are encrypted using the Content Scrambling System (CSS). To be able to playback these encrypted DVDs, you can use the \texttt{libdvd} library, also available through packages and ports.
\item Be aware that a region code may be present on your DVD disk(s). This should not be much of a problem when playing DVDs on a computer.
\end{itemize}

\section{How do I burn CDs and DVDs?}

\subsection{Introduction and basic setup}

You should first make sure your CD/DVD writer has been recognized and configured by the kernel. Most SCSI devices are supported. SATA, IDE/ATAPI and USB devices are supported through SCSI emulation. You will quickly find your device in a \texttt{dmesg(8)} output. Just look for lines beginning with "cd", for example

\begin{alltt}cd0 at scsibus0 targ 0 lun 0: \textless{}TOSHIBA, CD-ROM XM-5702B, 2826\textgreater{} SCSI0 5/cdrom removable
cd1 at scsibus1 targ 4 lun 0: \textless{}PLEXTOR, CD-R PX-R412C, 1.04\textgreater{} SCSI2 5/cdrom removable\end{alltt}

\subsubsection*{But \texttt{cdrecord -scanbus} does not work!}

Yes. OpenBSD uses a different device namespace than the OS for which the cdrecord utility was written. All configured devices should be in the dmesg output, as mentioned above. The information you need is right there.

\subsubsection*{Error: \texttt{mount\_cd9660: /dev/cd2c on /mnt/cdrom: No such file or directory}}

By default, the OpenBSD installer creates only two cd device nodes, cd0 and cd1. To start using your cd2 device, you must create the necessary device nodes for it. The recommended way to do that is using the \texttt{MAKEDEV(8)} (select your specific platform) script:

\begin{alltt}\# \textbf{cd /dev}
\# \textbf{./MAKEDEV cd2}\end{alltt}

In what follows, we will mostly be accessing the CD/DVD writer through the \textit{raw} character device, \textbf{not} the \textit{block} device.

\subsubsection*{Checking CD/DVD writer operation}

It is recommended to check whether your CD/DVD writer is working correctly. In this example, I'll be using this USB 2.0 DVD writer:

\begin{alltt}cd2 at scsibus2 targ 1 lun 0: \textless{}LITE-ON, DVDRW LDW-851S, GS0C\textgreater{} SCSI0 5/cdrom removable\end{alltt}


Try to use it by mounting an existing CD/DVD in it. If desired, you could also check the transfer rate you are getting when copying files to your hard disk. The \texttt{time(1)} command will be your willing assistant.

If something goes wrong here and you are getting errors during this phase, it is wise to fix the problem and not to start writing a CD/DVD yet.

\subsubsection*{I want to write a CD here! Can we get on with it?}

Before proceeding, it is a good idea to keep a few words of advice in mind:

\begin{itemize}
\item Do not run any disk-intensive jobs while writing a CD/DVD. Doing this will reduce the throughput to your CD/DVD writer. If the throughput drops below what the writer is expecting for too long, its buffer will run empty. This phenomenon is also known as a "buffer underrun".
\item Prevent shocks during writing as this may cause the laser beam to drift from its track, which may lead to errors on the disc.
\item Not every DVD writer supports every DVD format, see below.
\end{itemize}

\subsection{Writing CDs}

\subsubsection*{Creating data CD-ROMs}

First, you will want to create an ISO 9660 filesystem to put on a CD-ROM. To do this you can use the \texttt{mkhybrid(8)} utility in the base system, or the mkisofs utility which comes with the cdrtools package and which does a better job with large file trees. In the examples below, we will use \texttt{mkhybrid}, although mkisofs usage is very similar.

As an example usage, let's say I wanted to store the OpenBSD kernel sources in an ISO 9660 image:

\begin{alltt}\$ \textbf{mkhybrid -R -o sys.iso /usr/src/sys}

Using ALTQ_RMC.000;1 for  /usr/src/sys/altq/altq_rmclass_debug.h (altq_rmclass.h)
\ldots{}
Using IEEE8021.00H;1 for  /usr/src/sys/net80211/ieee80211_amrr.c (ieee80211.c)
 10.89\% done, estimate finish Sat Nov  3 08:01:23 2007
 21.78\% done, estimate finish Sat Nov  3 08:01:28 2007
\ldots{}
 87.12\% done, estimate finish Sat Nov  3 08:01:31 2007
 98.01\% done, estimate finish Sat Nov  3 08:01:32 2007
Total translation table size: 0
Total rockridge attributes bytes: 896209
Total directory bytes: 2586624
Path table size(bytes): 11886
Max brk space used 0
45919 extents written (89 Mb)\end{alltt}

The -R option tells \texttt{mkhybrid} to create Rock Ridge extensions in the ISO 9660 image. The Rock Ridge Interchange Protocol was created to support POSIX filesystem semantics in ISO 9660 filesystems, such as longer file names, ownerships, permissions, file links, soft links, device nodes, deep file hierarchies (more than 8 levels of subdirectories), etc.

If you want the long file names on your CD-ROM to be readable on Windows or DOS systems, you should add the \texttt{-J} flag to include Joliet extensions in the ISO 9660 image as well.

After creating the filesystem, you can verify it by mounting the ISO 9660 image. If all is well, you are now ready to burn the CD-R(W). The easiest way to do this is to use the \texttt{cdio(1)} utility.

If you are using multi-write media such as CD-RW, you will need to blank the media before burning it.

\begin{alltt}\# \textbf{cdio -f cd1c blank}\end{alltt}

You are now ready to burn the image created in the above example to a blank CD-R(W). You could use a command similar to:

\begin{alltt}\# \textbf{cdio -f cd1c tao sys.iso}\end{alltt}

With the options specified above, we're asking cdio to use the second CD-ROM device as the CD writer.

To verify whether the CD-ROM has been written correctly, you can mount it and check whether everything is there. To mount the filesystem, you should use the \textbf{block} device for the CD-ROM drive, which in this case is still the CD writer:

\begin{alltt}\# \textbf{mount /dev/cd1c /mnt/cdrom}\end{alltt}

\subsubsection{Creating audio CDs}

To burn audio CDs, you can again use \texttt{cdio(1)} with the \texttt{tao -a} option.

As an example, I'll be making a backup copy of one of my music CDs. This involves two steps:

\begin{enumerate}
\item Fetch the audio tracks from the original CD. For example:

\begin{alltt}\# \textbf{cdio -f cd1c cdrip}\end{alltt}

This command will extract a series of WAV files from your second CD-ROM drive to your disk.

\item Write the audio tracks to a blank CD. For example:

\begin{alltt}\# \textbf{cdio -f cd1c tao -a *.wav}\end{alltt}
\end{enumerate}

\subsection{Writing DVDs}

There are a few important things about DVD you should know about before proceeding to write your own DVDs.

\textbf{Important notes:}

\begin{itemize}
\item If you really want to know all about DVD, I suggest you read the very extensive DVD FAQ document to start with.
\item This section has seen only very limited testing, and we certainly have not tried every possible media and writer combination. Nevertheless, we have had, or have heard of, positive experiences with all of the DVD formats mentioned below. You are welcome to let us know about your successes or failures.
\end{itemize}

\subsubsection*{Different DVD formats}

There are a number of different DVD formats. Commonly used are the DVD-R, DVD-RW, DVD+R, and DVD+RW formats (R means writable once, RW means it can be rewritten a few thousand times). They are pretty much competing standards.

A pretty different format is DVD-RAM, which was mainly developed as a data drive and has advanced packet writing functions, allowing it to be used like a kind of optical hard disk. DVD-RAM is not recommended for video usage as video gets written to the discs in a format not compatible with normal DVD players.

The important thing is you use media which suit your DVD writer. If you expect compatibility with other DVD players, watch your step and be sure to read this section of the DVD FAQ.

\subsubsection*{DVD writing speed}

It may be useful to point out that DVD speed indications differ from CD-ROM speed indications. The following table gives an overview:

\begin{center}
\begin{tabular}{| l | c | r | }
\hline
DVD read/write speed & Transfer rate (MB/s) & Equivalent CD-R(W) read/write speed \\ \hline
1x & 1.32 & 9x \\ \hline
2x & 2.64 & 18x \\ \hline
4x & 5.28 & 36x \\ \hline
8c & 10.57 & 72x \\ \hline
\end{tabular}
\end{center}

As can been seen from the table, the transfer rates are relatively high, and you should check whether your bus (SCSI, IDE/ATAPI, SATA, USB) is performant enough to handle this throughput. Especially the older USB 1.0 and 1.1 interfaces work at slower transfer rates, with maximal rates of 1.5 Mbit/s and 12 Mbit/s, respectively. That means USB 1.0 has a maximal throughput of 178.8 kByte/s and USB 1.1 has a maximal throughput of 1.43 MB/s. USB 2.0 is much faster: 480 Mbit/s or 57.2 MB/s. In general, the speed of SCSI, SATA, and IDE/ATAPI buses should be just fine.

\subsubsection*{Writing the DVD}

Basically, the process is very similar to writing CD-R(W)s. The software used, however, is different. At the moment, the best option is \texttt{growisofs} from the \texttt{sysutils/dvd+rw-tools} package. This utility writes an ISO 9660 image to the DVD medium. All recordable DVD formats are supported by the dvd+rw-tools.

In case you want to find out more info about the media in your DVD writer (for example if you lost the info label in the jewel case or are just disorganized like me), you can use the \texttt{dvd+rw-mediainfo} utility. There are two options to write the DVD:

\begin{itemize}
\item Pre-master an ISO 9660 from your data, storing the image on your hard disk; then write this image to the DVD.
\item Write an ISO 9660 from your data immediately to the DVD.
\end{itemize}

I created a pre-mastered ISO 9660 image from the OpenBSD CVS modules (\texttt{src}, \texttt{xenocara}, \texttt{ports} and \texttt{www}) contained in the \texttt{/cvs} directory on my disk. I used the following command, which looks very similar to the one I used to create the CD-ROM image above.

\begin{alltt}\$ \textbf{mkhybrid -r -o cvs.iso /cvs}\end{alltt}

If desired, check the ISO 9660 filesystem by mounting the image. To write this image (about 2 GB) to an empty DVD disc, one could use:

\begin{alltt}\# \textbf{growisofs -dvd-compat -Z /dev/rcd2c=cvs.iso}
Executing 'builtin\_dd if=cvs.iso of=/dev/rcd2c obs=32k seek=0'
/dev/rcd2c: pre-formatting blank DVD+RW\ldots{}
/dev/rcd2c: "Current Write Speed" is 4.1x1385KBps.
  23822336/1545832448 ( 1.5\%) @3.9x, remaining 5:19
  42172416/1545832448 ( 2.7\%) @3.9x, remaining 5:20
  60522496/1545832448 ( 3.9\%) @3.9x, remaining 4:54
\ldots{}
1504706560/1545832448 (97.3\%) @3.9x, remaining 0:07
1523318784/1545832448 (98.5\%) @3.9x, remaining 0:04
1541898240/1545832448 (99.7\%) @3.9x, remaining 0:00
/dev/rcd2c: flushing cache
/dev/rcd2c: writing lead-out
/dev/rcd2c: reloading tray\end{alltt}

The \texttt{-Z} option tells growisofs to burn an initial session to the device, which in this case is my DVD writer, attached to cd2. The \texttt{-dvd-compat} option closes the disk, which means no more sessions can be appended to it. This should provide better compatibility with video DVD players and some older DVD-ROM units.

Notice how growisofs indicates the writing speed, in this case 3.9x DVD speed, which is what could be expected from the media and writer combination, as indicated by dvd+rw-mediainfo.

If you are short on disk space and cannot store an ISO 9660 image for a DVD, you can write your data directly to the DVD. Let's first do a dry run, which simulates the creation of the filesystem.

\begin{alltt}\# \textbf{growisofs -dry-run -Z /dev/rcd2c -R /cvs}\end{alltt}

If this succeeds, just leave out the -dry-run option and start burning the DVD.

\begin{alltt}\# \textbf{growisofs -Z /dev/rcd2c -R /cvs}\end{alltt}

It is also possible to append data to an existing DVD, by using the -M option, which merges a new session to an existing one:

\begin{alltt}\# \textbf{growisofs -M /dev/rcd2c -R /mydata}\end{alltt}

For more information about growisofs, refer to the manual page.

When you have finished writing the DVD, mount it and see whether everything you expected to be there, is indeed there.

\subsubsection*{Why am I not getting the writing speed I expected?}

Instead of the above writing output, you may see something like:

\begin{alltt} 4784128/1545832448 ( 0.3\%) @0.7x, remaining 26:50
   7929856/1545832448 ( 0.5\%) @0.7x, remaining 29:05
  14123008/1545832448 ( 0.9\%) @0.7x, remaining 27:06
\ldots{}\end{alltt}

which is much slower. It means you are somehow not getting enough throughput on whatever bus your DVD writer is using. In the above example, the USB DVD writer was attached to a machine on which the \texttt{ehci(4)} driver, used by USB 2.0 controllers, failed to initialize properly. As always, you are welcome to provide patches and test results. The DVD writer fell back to the slower USB 1.1 interface, which causes reduced throughput. Indeed, USB 1.1 is limited to 12 Mbit/s, which amounts to 1.43 MB/s or 1.08x in DVD speed terms. The DVD writer falls back to a lower pace than the maximum, to reduce the risk of buffer underruns.

\section{But I want my media files in format FOO.}

\subsection*{Converting between different audio formats}

Let's say we want to process the sound recording from chapter 13 - Audio Recording. This recording has been stored in the raw format. It will be useful to convert it, because the raw format does not include headers and the recording parameters will need to be specified at every usage of the file.

One sound conversion tool is \texttt{audio/sox}, available through packages and ports. \textbf{sox} supports AIFF, AU, MP3, Ogg Vorbis, RIFF WAV and raw formats, as well as some of the more exotic audio formats out there. Below is an example for converting the recording to RIFF WAV format.

\begin{alltt}\$ \textbf{sox -U -c 1 -r 8000 -b myvoice.raw myvoice.wav}\end{alltt}

Note that the specified parameters correspond to the recording parameters specified before the recording. This was just an example. More audio-related libraries and software can be used for audio conversion.

\textbf{Note}: It is not recommended to convert between different lossy compression formats. For instance, the MP3 and Vorbis codecs throw away different parts of an original audio waveform. Therefore, when converting a MP3 file to Ogg Vorbis, the end result will probably sound worse than the original MP3.

\subsection*{Converting between different video formats}

It's important to make a clear distinction between

\begin{itemize}
\item the container file format - popular examples are MP4, OGG, MPEG, MOV, AVI, ASF.
\item the video codec - for example MPEG-1, MPEG-2, MPEG-4 compliant codecs (like Xvid and DivX), FFmpeg, WMV, ... - read this Wikipedia article about video codecs to find out more.
\end{itemize}

In OpenBSD, support for MPEG and AVI containers is most mature at this time.

Two popular utilities are \texttt{multimedia/transcode} and \texttt{mencoder} (part of \texttt{x11/mplayer}). They use or can use the \texttt{libavcodec} library as part of the \texttt{graphics/ffmpeg} port, which generates good quality output. You can, of course, also use \texttt{ffmpeg} directly. It should also be possible to use the XviD encoder in \texttt{multimedia/xvidcore}.

The documentation that comes with these packages, under the form of manual pages or HTML documents in \texttt{/usr/local/share/doc}, contains many examples, so it is HIGHLY recommended to read those documents.

\section{Is it possible to play streaming media under OpenBSD?}

Yes, it is. Many audio and video streams will work just fine, on a limited number of platforms. A few of them will not.

This is not meant to be a complete, overly detailed answer to have every possible streaming format work on any hardware architecture. You may want to learn more about streaming media to start with. A slightly dated but still good starting point is this chapter about streaming media from the O'Reilly book titled Designing Web Audio.

The first thing to understand is that there are a number of different streaming protocols around. The streaming \textbf{protocol} defines how the streams will be sent over the network. They have been developed to allow efficient transmission of audio/video over the internet in real-time. Mostly, the streaming protocol is a (Layer 7) application protocol, which can use either UDP or TCP (Layer 4) transport protocols. The User Datagram Protocol (UDP) is very suited for this type of application since it doesn't do any retransmission of packets or other overhead. A number of specialized but proprietary protocols have been developed, e.g. Microsoft Media Services (MMS) and the Real Time Streaming Protocol (RTSP). As we will see, HTTP (which uses TCP) is sometimes used as well, even though it does not allow serving streams at a steady bitrate like UDP, RTSP and MMS.

Next, there is the streaming \textbf{format}, which is how the audio/video data has been organized and can be played. The most widely used streaming formats are MP3, Real Audio (RA, RM) and Windows Media (ASF), all proprietary technologies. Occasionally you will also encounter streams in the open Ogg Vorbis format.

As an example, I'll explain in a few steps how I get to listen to Radio 1, one of the Belgian national radio stations. Browser-embedded plugins are not available on OpenBSD, so the story is usually not an instant "click and play".

\begin{itemize}
\item Determine the streaming protocol and format.
This is usually indicated on the website where you access the stream. In this case, I followed the link "Listen live" from the main site, and it's telling me my operating system is not supported. They are being nice by saying I can also listen to their MP3 streams without their embedded Flash player. Apart from that, a list of links to the national radio stations appears, allowing me to proceed to the next step. Note that I used a JavaScript-enabled browser to get this far.
\item Figure out the precise URL.
Many websites link to a container metafile or playlist (such as M3U, ASX, RAM), which contains the actual location of the stream. Just save the file, and read the URL from it. In my example this is

\begin{alltt}\$ \textbf{ftp http://internetradio.vrt.be/dab/hoeluisteren/pc/help/gebruiksvoorwaarden/stream_11.m3U}
\$ \textbf{cat stream_11.m3U}
http://mp3.streampower.be/radio1-mid.mp3
http://mp3.streampower.be/radio1-low.mp3
http://mp3.streampower.be/radio1-high.mp3\end{alltt}

It looks like I can even choose between low, medium and high quality streams. Other websites may contain some JavaScript code to generate the URL. In that case, the best tip is: dig up the HTML source and scripts it refers to. There is a good chance you can reconstruct the URL from it.
\item To play streams, your best bet is probably \texttt{x11/mplayer}, which is available through packages and ports. It supports most of the streaming protocols and formats, and has been reported to work on amd64, i386, powerpc and sparc64 platforms. But there are alternatives: \texttt{ogg123} from \texttt{audio/vorbis-tools} (for Ogg Vorbis streams), \texttt{audio/mpg123} and \texttt{audio/mpg321} (for MP3 streams), XMMS in \texttt{audio/xmms} and the Videolan Client in \texttt{x11/vlc}. Continuing the example:

\begin{alltt}\$ \textbf{mplayer http://mp3.streampower.be/radio1-mid.mp3}\end{alltt}
\item Optionally, you may want to make it a little easier by including an alias in your \texttt{.profile}:

\begin{alltt}alias radio1='mplayer http://mp3.streampower.be/radio1-mid.mp3'\end{alltt}
\end{itemize}

Windows Media (ASF) streams will often work, though they may contain data in formats supported only through the \texttt{graphics/win32-codecs} port, which runs on i386 only ('pkg\_info win32-codecs' will tell you which codecs). Some Real Audio streams can be made to work on i386 using \texttt{mplayer} in conjunction with the \texttt{graphics/win32-codecs} and \texttt{emulators/fedora/base} ports (see this thread on the ports mailing list).

\section{Can I have Java support in my web browser? (i386 \& amd64 only)}

The Java plugin is part of the Java Development Toolkit (JDK). For licensing reasons, OpenBSD cannot ship binary packages for the JDK. This means you will have to build it from ports. Further information on building the JDK can be found in chapter 8 - Programming Languages. Once you have finished building the JDK, you can install either the full JDK package or just the Java Runtime Environment (JRE) which is in a subpackage and contains the browser plugin.

Upon installation, instructions are displayed for using the Java plugin with the Firefox or Seamonkey web browser. Create the symlink as explained, and then you should see the Java plugin upon entering "\texttt{about:plugins}" in the URL bar.

For KDE's Konqueror web browser, either the java binary must be in your PATH, or its absolute path can be configured from the menu Settings -> Configure Konqueror -\textgreater{} Java \& JavaScript. By default, the java binary is located in \texttt{/usr/local/jre-version/bin/} or \texttt{/usr/local/jdk-version/bin/}, depending on whether you installed the JRE or the JDK.

\textbf{Note}: Java support has only been tested with the Firefox, Seamonkey, and Konqueror web browsers. If it works well for you using a different browser, please let us know.

\section{Can I have Flash support in my web browser?}

The Flash plugin is distributed by Adobe in binary form only. Adobe does not provide a native OpenBSD plugin. Considering their security record, we thank them for this neglect.

If you are just looking to watch flash videos from common websites, there are a number of options in packages, including: get\_flash\_videos, minitube, youtube-dl, get\_iplayer and yt.
Also, the Gnash project has made a lot of progress lately, and may fill your needs.