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

File diff suppressed because it is too large Load diff

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"