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 ;
33 extern char *__slPendingError ;
47 audio_info_t ainfo; // ioctl structure
48 audio_offset_t audio_offset; // offset in audiostream
49 long counter; // counter-written packets
50 #elif defined(SL_USING_OSS_AUDIO)
51 audio_buf_info buff_info ;
53 ALconfig config; // configuration stuff
54 ALport port; // .. we are here
59 int ioctl ( int cmd, int param = 0 )
61 if ( error ) return param ;
63 if ( ::ioctl ( fd, cmd, & param ) == -1 )
65 perror ( "slDSP: ioctl" ) ;
74 HWAVEOUT hWaveOut; // device handle
75 WAVEFORMATEX Format; // open needs this
77 WAVEHDR wavehdr[ 3 ]; // for round robin ..
78 int curr_header; // index of actual wavehdr
79 long counter; // counter-written packets
83 void open ( char *device, int _rate, int _stereo, int _bps ) ;
85 void getBufferInfo () ;
86 void write ( void *buffer, size_t length ) ;
90 void setError () { error = SL_TRUE ; }
91 int getDriverBufferSize () ;
95 slDSP ( int _rate = SL_DEFAULT_SAMPLING_RATE,
96 int _stereo = SL_FALSE, int _bps = 8 )
98 open ( SLDSP_DEFAULT_DEVICE, _rate, _stereo, _bps ) ;
101 slDSP ( char *device, int _rate = SL_DEFAULT_SAMPLING_RATE,
102 int _stereo = SL_FALSE, int _bps = 8 )
104 open ( device, _rate, _stereo, _bps ) ;
107 ~slDSP () { close () ; }
109 float secondsRemaining () ;
110 float secondsUsed () ;
112 void play ( void *buffer, size_t length ) { write ( buffer, length ) ; }
114 int working () { return !error ; }
115 int not_working () { return error ; }
117 int getBps () { return bps ; }
118 int getRate () { return rate ; }
119 int getStereo() { return stereo ; }
145 rate = SL_DEFAULT_SAMPLING_RATE ;
152 slSample () { init () ; }
154 slSample ( Uchar *buff, int leng )
157 setBuffer ( buff, leng ) ;
160 slSample ( char *fname, class slDSP *dsp = NULL )
169 if ( ref_count != 0 && __slPendingError == NULL )
171 "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 && __slPendingError == NULL )
332 "slEnvelope: FATAL ERROR - Application deleted an envelope while it was playing.\n" ;
338 void ref () { ref_count++ ; }
339 void unRef () { ref_count-- ; }
341 int getPlayCount () { return ref_count ; }
343 void setStep ( int n, float _time, float _value )
345 if ( n >= 0 && n < nsteps )
348 value [ n ] = _value ;
352 float getStepValue ( int s ) { return value [ s ] ; }
353 float getStepTime ( int s ) { return time [ s ] ; }
355 int getNumSteps () { return nsteps ; }
357 float getValue ( float _time ) ;
359 void applyToPitch ( Uchar *dst, slSamplePlayer *src, int nframes, int start, int next_env ) ;
360 void applyToInvPitch ( Uchar *dst, slSamplePlayer *src, int nframes, int start, int next_env ) ;
361 void applyToVolume ( Uchar *dst, Uchar *src, int nframes, int start ) ;
362 void applyToInvVolume ( Uchar *dst, Uchar *src, int nframes, int start ) ;
365 #define SL_MAX_PRIORITY 16
366 #define SL_MAX_SAMPLES 16
367 #define SL_MAX_CALLBACKS (SL_MAX_SAMPLES * 2)
368 #define SL_MAX_ENVELOPES 4
372 SL_PITCH_ENVELOPE , SL_INVERSE_PITCH_ENVELOPE ,
373 SL_VOLUME_ENVELOPE, SL_INVERSE_VOLUME_ENVELOPE,
374 SL_FILTER_ENVELOPE, SL_INVERSE_FILTER_ENVELOPE,
375 SL_PAN_ENVELOPE , SL_INVERSE_PAN_ENVELOPE ,
376 SL_ECHO_ENVELOPE , SL_INVERSE_ECHO_ENVELOPE ,
381 struct slPendingCallBack
383 slCallBack callback ;
391 int lengthRemaining ; /* Sample frames remaining until repeat */
392 Uchar *bufferPos ; /* Sample frame to replay next */
395 slEnvelope *env [ SL_MAX_ENVELOPES ] ;
396 slEnvelopeType env_type [ SL_MAX_ENVELOPES ] ;
397 int env_start_time [ SL_MAX_ENVELOPES ] ;
399 slReplayMode replay_mode ;
400 slPreemptMode preempt_mode ;
401 slSampleStatus status ;
404 slCallBack callback ;
407 void low_read ( int nframes, Uchar *dest ) ;
411 slSamplePlayer ( slSample *s, slReplayMode rp_mode = SL_SAMPLE_ONE_SHOT,
412 int pri = 0, slPreemptMode pr_mode = SL_SAMPLE_DELAY,
413 int _magic = 0, slCallBack cb = NULL )
419 for ( int i = 0 ; i < SL_MAX_ENVELOPES ; i++ )
422 env_type [ i ] = SL_NULL_ENVELOPE ;
425 if ( sample ) sample -> ref () ;
429 replay_mode = rp_mode ;
430 preempt_mode = pr_mode ;
436 slPreemptMode getPreemptMode () { return preempt_mode ; }
440 return ( isRunning() &&
441 preempt_mode == SL_SAMPLE_CONTINUE ) ? (SL_MAX_PRIORITY+1) :
445 int preempt ( int delay ) ;
447 void addEnvelope ( int i, slEnvelope *_env, slEnvelopeType _type ) ;
451 if ( status != SL_SAMPLE_DONE )
452 status = SL_SAMPLE_PAUSED ;
457 if ( status == SL_SAMPLE_PAUSED )
458 status = SL_SAMPLE_RUNNING ;
463 status = SL_SAMPLE_WAITING ;
464 lengthRemaining = sample->getLength () ;
465 bufferPos = sample->getBuffer () ;
470 status = SL_SAMPLE_RUNNING ;
471 lengthRemaining = sample->getLength () ;
472 bufferPos = sample->getBuffer () ;
477 status = SL_SAMPLE_DONE ;
478 lengthRemaining = 0 ;
482 int getMagic () { return magic ; }
483 slSample *getSample () { return sample ; }
485 int isWaiting () { return status == SL_SAMPLE_WAITING ; }
486 int isPaused () { return status == SL_SAMPLE_PAUSED ; }
487 int isRunning () { return status == SL_SAMPLE_RUNNING ; }
488 int isDone () { return status == SL_SAMPLE_DONE ; }
490 void skip ( int nframes ) ;
491 void read ( int nframes, Uchar *dest, int next_env = 0 ) ;
495 class slScheduler : public slDSP
497 slPendingCallBack pending_callback [ SL_MAX_CALLBACKS ] ;
498 int num_pending_callbacks ;
500 float safety_margin ;
502 int mixer_buffer_size ;
504 Uchar *mixer_buffer ;
505 Uchar *spare_buffer0 ;
506 Uchar *spare_buffer1 ;
507 Uchar *spare_buffer2 ;
512 slSamplePlayer *samplePlayer [ SL_MAX_SAMPLES ] ;
516 void mixBuffer ( slSamplePlayer *a,
517 slSamplePlayer *b ) ;
519 void mixBuffer ( slSamplePlayer *a,
521 slSamplePlayer *c ) ;
523 Uchar mix ( Uchar a, Uchar b )
525 register int r = a + b - 0x80 ;
526 return ( r > 255 ) ? 255 :
530 Uchar mix ( Uchar a, Uchar b, Uchar c )
532 register int r = a + b + c - 0x80 - 0x80 ;
533 return ( r > 255 ) ? 255 :
537 void realUpdate ( int dump_first = SL_FALSE ) ;
539 void initBuffers () ;
543 static slScheduler *current ;
547 slScheduler ( int _rate = SL_DEFAULT_SAMPLING_RATE ) : slDSP ( _rate, SL_FALSE, 8 ) { init () ; }
548 slScheduler ( char *device,
549 int _rate = SL_DEFAULT_SAMPLING_RATE ) : slDSP ( device, _rate, SL_FALSE, 8 ) { init () ; }
552 static slScheduler *getCurrent () { return current ; }
554 int getTimeNow () { return now ; }
555 float getElapsedTime ( int then ) { return (float)(now-then)/(float)getRate() ; }
557 void flushCallBacks () ;
558 void addCallBack ( slCallBack c, slSample *s, slEvent e, int m ) ;
560 void update () { realUpdate ( SL_FALSE ) ; }
561 void dumpUpdate () { realUpdate ( SL_TRUE ) ; }
563 void addSampleEnvelope ( slSample *s = NULL, int magic = 0,
564 int slot = 1, slEnvelope *e = NULL,
565 slEnvelopeType t = SL_VOLUME_ENVELOPE )
567 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
568 if ( samplePlayer [ i ] != NULL &&
569 ( s == NULL || samplePlayer [ i ] -> getSample () == s ) &&
570 ( magic == 0 || samplePlayer [ i ] -> getMagic () == magic ) )
571 samplePlayer [ i ] -> addEnvelope ( slot, e, t ) ;
574 void resumeSample ( slSample *s = NULL, int magic = 0 )
576 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
577 if ( samplePlayer [ i ] != NULL &&
578 ( s == NULL || samplePlayer [ i ] -> getSample () == s ) &&
579 ( magic == 0 || samplePlayer [ i ] -> getMagic () == magic ) )
580 samplePlayer [ i ] -> resume () ;
583 void pauseSample ( slSample *s = NULL, int magic = 0 )
585 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
586 if ( samplePlayer [ i ] != NULL &&
587 ( s == NULL || samplePlayer [ i ] -> getSample () == s ) &&
588 ( magic == 0 || samplePlayer [ i ] -> getMagic () == magic ) )
589 samplePlayer [ i ] -> pause () ;
592 void stopSample ( slSample *s = NULL, int magic = 0 )
594 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
595 if ( samplePlayer [ i ] != NULL &&
596 ( s == NULL || samplePlayer [ i ] -> getSample () == s ) &&
597 ( magic == 0 || samplePlayer [ i ] -> getMagic () == magic ) )
598 samplePlayer [ i ] -> stop () ;
601 int loopSample ( slSample *s, int pri = 0, slPreemptMode mode = SL_SAMPLE_MUTE, int magic = 0, slCallBack cb = NULL )
603 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
604 if ( samplePlayer [ i ] == NULL )
606 samplePlayer [ i ] = new slSamplePlayer ( s, SL_SAMPLE_LOOP, pri, mode, magic, cb ) ;
613 int playSample ( slSample *s, int pri = 1, slPreemptMode mode = SL_SAMPLE_ABORT, int magic = 0, slCallBack cb = NULL )
615 for ( int i = 0 ; i < SL_MAX_SAMPLES ; i++ )
616 if ( samplePlayer [ i ] == NULL )
618 samplePlayer [ i ] = new slSamplePlayer ( s, SL_SAMPLE_ONE_SHOT, pri, mode, magic, cb ) ;
625 void setSafetyMargin ( float seconds ) { safety_margin = seconds ; }