home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************/
- /* */
- /* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
- /* ------------------------------- ------------------ */
- /* */
- /* Book: ACM System Amiga C Club */
- /* Chapter: Hardware Tulevagen 22 */
- /* File: Example2.c 181 41 LIDINGO */
- /* Author: Anders Bjerin SWEDEN */
- /* Date: 92-05-02 */
- /* Version: 1.00 */
- /* */
- /* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
- /* */
- /* Registered members may use this program freely in their */
- /* own commercial/noncommercial programs/articles. */
- /* */
- /***********************************************************/
-
- /* This is a very bad program that plays a tune with the */
- /* first audio channel. The reason why it is bad is because */
- /* the program does not "lock" the channel before it uses */
- /* the hardware. It simply steals the sound channel without */
- /* asking nor notifying anyone. */
- /* */
- /* If you lock the channel as described in the Audio Device */
- /* chapter you may use the hardware registers. However, */
- /* this program does not lock it nor tries to reserve the */
- /* channels as we should. The program is included here only */
- /* as a demonstration how the hardware registers are */
- /* working, but should not be used in your own programs!!! */
-
-
-
- #include <exec/types.h> /* Data types */
- #include <exec/memory.h> /* Memory types */
- #include <hardware/custom.h> /* Custom structure */
- #include <hardware/dmabits.h> /* DMA flags */
-
-
-
- /* Length of our waveform: */
- #define SQUARE_DATA_LENGTH 2
-
-
-
- /* The Custom structure is already declared by the Amiga, and */
- /* should therefore be declared as external data. Since the */
- /* hardware registers could be far away from our program we */
- /* have to declare it as "far". Note that this structure is */
- /* linked to the hardware registers, and if you change any */
- /* fields the corresponding hardware registers are affected. */
- extern struct Custom far custom;
-
-
-
- /* Declare a pointer to some soundwave data: */
- BYTE *square_wave = NULL;
-
-
-
- /* Declare our function: */
- void main();
- void clean_up( STRPTR text );
-
- void main()
- {
- /* Temporary loop counter: */
- int loop;
-
-
-
- /* Allocate some memory where we can store the waveform we */
- /* want to use. Note that it must be Chip memory, and placed */
- /* on a word boundary! */
- square_wave = (BYTE *) AllocMem( SQUARE_DATA_LENGTH, MEMF_CHIP );
- if( !square_wave )
- clean_up( "Could not allocate enough memory for the square wave!" );
-
- /* Initialize the waveform: (This is the smallest */
- /* waveform you can use, and undouptly the easiest.) */
- square_wave[ 0 ] = 127;
- square_wave[ 1 ] = -127;
-
-
-
- /* Prepare the hardware registers: (We do not care */
- /* if anyone else is using these registers. We simply */
- /* overwrite the data in that case. This is VERY BAD! */
- /* Do not do like this in your own programs!!!) */
-
- /* Give the audio channel a pointer to the wave form: */
- custom.aud[ DMAB_AUD0 ].ac_ptr = (UWORD *) square_wave;;
-
- /* Set the length of the wave form: */
- custom.aud[ DMAB_AUD0 ].ac_len = SQUARE_DATA_LENGTH;
-
- /* Set volume: */
- custom.aud[ DMAB_AUD0 ].ac_vol = 64;
-
- /* Set period value: */
- custom.aud[ DMAB_AUD0 ].ac_per = 256;
-
-
-
-
- /* The first audio channel has now been prepared to play */
- /* a waveform. We now tell the DMA channel which is */
- /* connected to the first audio channel to start to play: */
- custom.dmacon = DMAF_SETCLR | DMAF_AUD0;
-
-
-
- /* Take a small pause: */
- for( loop=0; loop < 100000; loop++ )
- /* Do nothing... */;
-
-
-
- /* Stop playing the sound: */
- custom.dmacon = DMAF_AUD0;
-
- /* If the DMAF_SETCLR flag is set together with the */
- /* channel flag, the channel is selected. If you only */
- /* set the channel flag without the DMAF_SETCLR flag, */
- /* the channel is deselected (stopped). */
-
-
-
- /* Quit: */
- clean_up( "The End!" );
- }
-
-
-
- void clean_up( STRPTR text )
- {
- /* Dealocate the square waveform: */
- if( square_wave )
- FreeMem( square_wave, SQUARE_DATA_LENGTH );
-
- /* Print the last message: */
- printf( "%s\n", text );
-
- /* Quit: */
- exit( 0 );
- }
-
-