1 /*
2  *   GRacer
3  *
4  *   Copyright (C) 1999 Takashi Matsuda <matsu@users.sourceforge.net>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  */
21 
22 #include <config.h>
23 #include "sound.h"
24 
25 #ifdef ENABLE_SOUND
26 #include <sl.h>
27 
28 static slScheduler *sched = NULL;
29 
Sound_Init(Tcl_Interp * interp)30 int Sound_Init (Tcl_Interp *interp)
31 {
32   sched = new slScheduler (8000);
33   /*
34    * check for sound working, otherwise we get a segfault in
35    * sched->addSampleEnvelope below if /dev/dsp is not configured
36    */
37   if (sched->notWorking ()) {
38 #ifndef __FreeBSD__
39     /* XXX this gets a few
40      * `gracer in free(): warning: junk pointer, too high to make sense.'
41      * don't ask me why. So we'll just leak memory, it should only
42      * be allocated once per process...
43      */
44     delete sched;
45 #endif
46     sched = NULL;
47   }
48   if (sched) {
49     sched->setSafetyMargin (0.2);
50   }
51 
52   return TCL_OK;
53 }
54 
gr_sound_update(void)55 int gr_sound_update (void)
56 {
57   if (sched) {
58     sched->update ();
59   }
60 
61   return 1;
62 }
63 
gr_sample_new(char * filename)64 GrSample gr_sample_new (char *filename)
65 {
66   if (!sched)
67     return NULL;
68 
69   slSample *s = new slSample (filename, sched);
70 
71   return (GrSample *) s;
72 }
73 
gr_sample_free(GrSample sample)74 void gr_sample_free (GrSample sample)
75 {
76   slSample *s = (slSample *) sample;
77 
78   if (s)
79     delete s;
80 }
81 
gr_sample_stop(GrSample sample,int magic)82 void gr_sample_stop (GrSample sample, int magic)
83 {
84   slSample *s = (slSample *) sample;
85 
86   if (sched) {
87     sched->stopSample (s, magic);
88   }
89 }
90 
gr_sample_pause(GrSample sample,int magic)91 void gr_sample_pause (GrSample sample, int magic)
92 {
93   slSample *s = (slSample *) sample;
94 
95   if (sched) {
96     sched->pauseSample (s, magic);
97   }
98 }
99 
gr_sample_resume(GrSample sample,int magic)100 void gr_sample_resume (GrSample sample, int magic)
101 {
102   slSample *s = (slSample *) sample;
103 
104   if (sched) {
105     sched->resumeSample (s, magic);
106   }
107 }
108 
gr_sample_adjust_volume(GrSample sample,float volume)109 void gr_sample_adjust_volume (GrSample sample, float volume)
110 {
111   slSample *s = (slSample *) sample;
112 
113   if (s) {
114     s->adjustVolume (volume);
115   }
116 }
117 
gr_sample_play(GrSample sample,int magic)118 void gr_sample_play (GrSample sample, int magic)
119 {
120   slSample *s = (slSample *) sample;
121 
122   if (sched) {
123     sched->playSample (s, 1, SL_SAMPLE_ABORT, magic);
124   }
125 }
126 
gr_sample_loop(GrSample sample,int magic)127 void gr_sample_loop (GrSample sample, int magic)
128 {
129   slSample *s = (slSample *) sample;
130 
131   if (sched) {
132     sched->loopSample (s);
133   }
134 }
135 
gr_envelope_new(int num_step,GrReplayMode mode)136 GrEnvelope gr_envelope_new (int num_step, GrReplayMode mode)
137 {
138   slReplayMode rmode;
139 
140   switch (mode) {
141   case GR_SAMPLE_LOOP:
142     rmode = SL_SAMPLE_LOOP;
143     break;
144 
145   case GR_SAMPLE_ONE_SHOT:
146     rmode = SL_SAMPLE_ONE_SHOT;
147     break;
148 
149   default:
150     return NULL;
151   }
152 
153   slEnvelope *e = new slEnvelope (num_step, rmode);
154 
155   return (GrEnvelope *) e;
156 }
157 
gr_envelope_free(GrEnvelope envelope)158 void gr_envelope_free (GrEnvelope envelope)
159 {
160   slEnvelope *e = (slEnvelope *) envelope;
161 
162   if (e)
163     delete e;
164 }
165 
gr_envelope_set_step(GrEnvelope envelope,int step,float _time,float _value)166 void gr_envelope_set_step (GrEnvelope envelope,
167 			  int step,
168 			  float _time,
169 			  float _value)
170 {
171   slEnvelope *e = (slEnvelope *) envelope;
172 
173   if (e) {
174     e->setStep (step, _time, _value);
175   }
176 }
177 
gr_sample_add_envelope(GrSample sample,int magic,int slot,GrEnvelope envelope,GrEnvelopeType type)178 void gr_sample_add_envelope	(GrSample sample,
179 				 int magic,
180 				 int slot,
181 				 GrEnvelope envelope,
182 				 GrEnvelopeType type)
183 {
184   slSample *s = (slSample *) sample;
185   slEnvelope *e = (slEnvelope *) envelope;
186   slEnvelopeType etype = (slEnvelopeType) type;
187 
188   if (!sched)
189     return;
190 
191   sched->addSampleEnvelope (s, magic, slot, e, etype);
192 }
193 
194 #else /* ! ENABLE_SOUND */
195 
Sound_Init(Tcl_Interp * interp)196 int Sound_Init (Tcl_Interp *interp)
197 {
198   return TCL_OK;
199 }
200 
gr_sound_update(void)201 int gr_sound_update (void)
202 {
203   return 1;
204 }
205 
gr_sample_new(char * filename)206 GrSample gr_sample_new (char *filename)
207 {
208   return NULL;
209 }
210 
gr_sample_free(GrSample sample)211 void gr_sample_free (GrSample sample)
212 {
213 }
214 
gr_sample_stop(GrSample sample,int magic)215 void gr_sample_stop (GrSample sample, int magic)
216 {
217 }
218 
gr_sample_pause(GrSample sample,int magic)219 void gr_sample_pause (GrSample sample, int magic)
220 {
221 }
222 
gr_sample_resume(GrSample sample,int magic)223 void gr_sample_resume (GrSample sample, int magic)
224 {
225 }
226 
gr_sample_adjust_volume(GrSample sample,float volume)227 void gr_sample_adjust_volume (GrSample sample, float volume)
228 {
229 }
230 
gr_sample_play(GrSample sample,int magic)231 void gr_sample_play (GrSample sample, int magic)
232 {
233 }
234 
gr_sample_loop(GrSample sample,int magic)235 void gr_sample_loop (GrSample sample, int magic)
236 {
237 }
238 
gr_envelope_new(int num_step,GrReplayMode mode)239 GrEnvelope gr_envelope_new (int num_step, GrReplayMode mode)
240 {
241   return NULL;
242 }
243 
gr_envelope_free(GrEnvelope envelope)244 void gr_envelope_free (GrEnvelope envelope)
245 {
246 }
247 
gr_envelope_set_step(GrEnvelope envelope,int step,float _time,float _value)248 void gr_envelope_set_step (GrEnvelope envelope,
249 			  int step,
250 			  float _time,
251 			  float _value)
252 {
253 }
254 
gr_sample_add_envelope(GrSample sample,int magic,int slot,GrEnvelope envelope,GrEnvelopeType type)255 void gr_sample_add_envelope	(GrSample sample,
256 				 int magic,
257 				 int slot,
258 				 GrEnvelope envelope,
259 				 GrEnvelopeType type)
260 {
261 }
262 
263 #endif /* ENABLE_SOUND */
264