Example of Playing a Sound Resource in Macintosh QuickBasic (43253)



The information in this article applies to:

  • Microsoft QuickBASIC Compiler for the Apple Macintosh 1.0
  • Microsoft QuickBASIC Compiler for the Apple Macintosh 1.0a
  • Microsoft QuickBASIC Compiler for the Apple Macintosh 1.0b

This article was previously published under Q43253

SUMMARY

The SOUND and WAVE statements in QuickBasic for the Macintosh let you generate four-voice sound. In addition, using Macintosh Sound Manager routines, you can also play sounds that have been stored on disk as resources, as described below.

The following is an explanation and example of playing a sound or "snd " resource using Macintosh QuickBasic. The routines for playing sounds are managed by the Sound Manager. The Sound Manager is documented in Volume V of "Inside Macintosh" by Apple Computer (published by Addison-Wesley).

There are seven routines that call the Sound Manager. SndPlay is the one that plays "snd " resources. The seven Sound Manager routines are not available as QuickBasic Toolbox Library routines (MBLCs), and they must be called with a modified version of QuickBasic's ToolBox statement. The ToolBox statement itself is an MBLC.

This information applies to Microsoft QuickBasic versions 1.0, 1.0a, and 1.0b for Apple Macintosh.

MORE INFORMATION

The "snd " resource name must be lowercase and followed by a space as shown.

To play large sounds (greater than 100K or so), you need to increase the memory available for QuickBasic for Macintosh so that it can load the resource (which is done when you call GetIndRes). Under Apple MultiFinder, you have to do this by changing the memory requirements from the 'Get Info' or with ResEdit so MultiFinder allocates enough memory when the program starts up. If you don't increase the memory limit, you will hang or get a system error when the program tries to invoke 'GetIndRes'.

The steps for calling Sound Manager routines are as follows:

  1. Steps to take before writing QuickBasic code are as follows:

    1. You must have an application that can create "snd " resources or have obtained them from a third party. QuickBasic does not create "snd " resources. To play "snd " resources, you must use ResEdit to insert them into some file that you can open with OpenResFile. If you cannot obtain any "snd " resources, there should be at least one in the resource fork of the SYSTEM itself so that this program example can be executed. You should verify this with ResEdit.
    2. QuickBasic's ToolBox routine itself needs modification. You must create two new MBLCs with ResEdit: ToolBoxOS and ToolBoxNewTool. For instructions to create these routines, query on the words ToolBoxNewTool or ToolBoxNewOS in this knowledge base. The instructions are also found in an application note, which can be obtained by calling Microsoft Technical Support at (425) 454-2030.
  2. Steps inside of the QuickBasic source code are as follows:

    1. Use OpenResFile to open the resource fork of the file where the "snd " resources are stored.
    2. Get a handle to the first "snd " resource available using GetIndRes. This MBLC routine uses the index, which is passed to it, to get the nth resource of that type available. It searches all of the resource forks currently open, including the SYSTEM itself. That is why it's important that the SYSTEM include at least one "snd " resource for this program to work.
    3. Use ToolBoxNewTool to call SndPlay with trap number &HA805. You must consult "Inside Macintosh," Volume V, and "Microsoft QuickBasic for Apple Macintosh: Language Reference," pages 497 to 503, to determine what parameters to pass, their data types, and whether to pass them by reference or by value.

Code Example

The following should be compiled with the "Include MBPCs & MBLCs" option:
' parameters for SndPlay
DIM chan&, sndHdl&, OSErr%, async%, ref%
' Trap Numbers for SndPlay
SndPlay% = &HA805
' Initialize program variables
chan& = 0  : sndHdl&= 0 : OSErr% = 0
async% = 0 : ref% = 0   : Index% = 1

ON ERROR GOTO errtrap
ON DIALOG GOSUB PlaySound
DIALOG ON
BUTTON 1, 1, "Sound", (100, 100)-(200, 200), 1

' Include the following line if you have an "snd " resource file.
'    OpenResFile "Your Resource File", ref%
     ToolBoxNewTool "I"

WHILE 1
' Loop waiting for the button to be pressed.
WEND
END
errtrap:
    Index% = 1
    RESUME retry

PlaySound:
    temp = DIALOG(0)
retry:
    GetIndRes "snd ",Index%, sndHdl&
    Index% = Index% + 1
    ToolBoxNewTool "WQ",SndPlay%,(chan&),(sndHdl&),(async%),OSErr%
    RETURN
				

Modification Type:MajorLast Reviewed:10/20/2003
Keywords:KB43253