]> git.mxchange.org Git - flightgear.git/blob - src/slSamplePlayer.cxx
fgPointLine() rewritten into fgPointLineSquared() ... this ultimately saves
[flightgear.git] / src / slSamplePlayer.cxx
1
2 #include "sl.h"
3
4 void slSamplePlayer::addEnvelope ( int i, slEnvelope *_env, slEnvelopeType _type )
5 {
6   if ( i < 0 || i >= SL_MAX_ENVELOPES ) return ;
7
8   if ( env [ i ] != NULL )
9     env [ i ] -> unRef () ;
10
11   env [ i ] = _env ;
12
13   if ( _env != NULL )
14     env [ i ] -> ref () ;
15
16   env_type [ i ] = _type ;
17   env_start_time [ i ] = slScheduler::getCurrent() -> getTimeNow () ;
18 }
19
20 int slSamplePlayer::preempt ( int delay )
21 {
22   slScheduler::getCurrent() -> addCallBack ( callback, sample, SL_EVENT_PREEMPTED, magic ) ;
23
24   switch ( preempt_mode )
25   {
26     case SL_SAMPLE_CONTINUE: if ( isRunning() )
27                                return SL_FALSE ;
28                              /* FALLTHROUGH! */
29     case SL_SAMPLE_DELAY   :                   break ;
30     case SL_SAMPLE_MUTE    : skip  ( delay ) ; break ;
31     case SL_SAMPLE_ABORT   : stop  ()        ; break ;
32     case SL_SAMPLE_RESTART : reset ()        ; break ;
33   }
34
35   return SL_TRUE ;
36 }
37
38 slSamplePlayer::~slSamplePlayer ()
39 {
40   if ( sample )
41     sample -> unRef () ;
42
43   slScheduler::getCurrent() -> addCallBack ( callback, sample, SL_EVENT_COMPLETE, magic ) ;
44 }
45
46 void slSamplePlayer::skip ( int nframes )
47 {
48   if ( nframes < lengthRemaining )
49   {
50     lengthRemaining -= nframes ;
51     bufferPos       += nframes ;
52   }
53   else 
54   if ( replay_mode == SL_SAMPLE_LOOP )
55   {
56     slScheduler::getCurrent() -> addCallBack ( callback, sample, SL_EVENT_LOOPED, magic ) ;
57
58     nframes -= lengthRemaining ;
59
60     while ( nframes >= sample->getLength () )
61       nframes -= sample->getLength () ;
62
63     lengthRemaining = sample->getLength() - nframes ;
64     bufferPos = & ( sample->getBuffer() [ nframes ] ) ;
65   }
66   else
67     stop () ;
68 }
69
70
71 Uchar *slSamplePlayer::read ( int nframes, Uchar *spare1, Uchar *spare2 )
72 {
73   if ( isWaiting() ) start () ;
74
75   if ( nframes > lengthRemaining ) /* This is an error */
76   {
77     fprintf ( stderr, "slSamplePlayer: FATAL ERROR - Mixer Requested too much data.\n" ) ;
78     abort () ;
79   }
80
81   Uchar *src = bufferPos ;
82   Uchar *dst = spare1 ;
83
84   for ( int i = 0 ; i < SL_MAX_ENVELOPES ; i++ )
85   {
86     if ( env[i] )
87     {
88       switch ( env_type [ i ] )
89       {
90         case SL_INVERSE_PITCH_ENVELOPE :
91         case SL_PITCH_ENVELOPE  :
92           memcpy ( dst, src, nframes ) /* Tricky! */ ;
93           break ;
94
95         case SL_INVERSE_VOLUME_ENVELOPE:
96           env[i]->applyToInvVolume ( dst,src,nframes,env_start_time[i] ) ;
97           break ;
98
99         case SL_VOLUME_ENVELOPE :
100           env[i]->applyToVolume ( dst,src,nframes,env_start_time[i] ) ;
101           break ;
102
103         case SL_INVERSE_FILTER_ENVELOPE:
104         case SL_FILTER_ENVELOPE :
105           memcpy ( dst, src, nframes ) /* Tricky! */ ;
106           break ;
107
108         case SL_INVERSE_PAN_ENVELOPE   :
109         case SL_PAN_ENVELOPE    :
110           memcpy ( dst, src, nframes ) /* Tricky! */ ;
111           break ;
112
113         case SL_INVERSE_ECHO_ENVELOPE  :
114         case SL_ECHO_ENVELOPE   :
115           memcpy ( dst, src, nframes ) /* Tricky! */ ;
116           break ;
117       }
118
119       if ( dst == spare1 )
120       {
121         src = spare1 ;
122         dst = spare2 ;
123       }
124       else
125       {
126         dst = spare1 ;
127         src = spare2 ;
128       }
129     }
130   }
131  
132   if ( nframes < lengthRemaining ) /* Less data than there is left...  */
133   {
134     lengthRemaining -= nframes ;
135     bufferPos       += nframes ;
136   }
137   else  /* Read it all */
138   {
139     if ( replay_mode == SL_SAMPLE_ONE_SHOT )
140       stop () ;
141     else
142     {
143       slScheduler::getCurrent() -> addCallBack ( callback, sample, SL_EVENT_LOOPED, magic ) ;
144       start () ;
145     }
146   }
147
148   return src ;
149 }
150