This commit is contained in:
Christoph Oelckers 2015-12-29 09:27:57 +01:00
commit c6b3b90f37
11 changed files with 908 additions and 735 deletions

View file

@ -1175,12 +1175,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

View file

@ -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

View file

@ -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<BYTE> 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;
}

View file

@ -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;

View file

@ -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;
}

View file

@ -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

View file

@ -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 */

View file

@ -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,73 +3203,14 @@ _end: free(sysex_store);
return NULL;
}
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;
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 long int premix, left_mix, right_mix;
signed long int vol_mul;
signed int premix, left_mix, right_mix;
signed 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;
_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 = (long*)realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(signed long 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 */
count = real_samples_to_mix;
do {
note_data = mdi->note;
left_mix = right_mix = 0;
@ -3456,122 +3397,27 @@ static int WM_GetOutput_Linear(midi * handle, char * buffer,
right_mix /= 1024;
}
*tmp_buffer++ = left_mix;
*tmp_buffer++ = right_mix;
*buffer++ = left_mix;
*buffer++ = right_mix;
} while (--count);
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;
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;
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 long int premix, left_mix, right_mix;
signed long int vol_mul;
signed int premix, left_mix, right_mix;
signed 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;
_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 = (long*)realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(signed long 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 */
count = real_samples_to_mix;
do {
note_data = mdi->note;
left_mix = right_mix = 0;
@ -3791,9 +3637,84 @@ static int WM_GetOutput_Gauss(midi * handle, char * buffer,
right_mix /= 1024;
}
*tmp_buffer++ = left_mix;
*tmp_buffer++ = right_mix;
*buffer++ = left_mix;
*buffer++ = right_mix;
} while (--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;
unsigned long int i;
struct _mdi *mdi = (struct _mdi *) handle;
unsigned long int real_samples_to_mix = 0;
struct _event *event = mdi->current_event;
signed int *tmp_buffer;
signed int *out_buffer;
signed 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(handle, tmp_buffer, real_samples_to_mix);
buffer_used += real_samples_to_mix * 4;
size -= (real_samples_to_mix << 2);
@ -3828,10 +3749,9 @@ static int WM_GetOutput_Gauss(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;
@ -3892,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;
@ -4032,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;
@ -4158,11 +4100,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,
@ -4286,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;
}

View file

@ -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 */

View file

@ -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]);
}
}

View file

@ -2593,6 +2593,10 @@
RelativePath=".\src\sound\music_timidity_mididevice.cpp"
>
</File>
<File
RelativePath=".\src\sound\music_wildmidi_mididevice.cpp"
>
</File>
<File
RelativePath=".\src\sound\music_win_mididevice.cpp"
>
@ -2789,6 +2793,70 @@
</File>
</Filter>
</Filter>
<Filter
Name="WildMidi"
>
<Filter
Name="Headers"
>
<File
RelativePath=".\src\wildmidi\common.h"
>
</File>
<File
RelativePath=".\src\wildmidi\file_io.h"
>
</File>
<File
RelativePath=".\src\wildmidi\gus_pat.h"
>
</File>
<File
RelativePath=".\src\wildmidi\lock.h"
>
</File>
<File
RelativePath=".\src\wildmidi\reverb.h"
>
</File>
<File
RelativePath=".\src\wildmidi\wildmidi_lib.h"
>
</File>
<File
RelativePath=".\src\wildmidi\wm_error.h"
>
</File>
</Filter>
<Filter
Name="Source"
>
<File
RelativePath=".\src\wildmidi\file_io.cpp"
>
</File>
<File
RelativePath=".\src\wildmidi\gus_pat.cpp"
>
</File>
<File
RelativePath=".\src\wildmidi\lock.cpp"
>
</File>
<File
RelativePath=".\src\wildmidi\reverb.cpp"
>
</File>
<File
RelativePath=".\src\wildmidi\wildmidi_lib.cpp"
>
</File>
<File
RelativePath=".\src\wildmidi\wm_error.cpp"
>
</File>
</Filter>
</Filter>
</Filter>
<Filter
Name="SDL Files"