Debian Codegraph @

mednafen/sound/Blip_Buffer.c

raw symbols SCIP
1 // Blip_Buffer 0.4.1. http://www.slack.net/~ant/
2
3 #include "../include/blip/Blip_Buffer.h"
4
5 #include <limits.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <math.h>
9
10 #ifndef ULLONG_MAX
11 #define ULLONG_MAX 18446744073709551615ULL
12 #endif
13
14 /* Copyright (C) 2003-2006 Shay Green. This module is free software; you
15 can redistribute it and/or modify it under the terms of the GNU Lesser
16 General Public License as published by the Free Software Foundation; either
17 version 2.1 of the License, or (at your option) any later version. This
18 module is distributed in the hope that it will be useful, but WITHOUT ANY
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 details. You should have received a copy of the GNU Lesser General Public
22 License along with this module; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
24
25 #ifdef BLARGG_ENABLE_OPTIMIZER
26 #include BLARGG_ENABLE_OPTIMIZER
27 #endif
28
29 void Blip_Buffer_init(Blip_Buffer* bbuf)
30 {
31 bbuf->factor = (blip_u64)ULLONG_MAX;
32 bbuf->offset = 0;
33 bbuf->buffer = 0;
34 bbuf->buffer_size = 0;
35 bbuf->sample_rate = 0;
36 bbuf->reader_accum = 0;
37 bbuf->bass_shift = 0;
38 bbuf->clock_rate = 0;
39 bbuf->bass_freq = 16;
40 bbuf->length = 0;
41 }
42
43 void Blip_Buffer_deinit(Blip_Buffer* bbuf)
44 {
45 if (bbuf->buffer)
46 free(bbuf->buffer);
47 }
48
49 void Blip_Buffer_clear(Blip_Buffer* bbuf, int entire_buffer)
50 {
51 bbuf->offset = 0;
52 bbuf->reader_accum = 0;
53 bbuf->modified = 0;
54 if (bbuf->buffer)
55 {
56 long count = (entire_buffer ? bbuf->buffer_size : Blip_Buffer_samples_avail(bbuf));
57 memset(bbuf->buffer, 0, (count + blip_buffer_extra_) * sizeof(blip_buf_t_));
58 }
59 }
60
61 blargg_err_t Blip_Buffer_set_sample_rate(Blip_Buffer* bbuf, long new_rate, int msec)
62 {
63 // start with maximum length that resampled time can represent
64 blip_s64 new_size = (ULLONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64;
65
66 // simple safety check, since code elsewhere may not be safe for sizes approaching (2 ^ 31).
67 if (new_size > ((1LL << 30) - 1))
68 new_size = (1LL << 30) - 1;
69
70 if (msec != 0)
71 {
72 blip_s64 s = ((blip_s64)new_rate * (msec + 1) + 999) / 1000;
73 if (s < new_size)
74 new_size = s;
75 }
76
77 if (bbuf->buffer_size != new_size)
78 {
79 void* p = realloc(bbuf->buffer, (new_size + blip_buffer_extra_) * sizeof (bbuf->buffer));
80 if (!p)
81 return "Out of memory";
82
83 bbuf->buffer = (blip_buf_t_*) p;
84 }
85
86 bbuf->buffer_size = new_size;
87
88 // update things based on the sample rate
89 bbuf->sample_rate = new_rate;
90 bbuf->length = new_size * 1000 / new_rate - 1;
91 if (bbuf->clock_rate)
92 Blip_Buffer_set_clock_rate(bbuf, bbuf->clock_rate);
93 Blip_Buffer_bass_freq(bbuf, bbuf->bass_freq);
94
95 Blip_Buffer_clear(bbuf, 1);
96
97 return 0; // success
98 }
99
100 blip_resampled_time_t Blip_Buffer_clock_rate_factor(Blip_Buffer* bbuf,
101 long rate)
102 {
103 double ratio = (double) bbuf->sample_rate / rate;
104 blip_s64 factor = (blip_s64) floor(ratio * (1LL << BLIP_BUFFER_ACCURACY) + 0.5);
105 return (blip_resampled_time_t) factor;
106 }
107
108 void Blip_Buffer_bass_freq(Blip_Buffer* bbuf, int freq)
109 {
110 int shift = 31;
111
112 bbuf->bass_freq = freq;
113 if (freq > 0)
114 {
115 long f;
116 shift = 13;
117 f = (freq << 16) / bbuf->sample_rate;
118 while ((f >>= 1) && --shift) { }
119 }
120 bbuf->bass_shift = shift;
121 }
122
123 void Blip_Buffer_end_frame(Blip_Buffer* bbuf, blip_time_t t)
124 {
125 bbuf->offset += t * bbuf->factor;
126 }
127
128 void Blip_Buffer_remove_silence(Blip_Buffer* bbuf, long count)
129 {
130 bbuf->offset -= (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY;
131 }
132
133 long Blip_Buffer_count_samples(Blip_Buffer* bbuf, blip_time_t t)
134 {
135 unsigned long last_sample = Blip_Buffer_resampled_time(bbuf, t) >> BLIP_BUFFER_ACCURACY;
136 unsigned long first_sample = bbuf->offset >> BLIP_BUFFER_ACCURACY;
137 return (long)(last_sample - first_sample);
138 }
139
140 void Blip_Buffer_remove_samples(Blip_Buffer* bbuf, long count)
141 {
142 if (count)
143 {
144 long remain;
145 Blip_Buffer_remove_silence(bbuf, count);
146
147 // copy remaining samples to beginning and clear old samples
148 remain = Blip_Buffer_samples_avail(bbuf) + blip_buffer_extra_;
149 memmove(bbuf->buffer, bbuf->buffer + count, remain * sizeof(bbuf->buffer));
150 memset(bbuf->buffer + remain, 0, count * sizeof(bbuf->buffer));
151 }
152 }
153
154 long Blip_Buffer_read_samples(Blip_Buffer* bbuf, blip_sample_t* out,
155 long max_samples)
156 {
157 long count = Blip_Buffer_samples_avail(bbuf);
158 if (count > max_samples)
159 count = max_samples;
160
161 if (count)
162 {
163 int const bass = BLIP_READER_BASS(*bbuf);
164 BLIP_READER_BEGIN(reader, *bbuf);
165
166 blip_long n;
167
168 for (n = count; n; --n)
169 {
170 blip_long s = BLIP_READER_READ(reader);
171 if ((blip_sample_t) s != s)
172 s = 0x7FFF - (s >> 24);
173 *out = (blip_sample_t) s;
174 out += 2;
175 BLIP_READER_NEXT(reader, bass);
176 }
177
178 BLIP_READER_END(reader, *bbuf);
179 Blip_Buffer_remove_samples(bbuf, count);
180 }
181 return count;
182 }
183
184 void Blip_Buffer_mix_samples(Blip_Buffer* bbuf, blip_sample_t const* in, long count)
185 {
186 blip_buf_t_* out = bbuf->buffer + (bbuf->offset >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2;
187
188 int const sample_shift = blip_sample_bits - 16;
189 int prev = 0;
190 while (count--)
191 {
192 blip_long s = (blip_long) * in++ << sample_shift;
193 *out += s - prev;
194 prev = s;
195 ++out;
196 }
197 *out -= prev;
198 }