2006-02-24 04:48:15 +00:00
|
|
|
#include "i_musicinterns.h"
|
|
|
|
#include "c_cvars.h"
|
|
|
|
#include "cmdlib.h"
|
2008-01-27 16:56:25 +00:00
|
|
|
#include "templates.h"
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <wordexp.h>
|
2009-04-07 07:06:07 +00:00
|
|
|
#include <signal.h>
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
int ChildQuit;
|
|
|
|
|
|
|
|
void ChildSigHandler (int signum)
|
|
|
|
{
|
|
|
|
ChildQuit = waitpid (-1, NULL, WNOHANG);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2010-11-04 03:47:49 +00:00
|
|
|
BOOL SafeTerminateProcess(HANDLE hProcess, UINT uExitCode);
|
|
|
|
|
2007-03-29 01:03:50 +00:00
|
|
|
static char TimidityTitle[] = "TiMidity (ZDoom Launched)";
|
2010-11-04 03:47:49 +00:00
|
|
|
const char TimidityPPMIDIDevice::EventName[] = "TiMidity Killer";
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
CVAR (String, timidity_exe, "timidity.exe", CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
#else
|
|
|
|
CVAR (String, timidity_exe, "timidity", CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
#endif
|
|
|
|
CVAR (String, timidity_extargs, "", CVAR_ARCHIVE|CVAR_GLOBALCONFIG) // extra args to pass to Timidity
|
|
|
|
CVAR (String, timidity_chorus, "0", CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
CVAR (String, timidity_reverb, "0", CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
CVAR (Bool, timidity_stereo, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
CVAR (Bool, timidity_8bit, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
CVAR (Bool, timidity_byteswap, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
2010-10-02 03:36:19 +00:00
|
|
|
|
2008-01-27 16:56:25 +00:00
|
|
|
// added because Timidity's output is rather loud.
|
|
|
|
CUSTOM_CVAR (Float, timidity_mastervolume, 1.0f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
{
|
|
|
|
if (self < 0.f)
|
|
|
|
self = 0.f;
|
|
|
|
else if (self > 4.f)
|
|
|
|
self = 4.f;
|
2008-03-09 16:30:36 +00:00
|
|
|
if (currSong != NULL)
|
VERY IMPORTANT NOTE FOR ANYBODY BUILDING FROM THE TRUNK: This commit adds support
for FMOD Ex while at the same time removing support for FMOD 3. Be sure to update
your SDKs. GCC users, be sure to do a "make cleandep && make clean" before
building, or you will likely get inexplicable errors.
- Fixed: If you wanted to make cleandep with MinGW, you had to specifically
specify Makefile.mingw as the makefile to use.
- Added a normalizer to the OPL synth. It helped bring up the volume a little,
but not nearly as much as I would have liked.
- Removed MIDI Mapper references. It doesn't work with the stream API, and
it doesn't really exist on NT kernels, either.
- Reworked music volume: Except for MIDI, all music volume is controlled
through GSnd and not at the individual song level.
- Removed the mididevice global variable.
- Removed snd_midivolume. Now that all music uses a linear volume scale,
there's no need for two separate music volume controls.
- Increased snd_samplerate default up to 48000.
- Added snd_format, defaulting to "PCM-16".
- Added snd_speakermode, defaulting to "Auto".
- Replaced snd_fpu with snd_resampler, defaulting to "Linear".
- Bumped the snd_channels default up from a pitiful 12 to 32.
- Changed snd_3d default to true. The new cvar snd_hw3d determines if
hardware 3D support is used and default to false.
- Removed the libFLAC source, since FMOD Ex has native FLAC support.
- Removed the altsound code, since it was terribly gimped in comparison to
the FMOD code. It's original purpose was to have been as a springboard for
writing a non-FMOD sound system for Unix-y systems, but that never
happened.
- Finished preliminary FMOD Ex support.
SVN r789 (trunk)
2008-03-09 03:13:49 +00:00
|
|
|
currSong->TimidityVolumeChanged();
|
2008-01-27 16:56:25 +00:00
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
CUSTOM_CVAR (Int, timidity_pipe, 90, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
2006-02-24 04:48:15 +00:00
|
|
|
{ // pipe size in ms
|
|
|
|
if (timidity_pipe < 0)
|
|
|
|
{ // a negative size makes no sense
|
|
|
|
timidity_pipe = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_CVAR (Int, timidity_frequency, 22050, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
{ // Clamp frequency to Timidity's limits
|
|
|
|
if (self < 4000)
|
|
|
|
self = 4000;
|
|
|
|
else if (self > 65000)
|
|
|
|
self = 65000;
|
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice Constructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
TimidityPPMIDIDevice::TimidityPPMIDIDevice()
|
|
|
|
: DiskName("zmid"),
|
2006-02-24 04:48:15 +00:00
|
|
|
#ifdef _WIN32
|
2010-10-02 03:36:19 +00:00
|
|
|
ReadWavePipe(INVALID_HANDLE_VALUE), WriteWavePipe(INVALID_HANDLE_VALUE),
|
|
|
|
ChildProcess(INVALID_HANDLE_VALUE),
|
|
|
|
Validated(false)
|
2006-02-24 04:48:15 +00:00
|
|
|
#else
|
2010-10-02 03:36:19 +00:00
|
|
|
ChildProcess(-1)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
WavePipe[0] = WavePipe[1] = -1;
|
2006-02-24 04:48:15 +00:00
|
|
|
#endif
|
2010-10-02 03:36:19 +00:00
|
|
|
|
|
|
|
if (DiskName == NULL)
|
|
|
|
{
|
|
|
|
Printf(PRINT_BOLD, "Could not create temp music file\n");
|
|
|
|
return;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice Destructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
TimidityPPMIDIDevice::~TimidityPPMIDIDevice ()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
#if _WIN32
|
|
|
|
if (WriteWavePipe != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle (WriteWavePipe);
|
|
|
|
WriteWavePipe = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
if (ReadWavePipe != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle (ReadWavePipe);
|
|
|
|
ReadWavePipe = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (WavePipe[1] != -1)
|
|
|
|
{
|
|
|
|
close (WavePipe[1]);
|
|
|
|
WavePipe[1] = -1;
|
|
|
|
}
|
|
|
|
if (WavePipe[0] != -1)
|
|
|
|
{
|
|
|
|
close (WavePipe[0]);
|
|
|
|
WavePipe[0] = -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: Preprocess
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool TimidityPPMIDIDevice::Preprocess(MIDIStreamer *song, bool looping)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
TArray<BYTE> midi;
|
2006-02-24 04:48:15 +00:00
|
|
|
bool success;
|
|
|
|
FILE *f;
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
if (CommandLine.IsEmpty())
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
return false;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
// Tell TiMidity++ whether it should loop or not
|
|
|
|
CommandLine.LockBuffer()[LoopPos] = looping ? 'l' : ' ';
|
|
|
|
CommandLine.UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
// Write MIDI song to temporary file
|
2012-03-11 03:56:20 +00:00
|
|
|
song->CreateSMF(midi, looping ? 0 : 1);
|
2006-04-14 12:58:52 +00:00
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
f = fopen(DiskName, "wb");
|
|
|
|
if (f == NULL)
|
2006-04-14 12:58:52 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "Could not open temp music file\n");
|
|
|
|
return false;
|
2006-04-14 12:58:52 +00:00
|
|
|
}
|
2010-10-02 03:36:19 +00:00
|
|
|
success = (fwrite(&midi[0], 1, midi.Size(), f) == (size_t)midi.Size());
|
|
|
|
fclose (f);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
if (!success)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "Could not write temp music file\n");
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2010-10-02 03:36:19 +00:00
|
|
|
return false;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: Open
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int TimidityPPMIDIDevice::Open(void (*callback)(unsigned int, void *, DWORD, DWORD), void *userdata)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
int pipeSize;
|
2008-03-28 00:38:17 +00:00
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
static SECURITY_ATTRIBUTES inheritable = { sizeof(inheritable), NULL, TRUE };
|
|
|
|
|
|
|
|
if (!Validated && !ValidateTimidity ())
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
return 101;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Validated = true;
|
|
|
|
#endif // WIN32
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
CommandLine.Format("%s %s -EFchorus=%s -EFreverb=%s -s%d ",
|
2006-02-24 04:48:15 +00:00
|
|
|
*timidity_exe, *timidity_extargs,
|
|
|
|
*timidity_chorus, *timidity_reverb, *timidity_frequency);
|
|
|
|
|
|
|
|
pipeSize = (timidity_pipe * timidity_frequency / 1000)
|
|
|
|
<< (timidity_stereo + !timidity_8bit);
|
2008-03-28 00:38:17 +00:00
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
// Round pipe size up to nearest power of 2 to try and avoid partial
|
|
|
|
// buffer reads in FillStream() under NT. This does not seem to be an
|
|
|
|
// issue under 9x.
|
|
|
|
int bitmask = pipeSize & -pipeSize;
|
|
|
|
|
|
|
|
while (bitmask < pipeSize)
|
|
|
|
bitmask <<= 1;
|
|
|
|
pipeSize = bitmask;
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
if (!CreatePipe(&ReadWavePipe, &WriteWavePipe, &inheritable, pipeSize))
|
2006-02-24 04:48:15 +00:00
|
|
|
#else // WIN32
|
|
|
|
if (pipe (WavePipe) == -1)
|
|
|
|
#endif
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "Could not create a data pipe for TiMidity++.\n");
|
2006-02-24 04:48:15 +00:00
|
|
|
pipeSize = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Stream = GSnd->CreateStream(FillStream, pipeSize,
|
2006-02-24 04:48:15 +00:00
|
|
|
(timidity_stereo ? 0 : SoundStream::Mono) |
|
|
|
|
(timidity_8bit ? SoundStream::Bits8 : 0),
|
|
|
|
timidity_frequency, this);
|
2010-10-02 03:36:19 +00:00
|
|
|
if (Stream == NULL)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "Could not create music stream.\n");
|
2006-02-24 04:48:15 +00:00
|
|
|
pipeSize = 0;
|
|
|
|
#ifdef _WIN32
|
2010-10-02 03:36:19 +00:00
|
|
|
CloseHandle(WriteWavePipe);
|
|
|
|
CloseHandle(ReadWavePipe);
|
2006-02-24 04:48:15 +00:00
|
|
|
ReadWavePipe = WriteWavePipe = INVALID_HANDLE_VALUE;
|
|
|
|
#else
|
2010-10-02 03:36:19 +00:00
|
|
|
close(WavePipe[1]);
|
|
|
|
close(WavePipe[0]);
|
2006-02-24 04:48:15 +00:00
|
|
|
WavePipe[0] = WavePipe[1] = -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pipeSize == 0)
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "If your soundcard cannot play more than one\n"
|
2006-02-24 04:48:15 +00:00
|
|
|
"wave at a time, you will hear no music.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-09 19:34:35 +00:00
|
|
|
CommandLine += "-o - -Ors";
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pipeSize == 0)
|
|
|
|
{
|
2006-04-09 19:34:35 +00:00
|
|
|
CommandLine += "-Od";
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2006-04-09 19:34:35 +00:00
|
|
|
CommandLine += timidity_stereo ? 'S' : 'M';
|
|
|
|
CommandLine += timidity_8bit ? '8' : '1';
|
2006-02-24 04:48:15 +00:00
|
|
|
if (timidity_byteswap)
|
|
|
|
{
|
2006-04-09 19:34:35 +00:00
|
|
|
CommandLine += 'x';
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2006-04-09 19:34:35 +00:00
|
|
|
LoopPos = CommandLine.Len() + 4;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2006-04-09 19:34:35 +00:00
|
|
|
CommandLine += " -idl ";
|
|
|
|
CommandLine += DiskName.GetName();
|
2010-10-02 03:36:19 +00:00
|
|
|
return 0;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: ValidateTimidity
|
|
|
|
//
|
2006-02-24 04:48:15 +00:00
|
|
|
// Check that this TiMidity++ knows about the TiMidity Killer event.
|
|
|
|
// If not, then we can't use it, because Win32 provides no other way
|
|
|
|
// to conveniently signal it to quit. The check is done by simply
|
|
|
|
// searching for the event's name somewhere in the executable.
|
2010-10-02 03:36:19 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
bool TimidityPPMIDIDevice::ValidateTimidity()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
char foundPath[MAX_PATH];
|
|
|
|
char *filePart;
|
|
|
|
DWORD pathLen;
|
|
|
|
DWORD fileLen;
|
|
|
|
HANDLE diskFile;
|
|
|
|
HANDLE mapping;
|
|
|
|
const BYTE *exeBase;
|
|
|
|
const BYTE *exeEnd;
|
|
|
|
const BYTE *exe;
|
|
|
|
bool good;
|
|
|
|
|
|
|
|
pathLen = SearchPath (NULL, timidity_exe, NULL, MAX_PATH, foundPath, &filePart);
|
|
|
|
if (pathLen == 0)
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "Please set the timidity_exe cvar to the location of TiMidity++\n");
|
2006-02-24 04:48:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (pathLen > MAX_PATH)
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "The path to TiMidity++ is too long\n");
|
2006-02-24 04:48:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
diskFile = CreateFile (foundPath, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
|
|
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
|
|
|
if (diskFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "Could not access %s\n", foundPath);
|
2006-02-24 04:48:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fileLen = GetFileSize (diskFile, NULL);
|
|
|
|
mapping = CreateFileMapping (diskFile, NULL, PAGE_READONLY, 0, 0, NULL);
|
|
|
|
if (mapping == NULL)
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "Could not create mapping for %s\n", foundPath);
|
2006-02-24 04:48:15 +00:00
|
|
|
CloseHandle (diskFile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
exeBase = (const BYTE *)MapViewOfFile (mapping, FILE_MAP_READ, 0, 0, 0);
|
|
|
|
if (exeBase == NULL)
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "Could not map %s\n", foundPath);
|
2006-02-24 04:48:15 +00:00
|
|
|
CloseHandle (mapping);
|
|
|
|
CloseHandle (diskFile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
good = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
for (exe = exeBase, exeEnd = exeBase+fileLen; exe < exeEnd; )
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
const char *tSpot = (const char *)memchr(exe, 'T', exeEnd - exe);
|
2006-02-24 04:48:15 +00:00
|
|
|
if (tSpot == NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-10-02 03:36:19 +00:00
|
|
|
if (memcmp(tSpot+1, EventName+1, sizeof(EventName)-1) == 0)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
good = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
exe = (const BYTE *)tSpot + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "Error reading %s\n", foundPath);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
if (!good)
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "ZDoom requires a special version of TiMidity++\n");
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
UnmapViewOfFile((LPVOID)exeBase);
|
|
|
|
CloseHandle(mapping);
|
|
|
|
CloseHandle(diskFile);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
return good;
|
|
|
|
}
|
|
|
|
#endif // _WIN32
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: LaunchTimidity
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool TimidityPPMIDIDevice::LaunchTimidity ()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
if (CommandLine.IsEmpty())
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
DPrintf ("cmd: \x1cG%s\n", CommandLine.GetChars());
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
STARTUPINFO startup = { sizeof(startup), };
|
|
|
|
PROCESS_INFORMATION procInfo;
|
|
|
|
|
|
|
|
startup.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
|
|
|
|
|
|
|
startup.hStdInput = INVALID_HANDLE_VALUE;
|
|
|
|
startup.hStdOutput = WriteWavePipe != INVALID_HANDLE_VALUE ?
|
|
|
|
WriteWavePipe : GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
startup.hStdError = GetStdHandle (STD_ERROR_HANDLE);
|
|
|
|
|
2007-03-29 01:03:50 +00:00
|
|
|
startup.lpTitle = TimidityTitle;
|
2006-02-24 04:48:15 +00:00
|
|
|
startup.wShowWindow = SW_SHOWMINNOACTIVE;
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
if (CreateProcess(NULL, CommandLine.LockBuffer(), NULL, NULL, TRUE,
|
2010-11-04 03:47:49 +00:00
|
|
|
DETACHED_PROCESS, NULL, NULL, &startup, &procInfo))
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
ChildProcess = procInfo.hProcess;
|
|
|
|
//SetThreadPriority (procInfo.hThread, THREAD_PRIORITY_HIGHEST);
|
2010-11-04 03:47:49 +00:00
|
|
|
CloseHandle(procInfo.hThread); // Don't care about the created thread
|
2006-05-16 02:50:18 +00:00
|
|
|
CommandLine.UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
CommandLine.UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
char hres[9];
|
|
|
|
LPTSTR msgBuf;
|
2010-10-02 03:36:19 +00:00
|
|
|
HRESULT err = GetLastError();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
if (!FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, err, 0, (LPTSTR)&msgBuf, 0, NULL))
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
mysnprintf(hres, countof(hres), "%08lx", err);
|
2006-02-24 04:48:15 +00:00
|
|
|
msgBuf = hres;
|
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
Printf(PRINT_BOLD, "Could not run timidity with the command line:\n%s\n"
|
2006-04-09 19:34:35 +00:00
|
|
|
"Reason: %s\n", CommandLine.GetChars(), msgBuf);
|
2006-02-24 04:48:15 +00:00
|
|
|
if (msgBuf != hres)
|
|
|
|
{
|
|
|
|
LocalFree (msgBuf);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
#else
|
2010-10-02 16:49:44 +00:00
|
|
|
if (WavePipe[0] != -1 && WavePipe[1] == -1 && Stream != NULL)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
// Timidity was previously launched, so the write end of the pipe
|
|
|
|
// is closed, and the read end is still open. Close the pipe
|
|
|
|
// completely and reopen it.
|
|
|
|
|
|
|
|
close (WavePipe[0]);
|
|
|
|
WavePipe[0] = -1;
|
2010-10-02 16:49:44 +00:00
|
|
|
delete Stream;
|
|
|
|
Stream = NULL;
|
|
|
|
Open (NULL, NULL);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int forkres;
|
|
|
|
wordexp_t words;
|
|
|
|
|
2006-04-09 19:34:35 +00:00
|
|
|
switch (wordexp (CommandLine.GetChars(), &words, 0))
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
case 0: // all good
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WRDE_NOSPACE:
|
|
|
|
wordfree (&words);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
forkres = fork ();
|
|
|
|
|
|
|
|
if (forkres == 0)
|
|
|
|
{
|
|
|
|
close (WavePipe[0]);
|
|
|
|
dup2 (WavePipe[1], STDOUT_FILENO);
|
|
|
|
freopen ("/dev/null", "r", stdin);
|
2008-03-27 04:25:52 +00:00
|
|
|
// freopen ("/dev/null", "w", stderr);
|
2006-02-24 04:48:15 +00:00
|
|
|
close (WavePipe[1]);
|
2006-04-12 01:50:09 +00:00
|
|
|
|
2008-03-28 00:38:17 +00:00
|
|
|
execvp (words.we_wordv[0], words.we_wordv);
|
2008-03-27 04:25:52 +00:00
|
|
|
fprintf(stderr,"execvp failed\n");
|
2012-03-12 04:05:15 +00:00
|
|
|
_exit (0); // if execvp succeeds, we never get here
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
else if (forkres < 0)
|
|
|
|
{
|
|
|
|
Printf (PRINT_BOLD, "Could not fork when trying to start timidity\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// printf ("child is %d\n", forkres);
|
|
|
|
ChildProcess = forkres;
|
|
|
|
close (WavePipe[1]);
|
2008-03-28 00:38:17 +00:00
|
|
|
WavePipe[1] = -1;
|
|
|
|
/* usleep(1000000);
|
|
|
|
if (waitpid(ChildProcess, NULL, WNOHANG) == ChildProcess)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Launching timidity failed\n");
|
2008-03-27 04:25:52 +00:00
|
|
|
}*/
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wordfree (&words);
|
|
|
|
return ChildProcess != -1;
|
|
|
|
#endif // _WIN32
|
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: FillStream
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool TimidityPPMIDIDevice::FillStream(SoundStream *stream, void *buff, int len, void *userdata)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
TimidityPPMIDIDevice *song = (TimidityPPMIDIDevice *)userdata;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD avail, got, didget;
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
if (!PeekNamedPipe(song->ReadWavePipe, NULL, 0, NULL, &avail, NULL) || avail == 0)
|
2006-02-24 04:48:15 +00:00
|
|
|
{ // If nothing is available from the pipe, play silence.
|
|
|
|
memset (buff, 0, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
didget = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
ReadFile(song->ReadWavePipe, (BYTE *)buff+didget, len-didget, &got, NULL);
|
2006-02-24 04:48:15 +00:00
|
|
|
didget += got;
|
|
|
|
if (didget >= (DWORD)len)
|
|
|
|
break;
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
// Give TiMidity++ a chance to output something more to the pipe
|
2006-02-24 04:48:15 +00:00
|
|
|
Sleep (10);
|
2010-10-02 03:36:19 +00:00
|
|
|
if (!PeekNamedPipe(song->ReadWavePipe, NULL, 0, NULL, &avail, NULL) || avail == 0)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
memset ((BYTE *)buff+didget, 0, len-didget);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2008-03-28 00:38:17 +00:00
|
|
|
ssize_t got;
|
|
|
|
fd_set rfds;
|
|
|
|
struct timeval tv;
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
if (ChildQuit == song->ChildProcess)
|
|
|
|
{
|
|
|
|
ChildQuit = 0;
|
2010-10-02 03:36:19 +00:00
|
|
|
fprintf(stderr, "child gone\n");
|
2006-02-24 04:48:15 +00:00
|
|
|
song->ChildProcess = -1;
|
|
|
|
return false;
|
|
|
|
}
|
2008-03-28 00:38:17 +00:00
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(song->WavePipe[0], &rfds);
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 50;
|
|
|
|
// fprintf(stderr,"select\n");
|
|
|
|
if (select(1, &rfds, NULL, NULL, &tv) <= 0 && 0)
|
|
|
|
{ // Nothing available, so play silence.
|
|
|
|
// fprintf(stderr,"nothing\n");
|
|
|
|
// memset(buff, 0, len);
|
|
|
|
return true;
|
|
|
|
}
|
2008-03-27 04:25:52 +00:00
|
|
|
// fprintf(stderr,"something\n");
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
got = read(song->WavePipe[0], (BYTE *)buff, len);
|
2008-03-27 04:25:52 +00:00
|
|
|
if (got < len)
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
memset((BYTE *)buff+got, 0, len-got);
|
2008-03-27 04:25:52 +00:00
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: TimidityVolumeChanged
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void TimidityPPMIDIDevice::TimidityVolumeChanged()
|
2008-01-27 16:56:25 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
if (Stream != NULL)
|
VERY IMPORTANT NOTE FOR ANYBODY BUILDING FROM THE TRUNK: This commit adds support
for FMOD Ex while at the same time removing support for FMOD 3. Be sure to update
your SDKs. GCC users, be sure to do a "make cleandep && make clean" before
building, or you will likely get inexplicable errors.
- Fixed: If you wanted to make cleandep with MinGW, you had to specifically
specify Makefile.mingw as the makefile to use.
- Added a normalizer to the OPL synth. It helped bring up the volume a little,
but not nearly as much as I would have liked.
- Removed MIDI Mapper references. It doesn't work with the stream API, and
it doesn't really exist on NT kernels, either.
- Reworked music volume: Except for MIDI, all music volume is controlled
through GSnd and not at the individual song level.
- Removed the mididevice global variable.
- Removed snd_midivolume. Now that all music uses a linear volume scale,
there's no need for two separate music volume controls.
- Increased snd_samplerate default up to 48000.
- Added snd_format, defaulting to "PCM-16".
- Added snd_speakermode, defaulting to "Auto".
- Replaced snd_fpu with snd_resampler, defaulting to "Linear".
- Bumped the snd_channels default up from a pitiful 12 to 32.
- Changed snd_3d default to true. The new cvar snd_hw3d determines if
hardware 3D support is used and default to false.
- Removed the libFLAC source, since FMOD Ex has native FLAC support.
- Removed the altsound code, since it was terribly gimped in comparison to
the FMOD code. It's original purpose was to have been as a springboard for
writing a non-FMOD sound system for Unix-y systems, but that never
happened.
- Finished preliminary FMOD Ex support.
SVN r789 (trunk)
2008-03-09 03:13:49 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Stream->SetVolume(timidity_mastervolume);
|
VERY IMPORTANT NOTE FOR ANYBODY BUILDING FROM THE TRUNK: This commit adds support
for FMOD Ex while at the same time removing support for FMOD 3. Be sure to update
your SDKs. GCC users, be sure to do a "make cleandep && make clean" before
building, or you will likely get inexplicable errors.
- Fixed: If you wanted to make cleandep with MinGW, you had to specifically
specify Makefile.mingw as the makefile to use.
- Added a normalizer to the OPL synth. It helped bring up the volume a little,
but not nearly as much as I would have liked.
- Removed MIDI Mapper references. It doesn't work with the stream API, and
it doesn't really exist on NT kernels, either.
- Reworked music volume: Except for MIDI, all music volume is controlled
through GSnd and not at the individual song level.
- Removed the mididevice global variable.
- Removed snd_midivolume. Now that all music uses a linear volume scale,
there's no need for two separate music volume controls.
- Increased snd_samplerate default up to 48000.
- Added snd_format, defaulting to "PCM-16".
- Added snd_speakermode, defaulting to "Auto".
- Replaced snd_fpu with snd_resampler, defaulting to "Linear".
- Bumped the snd_channels default up from a pitiful 12 to 32.
- Changed snd_3d default to true. The new cvar snd_hw3d determines if
hardware 3D support is used and default to false.
- Removed the libFLAC source, since FMOD Ex has native FLAC support.
- Removed the altsound code, since it was terribly gimped in comparison to
the FMOD code. It's original purpose was to have been as a springboard for
writing a non-FMOD sound system for Unix-y systems, but that never
happened.
- Finished preliminary FMOD Ex support.
SVN r789 (trunk)
2008-03-09 03:13:49 +00:00
|
|
|
}
|
2008-01-27 16:56:25 +00:00
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: IsOpen
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool TimidityPPMIDIDevice::IsOpen() const
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (ChildProcess != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
if (WaitForSingleObject(ChildProcess, 0) != WAIT_TIMEOUT)
|
|
|
|
{ // Timidity++ has quit
|
|
|
|
CloseHandle(ChildProcess);
|
|
|
|
const_cast<TimidityPPMIDIDevice *>(this)->ChildProcess = INVALID_HANDLE_VALUE;
|
2006-02-24 04:48:15 +00:00
|
|
|
#else
|
|
|
|
if (ChildProcess != -1)
|
|
|
|
{
|
|
|
|
if (waitpid (ChildProcess, NULL, WNOHANG) == ChildProcess)
|
|
|
|
{
|
2010-10-02 16:49:44 +00:00
|
|
|
const_cast<TimidityPPMIDIDevice *>(this)->ChildProcess = -1;
|
2006-02-24 04:48:15 +00:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: Resume
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int TimidityPPMIDIDevice::Resume()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
if (!Started)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
if (LaunchTimidity())
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
// Assume success if not mixing with FMOD
|
|
|
|
if (Stream == NULL || Stream->Play(true, timidity_mastervolume))
|
|
|
|
{
|
|
|
|
Started = true;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2010-10-02 03:36:19 +00:00
|
|
|
return 1;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2010-10-02 03:36:19 +00:00
|
|
|
return 0;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: Stop
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2010-10-02 03:36:19 +00:00
|
|
|
void TimidityPPMIDIDevice::Stop ()
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
if (Started)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
if (Stream != NULL)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2010-10-02 03:36:19 +00:00
|
|
|
Stream->Stop();
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2010-10-02 03:36:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (ChildProcess != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2010-11-04 03:47:49 +00:00
|
|
|
if (!SafeTerminateProcess(ChildProcess, 666) && GetLastError() != ERROR_PROCESS_ABORTED)
|
2010-10-02 03:36:19 +00:00
|
|
|
{
|
|
|
|
TerminateProcess(ChildProcess, 666);
|
|
|
|
}
|
|
|
|
CloseHandle(ChildProcess);
|
|
|
|
ChildProcess = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (ChildProcess != -1)
|
|
|
|
{
|
|
|
|
if (kill(ChildProcess, SIGTERM) != 0)
|
|
|
|
{
|
|
|
|
kill(ChildProcess, SIGKILL);
|
|
|
|
}
|
|
|
|
waitpid(ChildProcess, NULL, 0);
|
|
|
|
ChildProcess = -1;
|
|
|
|
}
|
|
|
|
#endif
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2010-10-02 03:36:19 +00:00
|
|
|
Started = false;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
2010-11-04 03:47:49 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
/*
|
|
|
|
Safely terminate a process by creating a remote thread
|
|
|
|
in the process that calls ExitProcess
|
|
|
|
|
|
|
|
Source is a Dr Dobbs article circa 1999.
|
|
|
|
*/
|
|
|
|
typedef HANDLE (WINAPI *CreateRemoteThreadProto)(HANDLE,LPSECURITY_ATTRIBUTES,SIZE_T,LPTHREAD_START_ROUTINE,LPVOID,DWORD,LPDWORD);
|
|
|
|
|
|
|
|
BOOL SafeTerminateProcess(HANDLE hProcess, UINT uExitCode)
|
|
|
|
{
|
|
|
|
DWORD dwTID, dwCode;
|
|
|
|
HRESULT dwErr = 0;
|
|
|
|
HANDLE hRT = NULL;
|
|
|
|
HINSTANCE hKernel = GetModuleHandle("Kernel32");
|
|
|
|
BOOL bSuccess = FALSE;
|
|
|
|
|
|
|
|
// Detect the special case where the process is already dead...
|
|
|
|
if ( GetExitCodeProcess(hProcess, &dwCode) && (dwCode == STILL_ACTIVE) )
|
|
|
|
{
|
|
|
|
FARPROC pfnExitProc;
|
|
|
|
CreateRemoteThreadProto pfCreateRemoteThread;
|
|
|
|
|
|
|
|
pfnExitProc = GetProcAddress(hKernel, "ExitProcess");
|
|
|
|
|
|
|
|
// CreateRemoteThread does not exist on 9x systems.
|
|
|
|
pfCreateRemoteThread = (CreateRemoteThreadProto)GetProcAddress(hKernel, "CreateRemoteThread");
|
|
|
|
|
|
|
|
if (pfCreateRemoteThread == NULL)
|
|
|
|
{
|
|
|
|
dwErr = ERROR_INVALID_FUNCTION;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hRT = pfCreateRemoteThread(hProcess,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
(LPTHREAD_START_ROUTINE)pfnExitProc,
|
|
|
|
(PVOID)(UINT_PTR)uExitCode, 0, &dwTID);
|
|
|
|
|
|
|
|
if ( hRT == NULL )
|
|
|
|
dwErr = GetLastError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dwErr = ERROR_PROCESS_ABORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( hRT )
|
|
|
|
{
|
|
|
|
// Must wait process to terminate to guarantee that it has exited...
|
|
|
|
WaitForSingleObject(hProcess, INFINITE);
|
|
|
|
|
|
|
|
CloseHandle(hRT);
|
|
|
|
bSuccess = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !bSuccess )
|
|
|
|
SetLastError(dwErr);
|
|
|
|
|
|
|
|
return bSuccess;
|
|
|
|
}
|
|
|
|
#endif
|