From a2ebf771d30ed75413e746128a6057ea249ddae9 Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Mon, 28 Dec 2015 19:13:34 -0600 Subject: [PATCH 1/5] Remove '\r' character from beginning of WildMidi error messages --- src/wildmidi/wm_error.cpp | 8 ++--- zdoom.vcproj | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/wildmidi/wm_error.cpp b/src/wildmidi/wm_error.cpp index 9fabdff84..eab56c437 100644 --- a/src/wildmidi/wm_error.cpp +++ b/src/wildmidi/wm_error.cpp @@ -67,18 +67,18 @@ void _WM_ERROR(const char * func, unsigned int lne, int wmerno, if (wmfor != NULL) { if (error != 0) { - Printf(TEXTCOLOR_RED "\rlibWildMidi(%s:%u): ERROR %s %s (%s)\n", func, + Printf(TEXTCOLOR_RED "libWildMidi(%s:%u): ERROR %s %s (%s)\n", func, lne, errors[wmerno], wmfor, strerror(error)); } else { - Printf(TEXTCOLOR_RED "\rlibWildMidi(%s:%u): ERROR %s %s\n", func, lne, + Printf(TEXTCOLOR_RED "libWildMidi(%s:%u): ERROR %s %s\n", func, lne, errors[wmerno], wmfor); } } else { if (error != 0) { - Printf(TEXTCOLOR_RED "\rlibWildMidi(%s:%u): ERROR %s (%s)\n", func, lne, + Printf(TEXTCOLOR_RED "libWildMidi(%s:%u): ERROR %s (%s)\n", func, lne, errors[wmerno], strerror(error)); } else { - Printf(TEXTCOLOR_RED "\rlibWildMidi(%s:%u): ERROR %s\n", func, lne, + Printf(TEXTCOLOR_RED "libWildMidi(%s:%u): ERROR %s\n", func, lne, errors[wmerno]); } } diff --git a/zdoom.vcproj b/zdoom.vcproj index db4660d3b..126efb7f5 100644 --- a/zdoom.vcproj +++ b/zdoom.vcproj @@ -2553,6 +2553,10 @@ RelativePath="src\sound\music_midi_timidity.cpp" > + + @@ -2789,6 +2793,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Date: Mon, 28 Dec 2015 20:33:41 -0600 Subject: [PATCH 2/5] Separate WildMidi mixing from event handling - In order to use ZDoom's own MIDI sequencer event handling must be completely separate from mixing, but WildMidi had them intertwined because it wasn't designed for external sequencers. - Also remove all 'long's defining the output buffers to avoid having something that's 32 bits wide on Windows and 64 bits wide on Linux. --- src/wildmidi/reverb.cpp | 32 +- src/wildmidi/reverb.h | 16 +- src/wildmidi/wildmidi_lib.cpp | 884 +++++++++++++++++----------------- 3 files changed, 473 insertions(+), 459 deletions(-) diff --git a/src/wildmidi/reverb.cpp b/src/wildmidi/reverb.cpp index d183a1c71..6c6af88d4 100644 --- a/src/wildmidi/reverb.cpp +++ b/src/wildmidi/reverb.cpp @@ -269,23 +269,23 @@ _WM_init_reverb(int rate, float room_x, float room_y, float listen_x, double a1 = -2 * cs; double a2 = 1 - (alpha / A); - rtn_rvb->coeff[j][i][0] = (signed long int) ((b0 / a0) * 1024.0); - rtn_rvb->coeff[j][i][1] = (signed long int) ((b1 / a0) * 1024.0); - rtn_rvb->coeff[j][i][2] = (signed long int) ((b2 / a0) * 1024.0); - rtn_rvb->coeff[j][i][3] = (signed long int) ((a1 / a0) * 1024.0); - rtn_rvb->coeff[j][i][4] = (signed long int) ((a2 / a0) * 1024.0); + rtn_rvb->coeff[j][i][0] = (signed int) ((b0 / a0) * 1024.0); + rtn_rvb->coeff[j][i][1] = (signed int) ((b1 / a0) * 1024.0); + rtn_rvb->coeff[j][i][2] = (signed int) ((b2 / a0) * 1024.0); + rtn_rvb->coeff[j][i][3] = (signed int) ((a1 / a0) * 1024.0); + rtn_rvb->coeff[j][i][4] = (signed int) ((a2 / a0) * 1024.0); } } /* init the reverb buffers */ rtn_rvb->l_buf_size = (int) ((float) rate * (MAXL_DST / 340.29)); - rtn_rvb->l_buf = (long*)malloc( - sizeof(signed long int) * (rtn_rvb->l_buf_size + 1)); + rtn_rvb->l_buf = (int*)malloc( + sizeof(signed int) * (rtn_rvb->l_buf_size + 1)); rtn_rvb->l_out = 0; rtn_rvb->r_buf_size = (int) ((float) rate * (MAXR_DST / 340.29)); - rtn_rvb->r_buf = (long*)malloc( - sizeof(signed long int) * (rtn_rvb->r_buf_size + 1)); + rtn_rvb->r_buf = (int*)malloc( + sizeof(signed int) * (rtn_rvb->r_buf_size + 1)); rtn_rvb->r_out = 0; for (i = 0; i < 4; i++) { @@ -313,17 +313,17 @@ void _WM_free_reverb(struct _rvb *rvb) { free(rvb); } -void _WM_do_reverb(struct _rvb *rvb, signed long int *buffer, int size) { +void _WM_do_reverb(struct _rvb *rvb, signed int *buffer, int size) { int i, j, k; - signed long int l_buf_flt = 0; - signed long int r_buf_flt = 0; - signed long int l_rfl = 0; - signed long int r_rfl = 0; + signed int l_buf_flt = 0; + signed int r_buf_flt = 0; + signed int l_rfl = 0; + signed int r_rfl = 0; int vol_div = 64; for (i = 0; i < size; i += 2) { - signed long int tmp_l_val = 0; - signed long int tmp_r_val = 0; + signed int tmp_l_val = 0; + signed int tmp_r_val = 0; /* add the initial reflections from each speaker, 4 to go the left, 4 go to the right buffers diff --git a/src/wildmidi/reverb.h b/src/wildmidi/reverb.h index b8f5a6333..162de61eb 100644 --- a/src/wildmidi/reverb.h +++ b/src/wildmidi/reverb.h @@ -29,14 +29,14 @@ struct _rvb { /* filter data */ - signed long int l_buf_flt_in[8][6][2]; - signed long int l_buf_flt_out[8][6][2]; - signed long int r_buf_flt_in[8][6][2]; - signed long int r_buf_flt_out[8][6][2]; - signed long int coeff[8][6][5]; + signed int l_buf_flt_in[8][6][2]; + signed int l_buf_flt_out[8][6][2]; + signed int r_buf_flt_in[8][6][2]; + signed int r_buf_flt_out[8][6][2]; + signed int coeff[8][6][5]; /* buffer data */ - signed long int *l_buf; - signed long int *r_buf; + signed int *l_buf; + signed int *r_buf; int l_buf_size; int r_buf_size; int l_out; @@ -52,6 +52,6 @@ struct _rvb { extern void _WM_reset_reverb (struct _rvb *rvb); extern struct _rvb *_WM_init_reverb(int rate, float room_x, float room_y, float listen_x, float listen_y); extern void _WM_free_reverb (struct _rvb *rvb); - extern void _WM_do_reverb (struct _rvb *rvb, signed long int *buffer, int size); + extern void _WM_do_reverb (struct _rvb *rvb, signed int *buffer, int size); #endif /* __REVERB_H */ diff --git a/src/wildmidi/wildmidi_lib.cpp b/src/wildmidi/wildmidi_lib.cpp index b5ea6b887..b1ec8b459 100644 --- a/src/wildmidi/wildmidi_lib.cpp +++ b/src/wildmidi/wildmidi_lib.cpp @@ -182,7 +182,7 @@ struct _mdi { unsigned long int patch_count; signed short int amp; - signed long int *mix_buffer; + signed int *mix_buffer; unsigned long int mix_buffer_size; struct _rvb *reverb; @@ -3203,20 +3203,216 @@ _end: free(sysex_store); return NULL; } +static int *WM_Mix_Linear(midi * handle, int * buffer, unsigned long int count) +{ + struct _mdi *mdi = (struct _mdi *)handle; + unsigned long int data_pos; + signed int premix, left_mix, right_mix; + signed int vol_mul; + struct _note *note_data = NULL; + + do { + note_data = mdi->note; + left_mix = right_mix = 0; + if (__builtin_expect((note_data != NULL), 1)) { + while (note_data) { + /* + * =================== + * resample the sample + * =================== + */ + data_pos = note_data->sample_pos >> FPBITS; + vol_mul = ((note_data->vol_lvl + * (note_data->env_level >> 12)) >> FPBITS); + + premix = (note_data->sample->data[data_pos] + + ((note_data->sample->data[data_pos + 1] + - note_data->sample->data[data_pos]) + * (signed long int) (note_data->sample_pos + & FPMASK)>> FPBITS)) * vol_mul + / 1024; + + left_mix += premix + * mdi->channel[note_data->noteid >> 8].left_adjust; + right_mix += premix + * mdi->channel[note_data->noteid >> 8].right_adjust; + + /* + * ======================== + * sample position checking + * ======================== + */ + note_data->sample_pos += note_data->sample_inc; + if (__builtin_expect( + (note_data->sample_pos > note_data->sample->loop_end), + 0)) { + if (note_data->modes & SAMPLE_LOOP) { + note_data->sample_pos = + note_data->sample->loop_start + + ((note_data->sample_pos + - note_data->sample->loop_start) + % note_data->sample->loop_size); + } else if (__builtin_expect( + (note_data->sample_pos + >= note_data->sample->data_length), + 0)) { + if (__builtin_expect((note_data->replay == NULL), 1)) { + goto KILL_NOTE; + } + goto RESTART_NOTE; + } + } + + if (__builtin_expect((note_data->env_inc == 0), 0)) { + note_data = note_data->next; + continue; + } + + note_data->env_level += note_data->env_inc; + if (__builtin_expect((note_data->env_level > 4194304), 0)) { + note_data->env_level = + note_data->sample->env_target[note_data->env]; + } + if (__builtin_expect( + ((note_data->env_inc < 0) + && (note_data->env_level + > note_data->sample->env_target[note_data->env])) + || ((note_data->env_inc > 0) + && (note_data->env_level + < note_data->sample->env_target[note_data->env])), + 1)) { + note_data = note_data->next; + continue; + } + + note_data->env_level = + note_data->sample->env_target[note_data->env]; + switch (note_data->env) { + case 0: +#if 0 + if (!(note_data->modes & SAMPLE_ENVELOPE)) { + note_data->env_inc = 0; + note_data = note_data->next; + continue; + } +#endif + break; + case 2: + if (note_data->modes & SAMPLE_SUSTAIN) { + note_data->env_inc = 0; + note_data = note_data->next; + continue; + } else if (note_data->modes & SAMPLE_CLAMPED) { + note_data->env = 5; + if (note_data->env_level + > note_data->sample->env_target[5]) { + note_data->env_inc = + -note_data->sample->env_rate[5]; + } else { + note_data->env_inc = + note_data->sample->env_rate[5]; + } + continue; + } + break; + case 5: + if (__builtin_expect((note_data->env_level == 0), 1)) { + goto KILL_NOTE; + } + /* sample release */ + if (note_data->modes & SAMPLE_LOOP) + note_data->modes ^= SAMPLE_LOOP; + note_data->env_inc = 0; + note_data = note_data->next; + continue; + case 6: + if (__builtin_expect((note_data->replay != NULL), 1)) { + RESTART_NOTE: note_data->active = 0; + { + struct _note *prev_note = NULL; + struct _note *nte_array = mdi->note; + + if (nte_array != note_data) { + do { + prev_note = nte_array; + nte_array = nte_array->next; + } while (nte_array != note_data); + } + if (prev_note) { + prev_note->next = note_data->replay; + } else { + mdi->note = note_data->replay; + } + note_data->replay->next = note_data->next; + note_data = note_data->replay; + note_data->active = 1; + } + } else { + KILL_NOTE: note_data->active = 0; + { + struct _note *prev_note = NULL; + struct _note *nte_array = mdi->note; + + if (nte_array != note_data) { + do { + prev_note = nte_array; + nte_array = nte_array->next; + } while ((nte_array != note_data) + && (nte_array)); + } + if (prev_note) { + prev_note->next = note_data->next; + } else { + mdi->note = note_data->next; + } + note_data = note_data->next; + } + } + continue; + } + note_data->env++; + + if (note_data->is_off == 1) { + do_note_off_extra(note_data); + } + + if (note_data->env_level + > note_data->sample->env_target[note_data->env]) { + note_data->env_inc = + -note_data->sample->env_rate[note_data->env]; + } else { + note_data->env_inc = + note_data->sample->env_rate[note_data->env]; + } + note_data = note_data->next; + continue; + } + + /* + * ========================= + * mix the channels together + * ========================= + */ + left_mix /= 1024; + right_mix /= 1024; + } + + *buffer++ = left_mix; + *buffer++ = right_mix; + } while (--count); + return buffer; +} + static int WM_GetOutput_Linear(midi * handle, char * buffer, unsigned long int size) { unsigned long int buffer_used = 0; unsigned long int i; struct _mdi *mdi = (struct _mdi *) handle; unsigned long int real_samples_to_mix = 0; - unsigned long int data_pos; - signed long int premix, left_mix, right_mix; - signed long int vol_mul; - struct _note *note_data = NULL; - unsigned long int count; struct _event *event = mdi->current_event; - signed long int *tmp_buffer; - signed long int *out_buffer; + int *tmp_buffer; + int *out_buffer; + int left_mix, right_mix; _WM_Lock(&mdi->lock); @@ -3229,7 +3425,7 @@ static int WM_GetOutput_Linear(midi * handle, char * buffer, } else { mdi->mix_buffer_size = size / 2; } - mdi->mix_buffer = (long*)realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(signed long int)); + mdi->mix_buffer = (int*)realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(signed int)); } tmp_buffer = mdi->mix_buffer; @@ -3269,196 +3465,7 @@ static int WM_GetOutput_Linear(midi * handle, char * buffer, } /* do mixing here */ - count = real_samples_to_mix; - do { - note_data = mdi->note; - left_mix = right_mix = 0; - if (__builtin_expect((note_data != NULL), 1)) { - while (note_data) { - /* - * =================== - * resample the sample - * =================== - */ - data_pos = note_data->sample_pos >> FPBITS; - vol_mul = ((note_data->vol_lvl - * (note_data->env_level >> 12)) >> FPBITS); - - premix = (note_data->sample->data[data_pos] - + ((note_data->sample->data[data_pos + 1] - - note_data->sample->data[data_pos]) - * (signed long int) (note_data->sample_pos - & FPMASK)>> FPBITS)) * vol_mul - / 1024; - - left_mix += premix - * mdi->channel[note_data->noteid >> 8].left_adjust; - right_mix += premix - * mdi->channel[note_data->noteid >> 8].right_adjust; - - /* - * ======================== - * sample position checking - * ======================== - */ - note_data->sample_pos += note_data->sample_inc; - if (__builtin_expect( - (note_data->sample_pos > note_data->sample->loop_end), - 0)) { - if (note_data->modes & SAMPLE_LOOP) { - note_data->sample_pos = - note_data->sample->loop_start - + ((note_data->sample_pos - - note_data->sample->loop_start) - % note_data->sample->loop_size); - } else if (__builtin_expect( - (note_data->sample_pos - >= note_data->sample->data_length), - 0)) { - if (__builtin_expect((note_data->replay == NULL), 1)) { - goto KILL_NOTE; - } - goto RESTART_NOTE; - } - } - - if (__builtin_expect((note_data->env_inc == 0), 0)) { - note_data = note_data->next; - continue; - } - - note_data->env_level += note_data->env_inc; - if (__builtin_expect((note_data->env_level > 4194304), 0)) { - note_data->env_level = - note_data->sample->env_target[note_data->env]; - } - if (__builtin_expect( - ((note_data->env_inc < 0) - && (note_data->env_level - > note_data->sample->env_target[note_data->env])) - || ((note_data->env_inc > 0) - && (note_data->env_level - < note_data->sample->env_target[note_data->env])), - 1)) { - note_data = note_data->next; - continue; - } - - note_data->env_level = - note_data->sample->env_target[note_data->env]; - switch (note_data->env) { - case 0: -#if 0 - if (!(note_data->modes & SAMPLE_ENVELOPE)) { - note_data->env_inc = 0; - note_data = note_data->next; - continue; - } -#endif - break; - case 2: - if (note_data->modes & SAMPLE_SUSTAIN) { - note_data->env_inc = 0; - note_data = note_data->next; - continue; - } else if (note_data->modes & SAMPLE_CLAMPED) { - note_data->env = 5; - if (note_data->env_level - > note_data->sample->env_target[5]) { - note_data->env_inc = - -note_data->sample->env_rate[5]; - } else { - note_data->env_inc = - note_data->sample->env_rate[5]; - } - continue; - } - break; - case 5: - if (__builtin_expect((note_data->env_level == 0), 1)) { - goto KILL_NOTE; - } - /* sample release */ - if (note_data->modes & SAMPLE_LOOP) - note_data->modes ^= SAMPLE_LOOP; - note_data->env_inc = 0; - note_data = note_data->next; - continue; - case 6: - if (__builtin_expect((note_data->replay != NULL), 1)) { - RESTART_NOTE: note_data->active = 0; - { - struct _note *prev_note = NULL; - struct _note *nte_array = mdi->note; - - if (nte_array != note_data) { - do { - prev_note = nte_array; - nte_array = nte_array->next; - } while (nte_array != note_data); - } - if (prev_note) { - prev_note->next = note_data->replay; - } else { - mdi->note = note_data->replay; - } - note_data->replay->next = note_data->next; - note_data = note_data->replay; - note_data->active = 1; - } - } else { - KILL_NOTE: note_data->active = 0; - { - struct _note *prev_note = NULL; - struct _note *nte_array = mdi->note; - - if (nte_array != note_data) { - do { - prev_note = nte_array; - nte_array = nte_array->next; - } while ((nte_array != note_data) - && (nte_array)); - } - if (prev_note) { - prev_note->next = note_data->next; - } else { - mdi->note = note_data->next; - } - note_data = note_data->next; - } - } - continue; - } - note_data->env++; - - if (note_data->is_off == 1) { - do_note_off_extra(note_data); - } - - if (note_data->env_level - > note_data->sample->env_target[note_data->env]) { - note_data->env_inc = - -note_data->sample->env_rate[note_data->env]; - } else { - note_data->env_inc = - note_data->sample->env_rate[note_data->env]; - } - note_data = note_data->next; - continue; - } - - /* - * ========================= - * mix the channels together - * ========================= - */ - left_mix /= 1024; - right_mix /= 1024; - } - - *tmp_buffer++ = left_mix; - *tmp_buffer++ = right_mix; - } while (--count); + tmp_buffer = WM_Mix_Linear(handle, tmp_buffer, real_samples_to_mix); buffer_used += real_samples_to_mix * 4; size -= (real_samples_to_mix << 2); @@ -3503,25 +3510,254 @@ static int WM_GetOutput_Linear(midi * handle, char * buffer, return buffer_used; } +static int *WM_Mix_Gauss(midi * handle, int * buffer, unsigned long int count) +{ + struct _mdi *mdi = (struct _mdi *)handle; + unsigned long int data_pos; + signed int premix, left_mix, right_mix; + signed int vol_mul; + struct _note *note_data = NULL; + signed short int *sptr; + double y, xd; + double *gptr, *gend; + int left, right, temp_n; + int ii, jj; + + do { + note_data = mdi->note; + left_mix = right_mix = 0; + if (__builtin_expect((note_data != NULL), 1)) { + while (note_data) { + /* + * =================== + * resample the sample + * =================== + */ + data_pos = note_data->sample_pos >> FPBITS; + vol_mul = ((note_data->vol_lvl + * (note_data->env_level >> 12)) >> FPBITS); + + /* check to see if we're near one of the ends */ + left = data_pos; + right = (note_data->sample->data_length >> FPBITS) - left + - 1; + temp_n = (right << 1) - 1; + if (temp_n <= 0) + temp_n = 1; + if (temp_n > (left << 1) + 1) + temp_n = (left << 1) + 1; + + /* use Newton if we can't fill the window */ + if (temp_n < gauss_n) { + xd = note_data->sample_pos & FPMASK; + xd /= (1L << FPBITS); + xd += temp_n >> 1; + y = 0; + sptr = note_data->sample->data + + (note_data->sample_pos >> FPBITS) + - (temp_n >> 1); + for (ii = temp_n; ii;) { + for (jj = 0; jj <= ii; jj++) + y += sptr[jj] * newt_coeffs[ii][jj]; + y *= xd - --ii; + } + y += *sptr; + } else { /* otherwise, use Gauss as usual */ + y = 0; + gptr = &gauss_table[(note_data->sample_pos & FPMASK) * + (gauss_n + 1)]; + gend = gptr + gauss_n; + sptr = note_data->sample->data + + (note_data->sample_pos >> FPBITS) + - (gauss_n >> 1); + do { + y += *(sptr++) * *(gptr++); + } while (gptr <= gend); + } + + premix = (long) (y * vol_mul / 1024); + + left_mix += premix + * mdi->channel[note_data->noteid >> 8].left_adjust; + right_mix += premix + * mdi->channel[note_data->noteid >> 8].right_adjust; + + /* + * ======================== + * sample position checking + * ======================== + */ + note_data->sample_pos += note_data->sample_inc; + if (__builtin_expect( + (note_data->sample_pos > note_data->sample->loop_end), + 0)) { + if (note_data->modes & SAMPLE_LOOP) { + note_data->sample_pos = + note_data->sample->loop_start + + ((note_data->sample_pos + - note_data->sample->loop_start) + % note_data->sample->loop_size); + } else if (__builtin_expect( + (note_data->sample_pos + >= note_data->sample->data_length), + 0)) { + if (__builtin_expect((note_data->replay == NULL), 1)) { + goto KILL_NOTE; + } + goto RESTART_NOTE; + } + } + + if (__builtin_expect((note_data->env_inc == 0), 0)) { + note_data = note_data->next; + continue; + } + + note_data->env_level += note_data->env_inc; + if (__builtin_expect((note_data->env_level > 4194304), 0)) { + note_data->env_level = + note_data->sample->env_target[note_data->env]; + } + if (__builtin_expect( + ((note_data->env_inc < 0) + && (note_data->env_level + > note_data->sample->env_target[note_data->env])) + || ((note_data->env_inc > 0) + && (note_data->env_level + < note_data->sample->env_target[note_data->env])), + 1)) { + note_data = note_data->next; + continue; + } + + note_data->env_level = + note_data->sample->env_target[note_data->env]; + switch (note_data->env) { + case 0: +#if 0 + if (!(note_data->modes & SAMPLE_ENVELOPE)) { + note_data->env_inc = 0; + note_data = note_data->next; + continue; + } +#endif + break; + case 2: + if (note_data->modes & SAMPLE_SUSTAIN) { + note_data->env_inc = 0; + note_data = note_data->next; + continue; + } else if (note_data->modes & SAMPLE_CLAMPED) { + note_data->env = 5; + if (note_data->env_level + > note_data->sample->env_target[5]) { + note_data->env_inc = + -note_data->sample->env_rate[5]; + } else { + note_data->env_inc = + note_data->sample->env_rate[5]; + } + continue; + } + break; + case 5: + if (__builtin_expect((note_data->env_level == 0), 1)) { + goto KILL_NOTE; + } + /* sample release */ + if (note_data->modes & SAMPLE_LOOP) + note_data->modes ^= SAMPLE_LOOP; + note_data->env_inc = 0; + note_data = note_data->next; + continue; + case 6: + if (__builtin_expect((note_data->replay != NULL), 1)) { + RESTART_NOTE: note_data->active = 0; + { + struct _note *prev_note = NULL; + struct _note *nte_array = mdi->note; + + if (nte_array != note_data) { + do { + prev_note = nte_array; + nte_array = nte_array->next; + } while (nte_array != note_data); + } + if (prev_note) { + prev_note->next = note_data->replay; + } else { + mdi->note = note_data->replay; + } + note_data->replay->next = note_data->next; + note_data = note_data->replay; + note_data->active = 1; + } + } else { + KILL_NOTE: note_data->active = 0; + { + struct _note *prev_note = NULL; + struct _note *nte_array = mdi->note; + + if (nte_array != note_data) { + do { + prev_note = nte_array; + nte_array = nte_array->next; + } while ((nte_array != note_data) + && (nte_array)); + } + if (prev_note) { + prev_note->next = note_data->next; + } else { + mdi->note = note_data->next; + } + note_data = note_data->next; + } + } + continue; + } + note_data->env++; + + if (note_data->is_off == 1) { + do_note_off_extra(note_data); + } + + if (note_data->env_level + > note_data->sample->env_target[note_data->env]) { + note_data->env_inc = + -note_data->sample->env_rate[note_data->env]; + } else { + note_data->env_inc = + note_data->sample->env_rate[note_data->env]; + } + note_data = note_data->next; + continue; + } + + /* + * ========================= + * mix the channels together + * ========================= + */ + left_mix /= 1024; + right_mix /= 1024; + } + + *buffer++ = left_mix; + *buffer++ = right_mix; + } while (--count); + return buffer; +} + static int WM_GetOutput_Gauss(midi * handle, char * buffer, unsigned long int size) { unsigned long int buffer_used = 0; unsigned long int i; struct _mdi *mdi = (struct _mdi *) handle; unsigned long int real_samples_to_mix = 0; - unsigned long int data_pos; - signed long int premix, left_mix, right_mix; - signed long int vol_mul; - struct _note *note_data = NULL; - unsigned long int count; - signed short int *sptr; - double y, xd; - double *gptr, *gend; - int left, right, temp_n; - int ii, jj; struct _event *event = mdi->current_event; - signed long int *tmp_buffer; - signed long int *out_buffer; + signed int *tmp_buffer; + signed int *out_buffer; + signed int left_mix, right_mix; _WM_Lock(&mdi->lock); @@ -3533,7 +3769,7 @@ static int WM_GetOutput_Gauss(midi * handle, char * buffer, } else { mdi->mix_buffer_size = size / 2; } - mdi->mix_buffer = (long*)realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(signed long int)); + mdi->mix_buffer = (int*)realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(signed int)); } tmp_buffer = mdi->mix_buffer; memset(tmp_buffer, 0, ((size / 2) * sizeof(signed long int))); @@ -3571,229 +3807,7 @@ static int WM_GetOutput_Gauss(midi * handle, char * buffer, } /* do mixing here */ - count = real_samples_to_mix; - do { - note_data = mdi->note; - left_mix = right_mix = 0; - if (__builtin_expect((note_data != NULL), 1)) { - while (note_data) { - /* - * =================== - * resample the sample - * =================== - */ - data_pos = note_data->sample_pos >> FPBITS; - vol_mul = ((note_data->vol_lvl - * (note_data->env_level >> 12)) >> FPBITS); - - /* check to see if we're near one of the ends */ - left = data_pos; - right = (note_data->sample->data_length >> FPBITS) - left - - 1; - temp_n = (right << 1) - 1; - if (temp_n <= 0) - temp_n = 1; - if (temp_n > (left << 1) + 1) - temp_n = (left << 1) + 1; - - /* use Newton if we can't fill the window */ - if (temp_n < gauss_n) { - xd = note_data->sample_pos & FPMASK; - xd /= (1L << FPBITS); - xd += temp_n >> 1; - y = 0; - sptr = note_data->sample->data - + (note_data->sample_pos >> FPBITS) - - (temp_n >> 1); - for (ii = temp_n; ii;) { - for (jj = 0; jj <= ii; jj++) - y += sptr[jj] * newt_coeffs[ii][jj]; - y *= xd - --ii; - } - y += *sptr; - } else { /* otherwise, use Gauss as usual */ - y = 0; - gptr = &gauss_table[(note_data->sample_pos & FPMASK) * - (gauss_n + 1)]; - gend = gptr + gauss_n; - sptr = note_data->sample->data - + (note_data->sample_pos >> FPBITS) - - (gauss_n >> 1); - do { - y += *(sptr++) * *(gptr++); - } while (gptr <= gend); - } - - premix = (long) (y * vol_mul / 1024); - - left_mix += premix - * mdi->channel[note_data->noteid >> 8].left_adjust; - right_mix += premix - * mdi->channel[note_data->noteid >> 8].right_adjust; - - /* - * ======================== - * sample position checking - * ======================== - */ - note_data->sample_pos += note_data->sample_inc; - if (__builtin_expect( - (note_data->sample_pos > note_data->sample->loop_end), - 0)) { - if (note_data->modes & SAMPLE_LOOP) { - note_data->sample_pos = - note_data->sample->loop_start - + ((note_data->sample_pos - - note_data->sample->loop_start) - % note_data->sample->loop_size); - } else if (__builtin_expect( - (note_data->sample_pos - >= note_data->sample->data_length), - 0)) { - if (__builtin_expect((note_data->replay == NULL), 1)) { - goto KILL_NOTE; - } - goto RESTART_NOTE; - } - } - - if (__builtin_expect((note_data->env_inc == 0), 0)) { - note_data = note_data->next; - continue; - } - - note_data->env_level += note_data->env_inc; - if (__builtin_expect((note_data->env_level > 4194304), 0)) { - note_data->env_level = - note_data->sample->env_target[note_data->env]; - } - if (__builtin_expect( - ((note_data->env_inc < 0) - && (note_data->env_level - > note_data->sample->env_target[note_data->env])) - || ((note_data->env_inc > 0) - && (note_data->env_level - < note_data->sample->env_target[note_data->env])), - 1)) { - note_data = note_data->next; - continue; - } - - note_data->env_level = - note_data->sample->env_target[note_data->env]; - switch (note_data->env) { - case 0: -#if 0 - if (!(note_data->modes & SAMPLE_ENVELOPE)) { - note_data->env_inc = 0; - note_data = note_data->next; - continue; - } -#endif - break; - case 2: - if (note_data->modes & SAMPLE_SUSTAIN) { - note_data->env_inc = 0; - note_data = note_data->next; - continue; - } else if (note_data->modes & SAMPLE_CLAMPED) { - note_data->env = 5; - if (note_data->env_level - > note_data->sample->env_target[5]) { - note_data->env_inc = - -note_data->sample->env_rate[5]; - } else { - note_data->env_inc = - note_data->sample->env_rate[5]; - } - continue; - } - break; - case 5: - if (__builtin_expect((note_data->env_level == 0), 1)) { - goto KILL_NOTE; - } - /* sample release */ - if (note_data->modes & SAMPLE_LOOP) - note_data->modes ^= SAMPLE_LOOP; - note_data->env_inc = 0; - note_data = note_data->next; - continue; - case 6: - if (__builtin_expect((note_data->replay != NULL), 1)) { - RESTART_NOTE: note_data->active = 0; - { - struct _note *prev_note = NULL; - struct _note *nte_array = mdi->note; - - if (nte_array != note_data) { - do { - prev_note = nte_array; - nte_array = nte_array->next; - } while (nte_array != note_data); - } - if (prev_note) { - prev_note->next = note_data->replay; - } else { - mdi->note = note_data->replay; - } - note_data->replay->next = note_data->next; - note_data = note_data->replay; - note_data->active = 1; - } - } else { - KILL_NOTE: note_data->active = 0; - { - struct _note *prev_note = NULL; - struct _note *nte_array = mdi->note; - - if (nte_array != note_data) { - do { - prev_note = nte_array; - nte_array = nte_array->next; - } while ((nte_array != note_data) - && (nte_array)); - } - if (prev_note) { - prev_note->next = note_data->next; - } else { - mdi->note = note_data->next; - } - note_data = note_data->next; - } - } - continue; - } - note_data->env++; - - if (note_data->is_off == 1) { - do_note_off_extra(note_data); - } - - if (note_data->env_level - > note_data->sample->env_target[note_data->env]) { - note_data->env_inc = - -note_data->sample->env_rate[note_data->env]; - } else { - note_data->env_inc = - note_data->sample->env_rate[note_data->env]; - } - note_data = note_data->next; - continue; - } - - /* - * ========================= - * mix the channels together - * ========================= - */ - left_mix /= 1024; - right_mix /= 1024; - } - - *tmp_buffer++ = left_mix; - *tmp_buffer++ = right_mix; - } while (--count); + tmp_buffer = WM_Mix_Gauss(handle, tmp_buffer, real_samples_to_mix); buffer_used += real_samples_to_mix * 4; size -= (real_samples_to_mix << 2); From b1405921bf0503895ef25439e5ffbd1b5a293936 Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Mon, 28 Dec 2015 20:44:10 -0600 Subject: [PATCH 3/5] Merged WM_GetOutput_Linear and WM_GetOutput_Gauss into WM_DoGetOutput - With mixing moved into separate functions, these two functions became identical except for the function they called to do mixing. --- src/wildmidi/wildmidi_lib.cpp | 126 ++++------------------------------ 1 file changed, 12 insertions(+), 114 deletions(-) diff --git a/src/wildmidi/wildmidi_lib.cpp b/src/wildmidi/wildmidi_lib.cpp index b1ec8b459..6adaa0421 100644 --- a/src/wildmidi/wildmidi_lib.cpp +++ b/src/wildmidi/wildmidi_lib.cpp @@ -3403,115 +3403,10 @@ static int *WM_Mix_Linear(midi * handle, int * buffer, unsigned long int count) return buffer; } -static int WM_GetOutput_Linear(midi * handle, char * buffer, - unsigned long int size) { - unsigned long int buffer_used = 0; - unsigned long int i; - struct _mdi *mdi = (struct _mdi *) handle; - unsigned long int real_samples_to_mix = 0; - struct _event *event = mdi->current_event; - int *tmp_buffer; - int *out_buffer; - int left_mix, right_mix; - - _WM_Lock(&mdi->lock); - - buffer_used = 0; - memset(buffer, 0, size); - - if ( (size / 2) > mdi->mix_buffer_size) { - if ( (size / 2) <= ( mdi->mix_buffer_size * 2 )) { - mdi->mix_buffer_size += MEM_CHUNK; - } else { - mdi->mix_buffer_size = size / 2; - } - mdi->mix_buffer = (int*)realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(signed int)); - } - - tmp_buffer = mdi->mix_buffer; - - memset(tmp_buffer, 0, ((size / 2) * sizeof(signed long int))); - out_buffer = tmp_buffer; - - do { - if (__builtin_expect((!mdi->samples_to_mix), 0)) { - while ((!mdi->samples_to_mix) && (event->do_event)) { - event->do_event(mdi, &event->event_data); - event++; - mdi->samples_to_mix = event->samples_to_next; - mdi->current_event = event; - } - - if (!mdi->samples_to_mix) { - if (mdi->info.current_sample - >= mdi->info.approx_total_samples) { - break; - } else if ((mdi->info.approx_total_samples - - mdi->info.current_sample) > (size >> 2)) { - mdi->samples_to_mix = size >> 2; - } else { - mdi->samples_to_mix = mdi->info.approx_total_samples - - mdi->info.current_sample; - } - } - } - if (__builtin_expect((mdi->samples_to_mix > (size >> 2)), 1)) { - real_samples_to_mix = size >> 2; - } else { - real_samples_to_mix = mdi->samples_to_mix; - if (real_samples_to_mix == 0) { - continue; - } - } - - /* do mixing here */ - tmp_buffer = WM_Mix_Linear(handle, tmp_buffer, real_samples_to_mix); - - buffer_used += real_samples_to_mix * 4; - size -= (real_samples_to_mix << 2); - mdi->info.current_sample += real_samples_to_mix; - mdi->samples_to_mix -= real_samples_to_mix; - } while (size); - - tmp_buffer = out_buffer; - - if (mdi->info.mixer_options & WM_MO_REVERB) { - _WM_do_reverb(mdi->reverb, tmp_buffer, (buffer_used / 2)); - } - - for (i = 0; i < buffer_used; i += 4) { - left_mix = *tmp_buffer++; - right_mix = *tmp_buffer++; - - if (left_mix > 32767) { - left_mix = 32767; - } else if (left_mix < -32768) { - left_mix = -32768; - } - - if (right_mix > 32767) { - right_mix = 32767; - } else if (right_mix < -32768) { - right_mix = -32768; - } - - /* - * =================== - * Write to the buffer - * =================== - */ - (*buffer++) = left_mix & 0xff; - (*buffer++) = ((left_mix >> 8) & 0x7f) | ((left_mix >> 24) & 0x80); - (*buffer++) = right_mix & 0xff; - (*buffer++) = ((right_mix >> 8) & 0x7f) | ((right_mix >> 24) & 0x80); - } - - _WM_Unlock(&mdi->lock); - return buffer_used; -} - static int *WM_Mix_Gauss(midi * handle, int * buffer, unsigned long int count) { + if (!gauss_table) init_gauss(); + struct _mdi *mdi = (struct _mdi *)handle; unsigned long int data_pos; signed int premix, left_mix, right_mix; @@ -3748,7 +3643,7 @@ static int *WM_Mix_Gauss(midi * handle, int * buffer, unsigned long int count) return buffer; } -static int WM_GetOutput_Gauss(midi * handle, char * buffer, +static int WM_DoGetOutput(midi * handle, char * buffer, unsigned long int size) { unsigned long int buffer_used = 0; unsigned long int i; @@ -3807,7 +3702,14 @@ static int WM_GetOutput_Gauss(midi * handle, char * buffer, } /* do mixing here */ - tmp_buffer = WM_Mix_Gauss(handle, tmp_buffer, real_samples_to_mix); + if (mdi->info.mixer_options & WM_MO_ENHANCED_RESAMPLING) + { + tmp_buffer = WM_Mix_Gauss(handle, tmp_buffer, real_samples_to_mix); + } + else + { + tmp_buffer = WM_Mix_Linear(handle, tmp_buffer, real_samples_to_mix); + } buffer_used += real_samples_to_mix * 4; size -= (real_samples_to_mix << 2); @@ -4172,11 +4074,7 @@ WM_SYMBOL int WildMidi_GetOutput(midi * handle, char *buffer, unsigned long int "(size not a multiple of 4)", 0); return -1; } - if (((struct _mdi *) handle)->info.mixer_options & WM_MO_ENHANCED_RESAMPLING) { - if (!gauss_table) init_gauss(); - return WM_GetOutput_Gauss(handle, buffer, size); - } - return WM_GetOutput_Linear(handle, buffer, size); + return WM_DoGetOutput(handle, buffer, size); } WM_SYMBOL int WildMidi_SetOption(midi * handle, unsigned short int options, From ee46799d9ea7adec7aea5dee4e32df10bca4de96 Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Mon, 28 Dec 2015 20:51:53 -0600 Subject: [PATCH 4/5] Fix WM_DoGetOutput for big-endian machines - Besides being little-endian centric, this bit shifting madness was unneccessary since the values were already clamped to a 16-bit range, so all we need to do is cast them to a short. --- src/wildmidi/wildmidi_lib.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/wildmidi/wildmidi_lib.cpp b/src/wildmidi/wildmidi_lib.cpp index 6adaa0421..385ac6c1d 100644 --- a/src/wildmidi/wildmidi_lib.cpp +++ b/src/wildmidi/wildmidi_lib.cpp @@ -3744,10 +3744,9 @@ static int WM_DoGetOutput(midi * handle, char * buffer, * Write to the buffer * =================== */ - (*buffer++) = left_mix & 0xff; - (*buffer++) = ((left_mix >> 8) & 0x7f) | ((left_mix >> 24) & 0x80); - (*buffer++) = right_mix & 0xff; - (*buffer++) = ((right_mix >> 8) & 0x7f) | ((right_mix >> 24) & 0x80); + ((short *)buffer)[0] = (short)left_mix; + ((short *)buffer)[1] = (short)right_mix; + buffer += 4; } _WM_Unlock(&mdi->lock); return buffer_used; From afc36544b7e3ffdfe378343b42e2c6773a3514a5 Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Mon, 28 Dec 2015 22:07:51 -0600 Subject: [PATCH 5/5] Add a WildMidi softsynth device - This removes the preceding psuedo MIDI device for WildMidi. --- src/CMakeLists.txt | 2 +- src/sound/i_musicinterns.h | 39 ++--- src/sound/music_midi_wildmidi.cpp | 163 ------------------- src/sound/music_midistream.cpp | 2 +- src/sound/music_wildmidi_mididevice.cpp | 206 ++++++++++++++++++++++++ src/wildmidi/wildmidi_lib.cpp | 150 ++++++++++++++++- src/wildmidi/wildmidi_lib.h | 16 ++ zdoom.vcproj | 8 +- 8 files changed, 390 insertions(+), 196 deletions(-) delete mode 100644 src/sound/music_midi_wildmidi.cpp create mode 100644 src/sound/music_wildmidi_mididevice.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a86e96f01..18fdb992c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1086,12 +1086,12 @@ add_executable( zdoom WIN32 MACOSX_BUNDLE sound/music_midistream.cpp sound/music_midi_base.cpp sound/music_midi_timidity.cpp - sound/music_midi_wildmidi.cpp sound/music_mus_opl.cpp sound/music_stream.cpp sound/music_fluidsynth_mididevice.cpp sound/music_softsynth_mididevice.cpp sound/music_timidity_mididevice.cpp + sound/music_wildmidi_mididevice.cpp sound/music_win_mididevice.cpp sound/oalsound.cpp sound/sndfile_decoder.cpp diff --git a/src/sound/i_musicinterns.h b/src/sound/i_musicinterns.h index 4aa5f8254..03282ef37 100644 --- a/src/sound/i_musicinterns.h +++ b/src/sound/i_musicinterns.h @@ -219,25 +219,6 @@ protected: #endif }; -class WildMidiMIDIDevice : public PseudoMIDIDevice -{ -public: - WildMidiMIDIDevice(); - ~WildMidiMIDIDevice(); - - int Open(void (*callback)(unsigned int, void *, DWORD, DWORD), void *userdata); - bool Preprocess(MIDIStreamer *song, bool looping); - bool IsOpen() const; - -protected: - - midi *mMidi; - bool mLoop; - - static bool FillStream(SoundStream *stream, void *buff, int len, void *userdata); -}; - - // Base class for software synthesizer MIDI output devices ------------------ class SoftSynthMIDIDevice : public MIDIDevice @@ -350,6 +331,26 @@ protected: FILE *File; }; +// WildMidi implementation of a MIDI device --------------------------------- + +class WildMIDIDevice : public SoftSynthMIDIDevice +{ +public: + WildMIDIDevice(); + ~WildMIDIDevice(); + + int Open(void (*callback)(unsigned int, void *, DWORD, DWORD), void *userdata); + void PrecacheInstruments(const WORD *instruments, int count); + FString GetStats(); + +protected: + WildMidi_Renderer *Renderer; + + void HandleEvent(int status, int parm1, int parm2); + void HandleLongEvent(const BYTE *data, int len); + void ComputeOutput(float *buffer, int len); +}; + // FluidSynth implementation of a MIDI device ------------------------------- #ifdef HAVE_FLUIDSYNTH diff --git a/src/sound/music_midi_wildmidi.cpp b/src/sound/music_midi_wildmidi.cpp deleted file mode 100644 index f6df48459..000000000 --- a/src/sound/music_midi_wildmidi.cpp +++ /dev/null @@ -1,163 +0,0 @@ -#include "i_musicinterns.h" -#include "c_cvars.h" -#include "cmdlib.h" -#include "templates.h" -#include "version.h" - - -CVAR(String, wildmidi_config, "", CVAR_ARCHIVE | CVAR_GLOBALCONFIG) - -static FString currentConfig; - -// added because Timidity's output is rather loud. -CUSTOM_CVAR (Float, wildmidi_mastervolume, 1.0f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) -{ - if (self < 0.f) - self = 0.f; - else if (self > 1.f) - self = 1.f; -} - -CUSTOM_CVAR (Int, wildmidi_frequency, 44100, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) -{ // Clamp frequency to Timidity's limits - if (self < 11000) - self = 11000; - else if (self > 65000) - self = 65000; -} - -//========================================================================== -// -// WildMidiMIDIDevice Constructor -// -//========================================================================== - -WildMidiMIDIDevice::WildMidiMIDIDevice() -{ - mMidi = NULL; - mLoop = false; -} - -//========================================================================== -// -// WildMidiMIDIDevice Destructor -// -//========================================================================== - -WildMidiMIDIDevice::~WildMidiMIDIDevice () -{ - if (mMidi != NULL) WildMidi_Close(mMidi); - // do not shut down the device so that it can be reused for the next song being played. -} - -//========================================================================== -// -// WildMidiMIDIDevice :: Preprocess -// -//========================================================================== - -bool WildMidiMIDIDevice::Preprocess(MIDIStreamer *song, bool looping) -{ - TArray midi; - - // Write MIDI song to temporary file - song->CreateSMF(midi, looping ? 0 : 1); - - mMidi = WildMidi_OpenBuffer(&midi[0], midi.Size()); - if (mMidi == NULL) - { - Printf(PRINT_BOLD, "Could not open temp music file\n"); - } - mLoop = looping; - return false; -} - -//========================================================================== -// -// WildMidiMIDIDevice :: Open -// -//========================================================================== - -int WildMidiMIDIDevice::Open(void (*callback)(unsigned int, void *, DWORD, DWORD), void *userdata) -{ - if (currentConfig.CompareNoCase(wildmidi_config) != 0) - { - if (currentConfig.IsNotEmpty()) WildMidi_Shutdown(); - currentConfig = ""; - if (!WildMidi_Init(wildmidi_config, wildmidi_frequency, WM_MO_ENHANCED_RESAMPLING)) - { - currentConfig = wildmidi_config; - } - else - { - return 1; - } - } - - Stream = GSnd->CreateStream(FillStream, 32 * 1024, 0, wildmidi_frequency, this); - if (Stream == NULL) - { - Printf(PRINT_BOLD, "Could not create music stream.\n"); - return 1; - } - - return 0; -} - - -//========================================================================== -// -// WildMidiMIDIDevice :: FillStream -// -//========================================================================== - -bool WildMidiMIDIDevice::FillStream(SoundStream *stream, void *buff, int len, void *userdata) -{ - char *buffer = (char*)buff; - WildMidiMIDIDevice *song = (WildMidiMIDIDevice *)userdata; - if (song->mMidi != NULL) - { - while (len > 0) - { - int written = WildMidi_GetOutput(song->mMidi, buffer, len); - if (written < 0) - { - // error - memset(buffer, 0, len); - return false; - } - buffer += written; - len -= written; - - if (len > 0) - { - if (!song->mLoop) - { - memset(buffer, 0, len); - return written > 0; - } - else - { - // loop the sound (i.e. go back to start.) - unsigned long spos = 0; - WildMidi_FastSeek(song->mMidi, &spos); - } - - } - } - } - - return true; -} - -//========================================================================== -// -// WildMidiMIDIDevice :: IsOpen -// -//========================================================================== - -bool WildMidiMIDIDevice::IsOpen() const -{ - return mMidi != NULL; -} - diff --git a/src/sound/music_midistream.cpp b/src/sound/music_midistream.cpp index 42e8b5926..37616b9a6 100644 --- a/src/sound/music_midistream.cpp +++ b/src/sound/music_midistream.cpp @@ -294,7 +294,7 @@ MIDIDevice *MIDIStreamer::CreateMIDIDevice(EMidiDevice devtype) const return new TimidityPPMIDIDevice; case MDEV_WILDMIDI: - return new WildMidiMIDIDevice; + return new WildMIDIDevice; default: return NULL; diff --git a/src/sound/music_wildmidi_mididevice.cpp b/src/sound/music_wildmidi_mididevice.cpp new file mode 100644 index 000000000..44c060f26 --- /dev/null +++ b/src/sound/music_wildmidi_mididevice.cpp @@ -0,0 +1,206 @@ +/* +** music_wildmidi_mididevice.cpp +** Provides access to WildMidi as a generic MIDI device. +** +**--------------------------------------------------------------------------- +** Copyright 2015 Randy Heit +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +*/ + +// HEADER FILES ------------------------------------------------------------ + +#include "i_musicinterns.h" +#include "templates.h" +#include "doomdef.h" +#include "m_swap.h" +#include "w_wad.h" +#include "v_text.h" + +// MACROS ------------------------------------------------------------------ + +// TYPES ------------------------------------------------------------------- + +// EXTERNAL FUNCTION PROTOTYPES -------------------------------------------- + +// PUBLIC FUNCTION PROTOTYPES ---------------------------------------------- + +// PRIVATE FUNCTION PROTOTYPES --------------------------------------------- + +// EXTERNAL DATA DECLARATIONS ---------------------------------------------- + +// PRIVATE DATA DEFINITIONS ------------------------------------------------ + +static FString CurrentConfig; + +// PUBLIC DATA DEFINITIONS ------------------------------------------------- + +CVAR(String, wildmidi_config, "", CVAR_ARCHIVE | CVAR_GLOBALCONFIG) +CVAR(Int, wildmidi_frequency, 0, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) + +// CODE -------------------------------------------------------------------- + +//========================================================================== +// +// WildMIDIDevice Constructor +// +//========================================================================== + +WildMIDIDevice::WildMIDIDevice() +{ + Renderer = NULL; + + if (wildmidi_frequency >= 11025 && wildmidi_frequency < 65536) + { // Use our own sample rate instead of the global one + SampleRate = wildmidi_frequency; + } + else + { // Else make sure we're not outside of WildMidi's range + SampleRate = clamp(SampleRate, 11025, 65535); + } + + if (CurrentConfig.CompareNoCase(wildmidi_config) != 0 || SampleRate != WildMidi_GetSampleRate()) + { + if (CurrentConfig.IsNotEmpty()) + { + WildMidi_Shutdown(); + CurrentConfig = ""; + } + if (!WildMidi_Init(wildmidi_config, SampleRate, WM_MO_ENHANCED_RESAMPLING)) + { + CurrentConfig = wildmidi_config; + } + } + if (CurrentConfig.IsNotEmpty()) + { + Renderer = new WildMidi_Renderer(); + } +} + +//========================================================================== +// +// WildMIDIDevice Destructor +// +//========================================================================== + +WildMIDIDevice::~WildMIDIDevice() +{ + Close(); + if (Renderer != NULL) + { + delete Renderer; + } + // Do not shut down the device so that it can be reused for the next song being played. +} + +//========================================================================== +// +// WildMIDIDevice :: Open +// +// Returns 0 on success. +// +//========================================================================== + +int WildMIDIDevice::Open(void (*callback)(unsigned int, void *, DWORD, DWORD), void *userdata) +{ + if (Renderer == NULL) + { + return 1; + } + int ret = OpenStream(2, 0, callback, userdata); + if (ret == 0) + { +// Renderer->Reset(); + } + return ret; +} + +//========================================================================== +// +// WildMIDIDevice :: PrecacheInstruments +// +// Each entry is packed as follows: +// Bits 0- 6: Instrument number +// Bits 7-13: Bank number +// Bit 14: Select drum set if 1, tone bank if 0 +// +//========================================================================== + +void WildMIDIDevice::PrecacheInstruments(const WORD *instruments, int count) +{ + for (int i = 0; i < count; ++i) + { + Renderer->LoadInstrument((instruments[i] >> 7) & 127, instruments[i] >> 14, instruments[i] & 127); + } +} + + +//========================================================================== +// +// WildMIDIDevice :: HandleEvent +// +//========================================================================== + +void WildMIDIDevice::HandleEvent(int status, int parm1, int parm2) +{ + Renderer->ShortEvent(status, parm1, parm2); +} + +//========================================================================== +// +// WildMIDIDevice :: HandleLongEvent +// +//========================================================================== + +void WildMIDIDevice::HandleLongEvent(const BYTE *data, int len) +{ + Renderer->LongEvent((const char *)data, len); +} + +//========================================================================== +// +// WildMIDIDevice :: ComputeOutput +// +//========================================================================== + +void WildMIDIDevice::ComputeOutput(float *buffer, int len) +{ + Renderer->ComputeOutput(buffer, len); +} + +//========================================================================== +// +// WildMIDIDevice :: GetStats +// +//========================================================================== + +FString WildMIDIDevice::GetStats() +{ + FString out; + out.Format("%3d voices", Renderer->GetVoiceCount()); + return out; +} diff --git a/src/wildmidi/wildmidi_lib.cpp b/src/wildmidi/wildmidi_lib.cpp index 385ac6c1d..f20420e75 100644 --- a/src/wildmidi/wildmidi_lib.cpp +++ b/src/wildmidi/wildmidi_lib.cpp @@ -3643,6 +3643,18 @@ static int *WM_Mix_Gauss(midi * handle, int * buffer, unsigned long int count) return buffer; } +int *WM_Mix(midi *handle, int *buffer, unsigned long count) +{ + if (((struct _mdi *)handle)->info.mixer_options & WM_MO_ENHANCED_RESAMPLING) + { + return WM_Mix_Gauss(handle, buffer, count); + } + else + { + return WM_Mix_Linear(handle, buffer, count); + } +} + static int WM_DoGetOutput(midi * handle, char * buffer, unsigned long int size) { unsigned long int buffer_used = 0; @@ -3702,14 +3714,7 @@ static int WM_DoGetOutput(midi * handle, char * buffer, } /* do mixing here */ - if (mdi->info.mixer_options & WM_MO_ENHANCED_RESAMPLING) - { - tmp_buffer = WM_Mix_Gauss(handle, tmp_buffer, real_samples_to_mix); - } - else - { - tmp_buffer = WM_Mix_Linear(handle, tmp_buffer, real_samples_to_mix); - } + tmp_buffer = WM_Mix(handle, tmp_buffer, real_samples_to_mix); buffer_used += real_samples_to_mix * 4; size -= (real_samples_to_mix << 2); @@ -3807,6 +3812,11 @@ WM_SYMBOL int WildMidi_Init(const char * config_file, unsigned short int rate, return 0; } +WM_SYMBOL int WildMidi_GetSampleRate(void) +{ + return _WM_SampleRate; +} + WM_SYMBOL int WildMidi_MasterVolume(unsigned char master_volume) { struct _mdi *mdi = NULL; struct _hndl * tmp_handle = first_handle; @@ -3947,6 +3957,23 @@ WildMidi_OpenBuffer(unsigned char *midibuffer, unsigned long int size) { return ret; } +midi *WildMidi_NewMidi() { + midi * ret = NULL; + + if (!WM_Initialized) { + _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0); + return NULL; + } + ret = Init_MDI(); + if (ret) { + if (add_handle(ret) != 0) { + WildMidi_Close(ret); + ret = NULL; + } + } + return ret; +} + WM_SYMBOL int WildMidi_FastSeek(midi * handle, unsigned long int *sample_pos) { struct _mdi *mdi; struct _event *event; @@ -4197,3 +4224,110 @@ WM_SYMBOL int WildMidi_Shutdown(void) { return 0; } + +WildMidi_Renderer::WildMidi_Renderer() +{ + handle = WildMidi_NewMidi(); +} + +WildMidi_Renderer::~WildMidi_Renderer() +{ + WildMidi_Close((midi *)handle); +} + +void WildMidi_Renderer::ShortEvent(int status, int parm1, int parm2) +{ + _mdi *mdi = (_mdi *)handle; + _event_data ev; + + ev.channel = status & 0x0F; + switch ((status & 0xF0) >> 4) // command + { + case 0x8: + ev.data = (parm1 << 8) | parm2; + do_note_off(mdi, &ev); + break; + + case 0x9: + ev.data = (parm1 << 8) | parm2; + do_note_on(mdi, &ev); + break; + + case 0xA: + ev.data = (parm1 << 8) | parm2; + do_aftertouch(mdi, &ev); + break; + + case 0xC: + ev.data = parm1; + do_patch(mdi, &ev); + break; + + case 0xD: + ev.data = parm1; + do_channel_pressure(mdi, &ev); + break; + + case 0xE: + ev.data = parm1 | (parm2 << 7); + do_pitch(mdi, &ev); + break; + + case 0xB: // Controllers + ev.data = parm2; + switch (parm1) + { + case 0: do_control_bank_select(mdi, &ev); break; + case 6: do_control_data_entry_course(mdi, &ev); break; // [sic] + case 7: do_control_channel_volume(mdi, &ev); break; + case 8: do_control_channel_balance(mdi, &ev); break; + case 10: do_control_channel_pan(mdi, &ev); break; + case 11: do_control_channel_expression(mdi, &ev); break; + case 38: do_control_data_entry_fine(mdi, &ev); break; + case 64: do_control_channel_hold(mdi, &ev); break; + case 96: do_control_data_increment(mdi, &ev); break; + case 97: do_control_data_decrement(mdi, &ev); break; + case 98: + case 99: do_control_non_registered_param(mdi, &ev); break; + case 100: do_control_registered_param_fine(mdi, &ev); break; + case 101: do_control_registered_param_course(mdi, &ev); break; // [sic] + case 120: do_control_channel_sound_off(mdi, &ev); break; + case 121: do_control_channel_controllers_off(mdi, &ev); break; + case 123: do_control_channel_notes_off(mdi, &ev); break; + } + } +} + +void WildMidi_Renderer::LongEvent(const char *data, int len) +{ +} + +void WildMidi_Renderer::ComputeOutput(float *fbuffer, int len) +{ + _mdi *mdi = (_mdi *)handle; + int *buffer = (int *)fbuffer; + int *newbuf = WM_Mix(handle, buffer, len); +// assert(newbuf - buffer == len); + if (mdi->info.mixer_options & WM_MO_REVERB) { + _WM_do_reverb(mdi->reverb, buffer, len * 2); + } + for (; buffer < newbuf; ++buffer) + { + *(float *)buffer = (float)*buffer / 32768.f; + } +} + +void WildMidi_Renderer::LoadInstrument(int bank, int percussion, int instr) +{ + load_patch((_mdi *)handle, (bank << 8) | instr | (percussion ? 0x80 : 0)); +} + +int WildMidi_Renderer::GetVoiceCount() +{ + int count = 0; + for (_note *note_data = ((_mdi *)handle)->note; note_data != NULL; note_data = note_data->next) + { + count++; + } + return count; +} diff --git a/src/wildmidi/wildmidi_lib.h b/src/wildmidi/wildmidi_lib.h index e6317a271..46a517d90 100644 --- a/src/wildmidi/wildmidi_lib.h +++ b/src/wildmidi/wildmidi_lib.h @@ -64,6 +64,7 @@ WM_SYMBOL struct _WM_Info * WildMidi_GetInfo (midi * handle); WM_SYMBOL int WildMidi_FastSeek (midi * handle, unsigned long int *sample_pos); WM_SYMBOL int WildMidi_Close (midi * handle); WM_SYMBOL int WildMidi_Shutdown (void); +WM_SYMBOL int WildMidi_GetSampleRate (void); /* #if defined(__cplusplus) @@ -71,5 +72,20 @@ WM_SYMBOL int WildMidi_Shutdown (void); #endif */ +class WildMidi_Renderer +{ +public: + WildMidi_Renderer(); + ~WildMidi_Renderer(); + + void ShortEvent(int status, int parm1, int parm2); + void LongEvent(const char *data, int len); + void ComputeOutput(float *buffer, int len); + void LoadInstrument(int bank, int percussion, int instr); + int GetVoiceCount(); +private: + void *handle; +}; + #endif /* WILDMIDI_LIB_H */ diff --git a/zdoom.vcproj b/zdoom.vcproj index 126efb7f5..a5d7bfb8e 100644 --- a/zdoom.vcproj +++ b/zdoom.vcproj @@ -2553,10 +2553,6 @@ RelativePath="src\sound\music_midi_timidity.cpp" > - - @@ -2597,6 +2593,10 @@ RelativePath=".\src\sound\music_timidity_mididevice.cpp" > + +