|
10-04-01
Build a REALbasic Drum Machine by
Erick Tejkowsi
This week we'll create a fun little project that mimics a drum machine. This project is somewhat
unique in that it uses high quality samples for the drum loops, lets you start and stop the loops at will,
and yet the whole thing only requires two lines of code! How are we going to pull this one off? Read on!
Introduction
The secret behind this week's project involves two file formats : SoundFont(.sf2) and Downloadable Sounds(.dls).
These two file types are capable of storing multiple audio samples in one file. Think of them as audio packages.
All audio samples are located conveniently in one file. These samples might represent a variety of sounds: orchestral instruments, pop vocals,
car horns, or dog barks.
To hear the sounds within SF2 and DLS files, you can trigger them with MIDI from REALbasic.
QuickTime added SF2 and DLS support beginning with QuickTime 5 and REALbasic works great in combination with it. (For best results on this project,
download the latest version of QuickTime.)
To install a SF2 or DLS file, drag it into one of the following folders on your hard drive:
- For Mac OS 8/9 - Hard Drive:System Folder:Extensions Folder: QuickTime Extensions
- For Mac OS X - Home:Library:Audio:Sounds:Banks
In case you don't have any SF2 or DLS files, some are included with the project download at the end of this tutorial. There are also
many free files in SF2 and DLS format. SF2 has been fairly popular in the Windows world for several years, so you will find many of this file type on the web.
DLS is a somewhat newer format, so it hasn't gained as much momentum yet. Files in this format are harder to find right now. DLS, however, is now the de facto
standard (it's part of the MPEG4 spec., for example), so expect to see more it in the future. At the end of this tutorial are some links,
where you can find sounds or learn to build them yourself.
Once you have moved your SF2 and DLS files to the proper location on your hard drive, open the QuickTime Control Panel (Mac OS 9) or
the QuickTime Panel of the System Preferences (Mac OS X). Select your file and press "Make Default" for Mac OS X. For Mac OS 9, simply click the little black
circle to the left of the sound bank's name.
After you select a sound bank, close the QuickTime settings and launch REALbasic.
Build an Interface and Add the Code
To begin building the interface, double click Window1 in the Project Window. Drag four NotePlayers and eight PushButton controls from the Toolbar
to your interface. Name them according to this chart:
| Control |
Settings |
Number of Controls With This Name |
| NotePlayer |
Name : NotePlayer1 Give each NotePlayer a unique Index number starting at 0 (zero). Give each NotePlayer a unique Instrument number starting at 1 |
4 |
| PushButton |
Name : PlayButton Give each PlayButton a unique Index number starting at 0 (zero) |
4 |
| PushButton |
Name : StopButton Give each StopButton a unique Index number starting at 0 (zero) |
4 |
Notice above that we are using control arrays. In other words, 4 buttons share the same name (PlayButton, for example), but each has its
own unique Index number. This permits you to write code one time and let each control with the same name use that code. The NotePlayer Instrument number
normally corresponds to a General MIDI Instrument as listed in the RB Language Reference. It needn't, however. When a SF2 or DLS file is used as the QuickTime music
engine, the instruments are numbered according to the order they appear in the file. The demo files included with this project have four "instruments" per file.
The completed interface might look something like this:
The final step is to add the two lines of code. Double click any one of the PlayButtons and enter this line:
NotePlayer1(Index).PlayNote(60,127)
This simple line of code starts up the NotePlayer control that shares the same Index number as this PushButton. It plays MIDI note number 60 at
a volume of 127 (volume = 0-127). The second line of code is equally small - it turns off the note, by sending it a volume fo zero.
Place this code in the Action event of the StopButton
NotePlayer1(Index).PlayNote(60,0)
Conclusion
That's it! Test your project by selecting Debug->Run. If all is working well, you should hear some unique sounds coming out of QuickTime, instead of those
boring old QT Instruments you're used to. Feel free to download some other SoundFont or DLS files and activate them the same way. Your project can then instantly
make use of the new sounds.
If you're too lazy to build this week's project, there may be no hope for you. :-) We still have the
project available for download, though. It also contains a few sound
banks for your listening enjoyment. You can learn more about SoundFont and DLS at the following URLs:
BEST Software Design - Creators of PolyPhontics, the forthcoming Macintosh SoundFont and DLS Editor.
Creative Technologies - Co-inventors of the SF2 format. Creators of the Mac-compatible SoundBlaster card.
SynthZone - A huge listing of SoundFont and DLS sites.
Sonic Implants - Sells commerical SoundFont and DLS samples. They also hand out some great freebies too.
HammerSound - Another large collection of SoundFont files.
Have fun and see you next week!
10-2-01
REALbasic News
by Erick Tejkowsi
Aqua Aware Menus
Paul Mitchum has posted a unique code example, called AquaAwareMenuItem v.1.0
AquaAwareMenuItem is a MenuItem subclass that splits the EnableMenu event
into two new events: the EnableMenu event and the AquaEnableMenu event.
AquaEnableMenu is triggered if the application is running under Aqua;
EnableMenu is triggered otherwise.
Stimulating!
Electric Butterfly has released a new applicatin called
Stimulus 1.0. It's a nifty multimedia viewer, but more importantly, it was "Made with REALbasic" (MWRB). Check it out!
New Tip!
Need to write to the console in Mac OS X? The REALbasic Tips list has a nice example for you to try:
Sub WriteDebugMessage(msg as String)
dim msgBlock as MemoryBlock
Declare Sub DebugStr Lib "CarbonLib" (msg as Ptr)
msgBlock = NewMemoryBlock(256)
msgBlock.PString(0) = Left(msg, 255)
DebugStr msgBlock
End Sub
What?!?! You're not on the Tips List? Subscribe here.
Crazy Code.
REALbasic genius, Matthijs van Duin, has temporarily updated his web site
to make things easier to find. He promises to make it prettier in the future, but for now this is an excellent
site for RB Pros and Newbies alike.
Transfer Those Files!
Alfred Van Hoek has updated
his QuickTime File Transfer plugin again. This sucker will even do multiple simultaneous downloads, so
it's a must-have.
Play With Your Pictures.
Picture Play is an application for creating compositions of many
images. It is basically an electronic collage. You can use it to
incorporate multiple images into a single artistic image, and add
text labels as well. Oh, and it too is "Made With REALbasic".
|