Skip to content

Commit

Permalink
allocate the sp data with two additional samples in instrum.c,
Browse files Browse the repository at this point in the history
initialize the extra to 0. apply the timidity-0.2i-bugfix.pat
patch found at the onicos.com site.  apply a lot of resampler
fixes.  fixes all the valgrind warnings reported in bug #3892.
  • Loading branch information
sezero committed Oct 21, 2017
1 parent 0e668ea commit e141c9b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 41 deletions.
17 changes: 10 additions & 7 deletions timidity/instrum.c
Expand Up @@ -384,7 +384,7 @@ static Instrument *load_instrument(MidiSong *song, char *name, int percussion,
}

/* Then read the sample data */
sp->data = safe_malloc(sp->data_length);
sp->data = (sample_t *) safe_malloc(sp->data_length+4);
if (1 != SDL_RWread(rw, sp->data, sp->data_length, 1))
goto fail;

Expand All @@ -393,15 +393,14 @@ static Instrument *load_instrument(MidiSong *song, char *name, int percussion,
Sint32 k=sp->data_length;
Uint8 *cp=(Uint8 *)(sp->data);
Uint16 *tmp16,*new16;
tmp16 = new16 = (Uint16 *) safe_malloc(sp->data_length*2);
while (k--)
*tmp16++ = (Uint16)(*cp++) << 8;
cp=(Uint8 *)(sp->data);
sp->data = (sample_t *)new16;
free(cp);
sp->data_length *= 2;
sp->loop_start *= 2;
sp->loop_end *= 2;
tmp16 = new16 = (Uint16 *) safe_malloc(sp->data_length+4);
while (k--)
*tmp16++ = (Uint16)(*cp++) << 8;
free(sp->data);
sp->data = (sample_t *)new16;
}
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
else
Expand Down Expand Up @@ -475,6 +474,10 @@ static Instrument *load_instrument(MidiSong *song, char *name, int percussion,
sp->loop_start /= 2;
sp->loop_end /= 2;

/* initialize the added extra sample space (see the +4 bytes in
allocation) using the last actual sample: */
sp->data[sp->data_length] = sp->data[sp->data_length+1] = 0;

/* Then fractional samples */
sp->data_length <<= FRACTION_BITS;
sp->loop_start <<= FRACTION_BITS;
Expand Down
10 changes: 5 additions & 5 deletions timidity/mix.c
Expand Up @@ -55,7 +55,9 @@ int recompute_envelope(MidiSong *song, int v)
}
song->voice[v].envelope_stage=stage+1;

if (song->voice[v].envelope_volume==song->voice[v].sample->envelope_offset[stage])
if (song->voice[v].envelope_volume==song->voice[v].sample->envelope_offset[stage] ||
(stage > 2 && song->voice[v].envelope_volume <
song->voice[v].sample->envelope_offset[stage]))
return recompute_envelope(song, v);
song->voice[v].envelope_target = song->voice[v].sample->envelope_offset[stage];
song->voice[v].envelope_increment = song->voice[v].sample->envelope_rate[stage];
Expand Down Expand Up @@ -420,9 +422,6 @@ static void ramp_out(MidiSong *song, sample_t *sp, Sint32 *lp, int v, Sint32 c)

sample_t s=0; /* silly warning about uninitialized s */

/* Fix by James Caldwell */
if ( c == 0 ) c = 1;

left=song->voice[v].left_mix;
li=-(left/c);
if (!li) li=-1;
Expand Down Expand Up @@ -511,7 +510,8 @@ void mix_voice(MidiSong *song, Sint32 *buf, int v, Sint32 c)
if (c>=MAX_DIE_TIME)
c=MAX_DIE_TIME;
sp=resample_voice(song, v, &c);
ramp_out(song, sp, buf, v, c);
if(c > 0)
ramp_out(song, sp, buf, v, c);
vp->status=VOICE_FREE;
}
else
Expand Down
17 changes: 11 additions & 6 deletions timidity/readmidi.c
Expand Up @@ -171,7 +171,7 @@ static MidiEventList *read_midi_event(MidiSong *song)
case 7: control=ME_MAINVOLUME; break;
case 10: control=ME_PAN; break;
case 11: control=ME_EXPRESSION; break;
case 64: control=ME_SUSTAIN; break;
case 64: control=ME_SUSTAIN; b = (b >= 64); break;
case 120: control=ME_ALL_SOUNDS_OFF; break;
case 121: control=ME_RESET_CONTROLLERS; break;
case 123: control=ME_ALL_NOTES_OFF; break;
Expand All @@ -183,12 +183,14 @@ static MidiEventList *read_midi_event(MidiSong *song)
continuous controller. This will cause lots of
warnings about undefined tone banks. */
case 0: control=ME_TONE_BANK; break;
case 32:
case 32:
if (b!=0) {
SNDDBG(("(Strange: tone bank change 0x20%02x)\n", b));
SNDDBG(("(Strange: tone bank change 0x%02x)\n", b));
}
#if 0 /* `Bank Select LSB' is not worked at GS. Please ignore it. */
else
control=ME_TONE_BANK;
#endif
break;

case 100: nrpn=0; rpn_msb[lastchan]=b; break;
Expand Down Expand Up @@ -374,9 +376,6 @@ static MidiEvent *groom_list(MidiSong *song, Sint32 divisions,Sint32 *eventsp,

if (meep->event.type==ME_TEMPO)
{
tempo=
meep->event.channel + meep->event.b * 256 + meep->event.a * 65536;
compute_sample_increment(song, tempo, divisions);
skip_this_event=1;
}
else if (meep->event.channel >= MAXCHAN)
Expand Down Expand Up @@ -466,6 +465,12 @@ static MidiEvent *groom_list(MidiSong *song, Sint32 divisions,Sint32 *eventsp,
st += samples_to_do;
}
else if (counting_time==1) counting_time=0;
if (meep->event.type==ME_TEMPO)
{
tempo=
meep->event.channel + meep->event.b * 256 + meep->event.a * 65536;
compute_sample_increment(song, tempo, divisions);
}
if (!skip_this_event)
{
/* Add the event to the list */
Expand Down
59 changes: 36 additions & 23 deletions timidity/resample.c
Expand Up @@ -71,7 +71,7 @@ static sample_t *rs_plain(MidiSong *song, int v, Sint32 *countptr)
if (ofs >= le)
{
if (ofs == le)
*dest++ = src[ofs >> FRACTION_BITS];
*dest++ = src[(ofs>>FRACTION_BITS)-1]/2;
vp->status=VOICE_FREE;
*countptr-=count+1;
}
Expand All @@ -98,8 +98,7 @@ static sample_t *rs_loop(MidiSong *song, Voice *vp, Sint32 count)

while (count)
{
if (ofs >= le)
/* NOTE: Assumes that ll > incr and that incr > 0. */
while (ofs >= le)
ofs -= ll;
/* Precalc how many times we should go through the loop */
i = (le - ofs) / incr + 1;
Expand Down Expand Up @@ -310,7 +309,7 @@ static sample_t *rs_vib_plain(MidiSong *song, int v, Sint32 *countptr)
if (ofs >= le)
{
if (ofs == le)
*dest++ = src[ofs >> FRACTION_BITS];
*dest++ = src[(ofs>>FRACTION_BITS)-1]/2;
vp->status=VOICE_FREE;
*countptr-=count+1;
break;
Expand Down Expand Up @@ -346,7 +345,7 @@ static sample_t *rs_vib_loop(MidiSong *song, Voice *vp, Sint32 count)
while (count)
{
/* Hopefully the loop is longer than an increment */
if(ofs >= le)
while(ofs >= le)
ofs -= ll;
/* Precalc how many times to go through the loop, taking
the vibrato control ratio into account this time. */
Expand Down Expand Up @@ -536,8 +535,8 @@ void pre_resample(MidiSong *song, Sample *sp)
{
double a, xdiff;
Sint32 incr, ofs, newlen, count;
Sint16 *newdata, *dest, *src = (Sint16 *) sp->data;
Sint16 v1, v2, v3, v4, *vptr;
Sint16 *newdata, *dest, *src = (Sint16 *) sp->data, *vptr;
Sint32 v, v1, v2, v3, v4, v5, i;
#ifdef DEBUG_CHATTER
static const char note_name[12][3] =
{
Expand All @@ -549,49 +548,63 @@ void pre_resample(MidiSong *song, Sample *sp)
note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12));
#endif

a = ((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]) /
((double) (sp->root_freq) * song->rate);
newlen = (Sint32)(sp->data_length / a);
dest = newdata = safe_malloc(newlen >> (FRACTION_BITS - 1));
a = ((double) (sp->root_freq) * song->rate) /
((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]);
if(sp->data_length * a >= 0x7fffffffL) { /* Too large to compute */
SNDDBG((" *** Can't pre-resampling for note %d\n", sp->note_to_use));
return;
}

newlen = (Sint32)(sp->data_length * a);
count = (newlen >> FRACTION_BITS) - 1;
ofs = incr = (sp->data_length - (1 << FRACTION_BITS)) / count;

if((double)newlen + incr >= 0x7fffffffL) { /* Too large to compute */
SNDDBG((" *** Can't pre-resampling for note %d\n", sp->note_to_use));
return;
}

dest = newdata = (Sint16 *) safe_malloc((newlen >> (FRACTION_BITS - 1)) + 2);
if (!dest)
return;

if (--count)
*dest++ = src[0];

/* Since we're pre-processing and this doesn't have to be done in
real-time, we go ahead and do the full sliding cubic interpolation. */
while (--count)
count--;
for(i = 0; i < count; i++)
{
vptr = src + (ofs >> FRACTION_BITS);
/*
* Electric Fence to the rescue: Accessing *(vptr - 1) is not a
* good thing to do when vptr <= src. (TiMidity++ has a similar
* safe-guard here.)
*/
v1 = (vptr == src) ? *vptr : *(vptr - 1);
v1 = ((vptr>=src+1)? *(vptr - 1):0);
v2 = *vptr;
v3 = *(vptr + 1);
v4 = *(vptr + 2);
v5 = v2 - v3;
xdiff = FSCALENEG(ofs & FRACTION_MASK, FRACTION_BITS);
*dest++ = (Sint16)(v2 + (xdiff / 6.0) * (-2 * v1 - 3 * v2 + 6 * v3 - v4 +
xdiff * (3 * (v1 - 2 * v2 + v3) + xdiff * (-v1 + 3 * (v2 - v3) + v4))));
v = (Sint32)(v2 + xdiff * (1.0/6.0) * (3 * (v3 - v5) - 2 * v1 - v4 +
xdiff * (3 * (v1 - v2 - v5) + xdiff * (3 * v5 + v4 - v1))));
*dest++ = (Sint16)((v > 32767) ? 32767 : ((v < -32768) ? -32768 : v));
ofs += incr;
}

if (ofs & FRACTION_MASK)
{
v1 = src[ofs >> FRACTION_BITS];
v2 = src[(ofs >> FRACTION_BITS) + 1];
*dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
*dest++ = (Sint16)(v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
}
else
*dest++ = src[ofs >> FRACTION_BITS];

*dest = *(dest - 1) / 2;
++dest;
*dest = *(dest - 1) / 2;

sp->data_length = newlen;
sp->loop_start = (Sint32)(sp->loop_start / a);
sp->loop_end = (Sint32)(sp->loop_end / a);
sp->loop_start = (Sint32)(sp->loop_start * a);
sp->loop_end = (Sint32)(sp->loop_end * a);
free(sp->data);
sp->data = (sample_t *) newdata;
sp->sample_rate = 0;
Expand Down

0 comments on commit e141c9b

Please sign in to comment.