2003-09-10 05:20:51 +00:00
|
|
|
/*
|
|
|
|
midi.c
|
|
|
|
|
|
|
|
midi file loading for use with libWildMidi
|
|
|
|
|
|
|
|
Copyright (C) 2003 Chris Ison
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-09-10 05:20:51 +00:00
|
|
|
"$Id$";
|
|
|
|
|
|
|
|
#ifdef HAVE_WILDMIDI
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wildmidi_lib.h>
|
|
|
|
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/sound.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/quakefs.h"
|
|
|
|
|
|
|
|
#include "snd_render.h"
|
|
|
|
|
|
|
|
static int midi_intiialized = 0;
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2004-01-08 01:48:02 +00:00
|
|
|
static cvar_t *wildmidi_volume;
|
|
|
|
static cvar_t *wildmidi_config;
|
2003-09-10 05:20:51 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
midi_init ( void ) {
|
2004-03-01 11:51:34 +00:00
|
|
|
wildmidi_volume = Cvar_Get ("wildmidi_volume", "100", CVAR_ARCHIVE, NULL,
|
|
|
|
"Set the Master Volume");
|
|
|
|
wildmidi_config = Cvar_Get ("wildmidi_config", "/etc/timidity.cfg",
|
|
|
|
CVAR_ROM, NULL,
|
|
|
|
"path/filename of timidity.cfg");
|
|
|
|
|
2007-03-17 07:18:01 +00:00
|
|
|
if (WildMidi_Init (wildmidi_config->string, snd_shm->speed, 0) == -1)
|
2003-09-10 05:20:51 +00:00
|
|
|
return 1;
|
|
|
|
midi_intiialized = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static wavinfo_t
|
|
|
|
get_info (void * handle) {
|
|
|
|
wavinfo_t info;
|
|
|
|
struct _WM_Info *wm_info;
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2005-06-08 06:35:48 +00:00
|
|
|
memset (&info, 0, sizeof (info));
|
|
|
|
|
2004-03-01 11:51:34 +00:00
|
|
|
if ((wm_info = WildMidi_GetInfo (handle)) == NULL) {
|
2003-09-10 05:20:51 +00:00
|
|
|
Sys_Printf ("Could not obtain midi information\n");
|
|
|
|
return info;
|
|
|
|
}
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2007-03-17 07:18:01 +00:00
|
|
|
info.rate = snd_shm->speed;
|
2003-09-10 05:20:51 +00:00
|
|
|
info.width = 2;
|
|
|
|
info.channels = 2;
|
|
|
|
info.loopstart = -1;
|
|
|
|
info.samples = wm_info->approx_total_samples;
|
|
|
|
info.dataofs = 0;
|
|
|
|
info.datalen = info.samples * 4;
|
|
|
|
return info;
|
|
|
|
}
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2003-09-10 05:20:51 +00:00
|
|
|
static int
|
|
|
|
midi_stream_read (void *file, byte *buf, int count, wavinfo_t *info)
|
|
|
|
{
|
|
|
|
return WildMidi_GetOutput (file, (char *)buf, (unsigned long int)count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
midi_stream_seek (void *file, int pos, wavinfo_t *info)
|
|
|
|
{
|
|
|
|
unsigned long int new_pos;
|
|
|
|
pos *= info->width * info->channels;
|
|
|
|
pos += info->dataofs;
|
|
|
|
new_pos = pos;
|
|
|
|
|
|
|
|
return WildMidi_SampledSeek(file, &new_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
midi_stream_close (sfx_t *sfx)
|
|
|
|
{
|
2007-05-07 05:20:24 +00:00
|
|
|
sfxstream_t *stream = sfx->data.stream;
|
2003-09-10 05:20:51 +00:00
|
|
|
|
|
|
|
WildMidi_Close (stream->file);
|
|
|
|
free (stream);
|
|
|
|
free (sfx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: we only set the QF stream up here.
|
|
|
|
* The WildMidi stream was setup when SND_OpenMidi was called
|
|
|
|
* so stream->file contains the WildMidi handle for the midi
|
|
|
|
*/
|
|
|
|
|
|
|
|
static sfx_t *
|
2007-03-24 11:46:41 +00:00
|
|
|
midi_stream_open (sfx_t *sfx)
|
2003-09-10 05:20:51 +00:00
|
|
|
{
|
2007-05-07 05:20:24 +00:00
|
|
|
sfxstream_t *stream = sfx->data.stream;
|
2003-10-15 20:40:53 +00:00
|
|
|
QFile *file;
|
|
|
|
midi *handle;
|
|
|
|
unsigned char *local_buffer;
|
|
|
|
unsigned long int local_buffer_size;
|
|
|
|
|
|
|
|
QFS_FOpenFile (stream->file, &file);
|
2004-03-01 11:51:34 +00:00
|
|
|
|
|
|
|
local_buffer_size = Qfilesize (file);
|
|
|
|
|
|
|
|
local_buffer = malloc (local_buffer_size);
|
|
|
|
Qread (file, local_buffer, local_buffer_size);
|
|
|
|
Qclose (file);
|
2003-10-15 20:40:53 +00:00
|
|
|
|
|
|
|
handle = WildMidi_OpenBuffer(local_buffer, local_buffer_size);
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2003-10-15 20:40:53 +00:00
|
|
|
if (handle == NULL)
|
|
|
|
return NULL;
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2007-03-18 01:51:37 +00:00
|
|
|
return SND_SFX_StreamOpen (sfx, handle, midi_stream_read, midi_stream_seek,
|
|
|
|
midi_stream_close);
|
2003-09-10 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SND_LoadMidi (QFile *file, sfx_t *sfx, char *realname)
|
|
|
|
{
|
|
|
|
wavinfo_t info;
|
2004-03-01 11:51:34 +00:00
|
|
|
midi *handle;
|
2003-09-10 05:20:51 +00:00
|
|
|
unsigned char *local_buffer;
|
2004-03-01 11:51:34 +00:00
|
|
|
unsigned long int local_buffer_size = Qfilesize (file);
|
2003-09-10 05:20:51 +00:00
|
|
|
|
|
|
|
if (!midi_intiialized) {
|
2004-03-01 11:51:34 +00:00
|
|
|
if (midi_init ()) {
|
2003-09-10 05:20:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-01 11:51:34 +00:00
|
|
|
local_buffer = malloc (local_buffer_size);
|
|
|
|
Qread (file, local_buffer, local_buffer_size);
|
|
|
|
Qclose (file);
|
2003-09-10 05:20:51 +00:00
|
|
|
|
|
|
|
// WildMidi takes ownership, so be damned if you touch it
|
2004-03-01 11:51:34 +00:00
|
|
|
handle = WildMidi_OpenBuffer (local_buffer, local_buffer_size);
|
2003-10-15 20:40:53 +00:00
|
|
|
|
2003-09-10 05:20:51 +00:00
|
|
|
if (handle == NULL)
|
|
|
|
return;
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2003-09-10 05:20:51 +00:00
|
|
|
info = get_info (handle);
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2003-10-15 20:40:53 +00:00
|
|
|
WildMidi_Close (handle);
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2003-09-10 05:20:51 +00:00
|
|
|
Sys_DPrintf ("stream %s\n", realname);
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2003-09-10 05:20:51 +00:00
|
|
|
// we init stream here cause we will only ever stream
|
2007-03-18 01:44:46 +00:00
|
|
|
SND_SFX_Stream (sfx, realname, info, midi_stream_open);
|
2003-09-10 05:20:51 +00:00
|
|
|
}
|
|
|
|
#endif // HAVE_WILDMIDI
|