Debian Codegraph @

mednafen/sound/Blip_Buffer.cpp

raw symbols SCIP
1 // Blip_Buffer 0.4.1. http://www.slack.net/~ant/cxx . . $ `<file>/mednafen/sound/Blip_Buffer.cpp`/
2
3 #include "../include/blip/Blip_Buffer.h"cxx . . $ `<file>/mednafen/include/blip/Blip_Buffer.h`/
4
5 #include <assert.h>
6 #include <climits>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <math.h>
10
11 /* Copyright (C) 2003-2006 Shay Green. This module is free software; you
12 can redistribute it and/or modify it under the terms of the GNU Lesser
13 General Public License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version. This
15 module is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 details. You should have received a copy of the GNU Lesser General Public
19 License along with this module; if not, write to the Free Software Foundation,
20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
21
22 #ifdef BLARGG_ENABLE_OPTIMIZER
23 #include BLARGG_ENABLE_OPTIMIZER
24 #endif
25
26 Blip_Buffercxx . . $ Blip_Buffer#::Blip_Buffercxx . . $ Blip_Buffer#Blip_Buffer(49f6e7a06ebc5aa8).()
27 {
28 factor_cxx . . $ Blip_Buffer#factor_. = (blip_u64cxx . . $ blip_u64#)ULLONG_MAXcxx . . $ `<external>/13013819803669524346/limits.h:138:11`!;
29 offset_cxx . . $ Blip_Buffer#offset_. = 0;
30 buffer_cxx . . $ Blip_Buffer#buffer_. = 0;
31 buffer_size_cxx . . $ Blip_Buffer#buffer_size_. = 0;
32 sample_rate_cxx . . $ Blip_Buffer#sample_rate_. = 0;
33 reader_accum_cxx . . $ Blip_Buffer#reader_accum_. = 0;
34 bass_shift_cxx . . $ Blip_Buffer#bass_shift_. = 0;
35 clock_rate_cxx . . $ Blip_Buffer#clock_rate_. = 0;
36 bass_freq_cxx . . $ Blip_Buffer#bass_freq_. = 16;
37 length_cxx . . $ Blip_Buffer#length_. = 0;
38 }
39
40 Blip_Buffercxx . . $ Blip_Buffer#::~cxx . . $ Blip_Buffer#`~Blip_Buffer`(ced63f7c635d850d).Blip_Buffercxx . . $ Blip_Buffer#()
41 {
42 if (buffer_cxx . . $ Blip_Buffer#buffer_.)
43 freecxx . . $ free(bd21765a0afc8e3c).( buffer_cxx . . $ Blip_Buffer#buffer_. );
44 }
45
46 void Blip_Buffercxx . . $ Blip_Buffer#::clearcxx . . $ Blip_Buffer#clear(d4f767463ce0a6b3).( int entire_buffer )
47 {
48 offset_cxx . . $ Blip_Buffer#offset_. = 0;
49 reader_accum_cxx . . $ Blip_Buffer#reader_accum_. = 0;
50 modified_cxx . . $ Blip_Buffer#modified_. = 0;
51 if ( buffer_cxx . . $ Blip_Buffer#buffer_. )
52 {
53 long count = (entire_buffer ? buffer_size_cxx . . $ Blip_Buffer#buffer_size_. : samples_availcxx . . $ Blip_Buffer#samples_avail(57a65ae535d7db84).());
54 memset( buffer_cxx . . $ Blip_Buffer#buffer_., 0, (count + blip_buffer_extra_cxx . . $ blip_buffer_extra_.) * sizeof (buf_t_cxx . . $ Blip_Buffer#buf_t_#) );
55 }
56 }
57
58 Blip_Buffercxx . . $ Blip_Buffer#::blargg_err_tcxx . . $ Blip_Buffer#blargg_err_t# Blip_Buffercxx . . $ Blip_Buffer#::set_sample_ratecxx . . $ Blip_Buffer#set_sample_rate(ee827314cdea73de).( long new_rate, int msec )
59 {
60 // start with maximum length that resampled time can represent
61 blip_s64cxx . . $ blip_s64# new_size = (ULLONG_MAXcxx . . $ `<external>/13013819803669524346/limits.h:138:11`! >> BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:131:9`!) - blip_buffer_extra_cxx . . $ blip_buffer_extra_. - 64;
62
63 // simple safety check, since code elsewhere may not be safe for sizes approaching (2 ^ 31).
64 if(new_size > ((1LL << 30) - 1))
65 new_size = (1LL << 30) - 1;
66
67 if ( msec != blip_max_lengthcxx . . $ blip_max_length. )
68 {
69 blip_s64cxx . . $ blip_s64# s = ((blip_s64cxx . . $ blip_s64#)new_rate * (msec + 1) + 999) / 1000;
70 if ( s < new_size )
71 new_size = s;
72 else
73 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( 0 ); // fails if requested buffer length exceeds limit
74 }
75
76 if ( buffer_size_cxx . . $ Blip_Buffer#buffer_size_. != new_size )
77 {
78 void* p = realloccxx . . $ realloc(42b2a9dd8f084bf2).( buffer_cxx . . $ Blip_Buffer#buffer_., (new_size + blip_buffer_extra_cxx . . $ blip_buffer_extra_.) * sizeof *buffer_cxx . . $ Blip_Buffer#buffer_. );
79 if ( !p )
80 return "Out of memory";
81
82 buffer_cxx . . $ Blip_Buffer#buffer_. = (buf_t_cxx . . $ Blip_Buffer#buf_t_#*) p;
83 }
84
85 buffer_size_cxx . . $ Blip_Buffer#buffer_size_. = new_size;
86
87 // update things based on the sample rate
88 sample_rate_cxx . . $ Blip_Buffer#sample_rate_. = new_rate;
89 length_cxx . . $ Blip_Buffer#length_. = new_size * 1000 / new_rate - 1;
90 if ( msec )
91 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( length_ == msec ); // ensure length is same as that passed in
92 if ( clock_rate_cxx . . $ Blip_Buffer#clock_rate_. )
93 clock_ratecxx . . $ Blip_Buffer#clock_rate(1624ee8275b10bda).( clock_rate_cxx . . $ Blip_Buffer#clock_rate_. );
94 bass_freqcxx . . $ Blip_Buffer#bass_freq(d4f767463ce0a6b3).( bass_freq_cxx . . $ Blip_Buffer#bass_freq_. );
95
96 clearcxx . . $ Blip_Buffer#clear(d4f767463ce0a6b3).();
97
98 return 0; // success
99 }
100
101 blip_resampled_time_tcxx . . $ blip_resampled_time_t# Blip_Buffercxx . . $ Blip_Buffer#::clock_rate_factorcxx . . $ Blip_Buffer#clock_rate_factor(2867b430557b1c2e).( long rate ) const
102 {
103 double ratio = (double) sample_rate_cxx . . $ Blip_Buffer#sample_rate_. / rate;
104 blip_s64cxx . . $ blip_s64# factor = (blip_s64cxx . . $ blip_s64#) floorcxx . . $ floor(fee4f1d22bc8e617).( ratio * (1LL << BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:131:9`!) + 0.5 );
105 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( factor > 0 || !sample_rate_ ); // fails if clock/output ratio is too large
106 return (blip_resampled_time_tcxx . . $ Blip_Buffer#blip_resampled_time_t#) factor;
107 }
108
109 void Blip_Buffercxx . . $ Blip_Buffer#::bass_freqcxx . . $ Blip_Buffer#bass_freq(d4f767463ce0a6b3).( int freq )
110 {
111 bass_freq_cxx . . $ Blip_Buffer#bass_freq_. = freq;
112 int shift = 31;
113 if ( freq > 0 )
114 {
115 shift = 13;
116 long f = (freq << 16) / sample_rate_cxx . . $ Blip_Buffer#sample_rate_.;
117 while ( (f >>= 1) && --shift ) { }
118 }
119 bass_shift_cxx . . $ Blip_Buffer#bass_shift_. = shift;
120 }
121
122 void Blip_Buffercxx . . $ Blip_Buffer#::end_framecxx . . $ Blip_Buffer#end_frame(d4f767463ce0a6b3).( blip_time_tcxx . . $ blip_time_t# t )
123 {
124 offset_cxx . . $ Blip_Buffer#offset_. += t * factor_cxx . . $ Blip_Buffer#factor_.;
125 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( samples_avail() <= (long) buffer_size_ ); // time outside buffer length
126 }
127
128 void Blip_Buffercxx . . $ Blip_Buffer#::remove_silencecxx . . $ Blip_Buffer#remove_silence(1624ee8275b10bda).( long count )
129 {
130 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( count <= samples_avail() ); // tried to remove more samples than available
131 offset_cxx . . $ Blip_Buffer#offset_. -= (blip_resampled_time_tcxx . . $ Blip_Buffer#blip_resampled_time_t#) count << BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:131:9`!;
132 }
133
134 long Blip_Buffercxx . . $ Blip_Buffer#::count_samplescxx . . $ Blip_Buffer#count_samples(9ebf7bebeea16932).( blip_time_tcxx . . $ blip_time_t# t ) const
135 {
136 unsigned long last_sample = resampled_timecxx . . $ Blip_Buffer#resampled_time(79dc3568750208f6).( t ) >> BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:131:9`!;
137 unsigned long first_sample = offset_cxx . . $ Blip_Buffer#offset_. >> BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:131:9`!;
138 return (long) (last_sample - first_sample);
139 }
140
141 blip_time_tcxx . . $ blip_time_t# Blip_Buffercxx . . $ Blip_Buffer#::count_clockscxx . . $ Blip_Buffer#count_clocks(54963ccf8c7a5971).( long count ) const
142 {
143 if ( !factor_cxx . . $ Blip_Buffer#factor_. )
144 {
145 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( 0 ); // sample rate and clock rates must be set first
146 return 0;
147 }
148
149 if ( count > buffer_size_cxx . . $ Blip_Buffer#buffer_size_. )
150 count = buffer_size_cxx . . $ Blip_Buffer#buffer_size_.;
151 blip_resampled_time_tcxx . . $ Blip_Buffer#blip_resampled_time_t# time = (blip_resampled_time_tcxx . . $ Blip_Buffer#blip_resampled_time_t#) count << BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:131:9`!;
152 return (blip_time_tcxx . . $ blip_time_t#) ((time - offset_cxx . . $ Blip_Buffer#offset_. + factor_cxx . . $ Blip_Buffer#factor_. - 1) / factor_cxx . . $ Blip_Buffer#factor_.);
153 }
154
155 void Blip_Buffercxx . . $ Blip_Buffer#::remove_samplescxx . . $ Blip_Buffer#remove_samples(1624ee8275b10bda).( long count )
156 {
157 if ( count )
158 {
159 remove_silencecxx . . $ Blip_Buffer#remove_silence(1624ee8275b10bda).( count );
160
161 // copy remaining samples to beginning and clear old samples
162 long remain = samples_availcxx . . $ Blip_Buffer#samples_avail(57a65ae535d7db84).() + blip_buffer_extra_cxx . . $ blip_buffer_extra_.;
163 memmove( buffer_cxx . . $ Blip_Buffer#buffer_., buffer_cxx . . $ Blip_Buffer#buffer_. + count, remain * sizeof *buffer_cxx . . $ Blip_Buffer#buffer_. );
164 memset( buffer_cxx . . $ Blip_Buffer#buffer_. + remain, 0, count * sizeof *buffer_cxx . . $ Blip_Buffer#buffer_. );
165 }
166 }
167
168 // Blip_Synth_
169
170 Blip_Synth_Fast_cxx . . $ Blip_Synth_Fast_#::Blip_Synth_Fast_cxx . . $ Blip_Synth_Fast_#Blip_Synth_Fast_(49f6e7a06ebc5aa8).()
171 {
172 bufcxx . . $ Blip_Synth_Fast_#buf. = 0;
173 last_ampcxx . . $ Blip_Synth_Fast_#last_amp. = 0;
174 delta_factorcxx . . $ Blip_Synth_Fast_#delta_factor. = 0;
175 }
176
177 void Blip_Synth_Fast_cxx . . $ Blip_Synth_Fast_#::volume_unitcxx . . $ Blip_Synth_Fast_#volume_unit(ba8836b59fb42830).( double new_unit )
178 {
179 delta_factorcxx . . $ Blip_Synth_Fast_#delta_factor. = int (new_unit * (1L << blip_sample_bitscxx . . $ blip_sample_bits.) + 0.5);
180 }
181
182 long Blip_Buffercxx . . $ Blip_Buffer#::read_samplescxx . . $ Blip_Buffer#read_samples(44cf187bb4d07f06).( blip_sample_tcxx . . $ blip_sample_t#* BLIP_RESTRICTcxx . . $ `mednafen/include/blip/Blip_Buffer.h:256:9`! out, long max_samples, int stereo )
183 {
184 long count = samples_availcxx . . $ Blip_Buffer#samples_avail(57a65ae535d7db84).();
185 if ( count > max_samples )
186 count = max_samples;
187
188 if ( count )
189 {
190 int const bass = BLIP_READER_BASScxx . . $ `mednafen/include/blip/Blip_Buffer.h:266:9`!( *this );
191 BLIP_READER_BEGINcxx . . $ `mednafen/include/blip/Blip_Buffer.h:261:9`!( reader, *this );
192
193 #ifndef WANT_STEREO_SOUNDcxx . . $ `<built-in>:496:9`!
194 if ( !stereo )
195 {
196 for ( blip_long n = count; n; --n )
197 {
198 blip_long s = BLIP_READER_READ( reader );
199 if ( (blip_sample_t) s != s )
200 s = 0x7FFF - (s >> 24);
201 *out++ = (blip_sample_t) s;
202 BLIP_READER_NEXT( reader, bass );
203 }
204 }
205 else
206 #endif
207 {
208 for ( blip_longcxx . . $ blip_long# n = count; n; --n )
209 {
210 blip_longcxx . . $ blip_long# s = BLIP_READER_READcxx . . $ `mednafen/include/blip/Blip_Buffer.h:273:9`!( reader );
211 if ( (blip_sample_tcxx . . $ blip_sample_t#) s != s )
212 s = 0x7FFF - (s >> 24);
213 *out = (blip_sample_tcxx . . $ blip_sample_t#) s;
214 out += 2;
215 BLIP_READER_NEXTcxx . . $ `mednafen/include/blip/Blip_Buffer.h:279:9`!( reader, bass );
216 }
217 }
218 BLIP_READER_ENDcxx . . $ `mednafen/include/blip/Blip_Buffer.h:284:9`!( reader, *this );
219
220 remove_samplescxx . . $ Blip_Buffer#remove_samples(1624ee8275b10bda).( count );
221 }
222 return count;
223 }