annotate Plugins/Input/timidity/libtimidity/resample.c @ 285:d1762728ea4b trunk

[svn] Timidity support, via external contractor dai+audacious@cdr.jp.
author nenolod
date Wed, 14 Dec 2005 08:51:51 -0800
parents
children c1dfb4b13be8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
285
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
1 /*
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
2
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
3 TiMidity -- Experimental MIDI to WAVE converter
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
4 Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
5
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
6 This program is free software; you can redistribute it and/or modify
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
8 the Free Software Foundation; either version 2 of the License, or
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
9 (at your option) any later version.
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
10
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
11 This program is distributed in the hope that it will be useful,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
14 GNU General Public License for more details.
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
15
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
17 along with this program; if not, write to the Free Software
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
19
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
20 resample.c
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
21 */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
22
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
23 #if HAVE_CONFIG_H
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
24 # include <config.h>
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
25 #endif
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
26
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
27 #include <math.h>
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
28 #include <stdio.h>
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
29 #include <stdlib.h>
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
30
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
31 #include "timidity.h"
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
32 #include "timidity_internal.h"
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
33 #include "options.h"
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
34 #include "common.h"
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
35 #include "instrum.h"
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
36 #include "playmidi.h"
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
37 #include "tables.h"
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
38 #include "resample.h"
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
39
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
40 /*************** resampling with fixed increment *****************/
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
41
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
42 static sample_t *rs_plain(MidSong *song, int v, sint32 *countptr)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
43 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
44
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
45 /* Play sample until end, then free the voice. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
46
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
47 sample_t v1, v2;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
48 MidVoice
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
49 *vp=&(song->voice[v]);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
50 sample_t
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
51 *dest=song->resample_buffer,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
52 *src=vp->sample->data;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
53 sint32
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
54 ofs=vp->sample_offset,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
55 incr=vp->sample_increment,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
56 le=vp->sample->data_length,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
57 count=*countptr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
58 sint32 i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
59
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
60 if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
61
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
62 /* Precalc how many times we should go through the loop.
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
63 NOTE: Assumes that incr > 0 and that ofs <= le */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
64 i = (le - ofs) / incr + 1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
65
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
66 if (i > count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
67 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
68 i = count;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
69 count = 0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
70 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
71 else count -= i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
72
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
73 while (i--)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
74 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
75 v1 = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
76 v2 = src[(ofs >> FRACTION_BITS)+1];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
77 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
78 ofs += incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
79 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
80
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
81 if (ofs >= le)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
82 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
83 if (ofs == le)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
84 *dest++ = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
85 vp->status=VOICE_FREE;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
86 *countptr-=count+1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
87 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
88
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
89 vp->sample_offset=ofs; /* Update offset */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
90 return song->resample_buffer;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
91 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
92
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
93 static sample_t *rs_loop(MidSong *song, MidVoice *vp, sint32 count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
94 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
95
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
96 /* Play sample until end-of-loop, skip back and continue. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
97
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
98 sample_t v1, v2;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
99 sint32
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
100 ofs=vp->sample_offset,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
101 incr=vp->sample_increment,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
102 le=vp->sample->loop_end,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
103 ll=le - vp->sample->loop_start;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
104 sample_t
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
105 *dest=song->resample_buffer,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
106 *src=vp->sample->data;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
107 sint32 i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
108
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
109 while (count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
110 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
111 if (ofs >= le)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
112 /* NOTE: Assumes that ll > incr and that incr > 0. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
113 ofs -= ll;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
114 /* Precalc how many times we should go through the loop */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
115 i = (le - ofs) / incr + 1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
116 if (i > count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
117 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
118 i = count;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
119 count = 0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
120 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
121 else count -= i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
122 while (i--)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
123 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
124 v1 = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
125 v2 = src[(ofs >> FRACTION_BITS)+1];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
126 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
127 ofs += incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
128 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
129 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
130
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
131 vp->sample_offset=ofs; /* Update offset */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
132 return song->resample_buffer;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
133 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
134
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
135 static sample_t *rs_bidir(MidSong *song, MidVoice *vp, sint32 count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
136 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
137 sample_t v1, v2;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
138 sint32
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
139 ofs=vp->sample_offset,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
140 incr=vp->sample_increment,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
141 le=vp->sample->loop_end,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
142 ls=vp->sample->loop_start;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
143 sample_t
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
144 *dest=song->resample_buffer,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
145 *src=vp->sample->data;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
146 sint32
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
147 le2 = le<<1,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
148 ls2 = ls<<1,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
149 i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
150 /* Play normally until inside the loop region */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
151
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
152 if (ofs <= ls)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
153 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
154 /* NOTE: Assumes that incr > 0, which is NOT always the case
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
155 when doing bidirectional looping. I have yet to see a case
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
156 where both ofs <= ls AND incr < 0, however. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
157 i = (ls - ofs) / incr + 1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
158 if (i > count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
159 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
160 i = count;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
161 count = 0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
162 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
163 else count -= i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
164 while (i--)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
165 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
166 v1 = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
167 v2 = src[(ofs >> FRACTION_BITS)+1];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
168 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
169 ofs += incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
170 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
171 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
172
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
173 /* Then do the bidirectional looping */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
174
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
175 while(count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
176 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
177 /* Precalc how many times we should go through the loop */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
178 i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
179 if (i > count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
180 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
181 i = count;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
182 count = 0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
183 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
184 else count -= i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
185 while (i--)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
186 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
187 v1 = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
188 v2 = src[(ofs >> FRACTION_BITS)+1];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
189 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
190 ofs += incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
191 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
192 if (ofs>=le)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
193 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
194 /* fold the overshoot back in */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
195 ofs = le2 - ofs;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
196 incr *= -1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
197 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
198 else if (ofs <= ls)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
199 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
200 ofs = ls2 - ofs;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
201 incr *= -1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
202 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
203 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
204
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
205 vp->sample_increment=incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
206 vp->sample_offset=ofs; /* Update offset */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
207 return song->resample_buffer;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
208 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
209
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
210 /*********************** vibrato versions ***************************/
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
211
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
212 /* We only need to compute one half of the vibrato sine cycle */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
213 static int vib_phase_to_inc_ptr(int phase)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
214 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
215 if (phase < MID_VIBRATO_SAMPLE_INCREMENTS/2)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
216 return MID_VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
217 else if (phase >= 3*MID_VIBRATO_SAMPLE_INCREMENTS/2)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
218 return 5*MID_VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
219 else
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
220 return phase-MID_VIBRATO_SAMPLE_INCREMENTS/2;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
221 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
222
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
223 static sint32 update_vibrato(MidSong *song, MidVoice *vp, int sign)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
224 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
225 sint32 depth;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
226 int phase, pb;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
227 double a;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
228
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
229 if (vp->vibrato_phase++ >= 2*MID_VIBRATO_SAMPLE_INCREMENTS-1)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
230 vp->vibrato_phase=0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
231 phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
232
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
233 if (vp->vibrato_sample_increment[phase])
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
234 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
235 if (sign)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
236 return -vp->vibrato_sample_increment[phase];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
237 else
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
238 return vp->vibrato_sample_increment[phase];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
239 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
240
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
241 /* Need to compute this sample increment. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
242
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
243 depth=vp->sample->vibrato_depth<<7;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
244
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
245 if (vp->vibrato_sweep)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
246 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
247 /* Need to update sweep */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
248 vp->vibrato_sweep_position += vp->vibrato_sweep;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
249 if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
250 vp->vibrato_sweep=0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
251 else
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
252 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
253 /* Adjust depth */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
254 depth *= vp->vibrato_sweep_position;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
255 depth >>= SWEEP_SHIFT;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
256 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
257 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
258
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
259 a = FSCALE(((double)(vp->sample->sample_rate) *
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
260 (double)(vp->frequency)) /
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
261 ((double)(vp->sample->root_freq) *
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
262 (double)(song->rate)),
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
263 FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
264
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
265 pb=(int)((sine(vp->vibrato_phase *
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
266 (SINE_CYCLE_LENGTH/(2*MID_VIBRATO_SAMPLE_INCREMENTS)))
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
267 * (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
268
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
269 if (pb<0)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
270 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
271 pb=-pb;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
272 a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
273 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
274 else
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
275 a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
276
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
277 /* If the sweep's over, we can store the newly computed sample_increment */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
278 if (!vp->vibrato_sweep)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
279 vp->vibrato_sample_increment[phase]=(sint32) a;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
280
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
281 if (sign)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
282 a = -a; /* need to preserve the loop direction */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
283
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
284 return (sint32) a;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
285 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
286
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
287 static sample_t *rs_vib_plain(MidSong *song, int v, sint32 *countptr)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
288 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
289
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
290 /* Play sample until end, then free the voice. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
291
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
292 sample_t v1, v2;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
293 MidVoice *vp=&(song->voice[v]);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
294 sample_t
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
295 *dest=song->resample_buffer,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
296 *src=vp->sample->data;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
297 sint32
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
298 le=vp->sample->data_length,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
299 ofs=vp->sample_offset,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
300 incr=vp->sample_increment,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
301 count=*countptr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
302 int
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
303 cc=vp->vibrato_control_counter;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
304
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
305 /* This has never been tested */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
306
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
307 if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
308
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
309 while (count--)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
310 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
311 if (!cc--)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
312 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
313 cc=vp->vibrato_control_ratio;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
314 incr=update_vibrato(song, vp, 0);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
315 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
316 v1 = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
317 v2 = src[(ofs >> FRACTION_BITS)+1];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
318 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
319 ofs += incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
320 if (ofs >= le)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
321 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
322 if (ofs == le)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
323 *dest++ = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
324 vp->status=VOICE_FREE;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
325 *countptr-=count+1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
326 break;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
327 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
328 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
329
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
330 vp->vibrato_control_counter=cc;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
331 vp->sample_increment=incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
332 vp->sample_offset=ofs; /* Update offset */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
333 return song->resample_buffer;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
334 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
335
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
336 static sample_t *rs_vib_loop(MidSong *song, MidVoice *vp, sint32 count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
337 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
338
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
339 /* Play sample until end-of-loop, skip back and continue. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
340
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
341 sample_t v1, v2;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
342 sint32
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
343 ofs=vp->sample_offset,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
344 incr=vp->sample_increment,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
345 le=vp->sample->loop_end,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
346 ll=le - vp->sample->loop_start;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
347 sample_t
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
348 *dest=song->resample_buffer,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
349 *src=vp->sample->data;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
350 int
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
351 cc=vp->vibrato_control_counter;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
352 sint32 i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
353 int
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
354 vibflag=0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
355
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
356 while (count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
357 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
358 /* Hopefully the loop is longer than an increment */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
359 if(ofs >= le)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
360 ofs -= ll;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
361 /* Precalc how many times to go through the loop, taking
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
362 the vibrato control ratio into account this time. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
363 i = (le - ofs) / incr + 1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
364 if(i > count) i = count;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
365 if(i > cc)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
366 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
367 i = cc;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
368 vibflag = 1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
369 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
370 else cc -= i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
371 count -= i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
372 while(i--)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
373 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
374 v1 = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
375 v2 = src[(ofs >> FRACTION_BITS)+1];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
376 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
377 ofs += incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
378 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
379 if(vibflag)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
380 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
381 cc = vp->vibrato_control_ratio;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
382 incr = update_vibrato(song, vp, 0);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
383 vibflag = 0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
384 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
385 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
386
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
387 vp->vibrato_control_counter=cc;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
388 vp->sample_increment=incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
389 vp->sample_offset=ofs; /* Update offset */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
390 return song->resample_buffer;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
391 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
392
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
393 static sample_t *rs_vib_bidir(MidSong *song, MidVoice *vp, sint32 count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
394 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
395 sample_t v1, v2;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
396 sint32
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
397 ofs=vp->sample_offset,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
398 incr=vp->sample_increment,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
399 le=vp->sample->loop_end,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
400 ls=vp->sample->loop_start;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
401 sample_t
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
402 *dest=song->resample_buffer,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
403 *src=vp->sample->data;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
404 int
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
405 cc=vp->vibrato_control_counter;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
406 sint32
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
407 le2=le<<1,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
408 ls2=ls<<1,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
409 i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
410 int
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
411 vibflag = 0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
412
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
413 /* Play normally until inside the loop region */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
414 while (count && (ofs <= ls))
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
415 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
416 i = (ls - ofs) / incr + 1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
417 if (i > count) i = count;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
418 if (i > cc)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
419 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
420 i = cc;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
421 vibflag = 1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
422 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
423 else cc -= i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
424 count -= i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
425 while (i--)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
426 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
427 v1 = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
428 v2 = src[(ofs >> FRACTION_BITS)+1];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
429 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
430 ofs += incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
431 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
432 if (vibflag)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
433 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
434 cc = vp->vibrato_control_ratio;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
435 incr = update_vibrato(song, vp, 0);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
436 vibflag = 0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
437 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
438 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
439
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
440 /* Then do the bidirectional looping */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
441
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
442 while (count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
443 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
444 /* Precalc how many times we should go through the loop */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
445 i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
446 if(i > count) i = count;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
447 if(i > cc)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
448 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
449 i = cc;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
450 vibflag = 1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
451 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
452 else cc -= i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
453 count -= i;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
454 while (i--)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
455 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
456 v1 = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
457 v2 = src[(ofs >> FRACTION_BITS)+1];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
458 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
459 ofs += incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
460 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
461 if (vibflag)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
462 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
463 cc = vp->vibrato_control_ratio;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
464 incr = update_vibrato(song, vp, (incr < 0));
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
465 vibflag = 0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
466 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
467 if (ofs >= le)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
468 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
469 /* fold the overshoot back in */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
470 ofs = le2 - ofs;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
471 incr *= -1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
472 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
473 else if (ofs <= ls)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
474 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
475 ofs = ls2 - ofs;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
476 incr *= -1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
477 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
478 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
479
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
480 vp->vibrato_control_counter=cc;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
481 vp->sample_increment=incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
482 vp->sample_offset=ofs; /* Update offset */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
483 return song->resample_buffer;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
484 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
485
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
486 sample_t *resample_voice(MidSong *song, int v, sint32 *countptr)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
487 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
488 sint32 ofs;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
489 uint8 modes;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
490 MidVoice *vp=&(song->voice[v]);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
491
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
492 if (!(vp->sample->sample_rate))
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
493 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
494 /* Pre-resampled data -- just update the offset and check if
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
495 we're out of data. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
496 ofs=vp->sample_offset >> FRACTION_BITS; /* Kind of silly to use
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
497 FRACTION_BITS here... */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
498 if (*countptr >= (vp->sample->data_length>>FRACTION_BITS) - ofs)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
499 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
500 /* Note finished. Free the voice. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
501 vp->status = VOICE_FREE;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
502
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
503 /* Let the caller know how much data we had left */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
504 *countptr = (vp->sample->data_length>>FRACTION_BITS) - ofs;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
505 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
506 else
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
507 vp->sample_offset += *countptr << FRACTION_BITS;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
508
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
509 return vp->sample->data+ofs;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
510 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
511
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
512 /* Need to resample. Use the proper function. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
513 modes=vp->sample->modes;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
514
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
515 if (vp->vibrato_control_ratio)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
516 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
517 if ((modes & MODES_LOOPING) &&
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
518 ((modes & MODES_ENVELOPE) ||
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
519 (vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
520 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
521 if (modes & MODES_PINGPONG)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
522 return rs_vib_bidir(song, vp, *countptr);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
523 else
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
524 return rs_vib_loop(song, vp, *countptr);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
525 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
526 else
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
527 return rs_vib_plain(song, v, countptr);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
528 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
529 else
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
530 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
531 if ((modes & MODES_LOOPING) &&
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
532 ((modes & MODES_ENVELOPE) ||
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
533 (vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
534 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
535 if (modes & MODES_PINGPONG)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
536 return rs_bidir(song, vp, *countptr);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
537 else
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
538 return rs_loop(song, vp, *countptr);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
539 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
540 else
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
541 return rs_plain(song, v, countptr);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
542 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
543 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
544
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
545 void pre_resample(MidSong *song, MidSample *sp)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
546 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
547 double a, xdiff;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
548 sint32 incr, ofs, newlen, count;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
549 sint16 *newdata, *dest, *src = (sint16 *) sp->data;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
550 sint16 v1, v2, v3, v4, *vptr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
551 #ifdef DEBUG_CHATTER
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
552 static const char note_name[12][3] =
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
553 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
554 "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
555 };
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
556 #endif
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
557
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
558 DEBUG_MSG(" * pre-resampling for note %d (%s%d)\n",
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
559 sp->note_to_use,
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
560 note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
561
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
562 a = ((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]) /
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
563 ((double) (sp->root_freq) * song->rate);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
564 newlen = (sint32)(sp->data_length / a);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
565 dest = newdata = safe_malloc(newlen >> (FRACTION_BITS - 1));
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
566
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
567 count = (newlen >> FRACTION_BITS) - 1;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
568 ofs = incr = (sp->data_length - (1 << FRACTION_BITS)) / count;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
569
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
570 if (--count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
571 *dest++ = src[0];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
572
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
573 /* Since we're pre-processing and this doesn't have to be done in
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
574 real-time, we go ahead and do the full sliding cubic interpolation. */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
575 while (--count)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
576 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
577 vptr = src + (ofs >> FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
578 /*
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
579 * Electric Fence to the rescue: Accessing *(vptr - 1) is not a
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
580 * good thing to do when vptr <= src. (TiMidity++ has a similar
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
581 * safe-guard here.)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
582 */
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
583 v1 = (vptr > src) ? *(vptr - 1) : 0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
584 v2 = *vptr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
585 v3 = *(vptr + 1);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
586 v4 = *(vptr + 2);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
587 xdiff = FSCALENEG(ofs & FRACTION_MASK, FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
588 *dest++ = (sint16)(v2 + (xdiff / 6.0) * (-2 * v1 - 3 * v2 + 6 * v3 - v4 +
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
589 xdiff * (3 * (v1 - 2 * v2 + v3) + xdiff * (-v1 + 3 * (v2 - v3) + v4))));
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
590 ofs += incr;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
591 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
592
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
593 if (ofs & FRACTION_MASK)
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
594 {
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
595 v1 = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
596 v2 = src[(ofs >> FRACTION_BITS) + 1];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
597 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
598 }
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
599 else
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
600 *dest++ = src[ofs >> FRACTION_BITS];
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
601
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
602 sp->data_length = newlen;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
603 sp->loop_start = (sint32)(sp->loop_start / a);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
604 sp->loop_end = (sint32)(sp->loop_end / a);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
605 free(sp->data);
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
606 sp->data = (sample_t *) newdata;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
607 sp->sample_rate = 0;
d1762728ea4b [svn] Timidity support, via external contractor dai+audacious@cdr.jp.
nenolod
parents:
diff changeset
608 }