home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeDesigning Web AudioSearch this book

10.2. Adding MIDI to your web pages

Making MIDI files can be fun and easy; placing them on your web page can be even easier. Simply enter this line of code in your HTML:

<BGSOUND SRC="Yoursound.mid">

Note that this example is for Explorer. If Navigator has trouble with the BGSOUND tag, try the following example in addition to the BGSOUND:

<EMBED SRC="Yoursound.mid" HIDDEN="true">

This code causes a MIDI file to begin playback whenever the page is loaded into the user's browser. Furthermore, you can set basic parameters such as looping, volume, and so forth, by simply adding them to the tag. Here is the most basic way to add MIDI sound to your web page:

<EMBED SRC="Yoursound.mid" HIDDEN="true"

This example HTML code will instruct a web page to play Yoursound.mid. The file will loop, the volume will be at the max limit of 10, the autostart function will be in effect, and the file will begin playback automatically

If you want to have MIDI sound available on your web page via a hyperlink, but do not want the file to begin playback automatically, simply add an HREF tag to the MIDI file, and it will be available for download whenever the user clicks on the link:

<A HREF="Yoursound.mid">

10.2.1. Making a MIDI jukebox

Many web designers have taken advantage of the short download times and cross-platform performance of MIDI and have created a variety of tools and tricks that you can use on your own web pages. One basic example of how to use MIDI on your site is the MIDI jukebox.

A MIDI jukebox functions as a typical jukebox functions in that a user selects a requested song by clicking a button. Once the button is clicked, the MIDI file is downloaded to the user's browser (both IE and Netscape have included MIDI-compatible plug-ins with releases 4.0 and later). It then begins playing almost immediately via any MIDI-compatible plug-in or external system (in actuality, the MIDI plug-in only pipes the MIDI data to the appropriate playback device, such as the Netscape MIDI window or QuickTime player). There's no waiting for long download times, just fast and easy music. A typical jukebox, as shown in Figure 10-1, can be used to provide examples of your music or simply to entertain visitors to your site.

Figure 10-1

Figure 10-1. A typical MIDI jukebox

To fill your MIDI jukebox with popular MIDI files, visit one of the many online MIDI archives. You also can easily create your own MIDI files with a MIDI sequencing application. We've used different applications, but one we have used extensively is Vision by Opcode Systems. Vision provides an ideal creative environment, syncs up easily to QuickTime and other standard digital audio file types, and is relatively inexpensive and easy to use. We have included a 30-day trial version of Vision at http://www.designingwebaudio.com. We recommend that you take advantage of the application and its tutorial sections. To find out where to find MIDI file libraries, check the Opcode site at http://www.opcode.com or try any of the following sites:

10.2.3. Playing a different tune

When using sound on your web site, keep in mind that once a user becomes familiar with the sound, he or she may get bored quickly. Accordingly, it is wise to frequently change or alter the sound on your site to avoid repetition. You can do this by using a simple script that rotates the MIDI file (or .au, .wav, or other file type) each time a user enters your site or with a script that changes the tune on the hour, and so on.

Here is an example of a script that changes the MIDI file based on the time of day. The code is simple -- just copy and paste it directly into your HTML code, then add your own MIDI files. Again, you can find MIDI files by visiting any number of MIDI archives on the Web, including those listed earlier in this chapter.

var numMIDI = 12
day = new Date(  )
seed = day.getTime(  )
ran = parseInt(((seed - (parseInt(seed/1000,10) * 1000))/10)/100*numMIDI + 1,10)
if (ran == (1))    
if (ran == (2))
if (ran == (3))
if (ran == (4))
if (ran == (5))       
if (ran == (6))       
if (ran == (7))       
if (ran == (8))       
if (ran == (9))       
if (ran == (10))       
if (ran == (11))       
if (ran == (12))       

document.write('<EMBED SRC="http://yoursite.com/MIDI/' + MIDI + '" WIDTH=0 HEIGHT=0 AUTOSTART=TRUE>')
document.write('<BGSOUND SRC="http://yoursite.com/MIDI/' + MIDI + '" AUTOSTART=TRUE>')
// -->

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.