home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of A1200
/
World_Of_A1200.iso
/
datafiles
/
text
/
c_manual
/
sound
/
easysound
/
example.c
< prev
next >
Wrap
Text File
|
1995-02-27
|
12KB
|
231 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Sound Amiga C Club */
/* Chapter: EasySound Tulevagen 22 */
/* File: Example.c 181 41 LIDINGO */
/* Author: Anders Bjerin SWEDEN */
/* Date: 92-05-10 */
/* Version: 2.15 */
/* */
/* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
/* */
/* Registered members may use this program freely in their */
/* own commercial/noncommercial programs/articles. */
/* */
/***********************************************************/
/* Now at last you can easily write C programs that plays digitized */
/* sound. You simply use four functions that will take care of all the */
/* work of allocating memory, loading the files, opening the ports and */
/* reserving the sound channels. Despite the simplicity you can still */
/* decide what volume and rate, which channel, and how many times the */
/* sound should be played etc. The functions contain full error checking, */
/* and will close and return everything that have been taken. */
/* */
/* I N S T R U C T I O N S */
/* ----------------------- */
/* */
/* 1. There are four functions that you need to use. The first one is */
/* called ESPrepareSound(), and must be called before you can play the */
/* soundeffect. You simply give it a file name as the only parameter, and */
/* it will allocate space and load the sound file. It will also prepare */
/* some other things that is needed before you may play the sound. If */
/* ESPrepareSound() has successfully done its task, it will return a */
/* pointer to a SoundInfo structure (struct SoundInfo *), else it will */
/* return NULL which means something went wrong. */
/* */
/* Synopsis: pointer = ESPrepareSound( filename ); */
/* */
/* pointer: (struct SoundInfo *) ESPrepareSound() will return a pointer */
/* to a SoundInfo structure if the sound was successfully */
/* prepared, else it will return NULL which means something went */
/* wrong. */
/* */
/* filename: (STRPTR) Pointer to a string containing the name of the */
/* sound file. */
/* */
/* */
/* */
/* 2. After you have prepared the sound, you may play it. You do it by */
/* calling the function ESPlaySound(). If the sound was played */
/* successfully, TRUE is returned, else FALSE is returned which means */
/* something went wrong. */
/* */
/* Synopsis: ok = ESPlaySound( ptr, vol, cha, pri, drate, times, ddata, */
/* time, wait ); */
/* */
/* ok: (BOOL) If the sound was played successfully TRUE is */
/* returned, else FALSE. */
/* */
/* ptr: (struct SoundInfo *) Pointer to a SoundInfo structure. This */
/* pointer was returned by ESPrepareSound(). */
/* */
/* vol: (UWORD) Volume, 0 to 64. */
/* */
/* cha: (UBYTE) Which channel should be used. (LEFT0, RIGHT0, */
/* RIGHT1 or LEFT1) */
/* */
/* pri (BYTE) What sound priority should be used. For normal sound */
/* effects, set the priority to "SOUND_PRI_EFFECT". See file */
/* "EasySound.h" for a complete list of recommended priorities. */
/* */
/* drate: (WORD) Delta rate. When the sound is prepared, the record */
/* rate is automatically stored in the SoundInfo structure, */
/* so if you do not want to change the rate, write NORMALRATE. */
/* */
/* times: (UWORD) How many times the sound should be played. If you */
/* want to play the sound forever, write NONSTOP. (To stop a */
/* sound call the function ESStopSound().) */
/* */
/* ddata: (ULONG) Where in the sound data we should start to play. (If */
/* you want to start at the beginning of the sound data set ddata */
/* to 0.) */
/* */
/* time: (ULONG) For how long time the sound should be played. If you */
/* want to play all of the sound data set time to 0. */
/* */
/* wait: (BOOL) If you want that the program waits for the sound to */
/* to be completed set the flag "WAIT". If you do not want to */
/* wait for the sound to be completed, set the flag "DO_NOT_WAIT". */
/* */
/* */
/* */
/* 3. If you want to stop an audio channel you simply call the function */
/* ESStopSound(). (It is not dangerous to stop a sound that has already */
/* terminated, or has not started.) */
/* */
/* Synopsis: ESStopSound( channel ); */
/* */
/* channel: (UBYTE) Which channel should be stopped. (LEFT0, RIGHT0, */
/* RIGHT1 or LEFT1) */
/* */
/* */
/* */
/* 4. Before your program terminates you must call the function */
/* ESRemoveSound() which will deallocate all memory that was allocated by */
/* the ESPrepareSound() function. IMPORTANT! All sound channels that is */
/* currentely playing the sound must have been stopped before this function */
/* may be called! */
/* */
/* Synopsis: ESRemoveSound( pointer ); */
/* */
/* pointer: (struct SoundInfo *) The pointer that was returned by */
/* ESPrepareSound(). */
/* */
/* */
/* */
/* I hope you will have a lot of use of EASY-SOUND and happy programming, AB */
/* This file should be compiled separately and then linked together */
/* with the EasySound.o module. If you have the SAS/Lattice C compiler */
/* you can do like this: (Do not include the quotations.) */
/* 1. Compile this file. Write: "lc Example" */
/* 2. If you have changed something in the EasySound.c file you must */
/* recompile it. However, if you have not modified it you do not */
/* have to recompile it. To compile it: "lc EasySound" */
/* 3. Finally you have to link these two files together. You can use */
/* the already prepared LNK file "Example.lnk" to do this. Simply */
/* write: "blink with Example.lnk" */
/* Include some important header files: */
#include <exec/types.h> /* Declares CPTR, BOOL and STRPTR. */
#include "EasySound.h" /* Declares LEFT0, LEFT1, RIGHT0, etc. */
/* Pointers to the three sound effects: */
struct SoundInfo *fire;
struct SoundInfo *explosion;
struct SoundInfo *background;
/* Declare the functions in this module: */
void main();
void free_memory( STRPTR message );
void main()
{
printf("\nE A S Y - S O U N D\n");
printf("Amiga C Club (ACC)\nAnders Bjerin\nTulevagen 22\n");
printf("181 41 LIDINGO\nSWEDEN\n\n");
printf("1. Prepare the sound Fire.snd\n");
fire = ESPrepareSound( "Fire.snd" );
if( !fire )
free_memory( "Could not prepare the sound effect!" );
printf(" Prepare the sound Explosion.snd\n");
explosion = ESPrepareSound( "Explosion.snd" );
if( !explosion )
free_memory( "Could not prepare the sound effect!" );
printf(" Prepare the sound Background.snd\n");
background = ESPrepareSound( "Background.snd" );
if( !background )
free_memory( "Could not prepare the sound effect!" );
printf("2. Play the sound\n");
/* Start with some atmospheric background sounds: */
ESPlaySound( background, MAXVOLUME/2, LEFT0, SOUND_PRI_EFFECT,
NORMALRATE, NONSTOP, 0, 0, DO_NOT_WAIT );
ESPlaySound( background, MAXVOLUME/2, RIGHT0, SOUND_PRI_EFFECT,
NORMALRATE, NONSTOP, 0, 0, DO_NOT_WAIT );
/* Wait four seconds: */
Delay( 4 * 50 );
ESPlaySound( fire, MAXVOLUME, LEFT1, SOUND_PRI_EFFECT,
NORMALRATE, 2, 0, 0, DO_NOT_WAIT );
/* Wait three seconds: */
Delay( 3 * 50 );
ESPlaySound( explosion, MAXVOLUME, RIGHT1,SOUND_PRI_EFFECT,
NORMALRATE, 2, 0, 0, DO_NOT_WAIT );
/* Wait four seconds: */
Delay( 4 * 50 );
printf("3. Stop the audio channels\n");
ESStopSound( LEFT0 );
ESStopSound( LEFT1 );
ESStopSound( RIGHT0 );
ESStopSound( RIGHT1 );
printf("4. Remove the sound effects\n");
free_memory( "THE END" );
}
void free_memory( STRPTR message )
{
printf("%s\n\n", message );
/* It is not dangerous to try to remove a sound that has not been */
/* prepared. We can therefore try to remove all sounds, even if some */
/* have not been initialized. However, all channels that are playing */
/* the sound must have been stopped before you may remove the sound! */
ESRemoveSound( fire );
ESRemoveSound( explosion );
ESRemoveSound( background );
exit();
}