5 #include "slPortability.h"
7 #ifdef SL_USING_OSS_AUDIO
8 #define SLDSP_DEFAULT_DEVICE "/dev/dsp"
10 #define SLDSP_DEFAULT_DEVICE "dsp"
11 #elif defined(__OpenBSD__)
12 #define SLDSP_DEFAULT_DEVICE "/dev/audio"
14 #define SLDSP_DEFAULT_DEVICE "dsp" // dummy ...
22 typedef unsigned char Uchar ;
23 typedef unsigned short Ushort ;
25 #define SL_DEFAULT_SAMPLING_RATE 11025
28 class slSamplePlayer ;
45 audio_info_t ainfo; // ioctl structure
46 audio_offset_t audio_offset; // offset in audiostream
47 long counter; // counter-written packets
48 #elif defined(SL_USING_OSS_AUDIO)
49 audio_buf_info buff_info ;
51 ALconfig config; // configuration stuff
52 ALport port; // .. we are here
57 int ioctl ( int cmd, int param = 0 )
59 if ( error ) return param ;
61 if ( ::ioctl ( fd, cmd, & param ) == -1 )
63 perror ( "slDSP: ioctl" ) ;
72 HWAVEOUT hWaveOut; // device handle
73 WAVEFORMATEX Format; // open needs this
75 WAVEHDR wavehdr[ 3 ]; // for round robin ..
76 int curr_header; // index of actual wavehdr
77 long counter; // counter-written packets
81 void open ( char *device, int _rate, int _stereo, int _bps ) ;
83 void getBufferInfo () ;
84 void write ( void *buffer, size_t length ) ;
88 void setError () { error = SL_TRUE ; }
89 int getDriverBufferSize () ;
93 slDSP ( int _rate = SL_DEFAULT_SAMPLING_RATE,
94 int _stereo = SL_FALSE, int _bps = 8 )
96 open ( SLDSP_DEFAULT_DEVICE, _rate, _stereo, _bps ) ;
99 slDSP ( char *device, int _rate = SL_DEFAULT_SAMPLING_RATE,
100 int _stereo = SL_FALSE, int _bps = 8 )
102 open ( device, _rate, _stereo, _bps ) ;
105 ~slDSP () { close () ; }
107 float secondsRemaining () ;
108 float secondsUsed () ;
110 void play ( void *buffer, size_t length ) { write ( buffer, length ) ; }
112 int not_working () { return error ; }
114 int getBps () { return bps ; }
115 int getRate () { return rate ; }
116 int getStereo() { return stereo ; }
142 rate = SL_DEFAULT_SAMPLING_RATE ;
149 slSample () { init () ; }
151 slSample ( Uchar *buff, int leng )
154 setBuffer ( buff, leng ) ;
157 slSample ( char *fname, class slDSP *dsp = NULL )
166 if ( ref_count != 0 )
169 "slSample: FATAL ERROR - Application deleted a sample while it was playing.\n" ) ;
176 void ref () { ref_count++ ; }
177 void unRef () { ref_count-- ; }
179 int getPlayCount () { return ref_count ; }
181 char *getComment () { return comment ; }
183 void setComment ( char *nc )
186 comment = new char [ strlen ( nc ) + 1 ] ;
187 strcpy ( comment, nc ) ;
190 Uchar *getBuffer () { return buffer ; }
191 int getLength () { return length ; }
193 void autoMatch ( slDSP *dsp ) ;
195 void setBuffer ( Uchar *buff, int leng )
199 buffer = new Uchar [ leng ] ;
202 memcpy ( buffer, buff, leng ) ;
207 /* These routines only set flags - use changeXXX () to convert a sound */
209 void setRate ( int r ) { rate = r ; }
210 void setBps ( int b ) { bps = b ; }
211 void setStereo ( int s ) { stereo = s ; }
213 int getRate () { return rate ; }
214 int getBps () { return bps ; }
215 int getStereo () { return stereo ; }
217 float getDuration () { return (float) getLength() /
218 (float) ( (getStereo()?2.0f:1.0f)*
219 (getBps()/8.0f)*getRate() ) ; }
221 int loadFile ( char *fname ) ;
223 int loadRawFile ( char *fname ) ;
224 int loadAUFile ( char *fname ) ;
225 int loadWavFile ( char *fname ) ;
227 void changeRate ( int r ) ;
228 void changeBps ( int b ) ;
229 void changeStereo ( int s ) ;
230 void changeToUnsigned () ;
232 void adjustVolume ( float vol ) ;
234 void print ( FILE *fd )
236 if ( buffer == NULL )
238 fprintf ( fd, "Empty sample buffer\n" ) ;
242 fprintf ( fd, "\"%s\"\n",(getComment() == NULL ||
243 getComment()[0]=='\0') ? "Sample" : comment ) ;
244 fprintf ( fd, "%s, %d bits per sample.\n",
245 getStereo() ? "Stereo" : "Mono", getBps() ) ;
246 fprintf ( fd, "%gKHz sample rate.\n", (float) getRate() / 1000.0f ) ;
247 fprintf ( fd, "%d bytes of samples == %g seconds duration.\n", getLength(), getDuration() ) ;
255 SL_SAMPLE_WAITING, /* Sound hasn't started playing yet */
256 SL_SAMPLE_RUNNING, /* Sound has started playing */
257 SL_SAMPLE_DONE , /* Sound is complete */
258 SL_SAMPLE_PAUSED /* Sound hasn't started playing yet */
264 SL_SAMPLE_CONTINUE, /* Don't allow yourself to be preempted */
265 SL_SAMPLE_ABORT , /* Abort playing the sound when preempted */
266 SL_SAMPLE_RESTART , /* Restart the sound when load permits */
267 SL_SAMPLE_MUTE , /* Continue silently until load permits */
268 SL_SAMPLE_DELAY /* Pause until load permits */
274 SL_SAMPLE_LOOP, /* Loop sound so that it plays forever */
275 SL_SAMPLE_ONE_SHOT /* Play sound just once */
280 SL_EVENT_COMPLETE, /* Sound finished playing */
281 SL_EVENT_LOOPED, /* Sound looped back to the start */
282 SL_EVENT_PREEMPTED /* Sound was preempted */
285 typedef void (*slCallBack) ( slSample *, slEvent, int ) ;
289 public: /* SJB TESTING! */
296 slReplayMode replay_mode ;
298 int getStepDelta ( float *_time, float *delta ) ;
302 slEnvelope ( int _nsteps, slReplayMode _rm, float *_times, float *_values )
306 time = new float [ nsteps ] ;
307 value = new float [ nsteps ] ;
308 memcpy ( time , _times , sizeof(float) * nsteps ) ;
309 memcpy ( value, _values, sizeof(float) * nsteps ) ;
315 slEnvelope ( int _nsteps = 1, slReplayMode _rm = SL_SAMPLE_ONE_SHOT )
319 time = new float [ nsteps ] ;
320 value = new float [ nsteps ] ;
322 for ( int i = 0 ; i < nsteps ; i++ )
323 time [ i ] = value [ i ] = 0.0 ;
330 if ( ref_count != 0 )
333 "slEnvelope: FATAL ERROR - Application deleted an envelope while it was playing.\n" ) ;
341 void ref () { ref_count++ ; }
342 void unRef () { ref_count-- ; }
344 int getPlayCount () { return ref_count ; }
346 void setStep ( int n, float _time, float _value )
348 if ( n >= 0 && n < nsteps )
351 value [ n ] = _value ;
355 float getStepValue ( int s ) { return value [ s ] ; }
356 float getStepTime ( int s ) { return time [ s ] ; }
358 int getNumSteps () { return nsteps ; }
360 float getValue ( float _time ) ;
362 void applyToPitch ( Uchar *dst, slSamplePlayer *src, int nframes, int start, int next_env ) ;
363 void applyToInvPitch ( Uchar *dst, slSamplePlayer *src, int nframes, int start, int next_env ) ;
364 void applyToVolume ( Uchar *dst, Uchar *src, int nframes, int start ) ;
365 void applyToInvVolume ( Uchar *dst, Uchar *src, int nframes, int start ) ;
368 #define SL_MAX_PRIORITY 16
369 #define SL_MAX_SAMPLES 16
370 #define SL_MAX_CALLBACKS (SL_MAX_SAMPLES * 2)
371 #define SL_MAX_ENVELOPES 4
375 SL_PITCH_ENVELOPE , SL_INVERSE_PITCH_ENVELOPE ,
376 SL_VOLUME_ENVELOPE, SL_INVERSE_VOLUME_ENVELOPE,
377 SL_FILTER_ENVELOPE, SL_INVERSE_FILTER_ENVELOPE,
378 SL_PAN_ENVELOPE , SL_INVERSE_PAN_ENVELOPE ,
379 SL_ECHO_ENVELOPE , SL_INVERSE_ECHO_ENVELOPE ,
384 struct slPendingCallBack
386 slCallBack callback ;
394 int lengthRemaining ; /* Sample frames remaining until repeat */
395 Uchar *bufferPos ; /* Sample frame to replay next */
398 slEnvelope *env [ SL_MAX_ENVELOPES ] ;
399 slEnvelopeType env_type [ SL_MAX_ENVELOPES ] ;
400 int env_start_time [ SL_MAX_ENVELOPES ] ;
402 slReplayMode replay_mode ;
403 slPreemptMode preempt_mode ;
404 slSampleStatus status ;
407 slCallBack callback ;
410 void low_read ( int nframes, Uchar *dest ) ;
414 slSamplePlayer ( slSample *s, slReplayMode rp_mode = SL_SAMPLE_ONE_SHOT,
415 int pri = 0, slPreemptMode pr_mode = SL_SAMPLE_DELAY,
416 int _magic = 0, slCallBack cb = NULL )
422 for ( int i = 0 ; i < SL_MAX_ENVELOPES ; i++ )
425 env_type [ i ] = SL_NULL_ENVELOPE ;
428 if ( sample ) sample -> ref () ;
432 replay_mode = rp_mode ;
433 preempt_mode = pr_mode ;
439 slPreemptMode getPreemptMode () { return preempt_mode ; }
443 return ( isRunning() &&
444 preempt_mode == SL_SAMPLE_CONTINUE ) ? (SL_MAX_PRIORITY+1) :
448 int preempt ( int delay ) ;
450 void addEnvelope ( int i, slEnvelope *_env, slEnvelopeType _type ) ;
454 if ( status != SL_SAMPLE_DONE )
455 status = SL_SAMPLE_PAUSED ;
460 if ( status == SL_SAMPLE_PAUSED )
461 status = SL_SAMPLE_RUNNING ;
466 status = SL_SAMPLE_WAITING ;
467 lengthRemaining = sample->getLength () ;
468 bufferPos = sample->getBuffer () ;
473 status = SL_SAMPLE_RUNNING ;
474 lengthRemaining = sample->getLength () ;
475 bufferPos = sample->getBuffer () ;
480 status = SL_SAMPLE_DONE ;
481 lengthRemaining = 0 ;
485 int getMagic () { return magic ; }
486 slSample *getSample () { return sample ; }
488 int isWaiting () { return status == SL_SAMPLE_WAITING ; }
489 int isPaused () { return status == SL_SAMPLE_PAUSED ; }
490 int isRunning () { return status == SL_SAMPLE_RUNNING ; }
491 int isDone () { return status == SL_SAMPLE_DONE ; }
493 void skip ( int nframes ) ;
494 void read ( int nframes, Uchar *dest, int next_env = 0 ) ;
498 class slScheduler : public slDSP
500 slPendingCallBack pending_callback [ SL_MAX_CALLBACKS ] ;
501 int num_pending_callbacks ;
503 float safety_margin ;
505 int mixer_buffer_size ;
507 Uchar *mixer_buffer ;
508 Uchar *spare_buffer0 ;
509 Uchar *spare_buffer1 ;
510 Uchar *spare_buffer2 ;
515 slSamplePlayer *samplePlayer [ SL_MAX_SAMPLES ] ;
519 void mixBuffer ( slSamplePlayer *a,
520 slSamplePlayer *b ) ;
522 void mixBuffer ( slSamplePlayer *a,
524 slSamplePlayer *c ) ;
526 Uchar mix ( Uchar a, Uchar b )
528 register int r = a + b - 0x80 ;
529 return ( r > 255 ) ? 255 :
533 Uchar mix ( Uchar a, Uchar b, Uchar c )
535 register int r = a + b + c - 0x80 - 0x80 ;
536 return ( r > 255 ) ? 255 :
540 void realUpdate ( int dump_first = SL_FALSE ) ;
542 void initBuffers () ;
546 static slScheduler *current ;
550 slScheduler ( int _rate = SL_DEFAULT_SAMPLING_RATE ) : slDSP ( _rate, SL_FALSE, 8 ) { init () ; }
551 slScheduler ( char *device,
552 int _rate = SL_DEFAULT_SAMPLING_RATE ) : slDSP ( device, _rate, SL_FALSE, 8 ) { init () ; }
555 static slScheduler *getCurrent () { return current ; }
557 int getTimeNow () { return now ; }
558 float getElapsedTime ( int then ) { return (float)(now-then)/(float)getRate() ; }
560 void flushCallBacks () ;
561 void addCallBack ( slCallBack c, slSample *s, slEvent e, int m ) ;
563 void update () { realUpdate ( SL_FALSE ) ; }
564 void dumpUpdate () { realUpdate ( SL_TRUE ) ; }
566 void addSampleEnvelope ( slSample *s = NULL, int magic = 0,
567 int slot = 1, slEnvelope *e = NULL,
568 slEnvelopeType t = SL_VOLUME_ENVELOPE )
570 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
571 if ( samplePlayer [ i ] != NULL &&
572 ( s == NULL || samplePlayer [ i ] -> getSample () == s ) &&
573 ( magic == 0 || samplePlayer [ i ] -> getMagic () == magic ) )
574 samplePlayer [ i ] -> addEnvelope ( slot, e, t ) ;
577 void resumeSample ( slSample *s = NULL, int magic = 0 )
579 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
580 if ( samplePlayer [ i ] != NULL &&
581 ( s == NULL || samplePlayer [ i ] -> getSample () == s ) &&
582 ( magic == 0 || samplePlayer [ i ] -> getMagic () == magic ) )
583 samplePlayer [ i ] -> resume () ;
586 void pauseSample ( slSample *s = NULL, int magic = 0 )
588 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
589 if ( samplePlayer [ i ] != NULL &&
590 ( s == NULL || samplePlayer [ i ] -> getSample () == s ) &&
591 ( magic == 0 || samplePlayer [ i ] -> getMagic () == magic ) )
592 samplePlayer [ i ] -> pause () ;
595 void stopSample ( slSample *s = NULL, int magic = 0 )
597 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
598 if ( samplePlayer [ i ] != NULL &&
599 ( s == NULL || samplePlayer [ i ] -> getSample () == s ) &&
600 ( magic == 0 || samplePlayer [ i ] -> getMagic () == magic ) )
601 samplePlayer [ i ] -> stop () ;
604 int loopSample ( slSample *s, int pri = 0, slPreemptMode mode = SL_SAMPLE_MUTE, int magic = 0, slCallBack cb = NULL )
606 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
607 if ( samplePlayer [ i ] == NULL )
609 samplePlayer [ i ] = new slSamplePlayer ( s, SL_SAMPLE_LOOP, pri, mode, magic, cb ) ;
616 int playSample ( slSample *s, int pri = 1, slPreemptMode mode = SL_SAMPLE_ABORT, int magic = 0, slCallBack cb = NULL )
618 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
619 if ( samplePlayer [ i ] == NULL )
621 samplePlayer [ i ] = new slSamplePlayer ( s, SL_SAMPLE_ONE_SHOT, pri, mode, magic, cb ) ;
628 void setSafetyMargin ( float seconds ) { safety_margin = seconds ; }