1 /***************************************************************************
2                           sound.c  -  description
3                              -------------------
4     begin                : Sun Jul 29 2001
5     copyright            : (C) 2001 by Michael Speck
6     email                : kulkanie@gmx.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifdef SOUND
19 
20 #include "ltris.h"
21 #include "audio.h"
22 
23 /*
24 ====================================================================
25 If audio device was properly initiated this flag is set.
26 If this flag is not set; no action will be taken for audio.
27 ====================================================================
28 */
29 int audio_ok = 0;
30 /*
31 ====================================================================
32 If this flag is not set no sound is played.
33 ====================================================================
34 */
35 int sound_enabled = 1;
36 
37 /*
38 ====================================================================
39 Initiate/close audio
40 ====================================================================
41 */
audio_open()42 int audio_open()
43 {
44     if ( Mix_OpenAudio( MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 256 ) < 0 ) {
45         fprintf( stderr, "audio_open: can't open audio device: %s\n", SDL_GetError() );
46         audio_ok = 0;
47         return 0;
48     }
49     audio_ok = 1;
50     return 1;
51 }
audio_close()52 void audio_close()
53 {
54     if ( !audio_ok ) return;
55     Mix_CloseAudio();
56 }
57 
58 /*
59 ====================================================================
60 Sound chunk
61 ====================================================================
62 */
sound_chunk_load(char * file_name)63 Sound_Chunk* sound_chunk_load( char *file_name )
64 {
65     char path[512];
66     Mix_Chunk *chunk = 0;
67 
68     if ( !audio_ok ) {
69         fprintf( stderr,
70                  "sound_chunk_load: cannot load WAV '%s' as audio device isn't initated properly\n",
71                  file_name );
72         return 0;
73     }
74 
75     /* use SRCDIR/sounds as source directory */
76     sprintf( path, "%s/sounds/%s", SRC_DIR, file_name );
77 
78     chunk = Mix_LoadWAV( path );
79     if ( chunk == 0 )
80         fprintf( stderr, "chunk_load: couldn't load WAV '%s': %s\n", path, SDL_GetError() );
81 
82     return (Sound_Chunk*)chunk;
83 }
sound_chunk_free(Sound_Chunk * chunk)84 void sound_chunk_free( Sound_Chunk *chunk )
85 {
86     if ( !audio_ok ) return;
87     Mix_FreeChunk( (Mix_Chunk*)chunk );
88 }
89 
90 /*
91 ====================================================================
92 Sound stuff
93 ====================================================================
94 */
95 
96 /*
97 ====================================================================
98 Enable/disable sound
99 ====================================================================
100 */
sound_enable(int enable)101 void sound_enable( int enable )
102 {
103     if ( !audio_ok ) return;
104     sound_enabled = enable;
105     if ( !enable )
106         Mix_Pause( -1 ); /* stop all sound channels */
107 }
108 /*
109 ====================================================================
110 Set general volume of sounds. 0 - 127
111 ====================================================================
112 */
sound_volume(int level)113 void sound_volume( int level )
114 {
115     if ( !audio_ok ) return;
116     if ( level < 0 ) level = 0;
117     if ( level > 127 ) level = 127;
118     Mix_Volume( -1, level ); /* all sound channels */
119 }
120 /*
121 ====================================================================
122 Play a chunk.
123 ====================================================================
124 */
sound_play(Sound_Chunk * chunk)125 void sound_play( Sound_Chunk *chunk )
126 {
127     if ( !audio_ok ) return;
128     if ( !sound_enabled ) return;
129     /* use first free sound channel and play sound one time */
130     Mix_PlayChannel( -1, (Mix_Chunk*)chunk, 0 );
131 }
132 
133 /*
134 ====================================================================
135 Music stuff
136 ====================================================================
137 */
138 
139 /*
140 ====================================================================
141 Set general volume of sounds. 0 - 127
142 ====================================================================
143 */
music_volume(int level)144 void music_volume( int level )
145 {
146     /* not implemented */
147 }
148 
149 #endif
150