1 /*
2 TiMidity++ -- MIDI to WAVE converter and player
3 Copyright (C) 1999-2004 Masanao Izumo <iz@onicos.co.jp>
4 Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 recache.c
21
22 Code related to resample cache.
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include <string.h>
29
30 #include "timidity.h"
31 #include "common.h"
32 #include "instrum.h"
33 #include "playmidi.h"
34 #include "tables.h"
35 #include "recache.h"
36 #include "resample.h"
37
38 namespace TimidityPlus
39 {
40
41 #define CACHE_DATA_LEN (allocate_cache_size / sizeof(sample_t))
42
sp_hash(Sample * sp,int note)43 inline uint32_t sp_hash(Sample *sp, int note)
44 {
45 return ((uint32_t)(intptr_t)(sp)+(uint32_t)(note));
46 }
47
48
49 #define RESAMPLATION_CACHE _x = do_resamplation(src, ofs, &resrc); \
50 dest[i] = (int16_t) ((_x > 32767) ? 32767 \
51 : ((_x < -32768) ? -32768 : _x))
52
53
54
55
free_cache_data(void)56 void Recache::free_cache_data(void) {
57 free(cache_data);
58 reuse_mblock(&hash_entry_pool);
59 }
60
resamp_cache_reset(void)61 void Recache::resamp_cache_reset(void)
62 {
63 if (cache_data == NULL) {
64 cache_data = (sample_t *)
65 safe_large_malloc((CACHE_DATA_LEN + 1) * sizeof(sample_t));
66 memset(cache_data, 0, (CACHE_DATA_LEN + 1) * sizeof(sample_t));
67 init_mblock(&hash_entry_pool);
68 }
69 cache_data_len = 0;
70 memset(cache_hash_table, 0, sizeof(cache_hash_table));
71 memset(channel_note_table, 0, sizeof(channel_note_table));
72 reuse_mblock(&hash_entry_pool);
73 }
74
resamp_cache_fetch(Sample * sp,int note)75 struct cache_hash *Recache::resamp_cache_fetch(Sample *sp, int note)
76 {
77 unsigned int addr;
78 struct cache_hash *p;
79
80 if (sp->vibrato_control_ratio || (sp->modes & MODES_PINGPONG)
81 || (sp->sample_rate == playback_rate
82 && sp->root_freq == get_note_freq(sp, sp->note_to_use)))
83 return NULL;
84 addr = sp_hash(sp, note) % HASH_TABLE_SIZE;
85 p = cache_hash_table[addr];
86 while (p && (p->note != note || p->sp != sp))
87 p = p->next;
88 if (p && p->resampled != NULL)
89 return p;
90 return NULL;
91 }
92
resamp_cache_refer_on(Voice * vp,int32_t sample_start)93 void Recache::resamp_cache_refer_on(Voice *vp, int32_t sample_start)
94 {
95 unsigned int addr;
96 struct cache_hash *p;
97 int note, ch;
98
99 ch = vp->channel;
100 if (vp->vibrato_control_ratio || player->channel[ch].portamento
101 || (vp->sample->modes & MODES_PINGPONG)
102 || vp->orig_frequency != vp->frequency
103 || (vp->sample->sample_rate == playback_rate
104 && vp->sample->root_freq
105 == get_note_freq(vp->sample, vp->sample->note_to_use)))
106 return;
107 note = vp->note;
108 if (channel_note_table[ch].cache[note])
109 resamp_cache_refer_off(ch, note, sample_start);
110 addr = sp_hash(vp->sample, note) % HASH_TABLE_SIZE;
111 p = cache_hash_table[addr];
112 while (p && (p->note != note || p->sp != vp->sample))
113 p = p->next;
114 if (! p) {
115 p = (struct cache_hash *)
116 new_segment(&hash_entry_pool, sizeof(struct cache_hash));
117 p->cnt = 0;
118 p->note = vp->note;
119 p->sp = vp->sample;
120 p->resampled = NULL;
121 p->next = cache_hash_table[addr];
122 cache_hash_table[addr] = p;
123 }
124 channel_note_table[ch].cache[note] = p;
125 channel_note_table[ch].on[note] = sample_start;
126 }
127
resamp_cache_refer_off(int ch,int note,int32_t sample_end)128 void Recache::resamp_cache_refer_off(int ch, int note, int32_t sample_end)
129 {
130 int32_t sample_start, len;
131 struct cache_hash *p;
132 Sample *sp;
133
134 p = channel_note_table[ch].cache[note];
135 if (p == NULL)
136 return;
137 sp = p->sp;
138 if (sp->sample_rate == playback_rate
139 && sp->root_freq == get_note_freq(sp, sp->note_to_use))
140 return;
141 sample_start = channel_note_table[ch].on[note];
142 len = sample_end - sample_start;
143 if (len < 0) {
144 channel_note_table[ch].cache[note] = NULL;
145 return;
146 }
147 if (! (sp->modes & MODES_LOOPING)) {
148 double a;
149 int32_t slen;
150
151 a = ((double) sp->root_freq * playback_rate)
152 / ((double) sp->sample_rate * get_note_freq(sp, note));
153 slen = (int32_t) ((sp->data_length >> FRACTION_BITS) * a);
154 if (len > slen)
155 len = slen;
156 }
157 p->cnt += len;
158 channel_note_table[ch].cache[note] = NULL;
159 }
160
resamp_cache_refer_alloff(int ch,int32_t sample_end)161 void Recache::resamp_cache_refer_alloff(int ch, int32_t sample_end)
162 {
163 int i;
164
165 for (i = 0; i < 128; i++)
166 resamp_cache_refer_off(ch, i, sample_end);
167 }
168
resamp_cache_create(void)169 void Recache::resamp_cache_create(void)
170 {
171 int i, skip;
172 int32_t n, t1, t2, total;
173 struct cache_hash **array;
174
175 /* It is NP completion that solve the best cache hit rate.
176 * So I thought better algorism O(n log n), but not a best solution.
177 * Follows implementation takes good hit rate, and it is fast.
178 */
179 n = t1 = t2 = 0;
180 total = 0;
181 /* set size per count */
182 for (i = 0; i < HASH_TABLE_SIZE; i++) {
183 struct cache_hash *p, *q;
184
185 p = cache_hash_table[i], q = NULL;
186 while (p) {
187 struct cache_hash *tmp;
188
189 t1 += p->cnt;
190 tmp = p, p = p->next;
191 if (tmp->cnt > 0) {
192 Sample *sp;
193 splen_t newlen;
194
195 sp = tmp->sp;
196 sample_resamp_info(sp, tmp->note, NULL, NULL, &newlen);
197 if (newlen > 0) {
198 total += tmp->cnt;
199 tmp->r = (double) newlen / tmp->cnt;
200 tmp->next = q, q = tmp;
201 n++;
202 }
203 }
204 }
205 cache_hash_table[i] = q;
206 }
207 if (n == 0) {
208 return;
209 }
210 array = (struct cache_hash **) new_segment(&hash_entry_pool,
211 n * sizeof(struct cache_hash *));
212 n = 0;
213 for (i = 0; i < HASH_TABLE_SIZE; i++) {
214 struct cache_hash *p;
215
216 for (p = cache_hash_table[i]; p; p = p->next)
217 array[n++] = p;
218 }
219 if ((uint32_t)total > CACHE_DATA_LEN)
220 qsort_cache_array(array, 0, n - 1);
221 skip = 0;
222 for (i = 0; i < n; i++) {
223 if (array[i]->r != 0
224 && cache_resampling(array[i]) == CACHE_RESAMPLING_OK)
225 t2 += array[i]->cnt;
226 else
227 skip++;
228 }
229 /* update cache_hash_table */
230 if (skip)
231 for (i = 0; i < HASH_TABLE_SIZE; i++) {
232 struct cache_hash *p, *q;
233
234 p = cache_hash_table[i], q = NULL;
235 while (p) {
236 struct cache_hash *tmp;
237
238 tmp = p, p = p->next;
239 if (tmp->resampled)
240 tmp->next = q, q = tmp;
241 }
242 cache_hash_table[i] = q;
243 }
244 }
245
sample_resamp_info(Sample * sp,int note,splen_t * loop_start,splen_t * loop_end,splen_t * data_length)246 double Recache::sample_resamp_info(Sample *sp, int note,
247 splen_t *loop_start, splen_t *loop_end, splen_t *data_length)
248 {
249 splen_t xls, xle, ls, le, ll, newlen;
250 double a, xxls, xxle, xn;
251
252 a = ((double) sp->sample_rate * get_note_freq(sp, note))
253 / ((double) sp->root_freq * playback_rate);
254 a = TIM_FSCALENEG((double) (int32_t) TIM_FSCALE(a, FRACTION_BITS),
255 FRACTION_BITS);
256 xn = sp->data_length / a;
257 if (xn >= SPLEN_T_MAX) {
258 /* Ignore this sample */
259 *data_length = 0;
260 return 0.0;
261 }
262 newlen = (splen_t) (TIM_FSCALENEG(xn, FRACTION_BITS) + 0.5);
263 ls = sp->loop_start;
264 le = sp->loop_end;
265 ll = le - ls;
266 xxls = ls / a + 0.5;
267 if (xxls >= SPLEN_T_MAX) {
268 /* Ignore this sample */
269 *data_length = 0;
270 return 0.0;
271 }
272 xls = (splen_t) xxls;
273 xxle = le / a + 0.5;
274 if (xxle >= SPLEN_T_MAX) {
275 /* Ignore this sample */
276 *data_length = 0;
277 return 0.0;
278 }
279 xle = (splen_t) xxle;
280 if ((sp->modes & MODES_LOOPING)
281 && ((xle - xls) >> FRACTION_BITS) < MIN_LOOPLEN) {
282 splen_t n;
283 splen_t newxle;
284 double xl; /* Resampled new loop length */
285 double xnewxle;
286
287 xl = ll / a;
288 if (xl >= SPLEN_T_MAX) {
289 /* Ignore this sample */
290 *data_length = 0;
291 return 0.0;
292 }
293 n = (splen_t) (0.0001 + MIN_LOOPLEN
294 / TIM_FSCALENEG(xl, FRACTION_BITS)) + 1;
295 xnewxle = le / a + n * xl + 0.5;
296 if (xnewxle >= SPLEN_T_MAX) {
297 /* Ignore this sample */
298 *data_length = 0;
299 return 0.0;
300 }
301 newxle = (splen_t) xnewxle;
302 newlen += (newxle - xle) >> FRACTION_BITS;
303 xle = newxle;
304 }
305 if (loop_start)
306 *loop_start = (splen_t) (xls & ~FRACTION_MASK);
307 if (loop_end)
308 *loop_end = (splen_t) (xle & ~FRACTION_MASK);
309 *data_length = newlen << FRACTION_BITS;
310 return a;
311 }
312
qsort_cache_array(struct cache_hash ** a,int32_t first,int32_t last)313 void Recache::qsort_cache_array(struct cache_hash **a, int32_t first, int32_t last)
314 {
315 int32_t i = first, j = last;
316 struct cache_hash *x, *t;
317
318 if (j - i < SORT_THRESHOLD) {
319 insort_cache_array(a + i, j - i + 1);
320 return;
321 }
322 x = a[(first + last) / 2];
323 for (;;) {
324 while (a[i]->r < x->r)
325 i++;
326 while (x->r < a[j]->r)
327 j--;
328 if (i >= j)
329 break;
330 t = a[i], a[i] = a[j], a[j] = t;
331 i++, j--;
332 }
333 if (first < i - 1)
334 qsort_cache_array(a, first, i - 1);
335 if (j + 1 < last)
336 qsort_cache_array(a, j + 1, last);
337 }
338
insort_cache_array(struct cache_hash ** data,int32_t n)339 void Recache::insort_cache_array(struct cache_hash **data, int32_t n)
340 {
341 int32_t i, j;
342 struct cache_hash *x;
343
344 for (i = 1; i < n; i++) {
345 x = data[i];
346 for (j = i - 1; j >= 0 && x->r < data[j]->r; j--)
347 data[j + 1] = data[j];
348 data[j + 1] = x;
349 }
350 }
351
cache_resampling(struct cache_hash * p)352 int Recache::cache_resampling(struct cache_hash *p)
353 {
354 Sample *sp, *newsp;
355 sample_t *src, *dest;
356 splen_t newlen, ofs, le, ls, ll, xls, xle;
357 int32_t incr, _x;
358 resample_rec_t resrc;
359 double a;
360 int8_t note;
361
362 sp = p->sp;
363 if (sp->note_to_use)
364 note = sp->note_to_use;
365 else
366 note = p->note;
367 a = sample_resamp_info(sp, note, &xls, &xle, &newlen);
368 if (newlen == 0)
369 return CACHE_RESAMPLING_NOTOK;
370 newlen >>= FRACTION_BITS;
371 if (cache_data_len + newlen + 1 > CACHE_DATA_LEN)
372 return CACHE_RESAMPLING_NOTOK;
373 resrc.loop_start = ls = sp->loop_start;
374 resrc.loop_end = le = sp->loop_end;
375 resrc.data_length = sp->data_length;
376 ll = sp->loop_end - sp->loop_start;
377 dest = cache_data + cache_data_len;
378 src = sp->data;
379 newsp = (Sample *) new_segment(&hash_entry_pool, sizeof(Sample));
380 memcpy(newsp, sp, sizeof(Sample));
381 newsp->data = dest;
382 ofs = 0;
383 incr = (splen_t) (TIM_FSCALE(a, FRACTION_BITS) + 0.5);
384 if (sp->modes & MODES_LOOPING)
385 for (splen_t i = 0; i < newlen; i++) {
386 if (ofs >= le)
387 ofs -= ll;
388 RESAMPLATION_CACHE;
389 ofs += incr;
390 }
391 else
392 for (splen_t i = 0; i < newlen; i++) {
393 RESAMPLATION_CACHE;
394 ofs += incr;
395 }
396 newsp->loop_start = xls;
397 newsp->loop_end = xle;
398 newsp->data_length = newlen << FRACTION_BITS;
399 if (sp->modes & MODES_LOOPING)
400 loop_connect(dest, (int32_t) (xls >> FRACTION_BITS),
401 (int32_t) (xle >> FRACTION_BITS));
402 dest[xle >> FRACTION_BITS] = dest[xls >> FRACTION_BITS];
403 newsp->root_freq = get_note_freq(newsp, note);
404 newsp->sample_rate = playback_rate;
405 p->resampled = newsp;
406 cache_data_len += newlen + 1;
407 return CACHE_RESAMPLING_OK;
408 }
409
loop_connect(sample_t * data,int32_t start,int32_t end)410 void Recache::loop_connect(sample_t *data, int32_t start, int32_t end)
411 {
412 int i, mixlen;
413 int32_t t0, t1;
414
415 mixlen = MIXLEN;
416 if (start < mixlen)
417 mixlen = start;
418 if (end - start < mixlen)
419 mixlen = end - start;
420 if (mixlen <= 0)
421 return;
422 t0 = start - mixlen;
423 t1 = end - mixlen;
424 for (i = 0; i < mixlen; i++) {
425 double x, b;
426
427 b = i / (double) mixlen; /* 0 <= b < 1 */
428 x = b * data[t0 + i] + (1.0 - b) * data[t1 + i];
429 if (x < -32768)
430 data[t1 + i] = -32768;
431 else if (x > 32767)
432 data[t1 + i] = 32767;
433 else
434 data[t1 + i] = (sample_t) x;
435 }
436 }
437
438 }