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
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wildmidi_lib.h>
|
|
|
|
|
2013-01-22 05:09:41 +00:00
|
|
|
#include "qfalloca.h"
|
|
|
|
|
2003-09-10 05:20:51 +00:00
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/sound.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/quakefs.h"
|
|
|
|
|
2011-07-23 06:56:22 +00:00
|
|
|
#include "snd_internal.h"
|
2003-09-10 05:20:51 +00:00
|
|
|
|
2010-08-11 23:47:03 +00:00
|
|
|
#define FRAMES 1024
|
|
|
|
#define CHANNELS 2
|
|
|
|
#define WIDTH 2
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
float data[FRAMES * CHANNELS];
|
|
|
|
midi *handle;
|
|
|
|
} midi_file_t;
|
|
|
|
|
2003-09-10 05:20:51 +00:00
|
|
|
static int midi_intiialized = 0;
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2022-04-24 11:04:06 +00:00
|
|
|
static float wildmidi_volume;
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
static cvar_t wildmidi_volume_cvar = {
|
|
|
|
.name = "wildmidi_volume",
|
|
|
|
.description =
|
|
|
|
"Set the Master Volume",
|
|
|
|
.default_value = "100",
|
|
|
|
.flags = CVAR_ARCHIVE,
|
2022-04-24 11:04:06 +00:00
|
|
|
.value = { .type = &cexpr_float, .value = &wildmidi_volume },
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
};
|
|
|
|
static char *wildmidi_config;
|
|
|
|
static cvar_t wildmidi_config_cvar = {
|
|
|
|
.name = "wildmidi_config",
|
|
|
|
.description =
|
2022-06-04 12:09:01 +00:00
|
|
|
"path/filename of wildmidi.cfg",
|
|
|
|
.default_value = "/etc/wildmidi/wildmidi.cfg",
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
.flags = CVAR_ROM,
|
|
|
|
.value = { .type = 0, .value = &wildmidi_config },
|
|
|
|
};
|
2003-09-10 05:20:51 +00:00
|
|
|
|
|
|
|
static int
|
2021-06-25 07:52:09 +00:00
|
|
|
midi_init (snd_t *snd)
|
|
|
|
{
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_Register (&wildmidi_volume_cvar, 0, 0);
|
|
|
|
Cvar_Register (&wildmidi_config_cvar, 0, 0);
|
2004-03-01 11:51:34 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (WildMidi_Init (wildmidi_config, snd->speed, 0) == -1)
|
2003-09-10 05:20:51 +00:00
|
|
|
return 1;
|
|
|
|
midi_intiialized = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static wavinfo_t
|
2021-06-25 07:52:09 +00:00
|
|
|
midi_get_info (snd_t *snd, void *handle)
|
|
|
|
{
|
2003-09-10 05:20:51 +00:00
|
|
|
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
|
|
|
|
2021-06-23 15:14:33 +00:00
|
|
|
info.rate = snd->speed;
|
2003-09-10 05:20:51 +00:00
|
|
|
info.width = 2;
|
|
|
|
info.channels = 2;
|
|
|
|
info.loopstart = -1;
|
2010-08-11 23:44:34 +00:00
|
|
|
info.frames = wm_info->approx_total_samples;
|
2003-09-10 05:20:51 +00:00
|
|
|
info.dataofs = 0;
|
2010-08-11 23:44:34 +00:00
|
|
|
info.datalen = info.frames * info.channels * info.width;
|
2003-09-10 05:20:51 +00:00
|
|
|
return info;
|
|
|
|
}
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2010-08-11 23:47:03 +00:00
|
|
|
static long
|
|
|
|
midi_stream_read (void *file, float **buf)
|
2003-09-10 05:20:51 +00:00
|
|
|
{
|
2010-08-11 23:47:03 +00:00
|
|
|
sfxstream_t *stream = (sfxstream_t *) file;
|
|
|
|
midi_file_t *mf = (midi_file_t *) stream->file;
|
|
|
|
int size = FRAMES * CHANNELS * WIDTH;
|
2010-08-11 23:44:34 +00:00
|
|
|
int res;
|
|
|
|
byte *data = alloca (size);
|
|
|
|
|
2016-10-24 15:10:30 +00:00
|
|
|
res = WildMidi_GetOutput (mf->handle, (int8_t *)data, size);
|
2010-08-13 01:48:20 +00:00
|
|
|
if (res <= 0) {
|
|
|
|
stream->error = 1;
|
2010-08-13 01:48:48 +00:00
|
|
|
return 0;
|
2010-08-13 01:48:20 +00:00
|
|
|
}
|
2010-08-11 23:47:03 +00:00
|
|
|
res /= CHANNELS * WIDTH;
|
|
|
|
SND_Convert (data, mf->data, res, CHANNELS, WIDTH);
|
|
|
|
*buf = mf->data;
|
2010-08-11 23:44:34 +00:00
|
|
|
return res;
|
2003-09-10 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-08-11 23:47:03 +00:00
|
|
|
midi_stream_seek (sfxstream_t *stream, int pos)
|
2003-09-10 05:20:51 +00:00
|
|
|
{
|
|
|
|
unsigned long int new_pos;
|
2010-08-11 23:47:03 +00:00
|
|
|
pos *= stream->wavinfo.width * stream->wavinfo.channels;
|
|
|
|
pos += stream->wavinfo.dataofs;
|
2003-09-10 05:20:51 +00:00
|
|
|
new_pos = pos;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2010-12-24 08:33:09 +00:00
|
|
|
return WildMidi_FastSeek(stream->file, &new_pos);
|
2003-09-10 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-06-05 07:57:13 +00:00
|
|
|
midi_stream_close (sfxbuffer_t *buffer)
|
2003-09-10 05:20:51 +00:00
|
|
|
{
|
2022-06-05 07:57:13 +00:00
|
|
|
sfxstream_t *stream = buffer->stream;
|
2010-08-11 23:47:03 +00:00
|
|
|
midi_file_t *mf = (midi_file_t *) stream->file;
|
2003-09-10 05:20:51 +00:00
|
|
|
|
2010-08-11 23:47:03 +00:00
|
|
|
WildMidi_Close (mf->handle);
|
|
|
|
free (mf);
|
2022-06-05 07:57:13 +00:00
|
|
|
SND_SFX_StreamClose (stream);
|
2003-09-10 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 07:57:13 +00:00
|
|
|
static sfxbuffer_t *
|
2007-03-24 11:46:41 +00:00
|
|
|
midi_stream_open (sfx_t *sfx)
|
2003-09-10 05:20:51 +00:00
|
|
|
{
|
2022-06-05 07:57:13 +00:00
|
|
|
sfxstream_t *stream = sfx->stream;
|
2003-10-15 20:40:53 +00:00
|
|
|
QFile *file;
|
|
|
|
midi *handle;
|
|
|
|
unsigned char *local_buffer;
|
|
|
|
unsigned long int local_buffer_size;
|
2010-08-11 23:47:03 +00:00
|
|
|
midi_file_t *mf;
|
2003-10-15 20:40:53 +00:00
|
|
|
|
2014-01-23 02:57:57 +00:00
|
|
|
file = QFS_FOpenFile (stream->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
|
|
|
|
2012-05-21 23:23:22 +00:00
|
|
|
if (handle == NULL)
|
|
|
|
return NULL;
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2010-08-11 23:47:03 +00:00
|
|
|
mf = calloc (sizeof (midi_file_t), 1);
|
|
|
|
mf->handle = handle;
|
|
|
|
|
|
|
|
return SND_SFX_StreamOpen (sfx, mf, midi_stream_read, midi_stream_seek,
|
2007-03-18 01:51:37 +00:00
|
|
|
midi_stream_close);
|
2003-09-10 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
2010-08-08 03:02:55 +00:00
|
|
|
int
|
2003-09-10 05:20:51 +00:00
|
|
|
SND_LoadMidi (QFile *file, sfx_t *sfx, char *realname)
|
|
|
|
{
|
2021-06-23 15:14:33 +00:00
|
|
|
snd_t *snd = sfx->snd;
|
2003-09-10 05:20:51 +00:00
|
|
|
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) {
|
2021-06-23 15:14:33 +00:00
|
|
|
if (midi_init (snd)) {
|
2010-08-08 03:02:55 +00:00
|
|
|
return -1;
|
2003-09-10 05:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-21 23:23:22 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-05-21 23:23:22 +00:00
|
|
|
if (handle == NULL)
|
2010-08-08 03:02:55 +00:00
|
|
|
return -1;
|
2004-03-01 11:51:34 +00:00
|
|
|
|
2021-06-23 15:14:33 +00:00
|
|
|
info = midi_get_info (snd, 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
|
|
|
|
2022-06-05 08:39:44 +00:00
|
|
|
Sys_MaskPrintf (SYS_snd, "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);
|
2010-08-08 03:02:55 +00:00
|
|
|
return 0;
|
2003-09-10 05:20:51 +00:00
|
|
|
}
|