- Added an alternate module replay engine that uses foo_dumb's replayer, a

heavily customized version of DUMB (Dynamic Universal Music Bibliotheque).
  It has been slightly modified by me:
  * Added support for Ogg Vorbis-compressed samples in XM files ala FMOD.
  * Removed excessive mallocs from the replay core.
  * Rerolled the loops in resample.c. Unrolling them made the object file
    ~250k large while providing little benefit. Even at ~100k, I think it's
    still larger than it ought to be, but I'll live with it for now.
  Other than that, it's essentially the same thing you'd hear in foobar2000,
  minus some subsong detection features. Release builds of the library look
  like they might even be slightly faster than FMOD, which is a plus.
- Fixed: Timidity::font_add() did not release the file reader it created.
- Fixed: The SF2 loader did not free the sample headers in its destructor.


SVN r995 (trunk)
This commit is contained in:
Randy Heit 2008-05-29 23:33:07 +00:00
parent 1dc9528b43
commit 01f59fa85f
125 changed files with 38137 additions and 43 deletions

View file

@ -28,6 +28,7 @@ basetools: ccdv.exe
$(MAKE) -C wadsrc -f Makefile.mgw
$(MAKE) -C jpeg-6b -f Makefile.mgw
$(MAKE) -C snes_spc
$(MAKE) -C dumb
cleanexe:
@$(MAKE) -C . -f Makefile.mingw clean
@ -43,6 +44,7 @@ clean:
@$(MAKE) -C zlib -f Makefile.mgw clean
@$(MAKE) -C jpeg-6b -f Makefile.mgw clean
@$(MAKE) -C snes_spc clean
@$(MAKE) -C dumb clean
ifeq ($(findstring msys,$(shell sh --version 2>nul)),msys)
rm -f ccdv.exe
else

View file

@ -25,18 +25,22 @@ DEBUGTARGET ?= zdoomgccd.exe
DEBUGOBJDIR ?= debugobj
RELEASEOBJDIR ?= releaseobj
# Can be libdumbd.a, if you really need to debug DUMB
DUMBLIB ?= libdumb.a
CCDV ?= @ccdv
RE2C = tools/re2c/re2c
LEMON = tools/lemon/lemon
CPPFLAGS = -DWIN32 -D_WIN32 -D_WINDOWS -DHAVE_STRUPR -DHAVE_FILELENGTH -DI_DO_NOT_LIKE_BIG_DOWNLOADS -D__forceinline=inline -MMD -Izlib -IFLAC -Ijpeg-6b -Isrc -Isrc/win32 -Isrc/g_doom -Isrc/g_heretic -Isrc/g_hexen -Isrc/g_raven -Isrc/g_strife -Isrc/g_shared -Isrc/oplsynth -Isrc/sound -Isrc/textures -Isrc/thingdef -Isnes_spc/snes_spc
LDFLAGS += zlib/libz.a jpeg-6b/libjpeg.a snes_spc/libsnes_spc.a -lfmodex -lwsock32 -lwinmm -lddraw -ldsound -ldxguid -ldinput8 -lole32 -luser32 -lgdi32 -lcomctl32 -lcomdlg32 -lsetupapi -lws2_32 -Wl,--subsystem,windows
ifdef FMODDIR
CPPFLAGS += -I$(FMODDIR)/api/inc
LDFLAGS += -L$(FMODDIR)/api/lib
endif
LDFLAGS += zlib/libz.a jpeg-6b/libjpeg.a snes_spc/libsnes_spc.a dumb/lib/$(DUMBLIB) -lfmodex -lwsock32 -lwinmm -lddraw -ldsound -ldxguid -ldinput8 -lole32 -luser32 -lgdi32 -lcomctl32 -lcomdlg32
LDFLAGS += -lsetupapi -Wl,--subsystem,windows -lws2_32
CFLAGS += -fno-strict-aliasing
ifeq ($(CONFIG),Debug)

View file

@ -1,3 +1,18 @@
May 28, 2008
- Added an alternate module replay engine that uses foo_dumb's replayer, a
heavily customized version of DUMB (Dynamic Universal Music Bibliotheque).
It has been slightly modified by me:
* Added support for Ogg Vorbis-compressed samples in XM files ala FMOD.
* Removed excessive mallocs from the replay core.
* Rerolled the loops in resample.c. Unrolling them made the object file
~250k large while providing little benefit. Even at ~100k, I think it's
still larger than it ought to be, but I'll live with it for now.
Other than that, it's essentially the same thing you'd hear in foobar2000,
minus some subsong detection features. Release builds of the library look
like they might even be slightly faster than FMOD, which is a plus.
- Fixed: Timidity::font_add() did not release the file reader it created.
- Fixed: The SF2 loader did not free the sample headers in its destructor.
May 23, 2008 (Changes by Graf Zahl)
- Fixed: The compatibility searches for teleport destinations did not work
properly when the teleporter had both a tid and a tag. Now, if a teleporter

2555
dumb/ChangeLog Normal file

File diff suppressed because it is too large Load diff

197
dumb/Makefile Normal file
View file

@ -0,0 +1,197 @@
# Makefile for DUMB, derived from zlib/Makefile.mgw.
# Yes, I have forgone the Makefiles provided with DUMB, since they weren't kept
# up to date with kode54's changes.
CMD=0
ifeq (Windows_NT,$(OS))
CMD=1
ifeq ($(findstring msys,$(shell sh --version 2>nul)),msys)
CMD=0
endif
endif
CCDV := @../ccdv
CC := gcc
CFLAGS := $(LOC) -O3 -Wall -fomit-frame-pointer
LD := $(CC)
LDFLAGS := $(LOC) -s
AR := ar
ARFLAGS := rcs
CONFIG ?= Release
# Macro for replacing / with \ where necessary. Usage: $(call FIX,path)
FIX = $(subst /,\,$(subst /*,\\\*,$(1)))
CORE_MODULES = \
core/atexit.c \
core/duhlen.c \
core/duhtag.c \
core/dumbfile.c \
core/loadduh.c \
core/makeduh.c \
core/rawsig.c \
core/readduh.c \
core/register.c \
core/rendduh.c \
core/rendsig.c \
core/unload.c \
helpers/barray.c \
helpers/clickrem.c \
helpers/memfile.c \
helpers/resample.c \
helpers/riff.c \
helpers/sampbuf.c \
helpers/silence.c \
helpers/stdfile.c \
it/filter.cpp \
it/itload.c \
it/itload2.c \
it/itmisc.c \
it/itorder.c \
it/itread.c \
it/itread2.c \
it/itrender.c \
it/itunload.c \
it/load669.c \
it/load6692.c \
it/loadasy.c \
it/loadasy2.c \
it/loadmod.c \
it/loadmod2.c \
it/loadmtm.c \
it/loadmtm2.c \
it/loadoldpsm.c \
it/loadoldpsm2.c \
it/loadpsm.c \
it/loadpsm2.c \
it/loadptm.c \
it/loadptm2.c \
it/loadriff.c \
it/loadriff2.c \
it/loads3m.c \
it/loads3m2.c \
it/loadstm.c \
it/loadstm2.c \
it/loadxm.c \
it/loadxm2.c \
it/ptmeffect.c \
it/read669.c \
it/read6692.c \
it/readam.c \
it/readasy.c \
it/readdsmf.c \
it/readmod.c \
it/readmod2.c \
it/readmtm.c \
it/readoldpsm.c \
it/readpsm.c \
it/readptm.c \
it/readriff.c \
it/reads3m.c \
it/reads3m2.c \
it/readstm.c \
it/readstm2.c \
it/readxm.c \
it/readxm2.c \
it/xmeffect.c
LIBDIR := lib
OBJDIR_BASE := obj
WFLAGS := -Wall -W -Wstrict-prototypes -Wmissing-declarations -Wno-pointer-sign -Wno-uninitialized
WFLAGS_ALLEGRO := -Wno-missing-declarations
OFLAGS := -O2 -ffast-math -fomit-frame-pointer
DBGFLAGS := -DDEBUGMODE=1 -g3
CFLAGS_RELEASE := -Iinclude $(OFLAGS) -DNDEBUG
CFLAGS_DEBUG := -Iinclude $(DBGFLAGS) -D_DEBUG
LDFLAGS := -s
CORE_LIB_FILE_RELEASE := $(LIBDIR)/libdumb.a
CORE_LIB_FILE_DEBUG := $(LIBDIR)/libdumbd.a
ifeq ($(CONFIG),Debug)
OBJDIR = $(OBJDIR_BASE)/debug
CFLAGS_BASE = $(CFLAGS_DEBUG)
CORE_LIB_FILE := $(CORE_LIB_FILE_DEBUG)
endif
ifeq ($(CONFIG),Release)
OBJDIR = $(OBJDIR_BASE)/release
CFLAGS_BASE = $(CFLAGS_RELEASE)
CORE_LIB_FILE := $(CORE_LIB_FILE_RELEASE)
endif
CFLAGS = $(CFLAGS_BASE) $(WFLAGS)
all: make-outdirs core
core: $(CORE_LIB_FILE)
make-outdirs:
ifeq (0,$(CMD))
mkdir -p $(LIBDIR)
mkdir -p $(OBJDIR_BASE)/debug
mkdir -p $(OBJDIR_BASE)/release
else
-@if not exist $(call FIX,"$(LIBDIR)") mkdir $(call FIX,"$(LIBDIR)")
-@if not exist $(call FIX,"$(OBJDIR_BASE)/debug") mkdir $(call FIX,"$(OBJDIR_BASE)/debug")
-@if not exist $(call FIX,"$(OBJDIR_BASE)/release") mkdir $(call FIX,"$(OBJDIR_BASE)/release")
endif
.PHONY: clean
clean:
ifeq (0,$(CMD))
rm -f $(CORE_LIB_FILE_RELEASE)
rm -f $(CORE_LIB_FILE_DEBUG)
rm -f $(OBJDIR_BASE)/debug/*.o
rm -f $(OBJDIR_BASE)/release/*.o
rmdir $(LIBDIR) >/dev/null
rmdir $(OBJDIR_BASE)/debug >/dev/null
rmdir $(OBJDIR_BASE)/release >/dev/null
rmdir $(OBJDIR_BASE) >/dev/null
else
-del /q /f $(call FIX,"$(CORE_LIB_FILE_RELEASE)") 2>nul
-del /q /f $(call FIX,"$(CORE_LIB_FILE_DEBUG)") 2>nul
-del /q /f $(call FIX,"$(OBJDIR_BASE)/debug/")*.o 2>nul
-del /q /f $(call FIX,"$(OBJDIR_BASE)/release/")*.o 2>nul
-rmdir /q $(call FIX,"$(LIBDIR)") 2>nul
-rmdir /q $(call FIX,"$(OBJDIR_BASE)/debug") 2>nul
-rmdir /q $(call FIX,"$(OBJDIR_BASE)/release") 2>nul
-rmdir /q $(call FIX,"$(OBJDIR_BASE)") 2>nul
endif
CORE_OBJECTS := $(addprefix $(OBJDIR)/, $(notdir $(patsubst %.cpp, %.o, $(CORE_MODULES:%.c=%.o))))
# Pass the current value of CFLAGS through to the commands. Or, more
# accurately, create a local copy of the current CFLAGS variable. This is
# necessary because Make doesn't expand variables in commands until they are
# executed.
$(CORE_LIB_FILE): CFLAGS := $(CFLAGS)
$(OBJDIR)/%.o: src/core/%.c include/dumb.h include/internal/dumb.h
$(CCDV) $(CC) $(CFLAGS) -c -o $@ $<
$(OBJDIR)/%.o: src/helpers/%.c include/dumb.h
$(CCDV) $(CC) $(CFLAGS) -c -o $@ $<
$(OBJDIR)/resample.o: src/helpers/resample.inc
$(OBJDIR)/%.o: src/it/%.c include/dumb.h include/internal/it.h
$(CCDV) $(CC) $(CFLAGS) -c -o $@ $<
$(OBJDIR)/%.o: src/it/%.cpp include/dumb.h include/internal/it.h
$(CCDV) $(CXX) $(CFLAGS_BASE) -msse -Wall -W -c -o $@ $<
$(OBJDIR)/%.o: src/sigtypes/%.c include/dumb.h
$(CCDV) $(CC) $(CFLAGS) -c -o $@ $<
$(CORE_LIB_FILE): $(CORE_OBJECTS)
$(CCDV) $(AR) $(ARFLAGS) $@ $^

281
dumb/docs/deprec.txt Normal file
View file

@ -0,0 +1,281 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* deprec.txt - Deprecated functions, why they / / \ \
* were deprecated, and what to do | < / \_
* instead. | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
**********************************************
*** How the functions have been deprecated ***
**********************************************
GCC 3.1 and later provide a very useful attribute. The following:
__attribute__((__deprecated__))
when written alongside a function prototype, variable declaration or type
definition, will result in a warning from GCC if any such part of the API
is used. The warning will even tell you where the declaration is, and I
have inserted comments by all the deprecated declarations, telling you
what to do.
Unfortunately, GCC 2.x and 3.0.x and MSVC do not have any means to
deprecate things. The approach I have taken with these compilers is to
avoid prototyping the deprecated parts of the API. This means you will get
warnings and errors, and they won't be very helpful. If your program
compiles, you may get strange crashes when you run it, since the compiler
needs the declarations in order to make sure function calls are carried
out correctly.
If you would like the deprecated parts of the API to be declared, you can
compile with the -DDUMB_DECLARE_DEPRECATED switch for GCC, or the
-D"DUMB_DECLARE_DEPRECATED" switch for MSVC. This will be accepted by
GCC 3.x but is unnecessary. Use this switch with other people's projects
if necessary, but please make the effort to update your own projects to
use the new API, as the deprecated parts may be removed in the future.
The rest of this file explains why some parts of the API were deprecated,
and how to adapt your code.
**************************************
*** What happened to DUH_RENDERER? ***
**************************************
The DUH_RENDERER struct was designed for rendering audio to an end-user
format - 8-bit or 16-bit, signed or unsigned, with stereo samples
interleaved. In order for it to do this, it was built on top of the
hitherto undocumented DUH_SIGRENDERER struct, which rendered audio in
DUMB's internal 32-bit signed format with channels (left/right) stored
separately. The DUH_RENDERER struct contained a pointer to a
DUH_SIGRENDERER struct, along with some other data like the position and
number of channels.
There were then some developments in the API. The DUH_SIGRENDERER struct
also stored the position and the number of channels, so I decided to write
functions for returning these. Suddenly there was no need to store them in
the DUH_RENDERER struct. Before long, the DUH_RENDERER struct contained
nothing but a pointer to a DUH_SIGRENDERER.
I decided it would be a good idea to unify the structs. After all, there
really is no difference between the data stored in each, and it would be
easy to make duh_render(DUH_RENDERER *dr, ...) and
duh_render_signal(DUH_SIGRENDERER *sr, ...) work on the same type of
struct. (Note that duh_render_signal() is now deprecated too; see the next
section.) It took some deliberation, but I decided I didn't want functions
to be #defined (it prevents you from using these names for member
functions in C++ classes), and that meant they had to be defined
somewhere. Defining redundant functions is a source of bloat, inefficiency
and general inelegance. After weighing things up, I decided it was better
to deprecate the redundant functions and have people begin to use the more
efficient versions, and eventually the redundant functions will be able to
be removed.
So why did I choose to keep the more complicated name, DUH_SIGRENDERER?
The reason has to do with what DUMB will become in the future. Signals are
an inherent part of the DUH struct and how .duh files will be constructed.
It will be possible to have multiple signals in a single DUH struct, and
you will be able to choose which one you want to play (this is the 'sig'
parameter passed to duh_start_sigrenderer()). But don't hold your breath;
we still have a long way to go before .duh files will start to appear...
typedef DUH_SIGRENDERER DUH_RENDERER;
Wherever you are using DUH_RENDERER in your program, simply replace it
with DUH_SIGRENDERER. An automated (case-sensitive!) search and replace
operation should get this done.
DUH_RENDERER *duh_start_renderer(DUH *duh, int n_channels, long pos);
Use duh_start_sigrenderer() instead. It takes an extra parameter, 'sig',
which comes after 'duh' and before 'n_channels'; pass 0 for this. So an
example would be, replace:
sr = duh_start_renderer(duh, 2, 0);
with:
sr = duh_start_sigrenderer(duh, 0, 2, 0);
int duh_renderer_get_n_channels(DUH_RENDERER *dr);
long duh_renderer_get_position(DUH_RENDERER *dr);
void duh_end_renderer(DUH_RENDERER *dr);
These are easy enough to fix; all you have to do is replace 'renderer'
with 'sigrenderer'. So the new functions are:
int duh_sigrenderer_get_n_channels(DUH_SIGRENDERER *sigrenderer);
long duh_sigrenderer_get_position(DUH_SIGRENDERER *sigrenderer);
void duh_end_sigrenderer(DUH_SIGRENDERER *sigrenderer);
Note that duh_render() has NOT been deprecated. It now uses DUH_SIGRENDERER
instead of DUH_RENDERER, but its functionality is unchanged. You do not have
to change calls to this function in any way.
DUH_RENDERER *duh_renderer_encapsulate_sigrenderer(DUH_SIGRENDERER *sr);
DUH_SIGRENDERER *duh_renderer_get_sigrenderer(DUH_RENDERER *dr);
DUH_SIGRENDERER *duh_renderer_decompose_to_sigrenderer(DUH_RENDERER *dr);
These functions did not exist in the last release of DUMB, so you are
probably not using them, but they are included here for completeness. All
you have to do here is unwrap the function, since the structs have been
unified. So, for instance, replace:
duh_renderer_encapsulate_sigrenderer(my_sigrenderer)
with:
my_sigrenderer
Simple!
AL_DUH_PLAYER *al_duh_encapsulate_renderer(DUH_RENDERER *dr,
float volume, long bufsize, int freq);
DUH_RENDERER *al_duh_get_renderer(AL_DUH_PLAYER *dp);
DUH_RENDERER *al_duh_decompose_to_renderer(AL_DUH_PLAYER *dp);
Again, these functions were not in the last release, so you probably
aren't using them. Nevertheless, the fix is simple as always: simply
replace 'renderer' with 'sigrenderer'. So the new functions are:
AL_DUH_PLAYER *al_duh_encapsulate_sigrenderer(DUH_SIGRENDERER *sr,
float volume, long bufsize, int freq);
DUH_SIGRENDERER *al_duh_get_sigrenderer(AL_DUH_PLAYER *dp);
DUH_SIGRENDERER *al_duh_decompose_to_sigrenderer(AL_DUH_PLAYER *dp);
*********************
*** Miscellaneous ***
*********************
long duh_render_signal(DUH_SIGRENDERER *sigrenderer,
float volume, float delta,
long size, sample_t **samples);
This function used to return samples in DUMB's internal format. This
format consisted of 32-bit integers whose 'normal range' was -0x8000 to
0x7FFF (any samples outside this range would have to be clipped when sent
to the sound card).
DUMB's internal format has changed. DUMB still uses 32-bit integers, but
now the normal range is -0x800000 to 0x7FFFFF. The lowest eight bits are
discarded at the final stage by duh_render() when you ask for 16-bit
output. A new function, duh_sigrenderer_get_samples(), will return samples
in DUMB's new internal format. It takes exactly the same parameters, so
all you have to do to the call itself is change the name; however, you
will most likely have to change your code to account for the new
normalised range.
duh_render_signal() will still be able to give you the samples in DUMB's
old internal format, but it is inefficient. You should change your code as
soon as possible.
typedef void (*DUH_SIGRENDERER_CALLBACK)(void *data, sample_t **samples,
int n_channels, long length);
void duh_sigrenderer_set_callback(DUH_SIGRENDERER *sigrenderer,
DUH_SIGRENDERER_CALLBACK callback, void *data);
This callback was intended to allow you to analyse the output. It was by
no means intended to let you modify the output. For this reason, the names
have been changed to DUH_SIGRENDERER_ANALYSER_CALLBACK and
duh_sigrenderer_set_analyser_callback, and the 'samples' parameter to your
callback should now be specified as follows:
const sample_t *const *samples
The first 'const' indicates that you must not modify the samples. The
second indicates that you must not modify the pointers to each channel.
There is a second reason why this change was necessary, and it is the one
described further up for duh_render_signal()'s entry: the format in which
the samples themselves are stored has changed. They are 256 times as
large, with a normal range from -0x800000 to 0x7FFFFF. You will most
likely need to change your code to account for this.
If you try to call the old function, it will print a message to stderr
directing you to this file, and it will not install the callback. You
shouldn't be able to get this far without a compiler warning (or, if you
don't have GCC 3.1 or later, some compiler errors).
If you wanted to use this callback to apply a DSP effect, don't worry;
there is a better way of doing this. It is undocumented, so contact me
and I shall try to help. Contact details are at the bottom of this file.
For reference, here are the new definitions:
typedef void (*DUH_SIGRENDERER_ANALYSER_CALLBACK)(void *data,
const sample_t *const *samples, int n_channels, long length);
void duh_sigrenderer_set_analyser_callback(DUH_SIGRENDERER *sigrenderer,
DUH_SIGRENDERER_ANALYSER_CALLBACK callback, void *data);
int dumb_resampling_quality;
This variable has changed meaning. It used to hold a value from 0 to 4,
whose meaning was as follows:
0 - aliasing
1,2 - linear interpolation
3 - quadratic interpolation
4 - cubic interpolation
0,1 - always use a straightforward interpolation algorithm
2,3,4 - when decimating (increasing the pitch), use a linear average
algorithm designed to reduce frequencies that would otherwise
reflect off the Nyquist
Now the variable only holds values from 0 to 2, and these values have
preprocessor constants associated with them. The somewhat inappropriate
quadratic interpolation has been removed. The linear average algorithm has
also been removed, and may or may not come back; there are probably more
efficient ways of achieving the same effect, which I shall be
investigating in the future.
This change will have hardly any noticeable effect on existing programs.
Levels 2, 3 and 4 used considerably more processor time because of the
linear average algorithm. Likewise, Level 2 in the new scheme (cubic) uses
considerably more processor time than Levels 1 and 0, and Levels 3 and 4
will behave identically to Level 2.
******************
*** Conclusion ***
******************
"I conclude that... DUMB is the bestest music player in the world because...
Complete this sentence in fifteen words or fewer... D'OH!"
The preceding conclusion formerly appeared in dumb.txt, and is deprecated
because it's lame.
Ben Davis
entheh@users.sf.net
IRC EFnet #dumb
See readme.txt for details on using IRC.

296
dumb/docs/duhspecs.txt Normal file
View file

@ -0,0 +1,296 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* duhspecs.txt - DUH File Specifications. / / \ \
* | < / \_
* Written by entheh, one of the few programmers | \/ /\ /
* in existance who can spell correctly. \_ / > /
* | \ / /
* | ' /
* \__/
*/
Technical Details
=================
WARNING: until this warning disappears, the DUH file format could change at
any moment. This should not be of great concern, since DUH files are not
designed to be edited directly, but will always be generated from some other
format. However, it is our intention that this warning be removed before the
first release.
This document is written chiefly in the context of writing a DUH file, since
the library already contains the necessary functionality to read and play a
DUH file.
DUH files are currently saved using Allegro's file compression routines. See
Allegro's documentation and source code for details on this system. If you
wish to port DUMB away from Allegro and wish to preserve the file compression
capabilities, you will have to borrow the packfile source code from Allegro.
If you are happy to do away with file compression, please store the following
four-byte signature before the rest of the file: "slh." Alternatively, write
your DUH file writer with Allegro, and open the file with F_WRITE_NOPACK.
This will enable versions of the library using Allegro's file compression
routines to load the file. If you are reading a DUH file and you detect the
signature "slh!", then the file is compressed (and is not necessarily a DUH
file).
All numbers are little-endian unless specified otherwise. Allegro's
pack_iget*() and pack_iput*() functions can be used to read and write data in
this format. However, the four-byte signatures can be encoded into long ints
with AL_ID() and read and written with pack_m*().
Overall Structure
=================
Size Type Value Example C code to save to PACKFILE *f
4 ID "DUH!" pack_mputl(AL_ID('D','U','H','!'), f);
4 Int Number of signals pack_iputl(n_signals, f);
For each signal { for (i = 0; i < n_signals; i++) {
4 ID Signal type pack_mputl(AL_ID('S','E','Q','U'), f);
* - Signal data write_sequence(f);
} }
* The size of the data for any signal must either be constant or somehow
encoded in the data themselves. The library contains functions to read
various standard signal types, including "SAMP" and "SEQU" (sample and
sequence respectively), and the formats for these types are laid out
further down. If you wish to create your own signals, you must provide your
own loading function for the signal. This will be described in more detail
in a separate file.
In order to play a DUH file, we simply play the first signal. Signals can
construct their sound from the samples of other signals, and they in turn can
use other signals. Thus a recursive structure is built up. Recursive cycles
are not permitted.
Signal: SAMP (Sample)
=====================
Size Type Value Example C code to save to PACKFILE *f
4 Int Size pack_iputl(size, f);
1 Bits Flags pack_putc(flags, f);
1 ID Compression type pack_putc(compress, f); /* NOT IMPLEMENTED YET */
The flags are stored in a bit-field. Bit 0 indicates whether 16-bit samples
(set) or 8-bit samples (clear) are stored in the file. In both cases, the
samples are signed. NOTE: this bit might be replaced with a system allowing
for various sample compression algorithms, or altered so there are different
signal types for the purpose.
If Bit 1 is set, the sample is a looping sample, and loops indefinitely. In
this case the loop start point will be saved. The loop end point is not
saved, and is assumed to be the end of the sample. (When creating DUH files
from other formats which allow for the loop end to be earlier, you should
truncate the sample.)
If Bit 1 is not set, then Bit 2 may be set to indicate that the sample is
looping but only loops a finite number of times before continuing to play
normally. In this mode, both loop points (start and end) are saved in the
file. The number of times to loop will be specified on an instance-by-
instance basis using signal parameter #0, which should be set immediately
(before any samples are rendered) if it is to be set at all. It defaults to 0
(so the sample just plays through normally). In fact this parameter's value
is added to the loop count, but this is immaterial since there is no reason
to specify it more than once.
If Bit 1 is set, you should make sure Bit 2 is clear to allow for the
possibility of future expansion.
If Bit 3 is set, a ping-pong loop is used. When the sample reaches the loop
end point, it starts to play backwards until it reaches the loop start point,
at which time it will resume forward playback. When using a finite loop,
every change of direction counts as one iteration. That means an odd loop
count will cause the sample to proceed backwards when the looping ends.
If neither Bit 1 nor Bit 2 is set, then neither loop point will be saved. In
this case, you should also make sure Bit 3 is clear for the same reason as
above.
You may find the following definitions useful:
#define SAMPFLAG_16BIT 1
#define SAMPFLAG_LOOP 2
#define SAMPFLAG_XLOOP 4
#define SAMPFLAG_PINGPONG 8
#define SAMPPARAM_N_LOOPS 0
Size Type Value Example C code to save to PACKFILE *f
4 Int Loop start pack_iputl(loop_start, f);
4 Int Loop end pack_iputl(loop_end, f);
For a 16-bit sample: if (flags & SAMPFLAG_16BIT)
for (n = 0; n < size; n++)
x*2 Int Sample data pack_iputw(sample[n], f);
For an 8-bit sample: else
for (n = 0; n < size; n++)
x*1 Int Sample data pack_putc(sample[n], f);
/*
Compression type is 0 for uncompressed PCM.
*/
Signal: SEQU (Sequence)
=======================
Size Type Value Example C code to save to PACKFILE *f
4 Int Size size = pack_igetl(f);
x - Sequencing data pack_fwrite(data, size, f);
The sequence signal provides a medium in which other signals can be played at
specific times for specific lengths. You can control the pitch, volume and
other parameters for a signal, and these can change during the signal.
A sequence consists of a series of commands. Each command is preceded by a
time, which measures how long to wait before executing this command. A time
of zero indicates that this command is simultaneous with the previous. A time
of -1 indicates the end of the sequence. Note that signals do not stop
playing when the end is reached.
All times are measured in units such that 65536 corresponds to one second.
The timing in DUMB is accurate to the nearest sample, and cannot be offset in
the way it can with much mixing software, so you can rely on timing to
achieve certain effects. Resampling should be accurate enough to satisfy the
most acute musician's ear, but juggling pitches at this level of accuracy
requires knowledge of temperaments such as many musicians do not have. The
vast majority of people are satisfied with the even temperament. More on this
later.
Size Type Value Example C code to save to PACKFILE *f
4 Int Time pack_iputl(time, f);
1 ID Command pack_putc(SEQUENCE_START_SIGNAL, f);
/********************************
Proposed change:
Time is a short, encoded in 2 bytes.
The value of 'time' is actually an unsigned offset from the time of the
previous command. 0 means at the same time as the last command.
If the time in between this signal and the previous one is larger than
65534 ticks, then the value 65535 is written, followed by 4 more bytes (uint)
indicating the time offset.
**********************************/
Here are definitions for the various commands:
#define SEQUENCE_START_SIGNAL 0
#define SEQUENCE_SET_VOLUME 1
#define SEQUENCE_SET_PITCH 2
#define SEQUENCE_SET_PARAMETER 3
#define SEQUENCE_STOP_SIGNAL 4
Below are the details of what to write after each command code. The various
fields are explained afterwards.
Size Type Value Example C code to save to PACKFILE *f
SEQUENCE_START_SIGNAL:
1 ID Reference pack_putc(ref, f);
4 Int Signal pack_iputl(signal, f); /* --> Can we drop this to 2 bytes? (65536 signals) */
4 Int Starting position pack_iputl(pos, f);
2 Int Volume pack_iputw(volume, f);
2 Int Pitch pack_iputw(pitch, f);
SEQUENCE_SET_VOLUME:
1 ID Reference pack_putc(ref, f);
2 Int Volume pack_iputw(volume, f);
SEQUENCE_SET_PITCH:
1 ID Reference pack_putc(ref, f);
2 Int Pitch pack_iputw(pitch, f);
SEQUENCE_SET_PARAMETER:
1 ID Reference pack_putc(ref, f);
1 ID Parameter ID pack_putc(id, f);
4 Int Value pack_iputl(value, f);
SEQUENCE_STOP_SIGNAL:
1 ID Reference pack_putc(ref, f);
When you initiate a signal, you must choose a reference number. If you want
to modify the signal's volume, pitch or parameters, or stop the signal later,
you must use this reference number to do so. Need more than 256 reference
numbers? Use two sequences, and get your brain seen to.
If you initiate a new signal with the same reference number, the reference
will belong to the new signal. The old signal becomes anonymous, and will
either continue to play indefinitely or stop of its own accord. Even if the
new signal stops, the old one remains anonymous. DUMB will safely ignore
operations on reference numbers not used by any signal, or which were used by
a signal which has now stopped.
Of course all signals will stop if the sequence itself is stopped.
To initiate a signal, you must index the signal. The index is 0-based, so to
initiate the fifth signal in the file you must specify 4. Out-of-range values
will be handled safely, as will the case where a signal tries to generate
itself directly or indirectly from its own samples (a recursive cycle).
When you initiate a signal, you can specify a starting position. This will be
passed directly to the appropriate signal's start_samples function, so for a
SAMP (sample) signal it represents the sample on which to start, after any
loops have been expanded (so you can start on the backwards-playing part of
a ping-pong loop for example by careful choice of the starting position).
Volume is probably the simplest parameter. It is on a linear scale ranging
from 0 to 65535. Note that most music sounds more dramatic if the volume
rises and falls exponentially or on a greater curve. Linear fades are more
suitable for fading in and out, and do not sound dramatic in the least.
Pitch is specified on what is perceived as a linear scale. It is in fact
logarithmic, but you will not need to worry about this for most purposes.
Pitch 0 represents that the sample will be played at 65536 Hz. (This is not
strictly true, and will be explained further later.) In the likely case that
your sample is not recorded at 65536 Hz, you will first need to calculate the
central pitch. Use the following formula:
pitch_centre = 12 * 256 * log(sampling_frequency / 65536.0) / log(2);
If your programming language does not have a log function, look for ln, or
any function that calculates the logarithm (to any base) of the number you
give it. If you are lucky enough to find a logarithm to base 2, you can omit
the final division since the divisor evaluates to 1.
Once you have calculated pitch_centre, you can use it to play the sample at
the frequency at which it was recorded. Each time you add or subtract 256,
the sample will increase or decrease respectively in pitch by one semitone in
the even temperament. (The even temperament was noted further up as being
suitable for most musical applications.) One octave is represented by an
interval of 12 * 256.
If you wish to use another temperament, you can calculate the appropriate
intervals in pitch as follows:
pitch_interval = 12 * 256 * log(ratio) / log(2);
where, for example, ratio = 1.5 for a perfect fifth. An octave is, of course,
still represented by 12 * 256.
The SEQUENCE_SET_PARAMETER command needs little explanation. Quite simply,
the parameter ID and value you specify are passed on to the set_parameter
function of the signal to which this reference belongs. Exactly what this
does depends on the signal in question.
Remember, a sequence is a signal in itself. Like all signals, it is subject
to changes in pitch. Increasing the pitch of a sequence will also speed it
up. This capability is used to allow DUH files to be rendered at different
sampling frequencies, and it is also available for use by the musician. This
means that samples are only played at 65536 Hz if the pitch of the sequence
itself has not been adjusted.

1699
dumb/docs/dumb.txt Normal file

File diff suppressed because it is too large Load diff

1717
dumb/docs/dumbfull.txt Normal file

File diff suppressed because it is too large Load diff

264
dumb/docs/faq.txt Normal file
View file

@ -0,0 +1,264 @@
TO DO: add question regarding set_close_button_callback vs set_window_close_hook
TO DO: add question regarding mixing of DJGPP and MinGW object files
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* faq.txt - Frequently Asked Questions. / / \ \
* | < / \_
* This file covers some of the common problems | \/ /\ /
* and misconceptions people have with DUMB. If \_ / > /
* your problem is not covered here, please | \ / /
* contact me. I'll do my best to help - but | ' /
* don't be offended if I just direct you to the \__/
* manual!
*/
*****************************************************************************
* I get a lot of strange warnings and errors when I compile my projects *
* with this release of DUMB. They work with older versions! What happened? *
*****************************************************************************
Some parts of DUMB's API have been deprecated. See docs/deprec.txt for
full details, including an explanation as to why your compiler warnings
and errors are so unfriendly, and information on how to fix each warning
or error.
*****************************************************************************
* When I try to compile DUMB with Allegro, it complains that it cannot find *
* 'internal/alconfig.h'! What's wrong? *
*****************************************************************************
In Allegro 4.0.1, and quite likely some other versions of Allegro, the
msvcmake batch file does not install Allegro properly. I believe this was
fixed in Allegro 4.0.2, but don't take my word for it. Some include files
are neglected, including alconfig.h. The fix is quite easy; you need to
copy all of Allegro's include files to your compiler's directory. The
following should do this for you (alter it accordingly depending on where
MSVC and Allegro are installed):
cd\progra~1\msvc\include
xcopy/s \allegro\include\*.*
You can safely tell it to overwrite all files.
*****************************************************************************
* When I build a project that uses DUMB, I get an error that it doesn't *
* find -laldmbd! What's wrong? *
*****************************************************************************
See the notes for DUMB v0.8 in release.txt; the existence of libaldmbd.a
in DUMB v0.7 was due to a mistake in the makefiles. It should be
libaldmd.a, in order to maintain DOS compatibility. All subsequent
releases get it right, but you will have to change your project files to
allow for the change. If this is someone else's project, please let them
know that it needs changing.
*****************************************************************************
* When I build a project that uses DUMB, I get some linker errors about *
* _free, _malloc, etc. already being defined in LIBC.lib! What's wrong? *
*****************************************************************************
MSVC offers three different implementations of the standard libraries.
When you link statically with a library, you have to use the same
implementation that the library uses. You need the multithreaded DLL
implementation, which you can select by passing /MD when you compile (not
when you link). See howto.txt for details.
*****************************************************************************
* I created an IT file with Impulse Tracker, but DUMB won't play it! Why? *
*****************************************************************************
You probably created some patterns but didn't give any information on the
order in which they should be played. Impulse Tracker will also fail to
play your music if you press F5. Press F11 and you will have an
opportunity to create an order list, required for playback.
*****************************************************************************
* I created an IT file with ModPlug Tracker and I have it fading out at the *
* end. Why won't it loop when I play it with DUMB? *
*****************************************************************************
It loops at zero volume. This is what Impulse Tracker itself does. Fix the
IT file by setting the global volume explicitly (Vxx in the effects
column), either at the start, or right at the end before looping. Also see
the next two questions.
*****************************************************************************
* My module plays too loud and distorts badly with DUMB! What can I do? *
*****************************************************************************
This problem is most often caused by ModPlug Tracker, which has a complete
lack of regard for the playback volume of the original tracker. See the
next question for DUMB's official position with regard to ModPlug Tracker.
If you wrote your module with ModPlug Tracker, please try loading it with
the original tracker and see if it distorts there too. If it does, reduce
the volume. If not, then it's a problem with DUMB; please let me know.
If for whatever reason you cannot modify the module file itself, you can
make it sound better by reducing the volume passed to al_start_duh().
*****************************************************************************
* I created a music module with ModPlug Tracker, and DUMB doesn't play it *
* right! *
*****************************************************************************
DUMB cannot and will not support ModPlug Tracker. Please see
docs/modplug.txt for details. The original trackers, which DUMB is
designed to mimic as closely as possible, are listed in readme.txt.
If you find DUMB plays your module differently from the original tracker,
then please contact me.
*****************************************************************************
* My program crashes as soon as I try to load anything with DUMB! *
*****************************************************************************
Please take my advice and use the debugging build of DUMB, not the
optimised build. Then you'll probably find it aborts instead of crashing.
In this case you probably forgot to register a DUMBFILE system; this is
necessary for loading stand-alone files, though not for loading Allegro
datafiles with embedded music. Follow the instructions in docs/howto.txt
carefully and you shouldn't have this problem.
If DUMB crashes with a specific music module, please let me know.
*****************************************************************************
* I want to use the stdio file access functions to load stand-alone music *
* files, but I also want to load datafiles containing music files. The docs *
* say I shouldn't call both dumb_register_stdfiles() and *
* dumb_register_packfiles(). What shall I do? *
*****************************************************************************
When you register a DUMBFILE system, it only applies to files opened with
dumbfile_open(), i.e. separate files. When a file is embedded in a
datafile, dumbfile_open_ex() is used to read it, enabling it to use
PACKFILEs regardless of which DUMBFILE system is registered. In short, you
do not need to call dumb_register_packfiles() in order to load datafiles
with embedded music. See the section on "Sequential File Input" in
docs/dumb.txt if you're interested in how all this works.
*****************************************************************************
* I want to read a specific object in a datafile using Allegro's *
* "demo.dat#MY_MUSIC" syntax. Why won't it work? *
*****************************************************************************
Did you call dumb_register_packfiles(), or did you call
dumb_register_stdfiles()? It will only work if you use the former.
*****************************************************************************
* My program runs, but no music plays! What am I doing wrong? *
*****************************************************************************
There are a number of possible causes for this. The most likely reason is
that you aren't calling al_poll_duh(); see docs/howto.txt for further
information.
Other possible causes are as follows:
- The speakers are turned down (duh)
- The volume of some system mixer is turned down
- Another program is using the sound card (not a problem for most modern
systems)
- You didn't initialise Allegro's sound system; see install_sound() in
Allegro's docs
- Allegro's drivers don't work on your system and chosen platform
In order to narrow down the cause, consider the following:
- Do you get any other sound from your program?
- Do other Allegro+DUMB programs generate sound?
- Do other Allegro programs generate sound?
- Do other non-Allegro programs generate sound?
- Does your program fail only on a specific platform (e.g. DOS but not
Windows)?
This problem is highly system-specific; please try hard to solve it by
yourself before contacting me. However, if you think this problem could
affect other people, please let me know what the problem is and how you
fixed it, if you did. Be as specific as possible.
*****************************************************************************
* The music stutters! What can I do? *
*****************************************************************************
If you have an older computer, it may not be able to cope with the load.
Try reducing quality options; look up dumb_resampling_quality and
dumb_it_max_to_mix in docs/dumb.txt, and consider changing the frequency
you pass to al_start_duh().
Stuttering may not be caused by excessive load. To find out, try
increasing the buffer size passed to al_start_duh(). Beware of making it
too big though; older systems will freeze periodically if it's too big,
because they render larger chunks less frequently. The timing of callbacks
will also be less accurate, if you are using those.
If you're using the 'dumbplay' example, you can control these parameters
by editing dumb.ini.
*****************************************************************************
* Why does DUMB use so much processor time compared with other players? *
*****************************************************************************
This should be less so in this release than in previous releases; the
resampling and filtering algorithms have been optimised.
By default, DUMB uses the most expensive resampling quality option. I've
found on an AthlonXP 1800+ and on a Pentium 233 that it typically uses
about twice as much processor time as the least expensive option.
Try setting dumb_resampling_quality to DUMB_RQ_ALIASING or DUMB_RQ_LINEAR.
See dumb.txt for more information. If you're using the example programs,
you can control this variable by editing dumb.ini.
DUMB uses 32-bit ints for mixing. Some players use 16-bit ints, and are
therefore marginally faster (not much!) and lower quality. So you can't
expect DUMB to beat these players. Furthermore, DUMB is currently written
entirely in C. GCC does an impressive job on the C code, but that's not to
say some custom-written assembly language couldn't beat it ...
*****************************************************************************
* Why does DUMB generate so much background noise? *
*****************************************************************************
You're probably using the DOS build on a system with bad Sound Blaster
compatibility (most Windows XP systems fall in this category). This would
mean DUMB could only access an 8-bit driver. The Windows build will almost
certainly give better results. Your DOS binary will still give good
results on systems with better compatibility (like my Windows 98 system).
*****************************************************************************
* I e-mailed you and you replied with "RTFM"! What does that mean? *
*****************************************************************************
Read The Manual. If it's a specific problem, I'll probably be kind and
tell you where to look in the manual. However, if I get the impression you
haven't even looked for a solution in the manual, expect no mercy ...
Ben Davis
entheh@users.sf.net
IRC EFnet #dumb
See readme.txt for details on using IRC.

113
dumb/docs/fnptr.txt Normal file
View file

@ -0,0 +1,113 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* fnptr.txt - Function pointer explanation. / / \ \
* | < / \_
* | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
C allows you to create and use function pointers. A function pointer is a
variable that points to a function, and you can use it to call that function.
Why is this useful?
Function pointers can be passed as parameters. As an example, here's a
function from Allegro:
void create_light_table(COLOR_MAP *table, const PALETTE pal, int r, g, b,
void (*callback)(int pos));
Don't worry about the syntax just yet, but the last parameter, 'callback', is
a pointer to a function that takes an int parameter. create_light_table() can
take some time to complete its work, and you may want to display a progress
indicator. So you write a function to draw the progress indicator, and then,
for 'callback', you specify a pointer to your function. This will enable
create_light_table() to call your function at intervals during its
processing. (If you don't want to use the callback, you can pass NULL, but
this only works because create_light_table() checks actively for NULL. You
can't always specify NULL when you want nothing to happen.)
There are many other uses. In addition to using function pointers as
parameters, Allegro has some global function pointers you can set to point to
your functions. Function pointers can also be used in structs, and this is
where DUMB makes the most use of them.
So how are they used?
void bar(void) { ... } /* Here's a function */
void (*foo)(void) = &bar; /* Take a pointer */
(*foo)(); /* Call the function */
char *baz(float a) { ... } /* Here's another function */
char *(*foobarbaz)(float a) = &baz; /* Take a pointer */
char *rv = (*foobarbaz)(0.1); /* Call the function */
In both these cases, note how the statement for calling the pointed-to
function (third line) resembles the definition of the function pointer
(second line). This is true of any variable in C, and can lead to some truly
obfuscated definitions if you are that way inclined. Such definitions can be
clarified with typedefs, but before you use those, it is important you
understand how the above statements work. I speak from experience: function
pointer notation looks random and scary, until you understand why it's the
way it is; then it makes perfect sense.
(It is actually permissible to omit the & when taking a pointer and to write
e.g. foobarbaz(0.1) instead of (*foobarbaz)(0.1). However, I recommend not
doing this, since the syntax for using the pointer no longer resembles the
definition. Writing e.g. (*foobarbaz)(0.1) also makes a clear distinction
between function pointer calls and ordinary function calls, which makes code
more readable.)
Note that function pointers have the return value and parameter list
specified. A function pointer can only point to a function with a matching
return value and matching parameters. (You can break this rule by casting the
pointer explicitly, but there is no situation where doing so is portable to
all computers, and I strongly advise against it unless you're writing system
code. If you're not sure whether you're writing system code or not, then
you're not.)
The parameter names need not match (although the types must). If you wish to
rename a parameter in your function, you do not have to change the function
pointer accordingly. In fact, when you define a function pointer, you don't
even have to specify the names of parameters if you don't want to. I normally
do so for clarity.
It is possible to typedef a function pointer. In order to typedef a function
pointer, you start by declaring the pointer as a variable:
void (*myfunc)(void);
Then you write 'typedef' before it and replace the variable name, which is
myfunc, with the type name (this rule can be applied to any variable when you
want to use typedef):
typedef void (*MYTYPE)(void);
Now 'MYTYPE' represents a pointer to a function with no parameters and no
return value. The following two lines are completely equivalent:
MYTYPE myfunc;
void (*myfunc)(void);
Note that we use MYTYPE without an asterisk (*), since it is already a
pointer.
That's it. If you feel anything should be explained better here, or if you
feel something should be added, please don't hesitate to let me know!
Ben Davis
entheh@users.sf.net
IRC EFnet #dumb
See readme.txt for details on using IRC.

845
dumb/docs/howto.txt Normal file
View file

@ -0,0 +1,845 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* howto.txt - How To Use DUMB. / / \ \
* | < / \_
* See readme.txt for general information on | \/ /\ /
* DUMB and how to set it up. \_ / > /
* | \ / /
* | ' /
* \__/
*/
********************
*** Introduction ***
********************
Welcome to the DUMB How-To! It is assumed here that you have already set DUMB
up on your system, with or without Allegro. If not, please see readme.txt.
*********************************
*** Adding music to your game ***
*********************************
These instructions will help you add a piece of music to your game, assuming
your music is stored in a stand-alone IT, XM, S3M or MOD file. If you wish to
use a different method (such as putting the music file in an Allegro
datafile), please follow these instructions first, test your program, and
then follow the instructions further down for adapting your code.
1. You need to include DUMB's header file. If you have Allegro, add the
following line to the top of your source file (or at the top of each file
where you wish to use DUMB):
#include <aldumb.h>
If you do not have Allegro or do not wish to use it, use dumb.h instead.
2. You need to link with DUMB's library file or files. If you are compiling
with GCC from a command line on any platform, you need to add the
following to the command line:
If you are using Allegro: -laldmd -ldumbd
If you are not using Allegro: -ldumbd
If you are using MSVC from the command line:
If you are using Allegro: /link aldmd.lib dumbd.lib
If you are not using Allegro: /link dumbd.lib
With MSVC, you must also add /MD to the command line when compiling (not
when linking).
Note that -laldmd or aldmd.lib must PRECEDE alleg.lib, -lalleg_s,
`allegro-config --libs`, or whatever you are already using to link with
Allegro. For MSVC users, the /MD flag selects the multithreaded DLL
implementation of the standard libraries; since DUMB is statically linked,
you have to use the same library DUMB uses. You would also need this flag
to link statically with Allegro; if you already have it, there's no need
to put it twice.
(If anyone would like to contribute instructions for doing the above using
MSVC's IDE, please contact me. Contact details are at the end of this
file.)
If you are using RHIDE, go to Options -> Libraries. You will need to type
'aldmd' and 'dumbd' in two boxes, making sure 'aldmd' comes above whatever
you are using to link with Allegro (or just put 'dumbd' if you are not
using Allegro). Make sure the box next to each of these libraries is
checked.
The above are the debugging libraries. It is VERY HIGHLY RECOMMENDED that
you use the debugging libraries at first. The reason is as follows.
Although DUMB is supposedly robust against corrupt music files and things
like lack of memory, it will NOT tolerate programmer error. If you write
faulty code, DUMB will probably crash rather than returning an error code
for you. However, the debugging libraries will abort in many cases,
enabling you to find out what the cause is.
Once your program is up and running reliably, you can replace 'aldmd' with
'aldmb' and 'dumbd' with 'dumb'. Don't forget to do this, or DUMB will be
a lot slower than it should be!
3. As you use DUMB, it may claim system resources (memory in particular). You
will need to arrange for these resources to be freed at the end. Doing so
is very easy. Simply write the following line at the top of your main
function, but below allegro_init() if you are using Allegro:
atexit(&dumb_exit);
This arranges for the function dumb_exit() to be called when your program
exits; you do not need to call dumb_exit() yourself. This method is
preferable to calling dumb_exit() manually, as it will free resources even
if your program aborts unexpectedly.
If you are happy with this, please skip ahead to Step 4. If you are
interested in alternative methods, read on, but read on carefully.
In fact it mostly doesn't matter where you put the above atexit() line,
provided it gets called only once, and before you do anything with DUMB.
If you are using DUMB with Allegro, it is recommended that you write the
functions in this order:
allegro_init();
atexit(&dumb_exit);
And then you must NOT call allegro_exit() yourself (because it has to be
called after dumb_exit()). Alternatively, if you prefer not to use
atexit() (or you cannot), you will have to do the following before
exiting:
dumb_exit();
allegro_exit();
4. DUMB does not automatically do any of its own file input. You have to tell
it how to read files. Don't worry, it's easy. Simply call the following
function near the beginning of your program, after your atexit() call:
dumb_register_stdfiles();
This tells DUMB to use ordinary stdio FILE structs for reading and writing
files. If you are using Allegro and would rather DUMB used PACKFILEs, call
the following function INSTEAD:
dumb_register_packfiles();
In the latter case, DUMB will be affected by any password you set with
packfile_password() in the same way that other PACKFILEs are.
Note that the procedure for loading datafiles with embedded music is
independent of these two functions; even if you will be loading datafiles,
you can use either of these functions. If you are loading datafiles, your
executable might be slightly smaller if you use dumb_register_packfiles().
On the other hand, dumb_register_stdfiles() will probably be faster. If
you are only ever going to load datafiles and never stand-alone files, you
can actually leave this step out; but I would recommend you put this in,
test your code with a stand-alone file, then follow the instructions in
the next section in order to adapt your code to use the datafile (you will
be reminded that you can remove the function call).
5. If you are using Allegro, you'll have to initialise Allegro's sound
system. In most cases the following line will do the job:
install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL);
You may like to initialise a MIDI driver though; see Allegro's docs for
details. Put this line after allegro_init().
6. All pieces of music are stored in memory in DUH structs. To handle these,
you must define pointers to them. Such pointers look like this:
DUH *myduh;
You can of course replace 'myduh' with anything you like. If you are
unfamiliar with pointers, please see ptr.txt. It is very important that
you understand these if you wish to use DUMB correctly.
You do not have direct access to the contents of a DUH struct, so do not
try. DUMB's functions provide everything you need; if you disagree, please
let me know and I shall see what I can do. Contact details are at the end
of this file.
Given the above definition, you can load a piece of music using one of the
following lines, depending on what file format you want to load:
myduh = dumb_load_it("a_one.it");
myduh = dumb_load_xm("a_two.xm");
myduh = dumb_load_s3m("a_one_two.s3m");
myduh = dumb_load_mod("three_four.mod");
Obviously you can use relative or absolute paths as normal. You should
always use forward slash (/), not backslash (\), when coding in C and
similar languages.
Every piece of music you load must be unloaded when you've finished with
it. When you type the above line in, it is good practice to type the
following line in at the same time, but put it at the end of the program:
unload_duh(myduh);
You will now be able to use the DUH struct anywhere in between the two
lines you just added. There is no need to check the return value; if the
DUH failed to load for one reason or another (this could be due to lack of
memory as well as the file not being there), then DUMB will do nothing -
safely.
7. From this step onwards, it will be assumed you're using Allegro. If not,
please read these steps anyway, and then see the section entitled
"Rendering music into a buffer". You will have to write your own playback
code using whatever sound output system is available. Alternatively you
may like to write data to a file (especially if you have a file that
consumes a lot of processor time), but beware that any streaming audio
format is likely to be substantially larger than the module file you
generate it from, and formats like MP3 will be lower quality. You might
not be able to hear the difference between the MP3 and the original, but
many people can and don't like it, so please consider them. I'm one of
them. If you really want to use a lossy compression format, I highly
recommend Ogg Vorbis:
http://www.vorbis.com/
But I digress.
In order to play the DUH you loaded, you need to define a pointer to an
AL_DUH_PLAYER struct:
AL_DUH_PLAYER *dp;
Two of the functions you will need are prototyped as follows:
AL_DUH_PLAYER *al_start_duh(DUH *duh, int n_channels, long pos,
float volume, long bufsize, int freq);
void al_stop_duh(AL_DUH_PLAYER *dp);
As you can see, al_start_duh() returns a pointer to an AL_DUH_PLAYER
struct when you call it. You then pass this pointer to all the other
functions. Again, if it is a NULL pointer for whatever reason (usually
lack of memory), DUMB will safely do nothing. When you call al_stop_duh(),
the pointer becomes invalid and you should not use it again; if there's
any risk of the pointer being used again, it is wise to set it to NULL at
this point. You can reassign the variable with a new call to
al_start_duh() of course.
Set 'n_channels' to 1 or 2 for mono or stereo respectively. Note that this
parameter has nothing to do with the number of samples that can play at
once in a music module. Set 'pos' to 0 to play from the beginning; each
time you add 65536, you will have advanced one second into the piece. As a
general rule, set the volume to 1.0f and adjust it later if the music is
too loud or too quiet - but see Allegro's set_volume_per_voice() function
first.
'bufsize' can generally be set to 4096. If your music stutters, try
increasing it; if your game freezes periodically, try reducing it. Find a
happy medium. Set 'freq' to 48000 for the best quality, though 44100 will
do in most cases. 22050 will be fine for a lot of music, though 11025 may
sound muffled. You can choose any other value, higher, lower or in
between. If your music stutters, and increasing 'bufsize' doesn't fix it,
try reducing this value.
Once you have put in a call to al_start_duh(), it is good practice to
insert the call to al_stop_duh() at the same time. You must call
al_stop_duh() before the DUH is unloaded (unload_duh(), Step 6 above).
Don't get impetuous, your program is not ready yet! Proceed to Step 8.
8. DUMB does not play music in the background for you; if you were expecting
it to do so, please see the explanation at the end of this step. For your
music to be played, you have to call another function at regular
intervals. Here is its prototype:
int al_poll_duh(AL_DUH_PLAYER *dp);
Do NOT call this function from inside a timer function unless you really
know what you are doing. The reasons why this is bad are explained
further down. You should call it from your main program.
Simply writing the following line will be sufficient in general, if you
have a variable 'dp' that points to your AL_DUH_PLAYER struct.
al_poll_duh(dp);
As a general rule, calling this once for each logic update will do the
trick. If, however, you are executing time-consuming algorithms such as
software 3D rendering, you may wish to insert calls to this function in
the middle of those algorithms. You cannot call this function too often
(within reason); if it has nothing to do it will return immediately.
Exactly how often you need to call the function depends on the values for
'bufsize' and 'freq' that you passed to al_start_duh():
n = freq / bufsize;
You have to call al_poll_duh() at least n times a second. Do not hesitate
to call it more often for safety; if the sound stutters, you may need to
do just that. (Or you may need to increase the buffer size or reduce the
quality settings; the only way to find out is to try.)
For now, don't worry about al_poll_duh()'s return value. As soon as you
need it, it will be explained.
If you are happy, please skip to Step 9. If you were expecting DUMB to
play your music in the background, please read on.
The natural way to play music in the background on most operating systems
nowadays is to use threads. DOS was not built with multithreading in mind,
and its system operations (notably disk access) assume they will only be
used from a single thread.
Interrupts are the next best thing to threads. A DOS hardware interrupt
could be triggered at any moment, and a handler function will be called.
This is how Allegro's timer functions work. Unfortunately, what you can do
inside an interrupt handler is very limited. For one thing, all code and
data used by the handler must be locked in memory; if not, it could get
written to disk (virtual memory). If the main program was accessing the
disk when it got interrupted, the system would then die a horrible death.
This precludes the possibility of allocating extra memory inside the
handler, and DUMB does a lot of that in al_poll_duh().
Given DUMB's architecture, which cannot change for reasons which will
become apparent in future versions, this renders it impossible to come up
with a portable solution for making DUMB play music in the background.
Having said that, if you wish to write your own wrapper for al_poll_duh()
and use it in a thread, there is nothing stopping you. If you do do this,
you will have to be very careful when stopping the music; see the
description of al_poll_duh() in dumb.txt for more information.
So why not kill DOS? It is all too common a practice among programmers to
quote the phrase, "DOS is as dead as the dodo." Despite being a decidedly
derisible demonstation of the dreary device of alliteration, it shows a
distinct lack of experience. Many embedded systems still use DOS because
it provides hardware access capabilities and real-time possibilities
unparalleled by any current multitasking operating system. For an argument
closer to home, I used to use RHIDE for DOS before I switched to Linux,
and I have not found a single Freeware Windows IDE that measures up to
RHIDE. I'm sure many people are in the same boat, and really appreciate
DUMB's DOS port.
We will not be removing DOS support from DUMB. Any blind suggestions to do
so will be met with fiery flames. You have been warned.
9. Test your program!
If you have trouble, check through the above steps to make sure you didn't
miss one out. Refer to faq.txt to see if your problem is addressed there.
If you still have trouble, contact me; details are at the end of this
file.
**********************************
*** Controlling music playback ***
**********************************
Here I describe some common operations you may wish to perform. The method
for doing so will seem a bit strange sometimes, as will the names of the
structs. However, there is a reason behind everything. If you would like to
do more exotic things, or better understand some of the methods used here,
then see dumb.txt, which covers everything from the ground up.
To control playback quality:
#define DUMB_RQ_ALIASING
#define DUMB_RQ_LINEAR
#define DUMB_RQ_CUBIC
#define DUMB_RQ_N_LEVELS
extern int dumb_resampling_quality;
extern int dumb_it_max_to_mix;
Please note that dumb_resampling_quality has changed in DUMB v0.9.2. See
deprec.txt for more details on the change.
dumb_resampling_quality can be set to any of the DUMB_RQ_* constants
(except DUMB_RQ_N_LEVELS; see below). Resampling is the term given to the
process of adjusting a sample's pitch (in this context).
dumb_resampling_quality defaults to DUMB_RQ_CUBIC, which sounds nice but
takes a lot of processor power. Try reducing it if you have an older
computer or if you are trying to mix an insane number of samples (or
both!). See dumb.txt for details on what the different values actually do.
If you wish to give this option to your user, you can use
DUMB_RQ_N_LEVELS. All the values from 0 to DUMB_RQ_N_LEVELS - 1 will be
valid resampling levels. If a value outside this range is chosen, it is
not the end of the world; DUMB will behave as if you had chosen the value
at whichever extreme you went beyond.
dumb_it_max_to_mix, defaulting to 64, is the maximum number of samples
DUMB will ever mix together when playing an IT, XM, S3M or MOD file.
Unlike many other music systems, DUMB will still keep track of all samples
(up to a fixed maximum of 256 of them, roughly speaking), and then will
just render as many of them as this variable permits, starting with the
loudest ones. When samples are cut or come back in, the exact timings will
not generally be predictable - but nor will they be important.
dumb_it_max_to_mix applies to each currently playing module file
independently. So if you set it to 64, but render two modules
simultaneously, DUMB could end up mixing up to 128 samples.
To pause and resume playback, set the volume, get the current playback
position, or get the length of time a DUH will play for before either looping
or freezing (effect F00 in XM and MOD files, which means no new notes will be
played but any existing notes will continue):
void al_pause_duh(AL_DUH_PLAYER *dp);
void al_resume_duh(AL_DUH_PLAYER *dp);
void al_duh_set_volume(AL_DUH_PLAYER *dp, float volume);
long al_duh_get_position(AL_DUH_PLAYER *dp);
long duh_get_length(DUH *duh);
These functions are pretty self-explanatory. The volume passed to
al_duh_set_volume() and the position returned by al_duh_get_position() are
in the same units as those you passed to al_start_duh(). The length
returned by duh_get_length() is in the same units as the aforementioned
position; see dumb.txt for more information on this function. Be careful
with al_duh_get_position(); it will return a position slightly ahead of
what you can hear, because the system has to keep ahead slightly to avoid
stuttering.
To prevent the music from looping and/or freezing:
DUH_SIGRENDERER *al_duh_get_sigrenderer(AL_DUH_PLAYER *dp);
DUMB_IT_SIGRENDERER *duh_get_it_sigrenderer(DUH_SIGRENDERER *sigrenderer);
void dumb_it_set_loop_callback(DUMB_IT_SIGRENDERER *sigrenderer,
int (*callback)(void *data), void *data);
void dumb_it_set_xm_speed_zero_callback(DUMB_IT_SIGRENDERER *sigrenderer,
int (*callback)(void *data), void *data);
int dumb_it_callback_terminate(void *data);
If you are unfamiliar with function pointers, please see fnptr.txt.
Note that these functions apply to IT, XM, S3M and MOD files - not just to
IT files. This holds true throughout DUMB, for all functions with "it" in
the name. The xm_speed_zero event can only occur with XM and MOD files.
The first two functions will return a pointer to a struct contained by the
struct you pass. This system is necessary to ensure that these operations
are possible when not using Allegro. Typically you would write the
following code:
{
DUH_SIGRENDERER *sr = al_duh_get_sigrenderer(dp);
DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer(sigrenderer);
dumb_it_set_loop_callback(itsr, &dumb_it_callback_terminate, NULL);
dumb_it_set_xm_speed_zero_callback
(itsr, &dumb_it_callback_terminate, NULL);
}
Once you have done this, the return value of al_poll_duh() becomes
significant. It will be 0 as long as the music is playing. When the music
stops, al_poll_duh() will return nonzero. You can call al_stop_duh() and
do something else as soon as you wish, but calling al_poll_duh() some more
will not do any harm.
al_poll_duh() will also return 1 if the music could not be loaded, or if
memory was short when trying to play it, or if it was a quirky music file
with no music in it (technically one with an empty order list). This
happens regardless of whether or not you execute the above code to disable
looping. Normally you shouldn't need to worry about this.
To undo the above and make DUMB loop or freeze again, pass NULL instead of
&dumb_it_callback_terminate. If you would like to fade on looping, or loop
a finite number of times, or display a message when looping, or whatever,
you will have to write your own callback function. In this case, please
see dumb.txt.
Note that the above code can safely be applied for a DUH that doesn't
contain a music module but contains some other kind of music.
duh_get_it_sigrenderer() will return NULL, and the code will do nothing.
To analyse the audio as it's generated:
typedef int sample_t;
typedef void (*DUH_SIGRENDERER_ANALYSER_CALLBACK)(void *data,
const sample_t *const *samples, int n_channels, long length);
void duh_sigrenderer_set_analyser_callback(DUH_SIGRENDERER *sigrenderer,
DUH_SIGRENDERER_ANALYSER_CALLBACK callback, void *data);
If the above confuses you, see fnptr.txt. These functions, along with
al_duh_get_sigrenderer() from the last section, enable you to register a
callback function. Every time some samples are generated, they will be
passed to this function. This enables you to display an oscilloscope or
spectrum analyser, for example.
Beware: your callback function may occasionally be called with
samples == NULL. This means the main program has decided to skip through
the music without generating any data. You should handle this case
elegantly, typically by returning immediately, but you may wish to make a
note of the fact that the music is being skipped, for whatever reason.
Beware again: if the main program ever calls duh_sigrenderer_get_samples()
on a buffer that isn't all silence, this callback function will be passed
the existing buffer after mixing, and thus it will include the original
data. This will not be an issue if you stick to duh_render(), which always
starts with a buffer filled with silence.
The samples array is two-dimensional. Refer to it as follows:
samples[channel_number][sample_position]
where 0 <= channel_number < n_channels,
and 0 <= sample_position < length.
In addition you can pass any 'data' pointer you like to
duh_sigrenderer_set_analyser_callback(), and this pointer will be relayed
to your callback function each time.
To remove the callback function, pass NULL to
duh_sigrenderer_set_analyser_callback().
Everything below this point assumes some knowledge of how a music module is
constructed. If you do not have this knowledge, talk to whoever is writing
music for you, or download a tracking program and play with it (see
readme.txt).
To start playing an IT, XM, S3M or MOD from an arbitrary order number (the
default being 0, the beginning of the song), use the following:
DUH_SIGRENDERER *dumb_it_start_at_order
(DUH *duh, int n_channels, int startorder);
AL_DUH_PLAYER *al_duh_encapsulate_sigrenderer
(DUH_SIGRENDERER *sigrenderer, float volume, long bufsize, int freq);
The usage of these functions is as follows:
{
DUH_SIGRENDERER *sr = dumb_it_start_at_order
(duh, n_channels, startorder);
dp = al_duh_encapsulate_sigrenderer(sr, volume, bufsize, freq);
}
Replace 'dp' with whatever your AL_DUH_PLAYER pointer is. You also need
to insert suitable values for n_channels, startorder, volume, bufsize and
freq. These have the same meaning as those passed to al_start_duh().
WARNING: after passing a pointer to an "encapsulate" function, do not use
that pointer again. (More specifically, do not use it again if
the function returns NULL, because the function will have
destroyed the pointer if this happens, to help prevent memory
leaks.) There will be a "get" function with which you can obtain
the original pointer if it is still valid, or NULL otherwise.
The above functions will fail (safely) if you try to use them with a DUH
that contains a different type of music.
Notice that there is no 'pos' parameter. If you would like to skip through
the music, you can use this function:
long duh_sigrenderer_get_samples(
DUH_SIGRENDERER *sigrenderer,
float volume, float delta,
long size, sample_t **samples
);
Pass 0 for volume and NULL for samples, and this function will skip
through the music nice and quickly. So insert the following between the
two above statements:
duh_sigrenderer_get_samples(sr, 0, 65536.0f / freq, pos, NULL);
Substitute for 'freq' and 'pos'. An explanation of the 'delta' parameter
can be found further down in this file.
Finally, note that duh_get_length() is only meaningful when you start
playing music from order 0.
If an IT file contains Zxx effects, DUMB will generate MIDI messages, which
will control the low-pass resonant filters unless the IT file actively
specifies something else. In rare cases this may not be what the Zxx effects
were intended to do; if this is the case, you can block the MIDI messages as
follows. Note that this does NOT mean filters are disabled; if an instrument
specifies initial cut-off and resonance values, or has a filter envelope,
then filters will be applied. It only makes sense to use this procedure at
the beginning of playback.
void dumb_it_set_midi_callback(DUMB_IT_SIGRENDERER *sigrenderer,
int (*callback)(void *data, int channel, unsigned char byte),
void *data);
int dumb_it_callback_midi_block(void *data, int channel,
unsigned char byte);
Using some functions described in the previous section, we arrive at the
following code:
{
DUH_SIGRENDERER *sr = al_duh_get_sigrenderer(dp);
DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer(sigrenderer);
dumb_it_set_midi_callback(itsr, &dumb_it_callback_midi_block, NULL);
}
DUMB offers no way of disabling filters completely. Disabling filters is not
recommended as a means to reduce processor usage, as it will completely
damage any piece of music that uses the filters. If you want lower processor
consumption, use a piece of music that does not use filters.
Finally, DUMB offers a myriad of functions for querying and adjusting
module playback. Those beginning with "dumb_it_sd" operate on the
DUMB_IT_SIGDATA struct, which represents the piece of music before it starts
to play. Those beginning with "dumb_it_sr" operate on the DUMB_IT_SIGRENDERER
struct, which represents a currently playing instance of the music. Note that
duh_get_length(), described above, becomes meaningless after some of these
functions are used.
The method for getting a DUMB_IT_SIGRENDERER struct has already been given,
but the function prototypes are repeated here for convenience:
DUH_SIGRENDERER *al_duh_get_sigrenderer(AL_DUH_PLAYER *dp);
DUMB_IT_SIGRENDERER *duh_get_it_sigrenderer(DUH_SIGRENDERER *sigrenderer);
Getting a DUMB_IT_SIGDATA struct is simpler:
DUMB_IT_SIGDATA *duh_get_it_sigdata(DUH *duh);
For a list of dumb_it_sd_*() and dumb_it_sr_*() functions, please see
dumb.txt. These functions are new, and may not provide exactly what you need;
if not, please let me know.
**************************************************
*** Embedding music files in Allegro datafiles ***
**************************************************
In this section it is assumed you are already reasonably familiar with how
Allegro datafiles are used. If not, please refer to Allegro's documentation.
At the time of writing, the documentation you need is off the beaten track,
so to speak, in allegro/tools/grabber.txt.
To add a piece of music to a datafile, you need to create an object of type
"IT ", "XM ", "S3M " or "MOD " (note the spaces used as padding, although
you do not need to type these into the grabber). Then grab the piece of music
in. The grabber will treat it as a binary object. Save the datafile as usual.
To use a piece of music you added to the datafile, follow these steps:
1. Before loading the datafile, call one or more of these functions,
depending on which music format or formats you'd like to support:
dumb_register_dat_it(DUMB_DAT_IT);
dumb_register_dat_xm(DUMB_DAT_XM);
dumb_register_dat_s3m(DUMB_DAT_S3M);
dumb_register_dat_mod(DUMB_DAT_MOD);
Remember, do not call multiple functions unless you want to support
multiple formats. Calling more functions will add unused code to your
executable.
It is important that you make call these before loading the datafile,
since they tell Allegro how to load the respective files straight from
datafiles in the future. They will not help Allegro interpret any module
files that have already been loaded as binary objects (but if you really
need to interpret a module that has been loaded in this fashion, have a
look at dumbfile_open_memory() in dumb.txt).
If for whatever reason your music objects are identified by a different
type in the datafile, you can tell DUMB what that type is by changing the
parameter to the registration function above. Use Allegro's DAT_ID()
macro, e.g. DAT_ID('B','L','A','H'). This is not really recommended
though, since it would prevent a hypothetical grabber plug-in from being
able to play your music files. Use the above types if possible.
2. Whenever you need a pointer to a DUH struct, simply use the 'dat' field.
Do this in the same way you would for a pointer to a BITMAP struct or
anything else. If it makes you feel more comfortable, you can extract the
pointer in advance:
DATAFILE *dat = load_datafile("smurf.dat");
if (!dat) abort(); /* There are much nicer ways of handling failure! */
DUH *myduh = (DUH *)dat[GAME_MUSIC].dat;
Note that the explicit (DUH *) cast is only necessary for C++, not for C.
However, it does no harm.
Be sure that you do NOT call unload_duh() for anything stored in the
datafile. These DUHs will be freed when you call unload_datafile(), and
freeing them twice is practically guaranteed to crash your program.
3. If you only ever load music as part of a datafile, and you never load any
stand-alone music files, you do not need to register a file input system
for DUMB to use. If you followed the instructions for the first section
you will have one of these two lines in your program:
dumb_register_stdfiles();
dumb_register_packfiles();
You can safely delete this line - but only if you never load any
stand-alone music files. The debugging library will bale you out if you
delete it when you shouldn't; the optimised library won't.
*************************************
*** Rendering music into a buffer ***
*************************************
NOTE: much of the API formerly described in this section has been deprecated,
and you will need to alter your code. See deprec.txt for details. If
you are reading this section for the first time, you can ignore this
note.
Rendering to a buffer is similar to playing using an AL_DUH_PLAYER. However,
you must use a DUH_SIGRENDERER struct instead. Here are the functions:
DUH_SIGRENDERER *duh_start_sigrenderer
(DUH *duh, int sig, int n_channels, long pos);
int duh_sigrenderer_get_n_channels(DUH_SIGRENDERER *sigrenderer);
long duh_sigrenderer_get_position(DUH_SIGRENDERER *sigrenderer);
long duh_sigrenderer_get_samples(DUH_SIGRENDERER *sigrenderer,
float volume, float delta, long size, sample_t **samples);
long duh_render(DUH_SIGRENDERER *sigrenderer,
int bits, int unsign, float volume, float delta, long size, void *sptr);
void duh_end_sigrenderer(DUH_SIGRENDERER *sigrenderer);
The parameters to duh_start_sigrenderer() have the same meanings as those to
al_start_duh(). However, note that the volume is not set at this stage. You
pass the desired volume each time you want to render a block. The 'sig'
parameter should be set to 0 for now.
Notice that there are two rendering functions. duh_sigrenderer_get_samples()
will generate samples in the internal 32-bit format, with a normal range from
-0x800000 to 0x7FFFFF and with each channel in a separate array; duh_render()
will convert to 8 or 16 bits, signed or unsigned, with stereo samples
interleaved, left first.
When you call duh_render(), pass 8 or 16 for 'bits'. If you pass 8, 'sptr' is
expected to be an array of chars. If you pass 16, 'sptr' is expected to be an
array of shorts. Endianness therefore depends on the platform, and you should
not try to interpret 16-bit wave data as an array of chars (unless you're
writing highly system-specific code anyway). Because DUMB renders internally
with 32 bits, there is no significant speed increase in rendering an 8-bit
stream.
If you are rendering in stereo, make sure your 'sptr' array is twice as big!
If you set 'unsign' to a nonzero value, then the samples generated will be
centred on 0x80 or 0x8000, suitably stored in an array of unsigned chars or
unsigned shorts. If 'unsign' is zero, the samples will be centred on 0,
suitably stored in an array of signed chars or signed shorts. Note that 8-bit
WAV files are unsigned while 16-bit WAV files are signed. This convention was
used by the SoundBlaster 16 when receiving samples to be sent to the
speakers. If you wish to write 16-bit sample data to a WAV file, don't use
fwrite(); instead, take the shorts one at a time, split them up into chars as
follows, and write the chars to the file.
short sptr[n];
char lsb = (char)sptr[n];
char msb = (char)(sptr[n] >> 8);
For a 16-bit WAV file, write the LSB (less significant byte) first.
The following applies equally to duh_render() and
duh_sigrenderer_get_samples(), except where otherwise stated.
If you set 'delta' to 1.0f, the sound generated will be suitable for playback
at 65536 Hz. Increasing 'delta' causes the wave to speed up, given a constant
sampling rate for playback. Supposing you want to vary the playback sampling
rate but keep the pitch constant, here's the equation for 'delta':
delta = 65536.0f / sampling_rate;
'size' is the number of samples you want rendered. For duh_render(), they
will be rendered into an array which you pass as 'sptr'. Note that stereo
samples count as one; so if you set n_channels to 2, your array must contain
(2 * size) elements.
For duh_sigrenderer_get_samples() you will have to use the following
functions:
sample_t **create_sample_buffer(int n_channels, long length);
void destroy_sample_buffer(sample_t **samples);
void dumb_silence(sample_t *samples, long length);
create_sample_buffer() allocates the channels sequentially in memory, so the
following technique is valid:
sample_t **samples = create_sample_buffer(n_channels, length);
dumb_silence(samples[0], n_channels * length);
It is necessary to fill the buffer with silence like this because
duh_sigrenderer_get_samples() mixes what it renders with the existing
contents of the buffer.
The return values from duh_render() and duh_sigrenderer_get_samples() tell
you how many samples were actually generated. In most cases, this will be the
same as the 'size' parameter. However, if you reach the end of the DUH (which
will happen if you disable looping or freezing as described further up), this
function will return less. When that happens, you can assume the stream has
finished. In the case of duh_render(), the remainder of the array will not
have been initialised, so you either have to initialise it yourself or avoid
using it.
If for whatever reason duh_start_sigrenderer() returns NULL, then
duh_render() and duh_sigrenderer_get_samples() will generate exactly 0
samples, duh_sigrenderer_get_n_channels() will return 0,
duh_sigrenderer_get_position() will return -1, and duh_end_sigrenderer() will
safely do nothing.
*********************
*** Miscellaneous ***
*********************
Please see dumb.txt for an API reference and for information on thread safety
with DUMB. The API reference has been stripped down, since some functions and
variables are subject to change. If something does not appear in dumb.txt,
please do not use it.
******************
*** Conclusion ***
******************
If you have any difficulties, or if you use DUMB successfully, please don't
hesitate to contact me (see below).
Enjoy!
Ben Davis
entheh@users.sf.net
IRC EFnet #dumb
See readme.txt for details on using IRC.

137
dumb/docs/modplug.txt Normal file
View file

@ -0,0 +1,137 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* modplug.txt - Our official position regarding / / \ \
* compatibility with ModPlug | < / \_
* Tracker. | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
********************
*** Introduction ***
********************
ModPlug Tracker is a very popular tracker for Windows. Its popularity is due
to the intuitive interface and its many advanced features. The author has
done a good job with this piece of software, but sadly in doing so he has
desecrated the IT file format.
I am not against ModPlug Tracker being used to write music modules. As
already stated, it has some very advanced and convenient features; I use it
myself. However, I believe its users should be aware of the entire situation
before using it for any serious work.
ModPlug Tracker - http://www.modplug.com/
*************************
*** Incompatibilities ***
*************************
There are a few situations in which ModPlug Tracker misinterprets the
original module formats. I shall list the five I am most aware of, from least
to most annoying:
5. Create a multisample instrument, for example a piano. Play a low note.
Then go up the scale, but in the pattern data, make sure the instrument
column is blank; put in only the notes. Play this with ModPlug Tracker,
and play it with Impulse Tracker. Impulse Tracker changes sample as you go
up the scale; ModPlug Tracker does not.
4. Arpeggio and Retrigger Note effects behave badly when combined with
Portamento, which can appear in the volume column. While Retrigger Note
isn't too bad, Arpeggio sounds completely wrong. Try it and see what
happens. Then repeat the experiment in Impulse Tracker.
3. The filter algorithm is incorrect, in more ways than one. When Jeffrey Lim
programmed the low-pass resonant filters into Impulse Tracker, he used a
standard filter algorithm with a slight modification to achieve greater
resonance. ModPlug Tracker does not incorporate this modification.
Furthermore, ModPlug Tracker uses integer arithmetic with nowhere near
enough precision; the wave output is really poor in some cases. I don't
doubt it damages the acoustic properties of the filters in subtle ways.
2. When looping, ModPlug Tracker resets all variables. The original trackers
do not do this.
1. Worst of all, ModPlug Tracker has no regard for playback volume, and
generally has a much lower output level than the original trackers.
Cases 3, 2 and 1 lead people to write IT files that play badly in the
original trackers. If some of these problems could be fixed, I'd be all for
it - but these problems have been reported to the author and he had no
motivation to fix them. ModPlug Tracker has been around long enough that
fixing 3, 2 and 1 would be detrimental to too many people's music.
******************
*** Extensions ***
******************
Worse than the incompatibilities are the extensions ModPlug Tracker makes,
mostly to the IT format. DUMB currently supports one of these extensions,
namely stereo samples, but supporting the others is not high on my list of
priorities.
Other extensions ModPlug Tracker has provided mostly take the form of extra
effects. For instance, S98 and S99 can be used to enable or disable reverb. I
believe the latest versions of ModPlug Tracker offer alternative types of
filter, such as high-pass and band-pass. As soon as an IT file uses any of
these features, it will play incorrectly with Impulse Tracker.
By far the most evil extension provided by ModPlug Tracker is the effect
plug-ins. These enable IT files to use VST effects. I recently downloaded an
IT file that uses some effects from a collection named "DirectX Media Audio
Effects". When can we expect these effects to be ported to Linux?
******************
*** Conclusion ***
******************
ModPlug Tracker is trying to be two things at once. It wants to be an editor
for the existing formats, but at the same time it wants to be proprietary,
with all its own features and extensions. Unfortunately it is succeeding;
there are many IT files out there that only play right in ModPlug Tracker. In
my opinion, ModPlug Tracker should have come out with its own file format, in
which all these extensions would have found a home.
If you are going to use ModPlug Tracker's extensions, I recommend you
ultimately convert your music to a streamed format such as Ogg Vorbis. (If
you were thinking of using MP3, then don't - consider using Ogg Vorbis
instead.) If you release IT files that use ModPlug Tracker's extensions,
please state prominently that the files are designed to be played with
ModPlug Tracker. Finally, don't ask me to support ModPlug Tracker's
extensions; ModPlug Tracker's playback code is available for use in your
games, so use that instead.
Ogg Vorbis - http://www.vorbis.com/
Despite all the above problems, don't forget that ModPlug Tracker does have a
lot of very useful features for editing files. These include a function for
removing unused patterns, samples and instruments, drag-and-drop sample and
instrument ripping, drop-down menus for selecting the effects by name without
having to memorise the codes or refer to help, and lots of other nice things.
I do recommend it as an editor, provided you make sure you are aware of the
situation and do not use ModPlug Tracker's extensions or incompatibilities
inadvertently.
Oh, and by the way, save your final version with Impulse Tracker. Then the
samples will be compressed for you!
Ben Davis
entheh@users.sf.net
IRC EFnet #dumb
See readme.txt for details on using IRC.

129
dumb/docs/ptr.txt Normal file
View file

@ -0,0 +1,129 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* ptr.txt - Pointer explanation. / / \ \
* | < / \_
* | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
A pointer is a small variable (often the same size as an int BUT NOT ALWAYS)
that holds the address of something in memory. You create a pointer by adding
a * to a variable, as follows:
int x, *y;
x = 5;
y = &x;
The & means 'address of', so &x gives us a pointer to x. We are storing it in
y.
(*y)++;
The * here means 'value at'. It's known as the 'dereferencing' operator. When
written before a pointer, as it is here, it allows you to treat the value
like a normal variable. In this case we are incrementing the value. If we
look at x, we'll find that it now contains 6, not 5.
y++;
Here we are incrementing the pointer itself. This is useful for traversing
through an array, but in this particular example it is not much use.
*y++;
Beware; this will increment the pointer, not the value stored there. It will
return the value stored at the pointer (before incrementing the pointer), so
you can use this in a bigger expression. This is why we needed brackets in
the first example.
Note that you will not need these three examples when working with DUMB; they
are simply to help illustrate the idea of pointers.
Also be aware that when defining pointers you attach the * to the variable,
not to the type. The following example will create a pointer and an int, not
two pointers:
int *a, b;
That is why I believe it's a good idea to put a space before the * and not
after it, although programmers are divided on this.
y = 0;
y = NULL;
These two statements are equivalent. 0, or NULL, is a special value that is
guaranteed to have a different value from any valid pointer. This is most
often used to indicate that something doesn't point anywhere. DUMB's
functions may return it on occasion. However, in simple usage of DUMB, you
will not actually need to check for it.
Some of DUMB's functions return pointers to structs. (A struct is an
aggregration of other variables, such as ints, pointers, or other structs.
You can generally treat a struct as a single unit.) Here's an example of such
a function:
DUH *dumb_load_it(const char *filename);
You do not know what the DUH struct actually contains; dumb.h and aldumb.h
only give the compiler enough information to deal with pointers to them. DUMB
will take charge of everything that happens inside a DUH struct.
The above function will create a DUH struct for you. First it allocates
the memory it needs, then it fills the struct with data, then it returns a
pointer. This DUH struct will contain the data necessary to play an IT file.
You can define a suitable variable and store the pointer in it as follows:
DUH *duh = dumb_load_it("music.it");
Or this can be split up:
DUH *duh;
duh = dumb_load_it("music.it");
In order to use this DUH struct later, you must pass its pointer to other
functions. To pass the pointer to a function, simply write 'duh' for the
appropriate parameter.
When you've finished with a DUH struct (this applies equally to the other
structs DUMB deals with), you must pass it to an appropriate function for
freeing up the memory:
unload_duh(duh);
After you've done this, the memory will no longer be allocated, and the
pointer will have no meaning. You may wish to set it to NULL at this point
for safety. Alternatively just be sure not to use the present value of the
pointer any more. You can of course assign a new value to the pointer, e.g.
by calling dumb_load_it() again.
Note the following:
DUH *duh2 = duh;
This only duplicates the pointer, not the DUH itself. You still only have one
copy of the DUH. There is no way of duplicating a DUH, short of loading it
twice. This is not a problem, because DUMB can play it 'twice at the same
time' anyway.
That should be all you need to know about pointers in order to use DUMB. If
there's anything you feel should be explained better here, or anything else
that should be added, please don't hesitate to let me know!
Ben Davis
entheh@users.sf.net
IRC EFnet #dumb
See readme.txt for details on using IRC.

94
dumb/docs/tutorial.txt Normal file
View file

@ -0,0 +1,94 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* tutorial.txt - DUMB Programmers' Tutorial. / / \ \
* | < / \_
* See readme.txt for general information on | \/ /\ /
* DUMB and how to set it up. \_ / > /
* | \ / /
* | ' /
* \__/
*/
********************
*** Introduction ***
********************
Welcome to the DUMB Programmers' Tutorial!
In order to follow this tutorial, you should already have set Allegro and
DUMB up on your system, including DUMB's support for Allegro and the example
programs. If you have not done so, see readme.txt for details.
You will also need some IT files. If you would like to compose your own, then
first I must offer a word of warning: not everyone is capable of composing
music. Do not assume you will be able to learn the art. By all means have a
go; if you can learn to play tunes on the computer keyboard, you're well on
the way to being a composer!
The best program for the job is Impulse Tracker itself, available from:
http://www.noisemusic.org/it/
This is a DOS program. Users of DOS-incapable operating systems may like to
try ModPlug Tracker, but should be aware that it does not support all
combinations of effects correctly, and some IT files will sound wrong. If you
use a different operating system, or if you know of a better IT editor for
Windows, please give me some links so I can put them here!
ModPlug Tracker is available from: http://www.modplug.com/
If you would like to download IT files composed by other people, check the
following sites:
http://www.modplug.com/
http://www.traxinspace.com/
Once again, if you know of more sites where IT files are available for
download, please let me know.
Once you've got some IT files, we're ready to begin!
Note that support for S3M files has very recently been added to DUMB, but it
is bound to be faulty. I recommend you use IT files in preference until later
releases of DUMB.
******************
*** Try It Out ***
******************
We'll start simply by running one of the example programs.
Find playit.exe, in the dumb/examples folder. Choose an IT file, and pass it
to playit.exe. In DOS, you can do this by typing 'playit' followed by the
name of the IT file. In Windows, you can drag the IT file and drop it on
playit.exe.
You should now hear the music play back. If not, make sure your speakers are
on and volume is not turned down. Try one of Allegro's example programs. If
you are using DJGPP under Windows, consider using a Windows compiler instead.
If you cannot get it working, see readme.txt for details on seeking help with
DUMB.
When you are satisfied, press any key to stop the music and return to the
operating system.
*************************
*** How Does It Work? ***
*************************
Now load playit.c into your favourite editor. Here is an explanation of what
it does:

44
dumb/examples/dumb.ini Normal file
View file

@ -0,0 +1,44 @@
# Please edit this file to control the playback quality for 'dumbplay'. Note
# that this does not affect DUMB when you use it in your own programs; you
# need to borrow some code from the example program in order to get that to
# happen.
# dumb_resampling_quality can be 0 for aliasing, 1 for linear interpolation
# or 2 for cubic interpolation. See docs/dumb.txt for details on what these
# terms mean.
# dumb_it_max_to_mix is the maximum number of samples DUMB will render at a
# time. See docs/dumb.txt for a more detailed description.
# Increase buffer_size to combat stuttering.
# The music module will be rendered at the sampling frequency specified by
# sound_freq. This variable is also used by Allegro for initialising the
# sound hardware.
# buffer_size and sound_freq are passed directly to al_start_duh(). See this
# function's description in docs/dumb.txt for information about how to use
# these variables.
# You can ignore the quality variable. Allegro uses it when relaying the
# audio stream to the sound card. Only a masochist would set it lower than 2;
# if your computer is powerful enough to run DUMB, it is powerful enough to
# use this setting with Allegro.
# For best results, choose a value for sound_freq that your sound card can do
# exactly. See Allegro's docs, "Standard config variables", for details. If
# you do not choose an exact value, Allegro will round it to the nearest
# value it can do; then when DUMB plays the stream at a sampling frequency of
# sound_freq, Allegro will have to resample it. Allegro's 'quality = 2'
# setting is only comparable with DUMB's 'dumb_resampling_quality = 1'
# setting. Therefore, in order to appreciate DUMB's cubic resampler fully,
# you will need to make sure Allegro doesn't do any resampling, by choosing
# an exact value for sound_freq.
[sound]
dumb_resampling_quality = 2
dumb_it_max_to_mix = 256
buffer_size = 4096
sound_freq = 44100
quality = 2

481
dumb/examples/dumb2wav.c Normal file
View file

@ -0,0 +1,481 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* dumb2wav.c - Utility to convert DUH to WAV. / / \ \
* | < / \_
* By Chad Austin, based on dumbout.c by entheh. | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <dumb.h>
#include <internal/it.h>
union {
float s32[4096];
short s16[8192];
char s8[16384];
} buffer;
sample_t ** internal_buffer;
int loop_count = 1;
static int write32_le(FILE* outf, unsigned int value) {
int total = 0;
total += fputc(value & 0xFF, outf);
total += fputc((value >> 8) & 0xFF, outf);
total += fputc((value >> 16) & 0xFF, outf);
total += fputc((value >> 24) & 0xFF, outf);
return total;
}
static int write16_le(FILE* outf, unsigned int value) {
int total = 0;
total += fputc(value & 0xFF, outf);
total += fputc((value >> 8) & 0xFF, outf);
return total;
}
static int loop_callback(void* data) {
return (--loop_count <= 0 ? -1 : 0);
}
int main(int argc, const char *argv[])
{
DUH *duh;
DUH_SIGRENDERER *sr;
const char *fn = NULL;
const char *fn_out = NULL;
FILE *outf;
int depth = 16;
int unsign = 0;
int freq = 44100;
int n_channels = 2;
int solo = -1;
float volume = 1.0f;
float delay = 0.0f;
float delta;
int bufsize;
clock_t start, end;
int data_written = 0; /* total bytes written to data chunk */
int i = 1;
LONG_LONG length;
LONG_LONG done;
int dots;
while (i < argc) {
const char *arg = argv[i++];
if (*arg != '-') {
if (fn) {
fprintf(stderr,
"Cannot specify multiple filenames!\n"
"Second filename found: \"%s\"\n", arg);
return 1;
}
fn = arg;
continue;
}
arg++;
while (*arg) {
char *endptr;
switch (*arg++) {
case 'o':
case 'O':
if (i >= argc) {
fprintf(stderr, "Out of arguments; output filename expected!\n");
return 1;
}
fn_out = argv[i++];
break;
case 'd':
case 'D':
if (i >= argc) {
fprintf(stderr, "Out of arguments; delay expected!\n");
return 1;
}
delay = (float)strtod(argv[i++], &endptr);
if (*endptr != 0 || delay < 0.0f || delay > 64.0f) {
fprintf(stderr, "Invalid delay!\n");
return 1;
}
break;
case 'v':
case 'V':
if (i >= argc) {
fprintf(stderr, "Out of arguments; volume expected!\n");
return 1;
}
volume = (float)strtod(argv[i++], &endptr);
if (*endptr != 0 || volume < -8.0f || volume > 8.0f) {
fprintf(stderr, "Invalid volume!\n");
return 1;
}
break;
case 's':
case 'S':
if (i >= argc) {
fprintf(stderr, "Out of arguments; sampling rate expected!\n");
return 1;
}
freq = strtol(argv[i++], &endptr, 10);
if (*endptr != 0 || freq < 1 || freq > 960000) {
fprintf(stderr, "Invalid sampling rate!\n");
return 1;
}
break;
case 'f':
depth = 32;
break;
case '8':
depth = 8;
break;
case 'l':
case 'L':
if (i >= argc) {
fprintf(stderr, "Out of arguments: loop count expected!\n");
return 1;
}
loop_count = strtol(argv[i++], &endptr, 10);
break;
case 'm':
case 'M':
n_channels = 1;
break;
case 'u':
case 'U':
unsign = 1;
break;
case 'r':
case 'R':
if (i >= argc) {
fprintf(stderr, "Out of arguments; resampling quality expected!\n");
return 1;
}
dumb_resampling_quality = strtol(argv[i++], &endptr, 10);
if (*endptr != 0 || dumb_resampling_quality < 0 || dumb_resampling_quality > 2) {
fprintf(stderr, "Invalid resampling quality!\n");
return 1;
}
break;
case 'c':
case 'C':
if (i >= argc) {
fprintf(stderr, "Out of arguments; channel number expected!\n");
return 1;
}
solo = strtol(argv[i++], &endptr, 10);
if (*endptr != 0 || solo < 0 || solo >= DUMB_IT_N_CHANNELS) {
fprintf(stderr, "Invalid channel number!\n");
return 1;
}
break;
default:
fprintf(stderr, "Invalid switch - '%c'!\n", isprint(arg[-1]) ? arg[-1] : '?');
return 1;
}
}
}
if (!fn) {
fprintf(stderr,
"Usage: dumb2wav [options] module [more-options]\n"
"\n"
"The module can be any IT, XM, S3M or MOD file. It will be rendered to a .wav\n"
"file of the same name, unless you specify otherwise with the -o option.\n"
"\n"
"The valid options are:\n"
"-o <file> specify the output filename (defaults to the input filename with\n"
" the extension replaced with .wav); use - to write to standard\n"
" output or . to write nowhere (useful for measuring DUMB's\n"
" performance, and DOS and Windows don't have /dev/null!)\n"
"-d <delay> set the initial delay, in seconds (default 0.0)\n"
"-v <volume> adjust the volume (default 1.0)\n"
"-s <freq> set the sampling rate in Hz (default 44100)\n"
"-8 generate 8-bit instead of 16-bit\n"
"-f generate floating point samples instead of 16-bit\n"
"-m generate mono output instead of stereo left/right pairs\n"
"-u generated unsigned output instead of signed\n"
"-r <value> specify the resampling quality to use\n"
"-l <value> specify the number of times to loop (default 1)\n"
"-c <value> specify a channel number to solo\n");
return 1;
}
atexit(&dumb_exit);
dumb_register_stdfiles();
dumb_it_max_to_mix = 256;
duh = load_duh(fn);
if (!duh) {
duh = dumb_load_it(fn);
if (!duh) {
duh = dumb_load_xm(fn);
if (!duh) {
duh = dumb_load_s3m(fn);
if (!duh) {
duh = dumb_load_mod(fn);
if (!duh) {
fprintf(stderr, "Unable to open %s!\n", fn);
return 1;
}
}
}
}
}
sr = duh_start_sigrenderer(duh, 0, n_channels, 0);
if (!sr) {
unload_duh(duh);
fprintf(stderr, "Unable to play file!\n");
return 1;
}
if (solo >= 0) {
DUMB_IT_SIGRENDERER * itsr = duh_get_it_sigrenderer(sr);
if (itsr) {
for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
if (i != solo) {
IT_CHANNEL * channel = &itsr->channel[i];
IT_PLAYING * playing = channel->playing;
channel->flags |= IT_CHANNEL_MUTED;
/* start_sigrenderer leaves me all of the channels the first tick triggered */
if (playing) {
playing->ramp_volume[0] = 0;
playing->ramp_volume[1] = 0;
playing->ramp_delta[0] = 0;
playing->ramp_delta[1] = 0;
}
}
}
}
}
if (fn_out) {
if (fn_out[0] == '-' && fn_out[1] == 0)
outf = stdout;
else if (fn_out[0] == '.' && fn_out[1] == 0)
outf = NULL;
else {
outf = fopen(fn_out, "wb");
if (!outf) {
fprintf(stderr, "Unable to open %s for writing!\n", fn_out);
duh_end_sigrenderer(sr);
unload_duh(duh);
return 1;
}
}
} else {
char *extptr = NULL, *p;
char *fn_out = malloc(strlen(fn)+5);
if (!fn_out) {
fprintf(stderr, "Out of memory!\n");
duh_end_sigrenderer(sr);
unload_duh(duh);
return 1;
}
strcpy(fn_out, fn);
for (p = fn_out; *p; p++)
if (*p == '.') extptr = p;
if (!extptr) extptr = p;
strcpy(extptr, ".wav");
outf = fopen(fn_out, "wb");
if (!outf) {
fprintf(stderr, "Unable to open %s for writing!\n", fn_out);
free(fn_out);
duh_end_sigrenderer(sr);
unload_duh(duh);
return 1;
}
free(fn_out);
}
{
DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer(sr);
dumb_it_set_ramp_style(itsr, 2);
dumb_it_set_loop_callback(itsr, loop_callback, NULL);
dumb_it_set_xm_speed_zero_callback(itsr, &dumb_it_callback_terminate, NULL);
dumb_it_set_global_volume_zero_callback(itsr, &dumb_it_callback_terminate, NULL);
}
if (outf) {
/* write RIFF header: fill file length later */
fwrite("RIFF", 1, 4, outf);
fwrite(" ", 1, 4, outf);
fwrite("WAVE", 1, 4, outf);
/* write format chunk */
fwrite("fmt ", 1, 4, outf);
if (depth == 32)
{
write32_le(outf, 18);
write16_le(outf, 3);
}
else
{
write32_le(outf, 16); /* header length */
write16_le(outf, 1); /* WAVE_FORMAT_PCM */
}
write16_le(outf, n_channels); /* channel count */
write32_le(outf, freq); /* frequency */
write32_le(outf, freq * n_channels * depth / 8); /*bytes/sec*/
write16_le(outf, n_channels * depth / 8); /* block alignment */
write16_le(outf, depth); /* bits per sample */
if (depth == 32)
{
write16_le(outf, 0);
}
/* start data chunk */
fwrite("data", 1, 4, outf);
fwrite(" ", 1, 4, outf); /* fill in later */
}
length = (LONG_LONG)_dumb_it_build_checkpoints(duh_get_it_sigdata(duh), 0) * freq >> 16;
done = 0;
dots = 0;
delta = 65536.0f / freq;
bufsize = sizeof(buffer);
if (depth == 32) bufsize /= sizeof(*buffer.s32);
else if (depth == 16) bufsize /= sizeof(*buffer.s16);
bufsize /= n_channels;
if (depth == 32) {
internal_buffer = create_sample_buffer(n_channels, bufsize);
if (!internal_buffer) {
fprintf(stderr, "Out of memory!\n");
duh_end_sigrenderer(sr);
unload_duh(duh);
}
}
{
long l = (long)floor(delay * freq + 0.5f);
l *= n_channels * (depth >> 3);
if (l) {
if (unsign && depth != 32) {
if (depth == 16) {
for (i = 0; i < 8192; i++) {
buffer.s8[i*2] = 0x00;
buffer.s8[i*2+1] = 0x80;
}
} else
memset(buffer.s8, 0x80, 16384);
} else
memset(buffer.s8, 0, 16384);
while (l >= 16384) {
if (outf) fwrite(buffer.s8, 1, 16384, outf);
l -= 16384;
data_written += 16384;
}
if (l) {
if (outf) fwrite(buffer.s8, 1, l, outf);
data_written += 1;
}
}
}
start = clock();
fprintf(stderr, "................................................................\n");
for (;;) {
int write_size;
int l;
if (depth != 32) {
l = duh_render(sr, depth, unsign, volume, delta, bufsize, &buffer);
if (depth == 16) {
for (i = 0; i < l * n_channels; i++) {
short val = buffer.s16[i];
buffer.s8[i*2] = val;
buffer.s8[i*2+1] = val >> 8;
}
}
} else {
int j;
dumb_silence(internal_buffer[0], bufsize * n_channels);
l = duh_sigrenderer_get_samples(sr, volume, delta, bufsize, internal_buffer);
for (i = 0; i < n_channels; i++) {
for (j = 0; j < l; j++) {
buffer.s32[j * n_channels + i] = (float)((double)internal_buffer[i][j] * (1.0 / (double)(0x800000)));
}
}
}
write_size = l * n_channels * (depth >> 3);
if (outf) fwrite(buffer.s8, 1, write_size, outf);
data_written += write_size;
if (l < bufsize) break;
done += l;
l = done * 64 / length;
while (dots < 64 && l > dots) {
fprintf(stderr, "|");
dots++;
}
if (dots >= 64) {
putchar('\n');
dots = 0;
done = 0;
}
}
while (64 > dots) {
fprintf(stderr, "|");
dots++;
}
fprintf(stderr, "\n");
end = clock();
if (depth == 32) destroy_sample_buffer(internal_buffer);
/* fill in blanks we left in WAVE file */
if (outf) {
/* file size, not including RIFF header */
const int fmt_size = 8 + ((depth == 32) ? 18 : 16);
const int data_size = 8 + data_written;
const int file_size = fmt_size + data_size;
/* can we seek stdout? */
fseek(outf, 4, SEEK_SET);
write32_le(outf, file_size);
fseek(outf, 12 + fmt_size + 4, SEEK_SET);
write32_le(outf, data_written);
}
duh_end_sigrenderer(sr);
unload_duh(duh);
if (outf && outf != stdout) fclose(outf);
fprintf(stderr, "Elapsed time: %f seconds\n", (end - start) / (float)CLOCKS_PER_SEC);
return 0;
}

404
dumb/examples/dumbout.c Normal file
View file

@ -0,0 +1,404 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* dumbout.c - Utility to stream music to a file. / / \ \
* | < / \_
* By entheh. | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <dumb.h>
#include <internal/it.h>
union {
float s32[4096];
short s16[8192];
char s8[16384];
} buffer;
sample_t ** internal_buffer;
int main(int argc, const char *const *argv) /* I'm const-crazy! */
{
DUH *duh;
DUH_SIGRENDERER *sr;
const char *fn = NULL;
const char *fn_out = NULL;
FILE *outf;
int depth = 16;
int bigendian = 0;
int unsign = 0;
int freq = 44100;
int n_channels = 2;
int solo = -1;
float volume = 1.0f;
float delay = 0.0f;
float delta;
int bufsize;
clock_t start, end;
int i = 1;
LONG_LONG length;
LONG_LONG done;
int dots;
while (i < argc) {
const char *arg = argv[i++];
if (*arg != '-') {
if (fn) {
fprintf(stderr,
"Cannot specify multiple filenames!\n"
"Second filename found: \"%s\"\n", arg);
return 1;
}
fn = arg;
continue;
}
arg++;
while (*arg) {
char *endptr;
switch (*arg++) {
case 'o':
case 'O':
if (i >= argc) {
fprintf(stderr, "Out of arguments; output filename expected!\n");
return 1;
}
fn_out = argv[i++];
break;
case 'd':
case 'D':
if (i >= argc) {
fprintf(stderr, "Out of arguments; delay expected!\n");
return 1;
}
delay = (float)strtod(argv[i++], &endptr);
if (*endptr != 0 || delay < 0.0f || delay > 64.0f) {
fprintf(stderr, "Invalid delay!\n");
return 1;
}
break;
case 'v':
case 'V':
if (i >= argc) {
fprintf(stderr, "Out of arguments; volume expected!\n");
return 1;
}
volume = (float)strtod(argv[i++], &endptr);
if (*endptr != 0 || volume < -8.0f || volume > 8.0f) {
fprintf(stderr, "Invalid volume!\n");
return 1;
}
break;
case 's':
case 'S':
if (i >= argc) {
fprintf(stderr, "Out of arguments; sampling rate expected!\n");
return 1;
}
freq = strtol(argv[i++], &endptr, 10);
if (*endptr != 0 || freq < 1 || freq > 960000) {
fprintf(stderr, "Invalid sampling rate!\n");
return 1;
}
break;
case 'f':
depth = 32;
break;
case '8':
depth = 8;
break;
case 'b':
case 'B':
bigendian = 1;
break;
case 'm':
case 'M':
n_channels = 1;
break;
case 'u':
case 'U':
unsign = 1;
break;
case 'r':
case 'R':
if (i >= argc) {
fprintf(stderr, "Out of arguments; resampling quality expected!\n");
return 1;
}
dumb_resampling_quality = strtol(argv[i++], &endptr, 10);
if (*endptr != 0 || dumb_resampling_quality < 0 || dumb_resampling_quality > 2) {
fprintf(stderr, "Invalid resampling quality!\n");
return 1;
}
break;
case 'c':
case 'C':
if (i >= argc) {
fprintf(stderr, "Out of arguments; channel number expected!\n");
return 1;
}
solo = strtol(argv[i++], &endptr, 10);
if (*endptr != 0 || solo < 0 || solo >= DUMB_IT_N_CHANNELS) {
fprintf(stderr, "Invalid channel number!\n");
return 1;
}
break;
default:
fprintf(stderr, "Invalid switch - '%c'!\n", isprint(arg[-1]) ? arg[-1] : '?');
return 1;
}
}
}
if (!fn) {
fprintf(stderr,
"Usage: dumbout [options] module [more-options]\n"
"\n"
"The module can be any IT, XM, S3M or MOD file. It will be rendered to a .pcm\n"
"file of the same name, unless you specify otherwise with the -o option.\n"
"\n"
"The valid options are:\n"
"-o <file> specify the output filename (defaults to the input filename with\n"
" the extension replaced with .pcm); use - to write to standard\n"
" output or . to write nowhere (useful for measuring DUMB's\n"
" performance, and DOS and Windows don't have /dev/null!)\n"
"-d <delay> set the initial delay, in seconds (default 0.0)\n"
"-v <volume> adjust the volume (default 1.0)\n"
"-s <freq> set the sampling rate in Hz (default 44100)\n"
"-8 generate 8-bit instead of 16-bit\n"
"-f generate 32-bit floating point data instead of 16-bit\n"
"-b generate big-endian data instead of little-endian (meaningless when\n"
" using -8)\n"
"-m generate mono output instead of stereo left/right pairs\n"
"-u generated unsigned output instead of signed\n"
"-r <value> specify the resampling quality to use\n"
"-c <value> specify a channel number to solo\n");
return 1;
}
atexit(&dumb_exit);
dumb_register_stdfiles();
dumb_it_max_to_mix = 256;
duh = load_duh(fn);
if (!duh) {
duh = dumb_load_it(fn);
if (!duh) {
duh = dumb_load_xm(fn);
if (!duh) {
duh = dumb_load_s3m(fn);
if (!duh) {
duh = dumb_load_mod(fn);
if (!duh) {
fprintf(stderr, "Unable to open %s!\n", fn);
return 1;
}
}
}
}
}
sr = duh_start_sigrenderer(duh, 0, n_channels, 0);
if (!sr) {
unload_duh(duh);
fprintf(stderr, "Unable to play file!\n");
return 1;
}
if (solo >= 0) {
DUMB_IT_SIGRENDERER * itsr = duh_get_it_sigrenderer(sr);
if (itsr) {
for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
if (i != solo) {
IT_CHANNEL * channel = &itsr->channel[i];
IT_PLAYING * playing = channel->playing;
channel->flags |= IT_CHANNEL_MUTED;
/* start_sigrenderer leaves me all of the channels the first tick triggered */
if (playing) {
playing->ramp_volume[0] = 0;
playing->ramp_volume[1] = 0;
playing->ramp_delta[0] = 0;
playing->ramp_delta[1] = 0;
}
}
}
}
}
if (fn_out) {
if (fn_out[0] == '-' && fn_out[1] == 0)
outf = stdout;
else if (fn_out[0] == '.' && fn_out[1] == 0)
outf = NULL;
else {
outf = fopen(fn_out, "wb");
if (!outf) {
fprintf(stderr, "Unable to open %s for writing!\n", fn_out);
duh_end_sigrenderer(sr);
unload_duh(duh);
return 1;
}
}
} else {
char *extptr = NULL, *p;
char *fn_out = malloc(strlen(fn)+5);
if (!fn_out) {
fprintf(stderr, "Out of memory!\n");
duh_end_sigrenderer(sr);
unload_duh(duh);
return 1;
}
strcpy(fn_out, fn);
for (p = fn_out; *p; p++)
if (*p == '.') extptr = p;
if (!extptr) extptr = p;
strcpy(extptr, ".pcm");
outf = fopen(fn_out, "wb");
if (!outf) {
fprintf(stderr, "Unable to open %s for writing!\n", fn_out);
free(fn_out);
duh_end_sigrenderer(sr);
unload_duh(duh);
return 1;
}
free(fn_out);
}
{
DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer(sr);
dumb_it_set_ramp_style(itsr, 2);
dumb_it_set_loop_callback(itsr, &dumb_it_callback_terminate, NULL);
dumb_it_set_xm_speed_zero_callback(itsr, &dumb_it_callback_terminate, NULL);
dumb_it_set_global_volume_zero_callback(itsr, &dumb_it_callback_terminate, NULL);
}
length = (LONG_LONG)_dumb_it_build_checkpoints(duh_get_it_sigdata(duh), 0) * freq >> 16;
done = 0;
dots = 0;
delta = 65536.0f / freq;
bufsize = sizeof(buffer);
if (depth == 32) bufsize /= sizeof(*buffer.s32);
else if (depth == 16) bufsize /= sizeof(*buffer.s16);
bufsize /= n_channels;
if (depth == 32) {
internal_buffer = create_sample_buffer(n_channels, bufsize);
if (!internal_buffer) {
fprintf(stderr, "Out of memory!\n");
duh_end_sigrenderer(sr);
unload_duh(duh);
}
}
{
long l = (long)floor(delay * freq + 0.5f);
l *= n_channels * (depth >> 3);
if (l) {
if (unsign && depth != 32) {
if (depth == 16) {
if (bigendian) {
for (i = 0; i < 8192; i++) {
buffer.s8[i*2] = 0x80;
buffer.s8[i*2+1] = 0x00;
}
} else {
for (i = 0; i < 8192; i++) {
buffer.s8[i*2] = 0x00;
buffer.s8[i*2+1] = 0x80;
}
}
} else
memset(buffer.s8, 0x80, 16384);
} else
memset(buffer.s8, 0, 16384);
while (l >= 16384) {
if (outf) fwrite(buffer.s8, 1, 16384, outf);
l -= 16384;
}
if (l && outf) fwrite(buffer.s8, 1, l, outf);
}
}
start = clock();
fprintf(stderr, "................................................................\n");
for (;;) {
int l;
if (depth != 32) {
l = duh_render(sr, depth, unsign, volume, delta, bufsize, &buffer);
if (depth == 16) {
if (bigendian) {
for (i = 0; i < l * n_channels; i++) {
short val = buffer.s16[i];
buffer.s8[i*2] = val >> 8;
buffer.s8[i*2+1] = val;
}
} else {
for (i = 0; i < l * n_channels; i++) {
short val = buffer.s16[i];
buffer.s8[i*2] = val;
buffer.s8[i*2+1] = val >> 8;
}
}
}
} else {
int j;
dumb_silence(internal_buffer[0], bufsize * n_channels);
l = duh_sigrenderer_get_samples(sr, volume, delta, bufsize, internal_buffer);
for (i = 0; i < n_channels; i++) {
for (j = i; j < l; j++) {
buffer.s32[j * n_channels] = (float)((double)internal_buffer[i][j] * (1.0 / (double)(0x800000)));
}
}
}
if (outf) fwrite(buffer.s8, 1, l * n_channels * (depth >> 3), outf);
if (l < bufsize) break;
done += l;
l = done * 64 / length;
while (dots < 64 && l > dots) {
fprintf(stderr, "|");
dots++;
}
}
while (64 > dots) {
fprintf(stderr, "|");
dots++;
}
fprintf(stderr, "\n");
end = clock();
if (depth == 32) destroy_sample_buffer(internal_buffer);
duh_end_sigrenderer(sr);
unload_duh(duh);
if (outf && outf != stdout) fclose(outf);
fprintf(stderr, "Elapsed time: %f seconds\n", (end - start) / (float)CLOCKS_PER_SEC);
return 0;
}

238
dumb/examples/dumbplay.c Normal file
View file

@ -0,0 +1,238 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* dumbplay.c - Not-so-simple program to play / / \ \
* music. It used to be simpler! | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* IMPORTANT NOTE: This file is not very friendly. | ' /
* I strongly recommend AGAINST using it as a \__/
* reference for writing your own code. If you would
* like to write a program that uses DUMB, or add DUMB to an existing
* project, please use docs/howto.txt. It will help you a lot more than this
* file can. (If you have difficulty reading documentation, you are lacking
* an important coding skill, and now is as good a time as any to learn.)
*/
#include <stdlib.h>
#include <allegro.h>
#ifndef ALLEGRO_DOS
#include <string.h>
#endif
/* Note that your own programs should use <aldumb.h> not "aldumb.h". <> tells
* the compiler to look in the compiler's default header directory, which is
* where DUMB should be installed before you use it (make install does this).
* Use "" when it is your own header file. This example uses "" because DUMB
* might not have been installed yet when the makefile builds it.
*/
#include "aldumb.h"
#ifndef ALLEGRO_DOS
static volatile int closed = 0;
static void closehook(void) { closed = 1; }
#else
#define closed 0
#endif
#ifdef ALLEGRO_WINDOWS
#define GFX_DUMB_MODE GFX_GDI
#include <winalleg.h>
#define YIELD() Sleep(1)
#else
#define GFX_DUMB_MODE GFX_AUTODETECT_WINDOWED
#ifdef ALLEGRO_UNIX
#include <sys/time.h>
static void YIELD(void)
{
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1;
select(0, NULL, NULL, NULL, &tv);
}
#else
#define YIELD() yield_timeslice()
#endif
#endif
#ifdef ALLEGRO_DOS
static int loop_callback(void *data)
{
(void)data;
printf("Music has looped.\n");
return 0;
}
static int xm_speed_zero_callback(void *data)
{
(void)data;
printf("Music has stopped.\n");
return 0;
}
#else
static int gfx_half_width;
static int loop_callback(void *data)
{
(void)data;
if (gfx_half_width) {
acquire_screen();
textout_centre(screen, font, "Music has looped.", gfx_half_width, 36, 10);
release_screen();
}
return 0;
}
static int xm_speed_zero_callback(void *data)
{
(void)data;
if (gfx_half_width) {
text_mode(0); /* In case this is overwriting "Music has looped." */
acquire_screen();
textout_centre(screen, font, "Music has stopped.", gfx_half_width, 36, 10);
release_screen();
}
return 0;
}
#endif
static void usage(const char *exename)
{
allegro_message(
#ifdef ALLEGRO_WINDOWS
"Usage:\n"
" At the command line: %s file\n"
" In Windows Explorer: drag a file on to this program's icon.\n"
#else
"Usage: %s file\n"
#endif
"This will play the music file specified.\n"
"File formats supported: IT XM S3M MOD.\n"
"You can control playback quality by editing dumb.ini.\n", exename);
exit(1);
}
int main(int argc, const char *const *argv) /* I'm const-crazy! */
{
DUH *duh;
AL_DUH_PLAYER *dp;
if (allegro_init())
return 1;
if (argc != 2)
usage(argv[0]);
set_config_file("dumb.ini");
if (install_keyboard()) {
allegro_message("Failed to initialise keyboard driver!\n");
return 1;
}
set_volume_per_voice(0);
if (install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
allegro_message("Failed to initialise sound driver!\n%s\n", allegro_error);
return 1;
}
atexit(&dumb_exit);
dumb_register_packfiles();
duh = dumb_load_it(argv[1]);
if (!duh) {
duh = dumb_load_xm(argv[1]);
if (!duh) {
duh = dumb_load_s3m(argv[1]);
if (!duh) {
duh = dumb_load_mod(argv[1]);
if (!duh) {
allegro_message("Failed to load %s!\n", argv[1]);
return 1;
}
}
}
}
dumb_resampling_quality = get_config_int("sound", "dumb_resampling_quality", 4);
dumb_it_max_to_mix = get_config_int("sound", "dumb_it_max_to_mix", 128);
#ifndef ALLEGRO_DOS
{
const char *fn = get_filename(argv[1]);
gfx_half_width = strlen(fn);
if (gfx_half_width < 22) gfx_half_width = 22;
gfx_half_width = (gfx_half_width + 2) * 4;
/* set_window_title() is not const-correct (yet). */
set_window_title((char *)"DUMB Music Player");
if (set_gfx_mode(GFX_DUMB_MODE, gfx_half_width*2, 80, 0, 0) == 0) {
acquire_screen();
textout_centre(screen, font, fn, gfx_half_width, 20, 14);
textout_centre(screen, font, "Press any key to exit.", gfx_half_width, 52, 11);
release_screen();
} else
gfx_half_width = 0;
}
#if ALLEGRO_VERSION*10000 + ALLEGRO_SUB_VERSION*100 + ALLEGRO_WIP_VERSION >= 40105
set_close_button_callback(&closehook);
#else
set_window_close_hook(&closehook);
#endif
#endif
set_display_switch_mode(SWITCH_BACKGROUND);
dp = al_start_duh(duh, 2, 0, 1.0f,
get_config_int("sound", "buffer_size", 4096),
get_config_int("sound", "sound_freq", 44100));
{
DUH_SIGRENDERER *sr = al_duh_get_sigrenderer(dp);
DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer(sr);
dumb_it_set_loop_callback(itsr, &loop_callback, NULL);
dumb_it_set_xm_speed_zero_callback(itsr, &xm_speed_zero_callback, NULL);
}
for (;;) {
if (keypressed()) {
readkey();
break;
}
if (al_poll_duh(dp) || closed)
break;
YIELD();
}
al_stop_duh(dp);
unload_duh(duh);
return 0;
}
END_OF_MAIN();

169
dumb/examples/playduh.c Normal file
View file

@ -0,0 +1,169 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* playduh.c - Simple program to play DUH files. / / \ \
* | < / \_
* By entheh. | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <allegro.h>
#ifndef ALLEGRO_DOS
#include <string.h>
#endif
/* Note that your own programs should use <aldumb.h> not "aldumb.h". <> tells
* the compiler to look in the compiler's default header directory, which is
* where DUMB should be installed before you use it (make install does this).
* Use "" when it is your own header file. This example uses "" because DUMB
* might not have been installed yet when the makefile builds it.
*/
#include "aldumb.h"
#ifndef ALLEGRO_DOS
static int closed = 0;
static void closehook(void) { closed = 1; }
#else
#define closed 0
#endif
#ifdef ALLEGRO_WINDOWS
#define GFX_DUMB_MODE GFX_GDI
#include <winalleg.h>
#define YIELD() Sleep(1)
#else
#define GFX_DUMB_MODE GFX_AUTODETECT_WINDOWED
#ifdef ALLEGRO_UNIX
#include <sys/time.h>
static void YIELD(void)
{
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1;
select(0, NULL, NULL, NULL, &tv);
}
#else
#define YIELD() yield_timeslice()
#endif
#endif
static void usage(void)
{
allegro_message(
"Usage: playduh file.duh\n"
"This will play the .duh file specified.\n"
"You can control playback quality by editing dumb.ini.\n"
);
exit(1);
}
int main(int argc, char *argv[])
{
DUH *duh;
AL_DUH_PLAYER *dp;
if (allegro_init())
return 1;
if (argc != 2)
usage();
set_config_file("dumb.ini");
if (install_keyboard()) {
allegro_message("Failed to initialise keyboard driver!\n");
return 1;
}
set_volume_per_voice(0);
if (install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
allegro_message("Failed to initialise sound driver!\n%s\n", allegro_error);
return 1;
}
atexit(&dumb_exit);
dumb_register_stdfiles();
/*
dumb_register_sigtype_sample();
dumb_register_sigtype_combining();
dumb_register_sigtype_stereopan();
dumb_register_sigtype_sequence();
*/
duh = load_duh(argv[1]);
if (!duh) {
allegro_message("Failed to load %s!\n", argv[1]);
return 1;
}
dumb_resampling_quality = get_config_int("sound", "dumb_resampling_quality", 4);
// Are we sure dumb_it_max_to_mix will be unused? Can decide when editor matures...
#ifndef ALLEGRO_DOS
{
const char *fn = get_filename(argv[1]);
int w = strlen(fn);
if (w < 22) w = 22;
w = (w + 2) * 4;
set_window_title("DUMB - IT player");
if (set_gfx_mode(GFX_DUMB_MODE, w*2, 80, 0, 0) == 0) {
acquire_screen();
textout_centre(screen, font, fn, w, 28, 14);
textout_centre(screen, font, "Press any key to exit.", w, 44, 11);
release_screen();
}
}
//set_window_close_hook(&closehook);
#endif
set_display_switch_mode(SWITCH_BACKGROUND);
dp = al_start_duh(duh, 2, 0, 1.0,
get_config_int("sound", "buffer_size", 4096),
get_config_int("sound", "sound_freq", 44100));
for (;;) {
if (keypressed()) {
readkey();
break;
}
if (al_poll_duh(dp) || closed)
break;
YIELD();
}
al_stop_duh(dp);
unload_duh(duh);
return 0;
}
END_OF_MAIN();

760
dumb/include/dumb.h Normal file
View file

@ -0,0 +1,760 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* dumb.h - The user header file for DUMB. / / \ \
* | < / \_
* Include this file in any of your files in | \/ /\ /
* which you wish to use the DUMB functions \_ / > /
* and variables. | \ / /
* | ' /
* Allegro users, you will probably want aldumb.h. \__/
*/
#ifndef DUMB_H
#define DUMB_H
#include <stdlib.h>
#include <stdio.h>
#if defined(_DEBUG) && defined(_MSC_VER)
#ifndef _CRTDBG_MAP_ALLOC
#define _CRTDBG_MAP_ALLOC
#endif
#include <crtdbg.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define DUMB_MAJOR_VERSION 0
#define DUMB_MINOR_VERSION 9
#define DUMB_REVISION_VERSION 3
#define DUMB_VERSION (DUMB_MAJOR_VERSION*10000 + DUMB_MINOR_VERSION*100 + DUMB_REVISION_VERSION)
#define DUMB_VERSION_STR "0.9.3"
#define DUMB_NAME "DUMB v"DUMB_VERSION_STR
#define DUMB_YEAR 2005
#define DUMB_MONTH 8
#define DUMB_DAY 7
#define DUMB_YEAR_STR2 "05"
#define DUMB_YEAR_STR4 "2005"
#define DUMB_MONTH_STR1 "8"
#define DUMB_DAY_STR1 "7"
#if DUMB_MONTH < 10
#define DUMB_MONTH_STR2 "0"DUMB_MONTH_STR1
#else
#define DUMB_MONTH_STR2 DUMB_MONTH_STR1
#endif
#if DUMB_DAY < 10
#define DUMB_DAY_STR2 "0"DUMB_DAY_STR1
#else
#define DUMB_DAY_STR2 DUMB_DAY_STR1
#endif
/* WARNING: The month and day were inadvertently swapped in the v0.8 release.
* Please do not compare this constant against any date in 2002. In
* any case, DUMB_VERSION is probably more useful for this purpose.
*/
#define DUMB_DATE (DUMB_YEAR*10000 + DUMB_MONTH*100 + DUMB_DAY)
#define DUMB_DATE_STR DUMB_DAY_STR1"."DUMB_MONTH_STR1"."DUMB_YEAR_STR4
#undef MIN
#undef MAX
#undef MID
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#define MAX(x,y) (((x) > (y)) ? (x) : (y))
#define MID(x,y,z) MAX((x), MIN((y), (z)))
#undef ABS
#define ABS(x) (((x) >= 0) ? (x) : (-(x)))
#ifdef DEBUGMODE
#ifndef ASSERT
#include <assert.h>
#define ASSERT(n) assert(n)
#endif
#ifndef TRACE
// it would be nice if this did actually trace ...
#define TRACE 1 ? (void)0 : (void)printf
#endif
#else
#ifndef ASSERT
#define ASSERT(n)
#endif
#ifndef TRACE
#define TRACE 1 ? (void)0 : (void)printf
#endif
#endif
#define DUMB_ID(a,b,c,d) (((unsigned int)(a) << 24) | \
((unsigned int)(b) << 16) | \
((unsigned int)(c) << 8) | \
((unsigned int)(d) ))
#ifdef __DOS__
typedef long int32;
typedef unsigned long uint32;
typedef signed long sint32;
#else
typedef int int32;
typedef unsigned int uint32;
typedef signed int sint32;
#endif
#define CDECL
#ifndef LONG_LONG
#if defined __GNUC__ || defined __INTEL_COMPILER || defined __MWERKS__
#define LONG_LONG long long
#elif defined _MSC_VER || defined __WATCOMC__
#define LONG_LONG __int64
#undef CDECL
#define CDECL __cdecl
#elif defined __sgi
#define LONG_LONG long long
#else
#error 64-bit integer type unknown
#endif
#endif
#if __GNUC__ * 100 + __GNUC_MINOR__ >= 301 /* GCC 3.1+ */
#ifndef DUMB_DECLARE_DEPRECATED
#define DUMB_DECLARE_DEPRECATED
#endif
#define DUMB_DEPRECATED __attribute__((__deprecated__))
#else
#define DUMB_DEPRECATED
#endif
#define DUMBEXPORT CDECL
#define DUMBCALLBACK CDECL
/* Basic Sample Type. Normal range is -0x800000 to 0x7FFFFF. */
typedef int sample_t;
/* Library Clean-up Management */
int dumb_atexit(void (*proc)(void));
void dumb_exit(void);
/* File Input Functions */
typedef struct DUMBFILE_SYSTEM
{
void *(*open)(const char *filename);
int (*skip)(void *f, int32 n);
int (*getc)(void *f);
int32 (*getnc)(char *ptr, int32 n, void *f);
void (*close)(void *f);
}
DUMBFILE_SYSTEM;
typedef struct DUMBFILE DUMBFILE;
void DUMBEXPORT register_dumbfile_system(const DUMBFILE_SYSTEM *dfs);
DUMBFILE *DUMBEXPORT dumbfile_open(const char *filename);
DUMBFILE *DUMBEXPORT dumbfile_open_ex(void *file, const DUMBFILE_SYSTEM *dfs);
int32 DUMBEXPORT dumbfile_pos(DUMBFILE *f);
int DUMBEXPORT dumbfile_skip(DUMBFILE *f, int32 n);
int DUMBEXPORT dumbfile_getc(DUMBFILE *f);
int DUMBEXPORT dumbfile_igetw(DUMBFILE *f);
int DUMBEXPORT dumbfile_mgetw(DUMBFILE *f);
int32 DUMBEXPORT dumbfile_igetl(DUMBFILE *f);
int32 DUMBEXPORT dumbfile_mgetl(DUMBFILE *f);
uint32 DUMBEXPORT dumbfile_cgetul(DUMBFILE *f);
sint32 DUMBEXPORT dumbfile_cgetsl(DUMBFILE *f);
int32 DUMBEXPORT dumbfile_getnc(char *ptr, int32 n, DUMBFILE *f);
int DUMBEXPORT dumbfile_error(DUMBFILE *f);
int DUMBEXPORT dumbfile_close(DUMBFILE *f);
/* stdio File Input Module */
void DUMBEXPORT dumb_register_stdfiles(void);
DUMBFILE *DUMBEXPORT dumbfile_open_stdfile(FILE *p);
/* Memory File Input Module */
DUMBFILE *DUMBEXPORT dumbfile_open_memory(const char *data, int32 size);
/* DUH Management */
typedef struct DUH DUH;
#define DUH_SIGNATURE DUMB_ID('D','U','H','!')
void DUMBEXPORT unload_duh(DUH *duh);
DUH *DUMBEXPORT load_duh(const char *filename);
DUH *DUMBEXPORT read_duh(DUMBFILE *f);
int32 DUMBEXPORT duh_get_length(DUH *duh);
const char *DUMBEXPORT duh_get_tag(DUH *duh, const char *key);
/* Signal Rendering Functions */
typedef struct DUH_SIGRENDERER DUH_SIGRENDERER;
DUH_SIGRENDERER *DUMBEXPORT duh_start_sigrenderer(DUH *duh, int sig, int n_channels, int32 pos);
#ifdef DUMB_DECLARE_DEPRECATED
typedef void (*DUH_SIGRENDERER_CALLBACK)(void *data, sample_t **samples, int n_channels, int32 length);
/* This is deprecated, but is not marked as such because GCC tends to
* complain spuriously when the typedef is used later. See comments below.
*/
void duh_sigrenderer_set_callback(
DUH_SIGRENDERER *sigrenderer,
DUH_SIGRENDERER_CALLBACK callback, void *data
) DUMB_DEPRECATED;
/* The 'callback' argument's type has changed for const-correctness. See the
* DUH_SIGRENDERER_CALLBACK definition just above. Also note that the samples
* in the buffer are now 256 times as large; the normal range is -0x800000 to
* 0x7FFFFF. The function has been renamed partly because its functionality
* has changed slightly and partly so that its name is more meaningful. The
* new one is duh_sigrenderer_set_analyser_callback(), and the typedef for
* the function pointer has also changed, from DUH_SIGRENDERER_CALLBACK to
* DUH_SIGRENDERER_ANALYSER_CALLBACK. (If you wanted to use this callback to
* apply a DSP effect, don't worry; there is a better way of doing this. It
* is undocumented, so contact me and I shall try to help. Contact details
* are in readme.txt.)
*/
typedef void (*DUH_SIGRENDERER_ANALYSER_CALLBACK)(void *data, const sample_t *const *samples, int n_channels, int32 length);
/* This is deprecated, but is not marked as such because GCC tends to
* complain spuriously when the typedef is used later. See comments below.
*/
void duh_sigrenderer_set_analyser_callback(
DUH_SIGRENDERER *sigrenderer,
DUH_SIGRENDERER_ANALYSER_CALLBACK callback, void *data
) DUMB_DEPRECATED;
/* This is deprecated because the meaning of the 'samples' parameter in the
* callback needed to change. For stereo applications, the array used to be
* indexed with samples[channel][pos]. It is now indexed with
* samples[0][pos*2+channel]. Mono sample data are still indexed with
* samples[0][pos]. The array is still 2D because samples will probably only
* ever be interleaved in twos. In order to fix your code, adapt it to the
* new sample layout and then call
* duh_sigrenderer_set_sample_analyser_callback below instead of this
* function.
*/
#endif
typedef void (*DUH_SIGRENDERER_SAMPLE_ANALYSER_CALLBACK)(void *data, const sample_t *const *samples, int n_channels, int32 length);
void duh_sigrenderer_set_sample_analyser_callback(
DUH_SIGRENDERER *sigrenderer,
DUH_SIGRENDERER_SAMPLE_ANALYSER_CALLBACK callback, void *data
);
int DUMBEXPORT duh_sigrenderer_get_n_channels(DUH_SIGRENDERER *sigrenderer);
int32 DUMBEXPORT duh_sigrenderer_get_position(DUH_SIGRENDERER *sigrenderer);
void DUMBEXPORT duh_sigrenderer_set_sigparam(DUH_SIGRENDERER *sigrenderer, unsigned char id, int32 value);
#ifdef DUMB_DECLARE_DEPRECATED
int32 duh_sigrenderer_get_samples(
DUH_SIGRENDERER *sigrenderer,
float volume, float delta,
int32 size, sample_t **samples
) DUMB_DEPRECATED;
/* The sample format has changed, so if you were using this function,
* you should switch to duh_sigrenderer_generate_samples() and change
* how you interpret the samples array. See the comments for
* duh_sigrenderer_set_analyser_callback().
*/
#endif
int32 DUMBEXPORT duh_sigrenderer_generate_samples(
DUH_SIGRENDERER *sigrenderer,
double volume, double delta,
int32 size, sample_t **samples
);
void DUMBEXPORT duh_sigrenderer_get_current_sample(DUH_SIGRENDERER *sigrenderer, float volume, sample_t *samples);
void DUMBEXPORT duh_end_sigrenderer(DUH_SIGRENDERER *sigrenderer);
/* DUH Rendering Functions */
int32 DUMBEXPORT duh_render(
DUH_SIGRENDERER *sigrenderer,
int bits, int unsign,
float volume, float delta,
int32 size, void *sptr
);
#ifdef DUMB_DECLARE_DEPRECATED
int32 duh_render_signal(
DUH_SIGRENDERER *sigrenderer,
float volume, float delta,
int32 size, sample_t **samples
) DUMB_DEPRECATED;
/* Please use duh_sigrenderer_generate_samples(), and see the
* comments for the deprecated duh_sigrenderer_get_samples() too.
*/
typedef DUH_SIGRENDERER DUH_RENDERER DUMB_DEPRECATED;
/* Please use DUH_SIGRENDERER instead of DUH_RENDERER. */
DUH_SIGRENDERER *duh_start_renderer(DUH *duh, int n_channels, int32 pos) DUMB_DEPRECATED;
/* Please use duh_start_sigrenderer() instead. Pass 0 for 'sig'. */
int duh_renderer_get_n_channels(DUH_SIGRENDERER *dr) DUMB_DEPRECATED;
int32 duh_renderer_get_position(DUH_SIGRENDERER *dr) DUMB_DEPRECATED;
/* Please use the duh_sigrenderer_*() equivalents of these two functions. */
void duh_end_renderer(DUH_SIGRENDERER *dr) DUMB_DEPRECATED;
/* Please use duh_end_sigrenderer() instead. */
DUH_SIGRENDERER *duh_renderer_encapsulate_sigrenderer(DUH_SIGRENDERER *sigrenderer) DUMB_DEPRECATED;
DUH_SIGRENDERER *duh_renderer_get_sigrenderer(DUH_SIGRENDERER *dr) DUMB_DEPRECATED;
DUH_SIGRENDERER *duh_renderer_decompose_to_sigrenderer(DUH_SIGRENDERER *dr) DUMB_DEPRECATED;
/* These functions have become no-ops that just return the parameter.
* So, for instance, replace
* duh_renderer_encapsulate_sigrenderer(my_sigrenderer)
* with
* my_sigrenderer
*/
#endif
/* Impulse Tracker Support */
extern int dumb_it_max_to_mix;
typedef struct DUMB_IT_SIGDATA DUMB_IT_SIGDATA;
typedef struct DUMB_IT_SIGRENDERER DUMB_IT_SIGRENDERER;
DUMB_IT_SIGDATA *DUMBEXPORT duh_get_it_sigdata(DUH *duh);
DUH_SIGRENDERER *DUMBEXPORT duh_encapsulate_it_sigrenderer(DUMB_IT_SIGRENDERER *it_sigrenderer, int n_channels, int32 pos);
DUMB_IT_SIGRENDERER *DUMBEXPORT duh_get_it_sigrenderer(DUH_SIGRENDERER *sigrenderer);
int DUMBEXPORT dumb_it_trim_silent_patterns(DUH * duh);
typedef int (*dumb_scan_callback)(void *, int, int32);
int DUMBEXPORT dumb_it_scan_for_playable_orders(DUMB_IT_SIGDATA *sigdata, dumb_scan_callback callback, void * callback_data);
DUH_SIGRENDERER *DUMBEXPORT dumb_it_start_at_order(DUH *duh, int n_channels, int startorder);
void DUMBEXPORT dumb_it_set_resampling_quality(DUMB_IT_SIGRENDERER * sigrenderer, int quality);
void DUMBEXPORT dumb_it_set_ramp_style(DUMB_IT_SIGRENDERER * sigrenderer, int ramp_style);
void DUMBEXPORT dumb_it_set_loop_callback(DUMB_IT_SIGRENDERER *sigrenderer, int (DUMBCALLBACK *callback)(void *data), void *data);
void DUMBEXPORT dumb_it_set_xm_speed_zero_callback(DUMB_IT_SIGRENDERER *sigrenderer, int (DUMBCALLBACK *callback)(void *data), void *data);
void DUMBEXPORT dumb_it_set_midi_callback(DUMB_IT_SIGRENDERER *sigrenderer, int (DUMBCALLBACK *callback)(void *data, int channel, unsigned char midi_byte), void *data);
void DUMBEXPORT dumb_it_set_global_volume_zero_callback(DUMB_IT_SIGRENDERER *sigrenderer, int (DUMBCALLBACK *callback)(void *data), void *data);
int DUMBCALLBACK dumb_it_callback_terminate(void *data);
int DUMBCALLBACK dumb_it_callback_midi_block(void *data, int channel, unsigned char midi_byte);
DUH *DUMBEXPORT dumb_load_it(const char *filename);
DUH *DUMBEXPORT dumb_load_xm(const char *filename);
DUH *DUMBEXPORT dumb_load_s3m(const char *filename);
DUH *DUMBEXPORT dumb_load_stm(const char *filename);
DUH *DUMBEXPORT dumb_load_mod(const char *filename, int restrict);
DUH *DUMBEXPORT dumb_load_ptm(const char *filename);
DUH *DUMBEXPORT dumb_load_669(const char *filename);
DUH *DUMBEXPORT dumb_load_psm(const char *filename, int subsong);
DUH *DUMBEXPORT dumb_load_old_psm(const char * filename);
DUH *DUMBEXPORT dumb_load_mtm(const char *filename);
DUH *DUMBEXPORT dumb_load_riff(const char *filename);
DUH *DUMBEXPORT dumb_load_asy(const char *filename);
DUH *DUMBEXPORT dumb_read_it(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_xm(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_s3m(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_stm(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_mod(DUMBFILE *f, int restrict);
DUH *DUMBEXPORT dumb_read_ptm(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_669(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_psm(DUMBFILE *f, int subsong);
DUH *DUMBEXPORT dumb_read_old_psm(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_mtm(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_riff(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_asy(DUMBFILE *f);
DUH *DUMBEXPORT dumb_load_it_quick(const char *filename);
DUH *DUMBEXPORT dumb_load_xm_quick(const char *filename);
DUH *DUMBEXPORT dumb_load_s3m_quick(const char *filename);
DUH *DUMBEXPORT dumb_load_stm_quick(const char *filename);
DUH *DUMBEXPORT dumb_load_mod_quick(const char *filename, int restrict);
DUH *DUMBEXPORT dumb_load_ptm_quick(const char *filename);
DUH *DUMBEXPORT dumb_load_669_quick(const char *filename);
DUH *DUMBEXPORT dumb_load_psm_quick(const char *filename, int subsong);
DUH *DUMBEXPORT dumb_load_old_psm_quick(const char * filename);
DUH *DUMBEXPORT dumb_load_mtm_quick(const char *filename);
DUH *DUMBEXPORT dumb_load_riff_quick(const char *filename);
DUH *DUMBEXPORT dumb_load_asy_quick(const char *filename);
DUH *DUMBEXPORT dumb_read_it_quick(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_xm_quick(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_s3m_quick(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_stm_quick(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_mod_quick(DUMBFILE *f, int restrict);
DUH *DUMBEXPORT dumb_read_ptm_quick(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_669_quick(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_psm_quick(DUMBFILE *f, int subsong);
DUH *DUMBEXPORT dumb_read_old_psm_quick(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_mtm_quick(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_riff_quick(DUMBFILE *f);
DUH *DUMBEXPORT dumb_read_asy_quick(DUMBFILE *f);
int32 DUMBEXPORT dumb_it_build_checkpoints(DUMB_IT_SIGDATA *sigdata, int startorder);
void DUMBEXPORT dumb_it_do_initial_runthrough(DUH *duh);
int DUMBEXPORT dumb_get_psm_subsong_count(DUMBFILE *f);
const unsigned char *DUMBEXPORT dumb_it_sd_get_song_message(DUMB_IT_SIGDATA *sd);
int DUMBEXPORT dumb_it_sd_get_n_orders(DUMB_IT_SIGDATA *sd);
int DUMBEXPORT dumb_it_sd_get_n_samples(DUMB_IT_SIGDATA *sd);
int DUMBEXPORT dumb_it_sd_get_n_instruments(DUMB_IT_SIGDATA *sd);
const unsigned char *DUMBEXPORT dumb_it_sd_get_sample_name(DUMB_IT_SIGDATA *sd, int i);
const unsigned char *DUMBEXPORT dumb_it_sd_get_sample_filename(DUMB_IT_SIGDATA *sd, int i);
const unsigned char *DUMBEXPORT dumb_it_sd_get_instrument_name(DUMB_IT_SIGDATA *sd, int i);
const unsigned char *DUMBEXPORT dumb_it_sd_get_instrument_filename(DUMB_IT_SIGDATA *sd, int i);
int DUMBEXPORT dumb_it_sd_get_initial_global_volume(DUMB_IT_SIGDATA *sd);
void DUMBEXPORT dumb_it_sd_set_initial_global_volume(DUMB_IT_SIGDATA *sd, int gv);
int DUMBEXPORT dumb_it_sd_get_mixing_volume(DUMB_IT_SIGDATA *sd);
void DUMBEXPORT dumb_it_sd_set_mixing_volume(DUMB_IT_SIGDATA *sd, int mv);
int DUMBEXPORT dumb_it_sd_get_initial_speed(DUMB_IT_SIGDATA *sd);
void DUMBEXPORT dumb_it_sd_set_initial_speed(DUMB_IT_SIGDATA *sd, int speed);
int DUMBEXPORT dumb_it_sd_get_initial_tempo(DUMB_IT_SIGDATA *sd);
void DUMBEXPORT dumb_it_sd_set_initial_tempo(DUMB_IT_SIGDATA *sd, int tempo);
int DUMBEXPORT dumb_it_sd_get_initial_channel_volume(DUMB_IT_SIGDATA *sd, int channel);
void DUMBEXPORT dumb_it_sd_set_initial_channel_volume(DUMB_IT_SIGDATA *sd, int channel, int volume);
int DUMBEXPORT dumb_it_sr_get_current_order(DUMB_IT_SIGRENDERER *sr);
int DUMBEXPORT dumb_it_sr_get_current_row(DUMB_IT_SIGRENDERER *sr);
int DUMBEXPORT dumb_it_sr_get_global_volume(DUMB_IT_SIGRENDERER *sr);
void DUMBEXPORT dumb_it_sr_set_global_volume(DUMB_IT_SIGRENDERER *sr, int gv);
int DUMBEXPORT dumb_it_sr_get_tempo(DUMB_IT_SIGRENDERER *sr);
void DUMBEXPORT dumb_it_sr_set_tempo(DUMB_IT_SIGRENDERER *sr, int tempo);
int DUMBEXPORT dumb_it_sr_get_speed(DUMB_IT_SIGRENDERER *sr);
void DUMBEXPORT dumb_it_sr_set_speed(DUMB_IT_SIGRENDERER *sr, int speed);
#define DUMB_IT_N_CHANNELS 64
#define DUMB_IT_N_NNA_CHANNELS 192
#define DUMB_IT_TOTAL_CHANNELS (DUMB_IT_N_CHANNELS + DUMB_IT_N_NNA_CHANNELS)
/* Channels passed to any of these functions are 0-based */
int DUMBEXPORT dumb_it_sr_get_channel_volume(DUMB_IT_SIGRENDERER *sr, int channel);
void DUMBEXPORT dumb_it_sr_set_channel_volume(DUMB_IT_SIGRENDERER *sr, int channel, int volume);
int DUMBEXPORT dumb_it_sr_get_channel_muted(DUMB_IT_SIGRENDERER *sr, int channel);
void DUMBEXPORT dumb_it_sr_set_channel_muted(DUMB_IT_SIGRENDERER *sr, int channel, int muted);
typedef struct DUMB_IT_CHANNEL_STATE DUMB_IT_CHANNEL_STATE;
struct DUMB_IT_CHANNEL_STATE
{
int channel; /* 0-based; meaningful for NNA channels */
int sample; /* 1-based; 0 if nothing playing, then other fields undef */
int freq; /* in Hz */
float volume; /* 1.0 maximum; affected by ALL factors, inc. mixing vol */
unsigned char pan; /* 0-64, 100 for surround */
signed char subpan; /* use (pan + subpan/256.0f) or ((pan<<8)+subpan) */
unsigned char filter_cutoff; /* 0-127 cutoff=127 AND resonance=0 */
unsigned char filter_subcutoff; /* 0-255 -> no filters (subcutoff */
unsigned char filter_resonance; /* 0-127 always 0 in this case) */
/* subcutoff only changes from zero if filter envelopes are in use. The
* calculation (filter_cutoff + filter_subcutoff/256.0f) gives a more
* accurate filter cutoff measurement as a float. It would often be more
* useful to use a scaled int such as ((cutoff<<8) + subcutoff).
*/
};
/* Values of 64 or more will access NNA channels here. */
void DUMBEXPORT dumb_it_sr_get_channel_state(DUMB_IT_SIGRENDERER *sr, int channel, DUMB_IT_CHANNEL_STATE *state);
/* Signal Design Helper Values */
/* Use pow(DUMB_SEMITONE_BASE, n) to get the 'delta' value to transpose up by
* n semitones. To transpose down, use negative n.
*/
#define DUMB_SEMITONE_BASE 1.059463094359295309843105314939748495817
/* Use pow(DUMB_QUARTERTONE_BASE, n) to get the 'delta' value to transpose up
* by n quartertones. To transpose down, use negative n.
*/
#define DUMB_QUARTERTONE_BASE 1.029302236643492074463779317738953977823
/* Use pow(DUMB_PITCH_BASE, n) to get the 'delta' value to transpose up by n
* units. In this case, 256 units represent one semitone; 3072 units
* represent one octave. These units are used by the sequence signal (SEQU).
*/
#define DUMB_PITCH_BASE 1.000225659305069791926712241547647863626
/* Signal Design Function Types */
typedef void sigdata_t;
typedef void sigrenderer_t;
typedef sigdata_t *(*DUH_LOAD_SIGDATA)(DUH *duh, DUMBFILE *file);
typedef sigrenderer_t *(*DUH_START_SIGRENDERER)(
DUH *duh,
sigdata_t *sigdata,
int n_channels,
int32 pos
);
typedef void (*DUH_SIGRENDERER_SET_SIGPARAM)(
sigrenderer_t *sigrenderer,
unsigned char id, int32 value
);
typedef int32 (*DUH_SIGRENDERER_GENERATE_SAMPLES)(
sigrenderer_t *sigrenderer,
double volume, double delta,
int32 size, sample_t **samples
);
typedef void (*DUH_SIGRENDERER_GET_CURRENT_SAMPLE)(
sigrenderer_t *sigrenderer,
double volume,
sample_t *samples
);
typedef void (*DUH_END_SIGRENDERER)(sigrenderer_t *sigrenderer);
typedef void (*DUH_UNLOAD_SIGDATA)(sigdata_t *sigdata);
/* Signal Design Function Registration */
typedef struct DUH_SIGTYPE_DESC
{
int32 type;
DUH_LOAD_SIGDATA load_sigdata;
DUH_START_SIGRENDERER start_sigrenderer;
DUH_SIGRENDERER_SET_SIGPARAM sigrenderer_set_sigparam;
DUH_SIGRENDERER_GENERATE_SAMPLES sigrenderer_generate_samples;
DUH_SIGRENDERER_GET_CURRENT_SAMPLE sigrenderer_get_current_sample;
DUH_END_SIGRENDERER end_sigrenderer;
DUH_UNLOAD_SIGDATA unload_sigdata;
}
DUH_SIGTYPE_DESC;
void DUMBEXPORT dumb_register_sigtype(DUH_SIGTYPE_DESC *desc);
// Decide where to put these functions; new heading?
sigdata_t *DUMBEXPORT duh_get_raw_sigdata(DUH *duh, int sig, int32 type);
DUH_SIGRENDERER *DUMBEXPORT duh_encapsulate_raw_sigrenderer(sigrenderer_t *vsigrenderer, DUH_SIGTYPE_DESC *desc, int n_channels, int32 pos);
sigrenderer_t *DUMBEXPORT duh_get_raw_sigrenderer(DUH_SIGRENDERER *sigrenderer, int32 type);
/* Standard Signal Types */
//void dumb_register_sigtype_sample(void);
/* Sample Buffer Allocation Helpers */
#ifdef DUMB_DECLARE_DEPRECATED
sample_t **create_sample_buffer(int n_channels, int32 length) DUMB_DEPRECATED;
/* DUMB has been changed to interleave stereo samples. Use
* allocate_sample_buffer() instead, and see the comments for
* duh_sigrenderer_set_analyser_callback().
*/
#endif
sample_t **DUMBEXPORT allocate_sample_buffer(int n_channels, int32 length);
void DUMBEXPORT destroy_sample_buffer(sample_t **samples);
/* Silencing Helper */
void DUMBEXPORT dumb_silence(sample_t *samples, int32 length);
/* Click Removal Helpers */
typedef struct DUMB_CLICK_REMOVER DUMB_CLICK_REMOVER;
DUMB_CLICK_REMOVER *DUMBEXPORT dumb_create_click_remover(void);
void DUMBEXPORT dumb_record_click(DUMB_CLICK_REMOVER *cr, int32 pos, sample_t step);
void DUMBEXPORT dumb_remove_clicks(DUMB_CLICK_REMOVER *cr, sample_t *samples, int32 length, int step, double halflife);
sample_t DUMBEXPORT dumb_click_remover_get_offset(DUMB_CLICK_REMOVER *cr);
void DUMBEXPORT dumb_destroy_click_remover(DUMB_CLICK_REMOVER *cr);
DUMB_CLICK_REMOVER **DUMBEXPORT dumb_create_click_remover_array(int n);
void DUMBEXPORT dumb_record_click_array(int n, DUMB_CLICK_REMOVER **cr, int32 pos, sample_t *step);
void DUMBEXPORT dumb_record_click_negative_array(int n, DUMB_CLICK_REMOVER **cr, int32 pos, sample_t *step);
void DUMBEXPORT dumb_remove_clicks_array(int n, DUMB_CLICK_REMOVER **cr, sample_t **samples, int32 length, double halflife);
void DUMBEXPORT dumb_click_remover_get_offset_array(int n, DUMB_CLICK_REMOVER **cr, sample_t *offset);
void DUMBEXPORT dumb_destroy_click_remover_array(int n, DUMB_CLICK_REMOVER **cr);
/* Resampling Helpers */
#define DUMB_RQ_ALIASING 0
#define DUMB_RQ_LINEAR 1
#define DUMB_RQ_CUBIC 2
#define DUMB_RQ_N_LEVELS 3
extern int dumb_resampling_quality;
typedef struct DUMB_RESAMPLER DUMB_RESAMPLER;
typedef struct DUMB_VOLUME_RAMP_INFO DUMB_VOLUME_RAMP_INFO;
typedef void (*DUMB_RESAMPLE_PICKUP)(DUMB_RESAMPLER *resampler, void *data);
struct DUMB_RESAMPLER
{
void *src;
int32 pos;
int subpos;
int32 start, end;
int dir;
DUMB_RESAMPLE_PICKUP pickup;
void *pickup_data;
int quality;
/* Everything below this point is internal: do not use. */
union {
sample_t x24[3*2];
short x16[3*2];
signed char x8[3*2];
} x;
int overshot;
};
struct DUMB_VOLUME_RAMP_INFO
{
float volume;
float delta;
float target;
float mix;
};
void dumb_reset_resampler(DUMB_RESAMPLER *resampler, sample_t *src, int src_channels, int32 pos, int32 start, int32 end, int quality);
DUMB_RESAMPLER *dumb_start_resampler(sample_t *src, int src_channels, int32 pos, int32 start, int32 end, int quality);
int32 dumb_resample_1_1(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume, double delta);
int32 dumb_resample_1_2(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
int32 dumb_resample_2_1(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
int32 dumb_resample_2_2(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
void dumb_resample_get_current_sample_1_1(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume, sample_t *dst);
void dumb_resample_get_current_sample_1_2(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_resample_get_current_sample_2_1(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_resample_get_current_sample_2_2(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_end_resampler(DUMB_RESAMPLER *resampler);
void dumb_reset_resampler_16(DUMB_RESAMPLER *resampler, short *src, int src_channels, int32 pos, int32 start, int32 end, int quality);
DUMB_RESAMPLER *dumb_start_resampler_16(short *src, int src_channels, int32 pos, int32 start, int32 end, int quality);
int32 dumb_resample_16_1_1(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume, double delta);
int32 dumb_resample_16_1_2(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
int32 dumb_resample_16_2_1(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
int32 dumb_resample_16_2_2(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
void dumb_resample_get_current_sample_16_1_1(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume, sample_t *dst);
void dumb_resample_get_current_sample_16_1_2(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_resample_get_current_sample_16_2_1(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_resample_get_current_sample_16_2_2(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_end_resampler_16(DUMB_RESAMPLER *resampler);
void dumb_reset_resampler_8(DUMB_RESAMPLER *resampler, signed char *src, int src_channels, int32 pos, int32 start, int32 end, int quality);
DUMB_RESAMPLER *dumb_start_resampler_8(signed char *src, int src_channels, int32 pos, int32 start, int32 end, int quality);
int32 dumb_resample_8_1_1(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume, double delta);
int32 dumb_resample_8_1_2(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
int32 dumb_resample_8_2_1(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
int32 dumb_resample_8_2_2(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
void dumb_resample_get_current_sample_8_1_1(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume, sample_t *dst);
void dumb_resample_get_current_sample_8_1_2(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_resample_get_current_sample_8_2_1(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_resample_get_current_sample_8_2_2(DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_end_resampler_8(DUMB_RESAMPLER *resampler);
void dumb_reset_resampler_n(int n, DUMB_RESAMPLER *resampler, void *src, int src_channels, int32 pos, int32 start, int32 end, int quality);
DUMB_RESAMPLER *dumb_start_resampler_n(int n, void *src, int src_channels, int32 pos, int32 start, int32 end, int quality);
int32 dumb_resample_n_1_1(int n, DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume, double delta);
int32 dumb_resample_n_1_2(int n, DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
int32 dumb_resample_n_2_1(int n, DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
int32 dumb_resample_n_2_2(int n, DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta);
void dumb_resample_get_current_sample_n_1_1(int n, DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume, sample_t *dst);
void dumb_resample_get_current_sample_n_1_2(int n, DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_resample_get_current_sample_n_2_1(int n, DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_resample_get_current_sample_n_2_2(int n, DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst);
void dumb_end_resampler_n(int n, DUMB_RESAMPLER *resampler);
/* DUH Construction */
DUH *make_duh(
int32 length,
int n_tags,
const char *const tag[][2],
int n_signals,
DUH_SIGTYPE_DESC *desc[],
sigdata_t *sigdata[]
);
void DUMBEXPORT duh_set_length(DUH *duh, int32 length);
#ifdef __cplusplus
}
#endif
#endif /* DUMB_H */

View file

@ -0,0 +1,20 @@
#ifndef _B_ARRAY_H_
#define _B_ARRAY_H_
#include <stdlib.h>
void * bit_array_create(size_t size);
void bit_array_destroy(void * array);
void * bit_array_dup(void * array);
void bit_array_reset(void * array);
void bit_array_set(void * array, size_t bit);
int bit_array_test(void * array, size_t bit);
int bit_array_test_range(void * array, size_t bit, size_t count);
void bit_array_clear(void * array, size_t bit);
void bit_array_merge(void * array, void * source, size_t offset);
void bit_array_mask(void * array, void * source, size_t offset);
#endif

View file

@ -0,0 +1,61 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* internal/dumb.h - DUMB's internal declarations. / / \ \
* | < / \_
* This header file provides access to the | \/ /\ /
* internal structure of DUMB, and is liable \_ / > /
* to change, mutate or cease to exist at any | \ / /
* moment. Include it at your own peril. | ' /
* \__/
* ...
*
* Seriously. You don't need access to anything in this file. All right, you
* probably do actually. But if you use it, you will be relying on a specific
* version of DUMB, so please check DUMB_VERSION defined in dumb.h. Please
* contact the authors so that we can provide a public API for what you need.
*/
#ifndef INTERNAL_DUMB_H
#define INTERNAL_DUMB_H
typedef struct DUH_SIGTYPE_DESC_LINK
{
struct DUH_SIGTYPE_DESC_LINK *next;
DUH_SIGTYPE_DESC *desc;
}
DUH_SIGTYPE_DESC_LINK;
typedef struct DUH_SIGNAL
{
sigdata_t *sigdata;
DUH_SIGTYPE_DESC *desc;
}
DUH_SIGNAL;
struct DUH
{
int32 length;
int n_tags;
char *(*tag)[2];
int n_signals;
DUH_SIGNAL **signal;
};
DUH_SIGTYPE_DESC *_dumb_get_sigtype_desc(int32 type);
#endif /* INTERNAL_DUMB_H */

883
dumb/include/internal/it.h Normal file
View file

@ -0,0 +1,883 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* internal/it.h - Internal stuff for IT playback / / \ \
* and MOD/XM/S3M conversion. | < / \_
* | \/ /\ /
* This header file provides access to the \_ / > /
* internal structure of DUMB, and is liable | \ / /
* to change, mutate or cease to exist at any | ' /
* moment. Include it at your own peril. \__/
*
* ...
*
* Seriously. You don't need access to anything in this file. All right, you
* probably do actually. But if you use it, you will be relying on a specific
* version of DUMB, so please check DUMB_VERSION defined in dumb.h. Please
* contact the authors so that we can provide a public API for what you need.
*/
#ifndef INTERNAL_IT_H
#define INTERNAL_IT_H
#define BIT_ARRAY_BULLSHIT
#include <stddef.h>
#include "barray.h"
/** TO DO: THINK ABOUT THE FOLLOWING:
sigdata->flags & IT_COMPATIBLE_GXX
Bit 5: On = Link Effect G's memory with Effect E/F. Also
Gxx with an instrument present will cause the
envelopes to be retriggered. If you change a
sample on a row with Gxx, it'll adjust the
frequency of the current note according to:
NewFrequency = OldFrequency * NewC5 / OldC5;
*/
/* These #defines are TEMPORARY. They are used to write alternative code to
* handle ambiguities in the format specification. The correct code in each
* case will be determined most likely by experimentation.
*/
#define STEREO_SAMPLES_COUNT_AS_TWO
#define INVALID_ORDERS_END_SONG
#define INVALID_NOTES_CAUSE_NOTE_CUT
#define SUSTAIN_LOOP_OVERRIDES_NORMAL_LOOP
#define VOLUME_OUT_OF_RANGE_SETS_MAXIMUM
#define SIGTYPE_IT DUMB_ID('I', 'T', ' ', ' ')
#define IT_SIGNATURE DUMB_ID('I', 'M', 'P', 'M')
#define IT_INSTRUMENT_SIGNATURE DUMB_ID('I', 'M', 'P', 'I')
#define IT_SAMPLE_SIGNATURE DUMB_ID('I', 'M', 'P', 'S')
// olivier sux
#define IT_MPTX_SIGNATURE DUMB_ID('X', 'T', 'P', 'M')
#define IT_INSM_SIGNATURE DUMB_ID('M', 'S', 'N', 'I')
/* 1 minute per 4 rows, each row 6 ticks; this is divided by the tempo to get
* the interval between ticks.
*/
#define TICK_TIME_DIVIDEND ((65536 * 60) / (4 * 6))
/* I'm not going to try to explain this, because I didn't derive it very
* formally ;)
*/
/* #define AMIGA_DIVISOR ((float)(4.0 * 14317056.0)) */
/* I believe the following one to be more accurate. */
//#define AMIGA_DIVISOR ((float)(8.0 * 7159090.5))
#define AMIGA_CLOCK 3546895
#define AMIGA_DIVISOR ((float)(16.0 * AMIGA_CLOCK))
typedef struct IT_MIDI IT_MIDI;
typedef struct IT_FILTER_STATE IT_FILTER_STATE;
typedef struct IT_ENVELOPE IT_ENVELOPE;
typedef struct IT_INSTRUMENT IT_INSTRUMENT;
typedef struct IT_SAMPLE IT_SAMPLE;
typedef struct IT_ENTRY IT_ENTRY;
typedef struct IT_PATTERN IT_PATTERN;
typedef struct IT_PLAYING_ENVELOPE IT_PLAYING_ENVELOPE;
typedef struct IT_PLAYING IT_PLAYING;
typedef struct IT_CHANNEL IT_CHANNEL;
typedef struct IT_CHECKPOINT IT_CHECKPOINT;
typedef struct IT_CALLBACKS IT_CALLBACKS;
struct IT_MIDI
{
unsigned char SFmacro[16][16]; // read these from 0x120
unsigned char SFmacrolen[16];
unsigned short SFmacroz[16]; /* Bitfield; bit 0 set = z in first position */
unsigned char Zmacro[128][16]; // read these from 0x320
unsigned char Zmacrolen[128];
};
struct IT_FILTER_STATE
{
sample_t currsample, prevsample;
};
#define IT_ENVELOPE_ON 1
#define IT_ENVELOPE_LOOP_ON 2
#define IT_ENVELOPE_SUSTAIN_LOOP 4
#define IT_ENVELOPE_CARRY 8
#define IT_ENVELOPE_PITCH_IS_FILTER 128
struct IT_ENVELOPE
{
unsigned char flags;
unsigned char n_nodes;
unsigned char loop_start;
unsigned char loop_end;
unsigned char sus_loop_start;
unsigned char sus_loop_end;
signed char node_y[25];
unsigned short node_t[25];
};
#define NNA_NOTE_CUT 0
#define NNA_NOTE_CONTINUE 1
#define NNA_NOTE_OFF 2
#define NNA_NOTE_FADE 3
#define DCT_OFF 0
#define DCT_NOTE 1
#define DCT_SAMPLE 2
#define DCT_INSTRUMENT 3
#define DCA_NOTE_CUT 0
#define DCA_NOTE_OFF 1
#define DCA_NOTE_FADE 2
struct IT_INSTRUMENT
{
unsigned char name[27];
unsigned char filename[14];
int fadeout;
IT_ENVELOPE volume_envelope;
IT_ENVELOPE pan_envelope;
IT_ENVELOPE pitch_envelope;
unsigned char new_note_action;
unsigned char dup_check_type;
unsigned char dup_check_action;
signed char pp_separation;
unsigned char pp_centre;
unsigned char global_volume;
unsigned char default_pan;
unsigned char random_volume;
unsigned char random_pan;
unsigned char filter_cutoff;
unsigned char filter_resonance;
unsigned char map_note[120];
unsigned short map_sample[120];
//int output;
};
#define IT_SAMPLE_EXISTS 1
#define IT_SAMPLE_16BIT 2
#define IT_SAMPLE_STEREO 4
#define IT_SAMPLE_LOOP 16
#define IT_SAMPLE_SUS_LOOP 32
#define IT_SAMPLE_PINGPONG_LOOP 64
#define IT_SAMPLE_PINGPONG_SUS_LOOP 128
#define IT_VIBRATO_SINE 0
#define IT_VIBRATO_SAWTOOTH 1
#define IT_VIBRATO_SQUARE 2
#define IT_VIBRATO_RANDOM 3
#define IT_VIBRATO_XM_SQUARE 4
#define IT_VIBRATO_RAMP_DOWN 5
#define IT_VIBRATO_RAMP_UP 6
struct IT_SAMPLE
{
unsigned char name[35];
unsigned char filename[14];
unsigned char flags;
unsigned char global_volume;
unsigned char default_volume;
unsigned char default_pan;
/* default_pan:
* 0-255 for XM
* ignored for MOD
* otherwise, 0-64, and add 128 to enable
*/
int32 length;
int32 loop_start;
int32 loop_end;
int32 C5_speed;
int32 sus_loop_start;
int32 sus_loop_end;
unsigned char vibrato_speed;
unsigned char vibrato_depth;
unsigned char vibrato_rate;
unsigned char vibrato_waveform;
signed short finetune;
void *data;
int max_resampling_quality;
};
#define IT_ENTRY_NOTE 1
#define IT_ENTRY_INSTRUMENT 2
#define IT_ENTRY_VOLPAN 4
#define IT_ENTRY_EFFECT 8
#define IT_SET_END_ROW(entry) ((entry)->channel = 255)
#define IT_IS_END_ROW(entry) ((entry)->channel >= DUMB_IT_N_CHANNELS)
#define IT_NOTE_OFF 255
#define IT_NOTE_CUT 254
#define IT_ENVELOPE_SHIFT 8
#define IT_SURROUND 100
#define IT_IS_SURROUND(pan) ((pan) > 64)
#define IT_IS_SURROUND_SHIFTED(pan) ((pan) > 64 << IT_ENVELOPE_SHIFT)
#define IT_SET_SPEED 1
#define IT_JUMP_TO_ORDER 2
#define IT_BREAK_TO_ROW 3
#define IT_VOLUME_SLIDE 4
#define IT_PORTAMENTO_DOWN 5
#define IT_PORTAMENTO_UP 6
#define IT_TONE_PORTAMENTO 7
#define IT_VIBRATO 8
#define IT_TREMOR 9
#define IT_ARPEGGIO 10
#define IT_VOLSLIDE_VIBRATO 11
#define IT_VOLSLIDE_TONEPORTA 12
#define IT_SET_CHANNEL_VOLUME 13
#define IT_CHANNEL_VOLUME_SLIDE 14
#define IT_SET_SAMPLE_OFFSET 15
#define IT_PANNING_SLIDE 16
#define IT_RETRIGGER_NOTE 17
#define IT_TREMOLO 18
#define IT_S 19
#define IT_SET_SONG_TEMPO 20
#define IT_FINE_VIBRATO 21
#define IT_SET_GLOBAL_VOLUME 22
#define IT_GLOBAL_VOLUME_SLIDE 23
#define IT_SET_PANNING 24
#define IT_PANBRELLO 25
#define IT_MIDI_MACRO 26 //see MIDI.TXT
/* Some effects needed for XM compatibility */
#define IT_XM_PORTAMENTO_DOWN 27
#define IT_XM_PORTAMENTO_UP 28
#define IT_XM_FINE_VOLSLIDE_DOWN 29
#define IT_XM_FINE_VOLSLIDE_UP 30
#define IT_XM_RETRIGGER_NOTE 31
#define IT_XM_KEY_OFF 32
#define IT_XM_SET_ENVELOPE_POSITION 33
/* More effects needed for PTM compatibility */
#define IT_PTM_NOTE_SLIDE_DOWN 34
#define IT_PTM_NOTE_SLIDE_UP 35
#define IT_PTM_NOTE_SLIDE_DOWN_RETRIG 36
#define IT_PTM_NOTE_SLIDE_UP_RETRIG 37
#define IT_N_EFFECTS 38
/* These represent the top nibble of the command value. */
#define IT_S_SET_FILTER 0 /* Greyed out in IT... */
#define IT_S_SET_GLISSANDO_CONTROL 1 /* Greyed out in IT... */
#define IT_S_FINETUNE 2 /* Greyed out in IT... */
#define IT_S_SET_VIBRATO_WAVEFORM 3
#define IT_S_SET_TREMOLO_WAVEFORM 4
#define IT_S_SET_PANBRELLO_WAVEFORM 5
#define IT_S_FINE_PATTERN_DELAY 6
#define IT_S7 7
#define IT_S_SET_PAN 8
#define IT_S_SET_SURROUND_SOUND 9
#define IT_S_SET_HIGH_OFFSET 10
#define IT_S_PATTERN_LOOP 11
#define IT_S_DELAYED_NOTE_CUT 12
#define IT_S_NOTE_DELAY 13
#define IT_S_PATTERN_DELAY 14
#define IT_S_SET_MIDI_MACRO 15
/*
S0x Set filter
S1x Set glissando control
S2x Set finetune
S3x Set vibrato waveform to type x
S4x Set tremelo waveform to type x
S5x Set panbrello waveform to type x
Waveforms for commands S3x, S4x and S5x:
0: Sine wave
1: Ramp down
2: Square wave
3: Random wave
S6x Pattern delay for x ticks
S70 Past note cut
S71 Past note off
S72 Past note fade
S73 Set NNA to note cut
S74 Set NNA to continue
S75 Set NNA to note off
S76 Set NNA to note fade
S77 Turn off volume envelope
S78 Turn on volume envelope
S79 Turn off panning envelope
S7A Turn on panning envelope
S7B Turn off pitch envelope
S7C Turn on pitch envelope
S8x Set panning position
S91 Set surround sound
SAy Set high value of sample offset yxx00h
SB0 Set loopback point
SBx Loop x times to loopback point
SCx Note cut after x ticks
SDx Note delay for x ticks
SEx Pattern delay for x rows
SFx Set parameterised MIDI Macro
*/
struct IT_ENTRY
{
unsigned char channel; /* End of row if channel >= DUMB_IT_N_CHANNELS */
unsigned char mask;
unsigned char note;
unsigned char instrument;
unsigned char volpan;
unsigned char effect;
unsigned char effectvalue;
};
struct IT_PATTERN
{
int n_rows;
int n_entries;
IT_ENTRY *entry;
};
#define IT_STEREO 1
#define IT_USE_INSTRUMENTS 4
#define IT_LINEAR_SLIDES 8 /* If not set, use Amiga slides */
#define IT_OLD_EFFECTS 16
#define IT_COMPATIBLE_GXX 32
/* Make sure IT_WAS_AN_XM and IT_WAS_A_MOD aren't set accidentally */
#define IT_REAL_FLAGS 63
#define IT_WAS_AN_XM 64 /* Set for both XMs and MODs */
#define IT_WAS_A_MOD 128
#define IT_WAS_AN_S3M 256
#define IT_WAS_A_PTM 512
#define IT_WAS_A_669 1024
#define IT_ORDER_END 255
#define IT_ORDER_SKIP 254
struct DUMB_IT_SIGDATA
{
unsigned char name[65];
unsigned char *song_message;
int n_orders;
int n_instruments;
int n_samples;
int n_patterns;
int n_pchannels;
int flags;
int global_volume;
int mixing_volume;
int speed;
int tempo;
int pan_separation;
unsigned char channel_pan[DUMB_IT_N_CHANNELS];
unsigned char channel_volume[DUMB_IT_N_CHANNELS];
unsigned char *order;
unsigned char restart_position; /* for XM compatiblity */
IT_INSTRUMENT *instrument;
IT_SAMPLE *sample;
IT_PATTERN *pattern;
IT_MIDI *midi;
IT_CHECKPOINT *checkpoint;
};
struct IT_PLAYING_ENVELOPE
{
int next_node;
int tick;
int value;
};
#define IT_PLAYING_BACKGROUND 1
#define IT_PLAYING_SUSTAINOFF 2
#define IT_PLAYING_FADING 4
#define IT_PLAYING_DEAD 8
struct IT_PLAYING
{
int flags;
int resampling_quality;
IT_CHANNEL *channel;
IT_SAMPLE *sample;
IT_INSTRUMENT *instrument;
IT_INSTRUMENT *env_instrument;
unsigned short sampnum;
unsigned char instnum;
unsigned char declick_stage;
float declick_volume;
float float_volume[2];
float ramp_volume[2];
float ramp_delta[2];
unsigned char channel_volume;
unsigned char volume;
unsigned short pan;
signed char volume_offset, panning_offset;
unsigned char note;
unsigned char enabled_envelopes;
unsigned char filter_cutoff;
unsigned char filter_resonance;
unsigned short true_filter_cutoff; /* These incorporate the filter envelope, and will not */
unsigned char true_filter_resonance; /* be changed if they would be set to 127<<8 and 0. */
unsigned char vibrato_speed;
unsigned char vibrato_depth;
unsigned char vibrato_n; /* May be specified twice: volpan & effect. */
unsigned char vibrato_time;
unsigned char vibrato_waveform;
unsigned char tremolo_speed;
unsigned char tremolo_depth;
unsigned char tremolo_time;
unsigned char tremolo_waveform;
unsigned char panbrello_speed;
unsigned char panbrello_depth;
unsigned char panbrello_time;
unsigned char panbrello_waveform;
signed char panbrello_random;
unsigned char sample_vibrato_time;
unsigned char sample_vibrato_waveform;
int sample_vibrato_depth; /* Starts at rate?0:depth, increases by rate */
int slide;
float delta;
int finetune;
IT_PLAYING_ENVELOPE volume_envelope;
IT_PLAYING_ENVELOPE pan_envelope;
IT_PLAYING_ENVELOPE pitch_envelope;
int fadeoutcount;
IT_FILTER_STATE filter_state[2]; /* Left and right */
DUMB_RESAMPLER resampler;
/* time_lost is used to emulate Impulse Tracker's sample looping
* characteristics. When time_lost is added to pos, the result represents
* the position in the theoretical version of the sample where all loops
* have been expanded. If this is stored, the resampling helpers will
* safely convert it for use with new loop boundaries. The situation is
* slightly more complicated if dir == -1 when the change takes place; we
* must reflect pos off the loop end point and set dir to 1 before
* proceeding.
*/
int32 time_lost;
//int output;
IT_PLAYING *next;
};
#define IT_CHANNEL_MUTED 1
#define IT_ENV_VOLUME 1
#define IT_ENV_PANNING 2
#define IT_ENV_PITCH 4
struct IT_CHANNEL
{
int flags;
unsigned char volume;
signed char volslide;
signed char xm_volslide;
signed char panslide;
/* xm_volslide is used for volume slides done in the volume column in an
* XM file, since it seems the volume column slide is applied first,
* followed by clamping, followed by the effects column slide. IT does
* not exhibit this behaviour, so xm_volslide is maintained at zero.
*/
unsigned char pan;
unsigned short truepan;
unsigned char channelvolume;
signed char channelvolslide;
unsigned char instrument;
unsigned char note;
unsigned char SFmacro;
unsigned char filter_cutoff;
unsigned char filter_resonance;
unsigned char key_off_count;
unsigned char note_cut_count;
unsigned char note_delay_count;
IT_ENTRY *note_delay_entry;
unsigned char new_note_action;
int arpeggio;
unsigned char retrig;
unsigned char xm_retrig;
int retrig_tick;
unsigned char tremor;
unsigned char tremor_time; /* Bit 6 set if note on; bit 7 set if tremor active. */
unsigned char vibrato_waveform;
unsigned char tremolo_waveform;
unsigned char panbrello_waveform;
int portamento;
int toneporta;
int toneslide;
unsigned char toneslide_tick, last_toneslide_tick, ptm_toneslide, ptm_last_toneslide;
unsigned char destnote;
unsigned char toneslide_retrig;
unsigned char glissando;
/** WARNING - for neatness, should one or both of these be in the IT_PLAYING struct? */
unsigned short sample;
unsigned char truenote;
unsigned char midi_state;
signed char lastvolslide;
unsigned char lastDKL;
unsigned char lastEF; /* Doubles as last portamento up for XM files */
unsigned char lastG;
unsigned char lastHspeed;
unsigned char lastHdepth;
unsigned char lastRspeed;
unsigned char lastRdepth;
unsigned char lastYspeed;
unsigned char lastYdepth;
unsigned char lastI;
unsigned char lastJ; /* Doubles as last portamento down for XM files */
unsigned char lastN;
unsigned char lastO;
unsigned char high_offset;
unsigned char lastP;
unsigned char lastQ;
unsigned char lastS;
unsigned char pat_loop_row;
unsigned char pat_loop_count;
unsigned char pat_loop_end_row; /* Used to catch infinite pattern loops */
unsigned char lastW;
unsigned char xm_lastE1;
unsigned char xm_lastE2;
unsigned char xm_lastEA;
unsigned char xm_lastEB;
unsigned char xm_lastX1;
unsigned char xm_lastX2;
IT_PLAYING *playing;
#ifdef BIT_ARRAY_BULLSHIT
void * played_patjump;
int played_patjump_order;
#endif
//int output;
};
struct DUMB_IT_SIGRENDERER
{
DUMB_IT_SIGDATA *sigdata;
int n_channels;
int resampling_quality;
unsigned char globalvolume;
signed char globalvolslide;
unsigned char tempo;
signed char temposlide;
IT_CHANNEL channel[DUMB_IT_N_CHANNELS];
IT_PLAYING *playing[DUMB_IT_N_NNA_CHANNELS];
int tick;
int speed;
int rowcount;
int order; /* Set to -1 if the song is terminated by a callback. */
int row;
int processorder;
int processrow;
int breakrow;
int restart_position;
int n_rows;
IT_ENTRY *entry_start;
IT_ENTRY *entry;
IT_ENTRY *entry_end;
int32 time_left; /* Time before the next tick is processed */
int sub_time_left;
DUMB_CLICK_REMOVER **click_remover;
IT_CALLBACKS *callbacks;
#ifdef BIT_ARRAY_BULLSHIT
/* bit array, which rows are played, only checked by pattern break or loop commands */
void * played;
#endif
int32 gvz_time;
int gvz_sub_time;
int ramp_style;
//int max_output;
IT_PLAYING *free_playing;
};
struct IT_CHECKPOINT
{
IT_CHECKPOINT *next;
int32 time;
DUMB_IT_SIGRENDERER *sigrenderer;
};
struct IT_CALLBACKS
{
int (DUMBCALLBACK *loop)(void *data);
void *loop_data;
/* Return 1 to prevent looping; the music will terminate abruptly. If you
* want to make the music stop but allow samples to fade (beware, as they
* might not fade at all!), use dumb_it_sr_set_speed() and set the speed
* to 0. Note that xm_speed_zero() will not be called if you set the
* speed manually, and also that this will work for IT and S3M files even
* though the music can't stop in this way by itself.
*/
int (DUMBCALLBACK *xm_speed_zero)(void *data);
void *xm_speed_zero_data;
/* Return 1 to terminate the mod, without letting samples fade. */
int (DUMBCALLBACK *midi)(void *data, int channel, unsigned char byte);
void *midi_data;
/* Return 1 to prevent DUMB from subsequently interpreting the MIDI bytes
* itself. In other words, return 1 if the Zxx macros in an IT file are
* controlling filters and shouldn't be.
*/
int (DUMBCALLBACK *global_volume_zero)(void *data);
void *global_volume_zero_data;
/* Return 1 to terminate the module when global volume is set to zero. */
};
void _dumb_it_end_sigrenderer(sigrenderer_t *sigrenderer);
void _dumb_it_unload_sigdata(sigdata_t *vsigdata);
extern DUH_SIGTYPE_DESC _dumb_sigtype_it;
#define XM_APPREGIO 0
#define XM_PORTAMENTO_UP 1
#define XM_PORTAMENTO_DOWN 2
#define XM_TONE_PORTAMENTO 3
#define XM_VIBRATO 4
#define XM_VOLSLIDE_TONEPORTA 5
#define XM_VOLSLIDE_VIBRATO 6
#define XM_TREMOLO 7
#define XM_SET_PANNING 8
#define XM_SAMPLE_OFFSET 9
#define XM_VOLUME_SLIDE 10 /* A */
#define XM_POSITION_JUMP 11 /* B */
#define XM_SET_CHANNEL_VOLUME 12 /* C */
#define XM_PATTERN_BREAK 13 /* D */
#define XM_E 14 /* E */
#define XM_SET_TEMPO_BPM 15 /* F */
#define XM_SET_GLOBAL_VOLUME 16 /* G */
#define XM_GLOBAL_VOLUME_SLIDE 17 /* H */
#define XM_KEY_OFF 20 /* K (undocumented) */
#define XM_SET_ENVELOPE_POSITION 21 /* L */
#define XM_PANNING_SLIDE 25 /* P */
#define XM_MULTI_RETRIG 27 /* R */
#define XM_TREMOR 29 /* T */
#define XM_X 33 /* X */
#define XM_N_EFFECTS (10+26)
#define XM_E_SET_FILTER 0x0
#define XM_E_FINE_PORTA_UP 0x1
#define XM_E_FINE_PORTA_DOWN 0x2
#define XM_E_SET_GLISSANDO_CONTROL 0x3
#define XM_E_SET_VIBRATO_CONTROL 0x4
#define XM_E_SET_FINETUNE 0x5
#define XM_E_SET_LOOP 0x6
#define XM_E_SET_TREMOLO_CONTROL 0x7
#define XM_E_SET_PANNING 0x8
#define XM_E_RETRIG_NOTE 0x9
#define XM_E_FINE_VOLSLIDE_UP 0xA
#define XM_E_FINE_VOLSLIDE_DOWN 0xB
#define XM_E_NOTE_CUT 0xC
#define XM_E_NOTE_DELAY 0xD
#define XM_E_PATTERN_DELAY 0xE
#define XM_X_EXTRAFINE_PORTA_UP 1
#define XM_X_EXTRAFINE_PORTA_DOWN 2
/* To make my life a bit simpler during conversion, effect E:xy is converted
* to effect number EBASE+x:y. The same applies to effect X, and IT's S. That
* way, these effects can be manipulated like regular effects.
*/
#define EBASE (XM_N_EFFECTS)
#define XBASE (EBASE+16)
#define SBASE (IT_N_EFFECTS)
#define EFFECT_VALUE(x, y) (((x)<<4)|(y))
#define HIGH(v) ((v)>>4)
#define LOW(v) ((v)&0x0F)
#define SET_HIGH(v, x) v = (((x)<<4)|((v)&0x0F))
#define SET_LOW(v, y) v = (((v)&0xF0)|(y))
#define BCD_TO_NORMAL(v) (HIGH(v)*10+LOW(v))
#if 0
unsigned char **_dumb_malloc2(int w, int h);
void _dumb_free2(unsigned char **line);
#endif
void _dumb_it_xm_convert_effect(int effect, int value, IT_ENTRY *entry, int mod);
int _dumb_it_fix_invalid_orders(DUMB_IT_SIGDATA *sigdata);
#define PTM_APPREGIO 0
#define PTM_PORTAMENTO_UP 1
#define PTM_PORTAMENTO_DOWN 2
#define PTM_TONE_PORTAMENTO 3
#define PTM_VIBRATO 4
#define PTM_VOLSLIDE_TONEPORTA 5
#define PTM_VOLSLIDE_VIBRATO 6
#define PTM_TREMOLO 7
#define PTM_SAMPLE_OFFSET 9
#define PTM_VOLUME_SLIDE 10 /* A */
#define PTM_POSITION_JUMP 11 /* B */
#define PTM_SET_CHANNEL_VOLUME 12 /* C */
#define PTM_PATTERN_BREAK 13 /* D */
#define PTM_E 14 /* E */
#define PTM_SET_TEMPO_BPM 15 /* F */
#define PTM_SET_GLOBAL_VOLUME 16 /* G */
#define PTM_RETRIGGER 17 /* H */
#define PTM_FINE_VIBRATO 18 /* I */
#define PTM_NOTE_SLIDE_UP 19 /* J */
#define PTM_NOTE_SLIDE_DOWN 20 /* K */
#define PTM_NOTE_SLIDE_UP_RETRIG 21 /* L */
#define PTM_NOTE_SLIDE_DOWN_RETRIG 22 /* M */
#define PTM_N_EFFECTS 23
#define PTM_E_FINE_PORTA_DOWN 0x1
#define PTM_E_FINE_PORTA_UP 0x2
#define PTM_E_SET_VIBRATO_CONTROL 0x4
#define PTM_E_SET_FINETUNE 0x5
#define PTM_E_SET_LOOP 0x6
#define PTM_E_SET_TREMOLO_CONTROL 0x7
#define PTM_E_SET_PANNING 0x8
#define PTM_E_RETRIG_NOTE 0x9
#define PTM_E_FINE_VOLSLIDE_UP 0xA
#define PTM_E_FINE_VOLSLIDE_DOWN 0xB
#define PTM_E_NOTE_CUT 0xC
#define PTM_E_NOTE_DELAY 0xD
#define PTM_E_PATTERN_DELAY 0xE
/* To make my life a bit simpler during conversion, effect E:xy is converted
* to effect number EBASE+x:y. The same applies to effect X, and IT's S. That
* way, these effects can be manipulated like regular effects.
*/
#define PTM_EBASE (PTM_N_EFFECTS)
void _dumb_it_ptm_convert_effect(int effect, int value, IT_ENTRY *entry);
int32 _dumb_it_read_sample_data_adpcm4(IT_SAMPLE *sample, DUMBFILE *f);
#endif /* INTERNAL_IT_H */

View file

@ -0,0 +1,21 @@
#ifndef RIFF_H
#define RIFF_H
struct riff_chunk
{
unsigned type;
void * data;
unsigned size;
};
struct riff
{
unsigned type;
unsigned chunk_count;
struct riff_chunk * chunks;
};
struct riff * riff_parse( unsigned char *, unsigned size, unsigned proper );
void riff_free( struct riff * );
#endif

54
dumb/licence.txt Normal file
View file

@ -0,0 +1,54 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* licence.txt - Conditions for use of DUMB. / / \ \
* | < / \_
* If you do not agree to these terms, please | \/ /\ /
* do not use DUMB. \_ / > /
* | \ / /
* Information in [brackets] is provided to aid | ' /
* interpretation of the licence. \__/
*/
Dynamic Universal Music Bibliotheque
Copyright (C) 2001-2003 Ben Davis, Robert J Ohannessian and Julien Cugniere
This software is provided 'as-is', without any express or implied warranty.
In no event shall the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a
product, you are requested to acknowledge its use in the product
documentation, along with details on where to get an unmodified version of
this software, but this is not a strict requirement.
[Note that the above point asks for a link to DUMB, not just a mention.
Googling for DUMB doesn't help much! The URL is "http://dumb.sf.net/".]
[The only reason why the link is not strictly required is that such a
requirement prevents DUMB from being used in projects with certain other
licences, notably the GPL. See http://www.gnu.org/philosophy/bsd.html .]
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed from or altered in any source distribution.
4. If you are using the Program in someone else's bedroom at any Monday
3:05 PM, you are not allowed to modify the Program for ten minutes. [This
clause provided by Inphernic; every licence should contain at least one
clause, the reasoning behind which is far from obvious.]

421
dumb/readme.txt Normal file
View file

@ -0,0 +1,421 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readme.txt - General information on DUMB. / / \ \
* | < / \_
* | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
********************
*** Introduction ***
********************
Thank you for downloading DUMB! You should have the following documentation:
readme.txt - This file
licence.txt - Conditions for the use of this software
release.txt - Release notes and changes for this and past releases
docs/
howto.txt - Step-by-step instructions on adding DUMB to your project
faq.txt - Frequently asked questions and answers to them
dumb.txt - DUMB library reference
deprec.txt - Information about deprecated parts of the API
ptr.txt - Quick introduction to pointers for those who need it
fnptr.txt - Explanation of function pointers for those who need it
modplug.txt - Our official position regarding ModPlug Tracker
This file will help you get DUMB set up. If you have not yet done so, please
read licence.txt and release.txt before proceeding. After you've got DUMB set
up, please refer to the files in the docs/ directory at your convenience. I
recommend you start with howto.txt.
****************
*** Features ***
****************
Here is the statutory feature list:
- Freeware
- Supports playback of IT, XM, S3M and MOD files
- Faithful to the original trackers, especially IT; if it plays your module
wrongly, please tell me so I can fix the bug! (But please don't complain
about differences between DUMB and ModPlug Tracker; see docs/modplug.txt)
- Accurate support for low-pass resonant filters for IT files
- Very accurate timing and pitching; completely deterministic playback
- Click removal
- Facility to embed music files in other files (e.g. Allegro datafiles)
- Three resampling quality settings: aliasing, linear interpolation and cubic
interpolation
- Number of samples playing at once can be limited to reduce processor usage,
but samples will come back in when other louder ones stop
- All notes will be present and correct even if you start a piece of music in
the middle
- Fast seeking to any point before the music first loops (seeking time
increases beyond this point)
- Audio generated can be used in any way; DUMB does not necessarily send it
straight to a sound output system
- Makefile provided for DJGPP, MinGW, Linux, BeOS and Mac OS X; project file
provided for MSVC 6 (please contact me if you'd like to submit or request
support for a new platform; the code itself should port anywhere that has a
32-bit C compiler)
- Can be used with Allegro, can be used without (if you'd like to help make
DUMB more approachable to people who aren't using Allegro, please contact
me)
*********************
*** What you need ***
*********************
To use DUMB, you need a 32-bit C compiler (GCC and MSVC are fine). If you
have Allegro, DUMB can integrate with its audio streams and datafiles, making
your life easier. If you do not wish to use Allegro, you will have to do some
work to get music playing back. The 'dumbplay' example program requires
Allegro.
Allegro - http://alleg.sf.net/
Neil Walker has kindly uploaded some DUMB binaries at
http://retrospec.sgn.net/allegro/ . They may not always be up to date, so you
should try to compile it yourself first.
**********************************************
*** How to set DUMB up with DJGPP or MinGW ***
**********************************************
You should have got the .zip version. If for some reason you got the .tar.gz
version instead, you may have to convert make/config.bat to DOS text file
format. WinZip does this automatically by default. Otherwise, loading it into
MS EDIT and saving it again should do the trick. You will have to do the same
for any files you want to view in Windows Notepad. If you have problems, just
go and download the .zip instead.
Make sure you preserved the directory structure when you extracted DUMB from
the archive. Most unzipping programs will do this by default, but pkunzip
requires you to pass -d. If not, please delete DUMB and extract it again
properly.
If you are using Windows, open an MS-DOS Prompt or a Windows Command Line.
Change to the directory into which you unzipped DUMB.
Type the following:
make
DUMB will ask you whether you wish to compile for DJGPP or MinGW. Then it
will ask you whether you want support for Allegro. (You have to have made and
installed Allegro's optimised library for this to work.) Finally, it will
compile optimised and debugging builds of DUMB, along with the example
programs. When it has finished, run the following to install the libraries:
make install
All done! If you ever need the configuration again (e.g. if you compiled for
DJGPP before and you want to compile for MinGW now), run the following:
make config
See the comments in the makefile for other targets.
Note: the makefile will only work properly if you have COMSPEC or ComSpec set
to point to command.com or cmd.exe. If you set it to point to a Unix-style
shell, the makefile won't work.
Please let me know if you have any trouble.
Scroll down for information on the example programs. Refer to docs/howto.txt
when you are ready to start programming with DUMB. If you use DUMB in a game,
let me know - I might decide to place a link to your game on DUMB's website!
******************************************************
*** How to set DUMB up with Microsoft Visual C++ 6 ***
******************************************************
You should have got the .zip version. If for some reason you got the .tar.gz
version instead, you may have to convert some files to DOS text file format.
WinZip does this automatically by default. Otherwise, loading such files into
MS EDIT and saving them again should do the trick. You will have to do this
for any files you want to view in Windows Notepad. If you have problems, just
go and download the .zip instead.
Make sure you preserved the directory structure when you extracted DUMB from
the archive. Most unzipping programs will do this by default, but pkunzip
requires you to pass -d. If not, please delete DUMB and extract it again
properly.
DUMB now comes with a project file for Microsoft Visual C++ 6. To add DUMB to
your project:
1. Open your project in VC++.
2. Select Project|Insert Project into Workspace...
3. Navigate to the dumb\vc6 directory, and select dumb.dsp.
4. Select Build|Set Active Configuration..., and reselect one of your
project's configurations.
5. Select Project|Dependencies... and ensure your project is dependent on
DUMB.
6. Select Project|Settings..., Settings for: All Configurations, C/C++ tab,
Preprocessor category. Add the DUMB include directory to the Additional
Include Directories box.
7. Ensure that for all the projects in the workspace (or more likely just all
the projects in a particular dependency chain) the run-time libraries are
the same. That's in Project|Settings, C/C++ tab, Code generation category,
Use run-time library dropdown. The settings for Release and Debug are
separate, so you'll have to change them one at a time. Exactly which run-
time library you use will depend on what you need; it doesn't appear that
DUMB has any particular requirements, so set it to whatever you're using
now.
Good thing you only have to do all that once ...
If you have the Intel compiler installed, it will - well, should - be used to
compile DUMB. The only setting I added is /QxiM. This allows the compiler to
use PPro and MMX instructions, and so when compiling with Intel the resultant
EXE will require a Pentium II or greater. I don't think this is unreasonable.
After all, it is 2003 :)
If you don't have the Intel compiler, VC will compile DUMB as normal.
This project file and these instructions were provided by Tom Seddon (I hope
I got his name right; I had to guess it from his e-mail address!). They are
untested by me. If you have problems, check the download page at
http://dumb.sf.net/ to see if they are addressed; failing that, direct
queries to me and I'll try to figure them out.
When you are ready to start using DUMB, refer to docs/howto.txt. If you use
DUMB in a game, let me know - I might decide to place a link to your game on
DUMB's website!
********************************************************************
*** How to set DUMB up on Linux, BeOS and possibly even Mac OS X ***
********************************************************************
You should have got the .tar.gz version. If for some reason you got the .zip
version instead, you may have to use dtou on some or all of the text files.
If you have problems, just go and download the .tar.gz instead.
First, run the following command as a normal user:
make
You will be asked whether you want Allegro support. Then, unless you are on
BeOS, you will be asked where you'd like DUMB to install its headers,
libraries and examples (which will go in the include/, lib/ and bin/
subdirectories of the prefix you specify). BeOS has fixed locations for these
files. Once you have specified these pieces of information, the optimised and
debugging builds of DUMB will be compiled, along with the examples. When it
has finished, you can install them with:
make install
You may need to be root for this to work. It depends on the prefix you chose.
Note: the makefile will only work if COMSPEC and ComSpec are both undefined.
If either of these is defined, the makefile will try to build for a Windows
system, and will fail.
Please let me know if you have any trouble.
Information on the example programs is just below. Refer to docs/howto.txt
when you are ready to start programming with DUMB. If you use DUMB in a game,
let me know - I might decide to place a link to your game on DUMB's website!
****************************
*** The example programs ***
****************************
Two example programs are provided. On DOS and Windows, you can find them in
the examples subdirectory. On other systems they will be installed system-
wide.
dumbplay
This program will only be built if you have Allegro. Pass it the filename
of an IT, XM, S3M or MOD file, and it will play it. It's not a polished
player with real-time threading or anything - so don't complain about it
stuttering while you use other programs - but it does show DUMB's fidelity
nicely. You can control the playback quality by editing dumb.ini, which
must be in the current working directory. (This is a flaw for systems
where the program is installed system-wide, but it is non-fatal.) Have a
look at the examples/dumb.ini file for further information.
dumbout
This program does not need Allegro. You can use it to stream an IT, XM,
S3M or MOD file to raw PCM. This can be used as input to an encoder like
oggenc (with appropriate command-line options), or it can be sent to a
.pcm file which can be read by any respectable waveform editor. No .wav
support yet, sorry. This program is also convenient for timing DUMB.
Compare the time it takes to render a module with the module's playing
time! dumbout doesn't try to read any configuration file; the options are
set on the command line.
*********************************************
*** Downloading music or writing your own ***
*********************************************
If you would like to compose your own music modules, then first I must offer
a word of warning: not everyone is capable of composing music. Do not assume
you will be able to learn the art. By all means have a go; if you can learn
to play tunes on the computer keyboard, you're well on the way to being a
composer!
The best programs for the job are the trackers that pioneered the file
formats:
Impulse Tracker - IT files - http://www.noisemusic.org/it/
Fast Tracker II - XM files - http://www.gwinternet.com/music/ft2/
Scream Tracker 3 - S3M files -
http://www.united-trackers.org/resources/software/screamtracker.htm
MOD files come from the Amiga; I do not know what PC tracker to recommend for
editing these. If you know of one, let me know! In the meantime, I would
recommend using a more advanced file format. However, don't convert your
existing MODs just for the sake of it.
Note that Fast Tracker II is Shareware. It arguably offers the best
interface, but the IT file format is more powerful and better defined.
Impulse Tracker and Scream Tracker 3 are Freeware. DUMB is likely to be at
its best with IT files.
These editors are DOS programs. Users of DOS-incapable operating systems may
like to try ModPlug Tracker, but should read docs/modplug.txt before using it
for any serious work. If you use a different operating system, or if you know
of any module editors for Windows that are more faithful to the original
trackers' playback, please give me some links so I can put them here!
ModPlug Tracker - http://www.modplug.com/
BEWARE OF WINAMP! Although it's excellent for MP3s, it is notorious for being
one of the worst module players in existence; very few modules play correctly
with it. There are plug-ins available to improve Winamp's module support, for
example WSP.
Winamp - http://www.winamp.com/
WSP - http://www.spytech.cz/index.php?sec=demo
Samples and instruments are the building blocks of music modules. You can
download samples at:
http://www.tump.net/
If you would like to download module files composed by other people, check
the following sites:
http://www.modarchive.com/
http://www.scene.org/
http://www.tump.net/
http://www.homemusic.cc/main.php
http://www.modplug.com/
Once again, if you know of more sites where samples or module files are
available for download, please let me know.
If you wish to use someone's music in your game, please respect the
composer's wishes. In general, you should ask the composer. Music that has
been placed in the Public Domain can be used by anyone for anything, but it
wouldn't do any harm to ask anyway if you know who the author is. In most
cases the author will be thrilled, so don't hesitate!
A note about converting modules from one format to another: don't do it,
unless you are a musician and are prepared to go through the file and make
sure everything sounds the way it should! The module formats are all slightly
different, and converting from one format to another will usually do some
damage.
Instead, it is recommended that you allow DUMB to interpret the original file
as it sees fit. DUMB may make mistakes (it does a lot of conversion on
loading), but future versions of DUMB will be able to rectify these mistakes.
On the other hand, if you convert the file, the damage is permanent.
***********************
*** Contact details ***
***********************
If you have trouble with DUMB, or want to contact me for any other reason, my
e-mail address is given below. However, I may be able to help more if you
come on to IRC EFnet #dumb.
IRC stands for Internet Relay Chat, and is a type of chat network. Several
such networks exist, and EFnet is a popular one. In order to connect to an
IRC network, you first need an IRC client. Here are some:
http://www.xchat.org/
http://www.visualirc.net/beta.php
http://www.mirc.com/
Getting on to IRC can be a steep cliff, but it is not insurmountable, and
it's well worth it. Once you have set up the client software, you need to
connect to a server. Here is a list of EFnet servers I have had success with.
Type "/server" (without quotes), then a space, then the name of a server.
irc.homelien.no
irc.webgiro.se
efnet.vuurwerk.nl
efnet.demon.co.uk
irc.isdnet.fr
irc.prison.net
If these servers do not work, visit http://efnet.org/ircdb/servers.php for a
huge list of other EFnet servers to try.
Once you're connected, type the following:
/join #dumb
A window will appear, and you can ask your question. It should be clear
what's going on from this point onwards. I am 'entheh'. Note that unlike many
other nerds I am not always at my computer, so if I don't answer your
question, don't take it personally! I will usually be able to read your
question when I come back.
******************
*** Conclusion ***
******************
This is the conclusion.
Ben Davis
entheh@users.sf.net
IRC EFnet #dumb

468
dumb/release.txt Normal file
View file

@ -0,0 +1,468 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* release.txt - Release notes for DUMB. / / \ \
* | < / \_
* | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
******************************************
*** DUMB v
******************************************
Pattern loop stuff improved; not quite right for XM files, but better than
before
say where ModPlug's source can be obtained
Notes now cease to exist if they have faded out, even if they are still in
the foreground. This means a new note will be started even if a Gxx occurs
on the same row, as Impulse Tracker does in this scenario.
XM key off fixed more: now the effect value acts as a delay, matching FT2
Added dumb2wav example to convert mod files to WAV files. (Chad
Austin)
Major update to VC6 build. It builds every example as well as both
dumb.lib and aldumb.lib. (Chad Austin)
Added first pass of the automake build. Unix users can compile and
install dumb with the standard "./configure && make && make install"
dance. Caveats: Allegro isn't searched for at configuration time, so
the examples don't build. The build also does not attempt to install
aldumb.h. (Chad Austin)
For IT files, tone portamento didn't work properly when no note was playing.
The effect value wasn't stored. Thanks to Maim from #trax on EFnet for
discovering this bug.
XM key off fixed - doesn't overwrite note any more
tagging system added - title
sample/instrument names/filenames
more functions (n_samples n_instruments)
made timing more accurate; this may affect length by a second or two ...
Thanks to networm for valgrinding the new envelope handling code
DUMB used to process the first row when you created the DUMB_IT_SIGRENDERER.
This happened before you had a chance to install any callbacks. If an F00
effect occurred on the first row, the music would stop immediately and the
xm_speed_zero callback would be called if it were present. Unfortunately, it
wasn't present, and the algorithm for calculating the length subsequently
went into an endless loop while waiting for it. Worse still, the same
algorithm accumulated data for fast seeking, and never stopped, so it pretty
quickly consumed all the resources.
DUMB will now not process the first row until you first request some samples,
provided you pass zero for pos. Ironically, any MOD or XM file with F00 in
the very first row won't play anyway! But at least such files won't crash the
library now.
* The make/config.bat file, responsible for generating make/config.txt, wrote a crucial line to the wrong place, causing it to be left out of the file. As a result, the makefile would fail to install everything for Allegro users, and enter infinite recursion for other users. If you are trying to compile with DJGPP or MinGW, please download dumb-0.9.2-fixed.zip (linked below, and incidentally including the fix for the bug detailed in the next point).
* DUMB's makefile doesn't install the example programs on any platforms. (It is supposed to install them on all platforms except DOS and Windows.) You can fix this by editing Makefile and changing the one occurrence of $COMSPEC to $(COMSPEC). Alternatively you can run the example programs from the examples directory.
* There is a subtle bug that affects a few XM files. For instruments with no associated samples, the array mapping notes to samples is uninitialised. This becomes a problem if such instruments are then used, which does happen sometimes. On many systems, memory is initialised to zero when first given to a program (for security reasons), so the problem won't come up most of the time. However, on platforms like DOS where memory isn't initialised, or in programs that reuse memory later on (this includes the XMMS plug-in), a rogue note will occasionally play. If you are worried that this might happen to you, then use DUMB CVS or wait for the next release (which won't be for a while). If you have access to an i386 Linux system, you can use Valgrind on the 'dumbout' example program to determine whether your XM file causes DUMB to use uninitialised values.
* For IT files, DUMB breaks out of single-point envelope sustain loops one tick too early. The correct behaviour for multi-point sustain loops is yet to be determined. The worst that can happen is that notes may seem to be ever so slightly shorter than they should be, except in pathological cases. This bug has not yet been fixed in CVS.
******************************************
*** DUMB v0.9.2, released 2 April 2003 ***
******************************************
Yes, there really has been a release. This is not a day-late April fools'
joke.
DUMB's full name has changed! The old "Dedicated Universal Music
Bastardisation" was rather silly, and not much more than a forced attempt at
finding words beginning with D, U, M and B. I spent weeks and weeks browsing
dictionaries and hopelessly asking others for bright ideas, until the
brilliant Chris "Kitty Cat" Robinson came up with "Dynamic". I decided to
keep the U as Universal, since a DUH struct can hold digital music in any
format. Now all that remained was the B, but it didn't take me long to come
up with Bibliotheque, which, despite looking French, is indeed considered an
English word by Oxford English Dictionary Online, to which my university has
a subscription. So there you have it - the name now makes sense.
The two most significant additions to the project would have to be the new
thread safety (with an important restriction, detailed in docs/dumb.txt), and
the new build system. The silly 'makeall' and 'makecore' scripts are gone. If
you are a GCC user, all you need do now is run 'make' and 'make install', as
for other projects. You don't even have to run a 'fix' script any more! There
are some caveats, which are covered in readme.txt. If you use Microsoft
Visual C++ 6, you no longer need to obtain GCC and GNU Make - there is a
project file just for you.
Huge thanks go to Steve Terry for testing on Windows XP - about five times -
and to lillo for testing on BeOS and Mac OS X. Thanks also to X-G for testing
on a Windows system that has consistently posed problems for DUMB's old
makefiles.
There was a bug whereby al_poll_duh() would sometimes cause the music to
resume playing if you called it after al_pause_duh(). Whether this was DUMB's
fault for misusing Allegro's API, or a bug in Allegro, is unclear, but this
release makes it work.
In one of my projects, I found that my AL_DUH_PLAYER stopped playing when
there were lots of other sound effects. In order to fix this, I programmed
DUMB to set the priority of the stream's voice to 255, the maximum. I also
added al_duh_set_priority(), so you can set the priority yourself if you need
to.
The resampling code has undergone a transformation. The bad news is that the
linear average code is no longer in use. The good news is that where DUMB's
resamplers used to require three extra samples' worth of memory to be
allocated and initialised, it now copes with just the sample data. And it
does a very good job at bouncing off loop points and otherwise hurtling
around the sample. The resampling code is considerably more complicated, but
the code that uses the resamplers is considerably simpler - and if you
noticed a slight click in some bidirectionally looping samples, you'll be
pleased to know that that click is gone!
I have also devoted some effort to optimisation. It seemed hopeless for a
while, but then I actually figured out a way of making it faster AND more
accurate at the same time! DUMB is now quite a bit faster than it was, and it
mixes not with 16-bit precision, but with 24-bit precision. (It used 32-bit
integers all along, but the difference is that it now makes use of 256 times
as much of the integer's range.)
There have been the usual improvements to playback. The last release occurred
rather too soon after I had fixed the XM effect memories; EAx and EBx, fine
volume ramps, had been neglected. These are now handled properly.
In previous versions of DUMB, muted channels in IT were actually played with
surround sound panning (where the right-hand channel is inverted). This has
been fixed, so muted channels will really be muted now.
There were also some subtle problems with the way DUMB handled New Note
Actions for IT files. It turned out that, in all releases of DUMB so far,
pitch, filter and panning envelopes and sample vibrato were not being
processed for any note that was forced into the background by a new note on
the same channel! This only affected IT files. Not only has this been fixed,
but envelope interpolation is much more accurate. Long trailing envelope-
driven fade-outs sound a lot better now!
Since panning and filter envelopes are more precise, extra fields have been
added to the DUMB_IT_CHANNEL_STATE struct, used by
dumb_it_sr_get_channel_state(). These fields hold the 'decimal' parts of the
pan and filter cut-off. See dumb.txt for details.
Mxx (set channel volume) now correctly only modifies the last note played on
the channel, not any previous notes that have been forced into the background
by New Note Actions, and filter effect processing is now closer to what
Impulse Tracker does.
The XM loader was slightly flawed and could crash on files containing samples
with out-of-range loop points. One such file was given to me. This has been
fixed.
Finally, the legal stuff. Julien Cugniere has been added to the list of
copyright owners. He deserves it, for all the work he did on the XM support!
And the licence has been changed. You are no longer required to include a
link to DUMB in a project that uses DUMB; the reasons for this relaxation are
explained in licence.txt. However, the request is still there ...
As usual, enjoy!
**********************************************
*** DUMB v0.9.1, released 19 December 2002 ***
**********************************************
Hi again! Lots to say this time, so I shall cut right to the chase.
DUMB now supports Impulse Tracker's low-pass resonant filters! Huge thanks go
to Jeffrey Lim, author of Impulse Tracker, for giving me what information he
still had regarding the algorithm; to cut a long story short, modifying
ModPlug Tracker's source code (which is in the Public Domain) led to an
algorithm whose output matched Impulse Tracker's perfectly.
Please note that ModPlug Tracker's filters as they stand do not match Impulse
Tracker's, and I have no interest in supporting ModPlug Tracker's variant
(especially not the integer rounding problems). Please see docs/modplug.txt,
new in this release, for details.
Thanks also go to Fatso Huuskonen for motivating me to add filter support,
and providing me with several great IT files to test it with!
The other important feature added for this release is click removal. Up until
now, DUMB has generated clicks when cutting notes, starting samples in the
middle, and so on. This version of DUMB will remove any such clicks. Note
that DUMB does not use volume ramps to accomplish this; the algorithm will
not take the bite out of the music!
In other news, DUMB now supports sample vibrato for IT files, and instrument
vibrato for XM files. A slight bug in New Note Action handling for IT files
has been fixed; Note Fade will not break the sustain loops of the sample and
envelope, as it did before. Tremor handling (Ixy) had a strange bug in it,
which has been fixed.
Support for XM files has been greatly enhanced. The XM envelope handling new
in the last release contained a huge bug, resulting in notes seeming not to
stop when they should; this has been fixed. Some XM files crashed DUMB, while
others failed to load; these problems have been solved. Effect memories now
work properly for XM and MOD files, to the best of my knowledge. Some other
differences between IT and XM have been accounted for, most notably the
Retrigger Note effects, Rxy and E9x.
DUMB's sound quality and accuracy are not the only areas that have been
enhanced. The API has been expanded, at last. You can now detect when a
module loops, or make it play through just once. You can ask DUMB to inform
you every time it generates some samples; this is useful for visualisation.
For IT files, you can intercept the MIDI messages generated by Zxx macros,
enabling you to synchronise your game with the music to some extent. (There
is no such method for XM, S3M or MOD files yet; sorry. Also note that the
function will be called before you actually hear the sound; I cannot improve
this until DUMB has its own sound drivers, which won't be for a while.) You
can query the current order and row. Finally, operations like changing the
speed and tempo are now possible, and you can query the playback state on
each channel.
Some parts of DUMB's API have been deprecated. Simple programs that use
Allegro will be unaffected, but if you get some compiler warnings or errors,
please review docs/deprec.txt. This file explains why those parts of the API
were deprecated, and tells you how to adapt your code; the changes you need
to make are straightforward. Sorry for the inconvenience.
For various reasons, I have made DUMB's makefiles use different compiler
flags depending on your GCC version (unless you are using MSVC). There is no
elegant way of getting the makefiles to detect when GCC is upgraded. If you
upgrade GCC, you should execute 'make clean' in order to make DUMB detect the
GCC version again. Otherwise you may get some annoying error messages. (It is
wise to do this in any case, so that all the object files are built with the
same GCC version.)
DUMB's example players have been unified into a single player called
'dumbplay'. The player has been enhanced to display messages when the music
loops, and when XM and MOD files freeze (effect F00; more information on this
in docs/howto.txt).
Finally, as noted on DUMB's website, the release notes from the last release
were inaccurate. It has been verified that DUMBOGG v0.5 does still work with
that release, and still works with this release. The esoteric DUMBOGG v0.6
has not been created yet, since DUMBOGG v0.5 still works.
Please scroll down and read through the indented paragraphs in the notes for
the last release; they are relevant for this release too.
That's all folks! Until next time.
*******************************************
*** DUMB v0.9, released 16 October 2002 ***
*******************************************
MOD support is here! DUMB now supports all four of the common module formats.
As usual, there have also been some improvements to the way modules are
played back. Most notably, handling of tone portamento in IT files has been
improved a lot, and XM envelopes are now processed correctly.
The other major change is that DUMB now does a dummy run through each module
on loading. It stores the playback state at thirty-second intervals. It stops
when the module first loops, and then stores the playback time. This results
in a slightly longer load time and a greater memory overhead, but seeking is
faster (to any point before the module first loops) and the length is
calculated! duh_get_length() will return this and is now documented in
docs/howto.txt and docs/dumb.txt.
DUMB's build process has been changed to use 'mingw' wherever it used
'mingw32' before; some directories have been renamed, and the 'fix' command
you had to run for MinGW has been changed from 'fix mingw32' to 'fix mingw'.
Last time, I directed you to scroll down and read the notes from a past
release, but ignore this point, and that point applies to something else, and
so on. Did anyone do so? Well, if you're reading this at all, you probably
did. Nevertheless, this time I shall be much less confusing and restate any
relevant information. So the least you can do is read it!
- If your program ever aborts with exit code 37 while loading an IT file,
PLEASE LET ME KNOW! The IT file in question has a stereo compressed sample
in it, and the format is unspecified for this case (Impulse Tracker itself
doesn't use stereo samples at all). I will need the IT file in question,
and any information you can give me about how the IT file was created (e.g.
what program). (If you don't get to see an exit code, let me know anyway.)
- If your program ever outputs a line resembling "Inst 01 Env: 0,64 8,32
15,48" to stderr while loading an IT file, PLEASE LET ME KNOW! You have an
old IT file (saved by an Impulse Tracker version older than 2.00), and
support for such files is STILL untested.
- Not all parts of DUMB's API are documented yet. You will find some
functions in dumb.h which are not listed in docs/dumb.txt; the reason is
that these functions still need work and will probably change. If you
really, really want to use them, talk to me first (IRC EFnet #dumb is a
good place for this; see readme.txt for details on using IRC). I intend to
finalise and document the whole of DUMB's API for Version 1.0.
There have been some changes to the naming conventions in DUMB's undocumented
API. DUMBOGG v0.5 will not work with this and subsequent releases of DUMB;
please upgrade to DUMBOGG v0.6. These changes should not break anything in
your own code, since you didn't use those parts of the API, did you ;)
There is still a great deal of work to be done before DUMB's API can be
finalised, and thus it will be a while before DUMB v1.0 comes out. It should
be worth the wait. In the meantime, there will be 0.9.x releases with
additional functionality, improved playback, and possibly support for some
extra file formats.
Finally I should like to offer an apology; there is a strong possibility that
some of DUMB's official API will change in the near future. There will not be
any drastic changes, and the corresponding changes to your source code will
be simple enough. If I didn't make these changes, DUMB's API would start to
become limited, or messy, or both, so it's for the better. I apologise in
advance for this.
Now scroll down and read the notes for the first r... oh wait, we already did
that. I guess that's it then. You can stop reading now.
Right after you've read this.
And this.
Off you go.
Bye.
********************************************
*** DUMB v0.8.1, released 11 August 2002 ***
********************************************
This is a minor release that fixes a few bugs. One of these bugs, however,
was pretty serious. dumb_register_dat_xm() was never coded! It was prototyped
in aldumb.h, so code would compile, but there would be an unresolved symbol
at the linking stage. This has been fixed.
Platforms other than Unix did not have a working 'make veryclean' target;
this has been fixed. In addition, the makefiles now use 'xcopy' instead of
'copy', since on some systems GNU Make seems to have trouble calling commands
built in to the shell.
Contrary to the errata that was on the DUMB website, the makeall.sh and
makecore.sh scripts actually DID install in /usr. This has now been
corrected, and regardless of whether you use these scripts or call make
directly, the files will now be installed to /usr/local by default.
The XM loader used to treat stereo samples as mono samples with the data for
the right channel positioned after the data for the left channel. This
generally resulted in an unwanted echo effect. This has been fixed.
When playing XM files, specifying an invalid instrument would cause an old
note on that channel to come back (roughly speaking). Fast Tracker 2 does not
exhibit this behaviour. This has been fixed.
The GCC makefiles used -mpentium, which is deprecated in gcc 3.x. This was
generating warnings, and has now been fixed.
In XM files, the length of a sample is stored in bytes. DUMB was assuming
that the length of a 16-bit sample would be even. I had two XM files where
this was not the case, and DUMB was unable to load them. This has been fixed.
In order to accommodate the extra part of the version number,
DUMB_REVISION_VERSION has been added. DUMB_VERSION has also been added in
order to facilitate checking if the version of DUMB installed is sufficient.
See docs/dumb.txt for details.
As a last-minute fix, the XM "Break to row" effect is now loaded properly. It
was necessary to convert from binary-coded decimal to hexadecimal (those who
have experience with Fast Tracker 2 will know what I mean). In short, this
means the effect will now work properly when breaking to row 10 or greater.
DUMB v0.8 had faulty release date constants; DUMB_MONTH and DUMB_DAY were
swapped! For this reason, DUMB_DATE should not be compared against any date
in 2002. This note has been added to docs/dumb.txt and also to dumb.h.
Please scroll to the end and read the release notes for the first version,
DUMB v0.7. Most of them apply equally to this release. However, the
non-portable code was rewritten for DUMB v0.8, so that point does not apply.
The point about length not being calculated also applies to XM files.
Enjoy :)
****************************************
*** DUMB v0.8, released 14 June 2002 ***
****************************************
Welcome to the second release of DUMB!
In addition to these notes, please read below the release notes for the
previous version, DUMB v0.7. Most of them apply equally to this release.
However, the non-portable code has been rewritten; DUMB should now port to
big-endian platforms.
The main improvement in this release of DUMB is the support for XM files.
Enormous thanks go to Julien Cugniere for working on this while I had to
revise for my exams!
There was a mistake in the makefiles in the last release. The debugging
Allegro interface library was mistakenly named libaldmbd.a instead of
libaldmd.a, meaning you had to compile with -laldmbd, contrary to what the
docs said. Apologies to everyone who lost sleep trying to work out what was
wrong! The reason for using libaldmd.a is to maintain compatibility with
plain DOS, where filenames are limited to eight characters (plus a three-
letter extension). The makefiles have now been changed to match the
information in the docs, so you may have to alter your project files
accordingly.
The example programs were faulty, and crashed on Windows if they were unable
to load the file. It was also difficult to work out how to exit them (you had
to click the taskbar button that didn't have a window, then press a key).
They have been improved in both these respects.
I have now added a docs/faq.txt file (Frequently Asked Questions), which is
based on problems and misconceptions people have had with the first release.
Please refer to it before contacting me with problems.
Thanks to networm for touching up the Unix makefile and writing the
instructions on using it.
Incidentally, today (Friday 14 June) is the Robinson College May Ball at
Cambridge Uni. God knows why it's called a May Ball if it's in June. I'm not
going myself (72 GBP, and I'd have to wear a suit, ugh), but with all the
noise outside I shall enjoy pumping up the speakers tonight!
****************************************
*** DUMB v0.7, released 2 March 2002 ***
****************************************
This is the first release of DUMB, and parts of the library are not
crystallised. Don't let this put you off! Provided you don't try to use any
features that aren't documented in docs/dumb.txt, the library should be rock
solid and you should be able to upgrade more or less without problems.
Here are some notes on this release:
- There is some non-portable code in this release of DUMB. It is likely that
the library will fail to load IT files with compressed samples on
big-endian machines such as the Apple Macintosh.
- If your program ever aborts with exit code 37 while loading an IT file,
PLEASE LET ME KNOW! The IT file in question has a stereo compressed sample
in it, and the format is unspecified for this case (Impulse Tracker itself
doesn't use stereo samples at all). I will need the IT file in question,
and any information you can give me about how the IT file was created (e.g.
what program). (If you don't get to see an exit code, let me know anyway.)
- If your program ever outputs a line resembling "Inst 01 Env: 0,64 8,32
15,48" to stderr while loading an IT file, PLEASE LET ME KNOW! You have an
old IT file (saved by an Impulse Tracker version older than 2.00), and
support for such files is untested.
- The length of IT and S3M files is not currently calculated. It is just set
to ten minutes.

71
dumb/src/core/atexit.c Normal file
View file

@ -0,0 +1,71 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* atexit.c - Library Clean-up Management. / / \ \
* | < / \_
* By entheh. | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include "dumb.h"
#include "internal/dumb.h"
typedef struct DUMB_ATEXIT_PROC
{
struct DUMB_ATEXIT_PROC *next;
void (*proc)(void);
}
DUMB_ATEXIT_PROC;
static DUMB_ATEXIT_PROC *dumb_atexit_proc = NULL;
int dumb_atexit(void (*proc)(void))
{
DUMB_ATEXIT_PROC *dap = dumb_atexit_proc;
while (dap) {
if (dap->proc == proc) return 0;
dap = dap->next;
}
dap = malloc(sizeof(*dap));
if (!dap)
return -1;
dap->next = dumb_atexit_proc;
dap->proc = proc;
dumb_atexit_proc = dap;
return 0;
}
void dumb_exit(void)
{
while (dumb_atexit_proc) {
DUMB_ATEXIT_PROC *next = dumb_atexit_proc->next;
(*dumb_atexit_proc->proc)();
free(dumb_atexit_proc);
dumb_atexit_proc = next;
}
}

42
dumb/src/core/duhlen.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* duhlen.c - Functions to set and return the / / \ \
* length of a DUH. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* Note that the length of a DUH is a constant | ' /
* stored in the DUH struct and in the DUH disk \__/
* format. It will be calculated on loading for
* other formats in which the length is not explicitly stored. Also note that
* it does not necessarily correspond to the length of time for which the DUH
* will generate samples. Rather it represents a suitable point for a player
* such as Winamp to stop, and in any good DUH it will allow for any final
* flourish to fade out and be appreciated.
*/
#include "dumb.h"
#include "internal/dumb.h"
int32 DUMBEXPORT duh_get_length(DUH *duh)
{
return duh ? duh->length : 0;
}
void DUMBEXPORT duh_set_length(DUH *duh, int32 length)
{
if (duh)
duh->length = length;
}

38
dumb/src/core/duhtag.c Normal file
View file

@ -0,0 +1,38 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* duhtag.c - Function to return the tags stored / / \ \
* in a DUH struct (typically author | < / \_
* information). | \/ /\ /
* \_ / > /
* By entheh. | \ / /
* | ' /
* \__/
*/
#include <string.h>
#include "dumb.h"
#include "internal/dumb.h"
const char *DUMBEXPORT duh_get_tag(DUH *duh, const char *key)
{
int i;
ASSERT(key);
if (!duh || !duh->tag) return NULL;
for (i = 0; i < duh->n_tags; i++)
if (strcmp(key, duh->tag[i][0]) == 0)
return duh->tag[i][1];
return NULL;
}

401
dumb/src/core/dumbfile.c Normal file
View file

@ -0,0 +1,401 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* dumbfile.c - Hookable, strictly sequential / / \ \
* file input functions. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include "dumb.h"
static const DUMBFILE_SYSTEM *the_dfs = NULL;
void DUMBEXPORT register_dumbfile_system(const DUMBFILE_SYSTEM *dfs)
{
ASSERT(dfs);
ASSERT(dfs->open);
ASSERT(dfs->getc);
ASSERT(dfs->close);
the_dfs = dfs;
}
struct DUMBFILE
{
const DUMBFILE_SYSTEM *dfs;
void *file;
int32 pos;
};
DUMBFILE *DUMBEXPORT dumbfile_open(const char *filename)
{
DUMBFILE *f;
ASSERT(the_dfs);
f = malloc(sizeof(*f));
if (!f)
return NULL;
f->dfs = the_dfs;
f->file = (*the_dfs->open)(filename);
if (!f->file) {
free(f);
return NULL;
}
f->pos = 0;
return f;
}
DUMBFILE *DUMBEXPORT dumbfile_open_ex(void *file, const DUMBFILE_SYSTEM *dfs)
{
DUMBFILE *f;
ASSERT(dfs);
ASSERT(dfs->getc);
ASSERT(file);
f = malloc(sizeof(*f));
if (!f) {
if (dfs->close)
(*dfs->close)(file);
return NULL;
}
f->dfs = dfs;
f->file = file;
f->pos = 0;
return f;
}
int32 DUMBEXPORT dumbfile_pos(DUMBFILE *f)
{
ASSERT(f);
return f->pos;
}
int DUMBEXPORT dumbfile_skip(DUMBFILE *f, int32 n)
{
int rv;
ASSERT(f);
ASSERT(n >= 0);
if (f->pos < 0)
return -1;
f->pos += n;
if (f->dfs->skip) {
rv = (*f->dfs->skip)(f->file, n);
if (rv) {
f->pos = -1;
return rv;
}
} else {
while (n) {
rv = (*f->dfs->getc)(f->file);
if (rv < 0) {
f->pos = -1;
return rv;
}
n--;
}
}
return 0;
}
int DUMBEXPORT dumbfile_getc(DUMBFILE *f)
{
int rv;
ASSERT(f);
if (f->pos < 0)
return -1;
rv = (*f->dfs->getc)(f->file);
if (rv < 0) {
f->pos = -1;
return rv;
}
f->pos++;
return rv;
}
int DUMBEXPORT dumbfile_igetw(DUMBFILE *f)
{
int l, h;
ASSERT(f);
if (f->pos < 0)
return -1;
l = (*f->dfs->getc)(f->file);
if (l < 0) {
f->pos = -1;
return l;
}
h = (*f->dfs->getc)(f->file);
if (h < 0) {
f->pos = -1;
return h;
}
f->pos += 2;
return l | (h << 8);
}
int DUMBEXPORT dumbfile_mgetw(DUMBFILE *f)
{
int l, h;
ASSERT(f);
if (f->pos < 0)
return -1;
h = (*f->dfs->getc)(f->file);
if (h < 0) {
f->pos = -1;
return h;
}
l = (*f->dfs->getc)(f->file);
if (l < 0) {
f->pos = -1;
return l;
}
f->pos += 2;
return l | (h << 8);
}
int32 DUMBEXPORT dumbfile_igetl(DUMBFILE *f)
{
uint32 rv, b;
ASSERT(f);
if (f->pos < 0)
return -1;
rv = (*f->dfs->getc)(f->file);
if ((sint32)rv < 0) {
f->pos = -1;
return rv;
}
b = (*f->dfs->getc)(f->file);
if ((sint32)b < 0) {
f->pos = -1;
return b;
}
rv |= b << 8;
b = (*f->dfs->getc)(f->file);
if ((sint32)b < 0) {
f->pos = -1;
return b;
}
rv |= b << 16;
b = (*f->dfs->getc)(f->file);
if ((sint32)b < 0) {
f->pos = -1;
return b;
}
rv |= b << 24;
f->pos += 4;
return rv;
}
int32 DUMBEXPORT dumbfile_mgetl(DUMBFILE *f)
{
uint32 rv, b;
ASSERT(f);
if (f->pos < 0)
return -1;
rv = (*f->dfs->getc)(f->file);
if ((sint32)rv < 0) {
f->pos = -1;
return rv;
}
rv <<= 24;
b = (*f->dfs->getc)(f->file);
if ((sint32)b < 0) {
f->pos = -1;
return b;
}
rv |= b << 16;
b = (*f->dfs->getc)(f->file);
if ((sint32)b < 0) {
f->pos = -1;
return b;
}
rv |= b << 8;
b = (*f->dfs->getc)(f->file);
if ((sint32)b < 0) {
f->pos = -1;
return b;
}
rv |= b;
f->pos += 4;
return rv;
}
uint32 DUMBEXPORT dumbfile_cgetul(DUMBFILE *f)
{
uint32 rv = 0;
int v;
do {
v = dumbfile_getc(f);
if (v < 0)
return v;
rv <<= 7;
rv |= v & 0x7F;
} while (v & 0x80);
return rv;
}
sint32 DUMBEXPORT dumbfile_cgetsl(DUMBFILE *f)
{
uint32 rv = dumbfile_cgetul(f);
if (f->pos < 0)
return rv;
return (rv >> 1) | (rv << 31);
}
int32 DUMBEXPORT dumbfile_getnc(char *ptr, int32 n, DUMBFILE *f)
{
int32 rv;
ASSERT(f);
ASSERT(n >= 0);
if (f->pos < 0)
return -1;
if (f->dfs->getnc) {
rv = (*f->dfs->getnc)(ptr, n, f->file);
if (rv < n) {
f->pos = -1;
return MAX(rv, 0);
}
} else {
for (rv = 0; rv < n; rv++) {
int c = (*f->dfs->getc)(f->file);
if (c < 0) {
f->pos = -1;
return rv;
}
*ptr++ = c;
}
}
f->pos += rv;
return rv;
}
int DUMBEXPORT dumbfile_error(DUMBFILE *f)
{
ASSERT(f);
return f->pos < 0;
}
int DUMBEXPORT dumbfile_close(DUMBFILE *f)
{
int rv;
ASSERT(f);
rv = f->pos < 0;
if (f->dfs->close)
(*f->dfs->close)(f->file);
free(f);
return rv;
}

42
dumb/src/core/loadduh.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadduh.c - Code to read a DUH from a file, / / \ \
* opening and closing the file for | < / \_
* you. | \/ /\ /
* \_ / > /
* By entheh. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/dumb.h"
/* load_duh(): loads a .duh file, returning a pointer to a DUH struct.
* When you have finished with it, you must pass the pointer to unload_duh()
* so that the memory can be freed.
*/
DUH *DUMBEXPORT load_duh(const char *filename)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = read_duh(f);
dumbfile_close(f);
return duh;
}

132
dumb/src/core/makeduh.c Normal file
View file

@ -0,0 +1,132 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* makeduh.c - Function to construct a DUH from / / \ \
* its components. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <string.h>
#include "dumb.h"
#include "internal/dumb.h"
static DUH_SIGNAL *make_signal(DUH_SIGTYPE_DESC *desc, sigdata_t *sigdata)
{
DUH_SIGNAL *signal;
ASSERT((desc->start_sigrenderer && desc->end_sigrenderer) || (!desc->start_sigrenderer && !desc->end_sigrenderer));
ASSERT(desc->sigrenderer_generate_samples && desc->sigrenderer_get_current_sample);
signal = malloc(sizeof(*signal));
if (!signal) {
if (desc->unload_sigdata)
if (sigdata)
(*desc->unload_sigdata)(sigdata);
return NULL;
}
signal->desc = desc;
signal->sigdata = sigdata;
return signal;
}
DUH *make_duh(
int32 length,
int n_tags,
const char *const tags[][2],
int n_signals,
DUH_SIGTYPE_DESC *desc[],
sigdata_t *sigdata[]
)
{
DUH *duh = malloc(sizeof(*duh));
int i;
int fail;
if (duh) {
duh->n_signals = n_signals;
duh->signal = malloc(n_signals * sizeof(*duh->signal));
if (!duh->signal) {
free(duh);
duh = NULL;
}
}
if (!duh) {
for (i = 0; i < n_signals; i++)
if (desc[i]->unload_sigdata)
if (sigdata[i])
(*desc[i]->unload_sigdata)(sigdata[i]);
return NULL;
}
duh->n_tags = 0;
duh->tag = NULL;
fail = 0;
for (i = 0; i < n_signals; i++) {
duh->signal[i] = make_signal(desc[i], sigdata[i]);
if (!duh->signal[i])
fail = 1;
}
if (fail) {
unload_duh(duh);
return NULL;
}
duh->length = length;
{
int mem = n_tags * 2; /* account for NUL terminators here */
char *ptr;
for (i = 0; i < n_tags; i++)
mem += (int)(strlen(tags[i][0]) + strlen(tags[i][1]));
if (mem <= 0) return duh;
duh->tag = malloc(n_tags * sizeof(*duh->tag));
if (!duh->tag) return duh;
duh->tag[0][0] = malloc(mem);
if (!duh->tag[0][0]) {
free(duh->tag);
duh->tag = NULL;
return duh;
}
duh->n_tags = n_tags;
ptr = duh->tag[0][0];
for (i = 0; i < n_tags; i++) {
duh->tag[i][0] = ptr;
strcpy(ptr, tags[i][0]);
ptr += strlen(tags[i][0]) + 1;
duh->tag[i][1] = ptr;
strcpy(ptr, tags[i][1]);
ptr += strlen(tags[i][1]) + 1;
}
}
return duh;
}

44
dumb/src/core/rawsig.c Normal file
View file

@ -0,0 +1,44 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* rawsig.c - Function to retrieve raw signal / / \ \
* data from a DUH provided you know | < / \_
* what type of signal it is. | \/ /\ /
* \_ / > /
* By entheh. | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include "dumb.h"
#include "internal/dumb.h"
/* You have to specify the type of sigdata, proving you know what to do with
* the pointer. If you get it wrong, you can expect NULL back.
*/
sigdata_t *DUMBEXPORT duh_get_raw_sigdata(DUH *duh, int sig, int32 type)
{
DUH_SIGNAL *signal;
if (!duh) return NULL;
if ((unsigned int)sig >= (unsigned int)duh->n_signals) return NULL;
signal = duh->signal[sig];
if (signal && signal->desc->type == type)
return signal->sigdata;
return NULL;
}

107
dumb/src/core/readduh.c Normal file
View file

@ -0,0 +1,107 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readduh.c - Code to read a DUH from an open / / \ \
* file. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include "dumb.h"
#include "internal/dumb.h"
static DUH_SIGNAL *read_signal(DUH *duh, DUMBFILE *f)
{
DUH_SIGNAL *signal;
int32 type;
signal = malloc(sizeof(*signal));
if (!signal)
return NULL;
type = dumbfile_mgetl(f);
if (dumbfile_error(f)) {
free(signal);
return NULL;
}
signal->desc = _dumb_get_sigtype_desc(type);
if (!signal->desc) {
free(signal);
return NULL;
}
if (signal->desc->load_sigdata) {
signal->sigdata = (*signal->desc->load_sigdata)(duh, f);
if (!signal->sigdata) {
free(signal);
return NULL;
}
} else
signal->sigdata = NULL;
return signal;
}
/* read_duh(): reads a DUH from an already open DUMBFILE, and returns its
* pointer, or null on error. The file is not closed.
*/
DUH *DUMBEXPORT read_duh(DUMBFILE *f)
{
DUH *duh;
int i;
if (dumbfile_mgetl(f) != DUH_SIGNATURE)
return NULL;
duh = malloc(sizeof(*duh));
if (!duh)
return NULL;
duh->length = dumbfile_igetl(f);
if (dumbfile_error(f) || duh->length <= 0) {
free(duh);
return NULL;
}
duh->n_signals = dumbfile_igetl(f);
if (dumbfile_error(f) || duh->n_signals <= 0) {
free(duh);
return NULL;
}
duh->signal = malloc(sizeof(*duh->signal) * duh->n_signals);
if (!duh->signal) {
free(duh);
return NULL;
}
for (i = 0; i < duh->n_signals; i++)
duh->signal[i] = NULL;
for (i = 0; i < duh->n_signals; i++) {
if (!(duh->signal[i] = read_signal(duh, f))) {
unload_duh(duh);
return NULL;
}
}
return duh;
}

104
dumb/src/core/register.c Normal file
View file

@ -0,0 +1,104 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* register.c - Signal type registration. / / \ \
* | < / \_
* By entheh. | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include "dumb.h"
#include "internal/dumb.h"
static DUH_SIGTYPE_DESC_LINK *sigtype_desc = NULL;
static DUH_SIGTYPE_DESC_LINK **sigtype_desc_tail = &sigtype_desc;
/* destroy_sigtypes(): frees all memory allocated while registering signal
* types. This function is set up to be called by dumb_exit().
*/
static void destroy_sigtypes(void)
{
DUH_SIGTYPE_DESC_LINK *desc_link = sigtype_desc, *next;
sigtype_desc = NULL;
sigtype_desc_tail = &sigtype_desc;
while (desc_link) {
next = desc_link->next;
free(desc_link);
desc_link = next;
}
}
/* dumb_register_sigtype(): registers a new signal type with DUMB. The signal
* type is identified by a four-character string (e.g. "WAVE"), which you can
* encode using the the DUMB_ID() macro (e.g. DUMB_ID('W','A','V','E')). The
* signal's behaviour is defined by four functions, whose pointers you pass
* here. See the documentation for details.
*
* If a DUH tries to use a signal that has not been registered using this
* function, then the library will fail to load the DUH.
*/
void DUMBEXPORT dumb_register_sigtype(DUH_SIGTYPE_DESC *desc)
{
DUH_SIGTYPE_DESC_LINK *desc_link = sigtype_desc;
ASSERT((desc->load_sigdata && desc->unload_sigdata) || (!desc->load_sigdata && !desc->unload_sigdata));
ASSERT((desc->start_sigrenderer && desc->end_sigrenderer) || (!desc->start_sigrenderer && !desc->end_sigrenderer));
ASSERT(desc->sigrenderer_generate_samples && desc->sigrenderer_get_current_sample);
if (desc_link) {
do {
if (desc_link->desc->type == desc->type) {
desc_link->desc = desc;
return;
}
desc_link = desc_link->next;
} while (desc_link);
} else
dumb_atexit(&destroy_sigtypes);
desc_link = *sigtype_desc_tail = malloc(sizeof(DUH_SIGTYPE_DESC_LINK));
if (!desc_link)
return;
desc_link->next = NULL;
sigtype_desc_tail = &desc_link->next;
desc_link->desc = desc;
}
/* _dumb_get_sigtype_desc(): searches the registered functions for a signal
* type matching the parameter. If such a sigtype is found, it returns a
* pointer to a sigtype descriptor containing the necessary functions to
* manage the signal. If none is found, it returns NULL.
*/
DUH_SIGTYPE_DESC *_dumb_get_sigtype_desc(int32 type)
{
DUH_SIGTYPE_DESC_LINK *desc_link = sigtype_desc;
while (desc_link && desc_link->desc->type != type)
desc_link = desc_link->next;
return desc_link ? desc_link->desc : NULL;
}

184
dumb/src/core/rendduh.c Normal file
View file

@ -0,0 +1,184 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* rendduh.c - Functions for rendering a DUH into / / \ \
* an end-user sample format. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <limits.h>
#include "dumb.h"
#include "internal/dumb.h"
/* On the x86, we can use some tricks to speed stuff up */
#if (defined _MSC_VER) || (defined __DJGPP__) || (defined __MINGW__)
// Can't we detect Linux and other x86 platforms here? :/
#define FAST_MID(var, min, max) { \
var -= (min); \
var &= (~var) >> (sizeof(var) * CHAR_BIT - 1); \
var += (min); \
var -= (max); \
var &= var >> (sizeof(var) * CHAR_BIT - 1); \
var += (max); \
}
#define CONVERT8(src, pos, signconv) { \
signed int f = (src + 0x8000) >> 16; \
FAST_MID(f, -128, 127); \
((char*)sptr)[pos] = (char)f ^ signconv; \
}
#define CONVERT16(src, pos, signconv) { \
signed int f = (src + 0x80) >> 8; \
FAST_MID(f, -32768, 32767); \
((short*)sptr)[pos] = (short)(f ^ signconv); \
}
#else
#define CONVERT8(src, pos, signconv) \
{ \
signed int f = (src + 0x8000) >> 16; \
f = MID(-128, f, 127); \
((char *)sptr)[pos] = (char)f ^ signconv; \
}
#define CONVERT16(src, pos, signconv) \
{ \
signed int f = (src + 0x80) >> 8; \
f = MID(-32768, f, 32767); \
((short *)sptr)[pos] = (short)(f ^ signconv); \
}
#endif
/* DEPRECATED */
DUH_SIGRENDERER *duh_start_renderer(DUH *duh, int n_channels, int32 pos)
{
return duh_start_sigrenderer(duh, 0, n_channels, pos);
}
int32 DUMBEXPORT duh_render(
DUH_SIGRENDERER *sigrenderer,
int bits, int unsign,
float volume, float delta,
int32 size, void *sptr
)
{
int32 n;
sample_t **sampptr;
int n_channels;
ASSERT(bits == 8 || bits == 16);
ASSERT(sptr);
if (!sigrenderer)
return 0;
n_channels = duh_sigrenderer_get_n_channels(sigrenderer);
ASSERT(n_channels > 0);
/* This restriction will be removed when need be. At the moment, tightly
* optimised loops exist for exactly one or two channels.
*/
ASSERT(n_channels <= 2);
sampptr = allocate_sample_buffer(n_channels, size);
if (!sampptr)
return 0;
dumb_silence(sampptr[0], n_channels * size);
size = duh_sigrenderer_generate_samples(sigrenderer, volume, delta, size, sampptr);
if (bits == 16) {
int signconv = unsign ? 0x8000 : 0x0000;
for (n = 0; n < size * n_channels; n++) {
CONVERT16(sampptr[0][n], n, signconv);
}
} else {
char signconv = unsign ? 0x80 : 0x00;
for (n = 0; n < size * n_channels; n++) {
CONVERT8(sampptr[0][n], n, signconv);
}
}
destroy_sample_buffer(sampptr);
return size;
}
/* DEPRECATED */
int duh_renderer_get_n_channels(DUH_SIGRENDERER *dr)
{
return duh_sigrenderer_get_n_channels(dr);
}
/* DEPRECATED */
int32 duh_renderer_get_position(DUH_SIGRENDERER *dr)
{
return duh_sigrenderer_get_position(dr);
}
/* DEPRECATED */
void duh_end_renderer(DUH_SIGRENDERER *dr)
{
duh_end_sigrenderer(dr);
}
/* DEPRECATED */
DUH_SIGRENDERER *duh_renderer_encapsulate_sigrenderer(DUH_SIGRENDERER *sigrenderer)
{
return sigrenderer;
}
/* DEPRECATED */
DUH_SIGRENDERER *duh_renderer_get_sigrenderer(DUH_SIGRENDERER *dr)
{
return dr;
}
/* DEPRECATED */
DUH_SIGRENDERER *duh_renderer_decompose_to_sigrenderer(DUH_SIGRENDERER *dr)
{
return dr;
}

344
dumb/src/core/rendsig.c Normal file
View file

@ -0,0 +1,344 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* rendsig.c - Wrappers to render samples from / / \ \
* the signals in a DUH. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include "dumb.h"
#include "internal/dumb.h"
struct DUH_SIGRENDERER
{
DUH_SIGTYPE_DESC *desc;
sigrenderer_t *sigrenderer;
int n_channels;
int32 pos;
int subpos;
DUH_SIGRENDERER_SAMPLE_ANALYSER_CALLBACK callback;
void *callback_data;
};
DUH_SIGRENDERER *DUMBEXPORT duh_start_sigrenderer(DUH *duh, int sig, int n_channels, int32 pos)
{
DUH_SIGRENDERER *sigrenderer;
DUH_SIGNAL *signal;
DUH_START_SIGRENDERER proc;
if (!duh)
return NULL;
if ((unsigned int)sig >= (unsigned int)duh->n_signals)
return NULL;
signal = duh->signal[sig];
if (!signal)
return NULL;
sigrenderer = malloc(sizeof(*sigrenderer));
if (!sigrenderer)
return NULL;
sigrenderer->desc = signal->desc;
proc = sigrenderer->desc->start_sigrenderer;
if (proc) {
duh->signal[sig] = NULL;
sigrenderer->sigrenderer = (*proc)(duh, signal->sigdata, n_channels, pos);
duh->signal[sig] = signal;
if (!sigrenderer->sigrenderer) {
free(sigrenderer);
return NULL;
}
} else
sigrenderer->sigrenderer = NULL;
sigrenderer->n_channels = n_channels;
sigrenderer->pos = pos;
sigrenderer->subpos = 0;
sigrenderer->callback = NULL;
return sigrenderer;
}
#ifdef DUMB_DECLARE_DEPRECATED
#include <stdio.h>
void duh_sigrenderer_set_callback(
DUH_SIGRENDERER *sigrenderer,
DUH_SIGRENDERER_CALLBACK callback, void *data
)
{
(void)sigrenderer;
(void)callback;
(void)data;
/*fprintf(stderr,
"Call to deprecated function duh_sigrenderer_set_callback(). The callback\n"
"was not installed. See dumb/docs/deprec.txt for how to fix this.\n");*/
}
void duh_sigrenderer_set_analyser_callback(
DUH_SIGRENDERER *sigrenderer,
DUH_SIGRENDERER_ANALYSER_CALLBACK callback, void *data
)
{
(void)sigrenderer;
(void)callback;
(void)data;
fprintf(stderr,
"Call to deprecated function duh_sigrenderer_set_analyser_callback(). The\n"
"callback was not installed. See dumb/docs/deprec.txt for how to fix this.\n");
}
#endif
void duh_sigrenderer_set_sample_analyser_callback(
DUH_SIGRENDERER *sigrenderer,
DUH_SIGRENDERER_SAMPLE_ANALYSER_CALLBACK callback, void *data
)
{
if (sigrenderer) {
sigrenderer->callback = callback;
sigrenderer->callback_data = data;
}
}
int DUMBEXPORT duh_sigrenderer_get_n_channels(DUH_SIGRENDERER *sigrenderer)
{
return sigrenderer ? sigrenderer->n_channels : 0;
}
int32 DUMBEXPORT duh_sigrenderer_get_position(DUH_SIGRENDERER *sigrenderer)
{
return sigrenderer ? sigrenderer->pos : -1;
}
void DUMBEXPORT duh_sigrenderer_set_sigparam(
DUH_SIGRENDERER *sigrenderer,
unsigned char id, int32 value
)
{
DUH_SIGRENDERER_SET_SIGPARAM proc;
if (!sigrenderer) return;
proc = sigrenderer->desc->sigrenderer_set_sigparam;
if (proc)
(*proc)(sigrenderer->sigrenderer, id, value);
else
TRACE("Parameter #%d = %d for signal %c%c%c%c, which does not take parameters.\n",
(int)id,
value,
(int)(sigrenderer->desc->type >> 24),
(int)(sigrenderer->desc->type >> 16),
(int)(sigrenderer->desc->type >> 8),
(int)(sigrenderer->desc->type));
}
int32 DUMBEXPORT duh_sigrenderer_generate_samples(
DUH_SIGRENDERER *sigrenderer,
double volume, double delta,
int32 size, sample_t **samples
)
{
int32 rendered;
LONG_LONG t;
if (!sigrenderer) return 0;
rendered = (*sigrenderer->desc->sigrenderer_generate_samples)
(sigrenderer->sigrenderer, volume, delta, size, samples);
if (rendered) {
if (sigrenderer->callback)
(*sigrenderer->callback)(sigrenderer->callback_data,
(const sample_t *const *)samples, sigrenderer->n_channels, rendered);
t = sigrenderer->subpos + (LONG_LONG)(delta * 65536.0 + 0.5) * rendered;
sigrenderer->pos += (int32)(t >> 16);
sigrenderer->subpos = (int)t & 65535;
}
return rendered;
}
/* DEPRECATED */
int32 duh_sigrenderer_get_samples(
DUH_SIGRENDERER *sigrenderer,
float volume, float delta,
int32 size, sample_t **samples
)
{
sample_t **s;
int32 rendered;
int32 i;
int j;
if (!samples) return duh_sigrenderer_generate_samples(sigrenderer, volume, delta, size, NULL);
s = allocate_sample_buffer(sigrenderer->n_channels, size);
if (!s) return 0;
dumb_silence(s[0], sigrenderer->n_channels * size);
rendered = duh_sigrenderer_generate_samples(sigrenderer, volume, delta, size, s);
for (j = 0; j < sigrenderer->n_channels; j++)
for (i = 0; i < rendered; i++)
samples[j][i] += s[0][i*sigrenderer->n_channels+j];
destroy_sample_buffer(s);
return rendered;
}
/* DEPRECATED */
int32 duh_render_signal(
DUH_SIGRENDERER *sigrenderer,
float volume, float delta,
int32 size, sample_t **samples
)
{
sample_t **s;
int32 rendered;
int32 i;
int j;
if (!samples) return duh_sigrenderer_generate_samples(sigrenderer, volume, delta, size, NULL);
s = allocate_sample_buffer(sigrenderer->n_channels, size);
if (!s) return 0;
dumb_silence(s[0], sigrenderer->n_channels * size);
rendered = duh_sigrenderer_generate_samples(sigrenderer, volume, delta, size, s);
for (j = 0; j < sigrenderer->n_channels; j++)
for (i = 0; i < rendered; i++)
samples[j][i] += s[0][i*sigrenderer->n_channels+j] >> 8;
destroy_sample_buffer(s);
return rendered;
}
void DUMBEXPORT duh_sigrenderer_get_current_sample(DUH_SIGRENDERER *sigrenderer, float volume, sample_t *samples)
{
if (sigrenderer)
(*sigrenderer->desc->sigrenderer_get_current_sample)(sigrenderer->sigrenderer, volume, samples);
}
void DUMBEXPORT duh_end_sigrenderer(DUH_SIGRENDERER *sigrenderer)
{
if (sigrenderer) {
if (sigrenderer->desc->end_sigrenderer)
if (sigrenderer->sigrenderer)
(*sigrenderer->desc->end_sigrenderer)(sigrenderer->sigrenderer);
free(sigrenderer);
}
}
DUH_SIGRENDERER *DUMBEXPORT duh_encapsulate_raw_sigrenderer(sigrenderer_t *vsigrenderer, DUH_SIGTYPE_DESC *desc, int n_channels, int32 pos)
{
DUH_SIGRENDERER *sigrenderer;
if (desc->start_sigrenderer && !vsigrenderer) return NULL;
sigrenderer = malloc(sizeof(*sigrenderer));
if (!sigrenderer) {
if (desc->end_sigrenderer)
if (vsigrenderer)
(*desc->end_sigrenderer)(vsigrenderer);
return NULL;
}
sigrenderer->desc = desc;
sigrenderer->sigrenderer = vsigrenderer;
sigrenderer->n_channels = n_channels;
sigrenderer->pos = pos;
sigrenderer->subpos = 0;
sigrenderer->callback = NULL;
return sigrenderer;
}
sigrenderer_t *DUMBEXPORT duh_get_raw_sigrenderer(DUH_SIGRENDERER *sigrenderer, int32 type)
{
if (sigrenderer && sigrenderer->desc->type == type)
return sigrenderer->sigrenderer;
return NULL;
}
#if 0
// This function is disabled because we don't know whether we want to destroy
// the sigrenderer if the type doesn't match. We don't even know if we need
// the function at all. Who would want to keep an IT_SIGRENDERER (for
// instance) without keeping the DUH_SIGRENDERER?
sigrenderer_t *duh_decompose_to_raw_sigrenderer(DUH_SIGRENDERER *sigrenderer, int32 type)
{
if (sigrenderer && sigrenderer->desc->type == type) {
if (sigrenderer) {
if (sigrenderer->desc->end_sigrenderer)
if (sigrenderer->sigrenderer)
(*sigrenderer->desc->end_sigrenderer)(sigrenderer->sigrenderer);
free(sigrenderer);
}
return sigrenderer->sigrenderer;
}
return NULL;
}
#endif

64
dumb/src/core/unload.c Normal file
View file

@ -0,0 +1,64 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* unload.c - Code to free a DUH from memory. / / \ \
* | < / \_
* By entheh. | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include "dumb.h"
#include "internal/dumb.h"
static void destroy_signal(DUH_SIGNAL *signal)
{
if (signal) {
if (signal->desc)
if (signal->desc->unload_sigdata)
if (signal->sigdata)
(*signal->desc->unload_sigdata)(signal->sigdata);
free(signal);
}
}
/* unload_duh(): destroys a DUH struct. You must call this for every DUH
* struct created, when you've finished with it.
*/
void DUMBEXPORT unload_duh(DUH *duh)
{
int i;
if (duh) {
if (duh->signal) {
for (i = 0; i < duh->n_signals; i++)
destroy_signal(duh->signal[i]);
free(duh->signal);
}
if (duh->tag) {
if (duh->tag[0][0])
free(duh->tag[0][0]);
free(duh->tag);
}
free(duh);
}
}

159
dumb/src/helpers/barray.c Normal file
View file

@ -0,0 +1,159 @@
#include "internal/barray.h"
#include <string.h>
void * bit_array_create(size_t size)
{
size_t bsize = ((size + 7) >> 3) + sizeof(size_t);
void * ret = calloc(1, bsize);
if (ret) *(size_t *)ret = size;
return ret;
}
void bit_array_destroy(void * array)
{
if (array) free(array);
}
void * bit_array_dup(void * array)
{
if (array)
{
size_t * size = (size_t *) array;
size_t bsize = ((*size + 7) >> 3) + sizeof(*size);
void * ret = malloc(bsize);
if (ret) memcpy(ret, array, bsize);
return ret;
}
return NULL;
}
void bit_array_reset(void * array)
{
if (array)
{
size_t * size = (size_t *) array;
size_t bsize = (*size + 7) >> 3;
memset(size + 1, 0, bsize);
}
}
void bit_array_set(void * array, size_t bit)
{
if (array)
{
size_t * size = (size_t *) array;
if (bit < *size)
{
unsigned char * ptr = (unsigned char *)(size + 1);
ptr[bit >> 3] |= (1U << (bit & 7));
}
}
}
int bit_array_test(void * array, size_t bit)
{
if (array)
{
size_t * size = (size_t *) array;
if (bit < *size)
{
unsigned char * ptr = (unsigned char *)(size + 1);
if (ptr[bit >> 3] & (1U << (bit & 7)))
{
return 1;
}
}
}
return 0;
}
int bit_array_test_range(void * array, size_t bit, size_t count)
{
if (array)
{
size_t * size = (size_t *) array;
if (bit < *size)
{
unsigned char * ptr = (unsigned char *)(size + 1);
if ((bit & 7) && (count > 8))
{
while ((bit < *size) && count && (bit & 7))
{
if (ptr[bit >> 3] & (1U << (bit & 7))) return 1;
bit++;
count--;
}
}
if (!(bit & 7))
{
while (((*size - bit) >= 8) && (count >= 8))
{
if (ptr[bit >> 3]) return 1;
bit += 8;
count -= 8;
}
}
while ((bit < *size) && count)
{
if (ptr[bit >> 3] & (1U << (bit & 7))) return 1;
bit++;
count--;
}
}
}
return 0;
}
void bit_array_clear(void * array, size_t bit)
{
if (array)
{
size_t * size = (size_t *) array;
if (bit < *size)
{
unsigned char * ptr = (unsigned char *)(size + 1);
ptr[bit >> 3] &= ~(1U << (bit & 7));
}
}
}
void bit_array_merge(void * dest, void * source, size_t offset)
{
if (dest && source)
{
size_t * dsize = (size_t *) dest;
size_t * ssize = (size_t *) source;
size_t soffset = 0;
while (offset < *dsize && soffset < *ssize)
{
if (bit_array_test(source, soffset))
{
bit_array_set(dest, offset);
}
soffset++;
offset++;
}
}
}
void bit_array_mask(void * dest, void * source, size_t offset)
{
if (dest && source)
{
size_t * dsize = (size_t *) dest;
size_t * ssize = (size_t *) source;
size_t soffset = 0;
while (offset < *dsize && soffset < *ssize)
{
if (bit_array_test(source, soffset))
{
bit_array_clear(dest, offset);
}
soffset++;
offset++;
}
}
}

306
dumb/src/helpers/clickrem.c Normal file
View file

@ -0,0 +1,306 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* clickrem.c - Click removal helpers. / / \ \
* | < / \_
* By entheh. | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <math.h>
#include "dumb.h"
typedef struct DUMB_CLICK DUMB_CLICK;
struct DUMB_CLICK_REMOVER
{
DUMB_CLICK *click;
int n_clicks;
int offset;
DUMB_CLICK *free_clicks;
};
struct DUMB_CLICK
{
DUMB_CLICK *next;
int32 pos;
sample_t step;
};
static DUMB_CLICK *alloc_click(DUMB_CLICK_REMOVER *cr)
{
if (cr->free_clicks != NULL)
{
DUMB_CLICK *click = cr->free_clicks;
cr->free_clicks = click->next;
return click;
}
return malloc(sizeof(DUMB_CLICK));
}
static void free_click(DUMB_CLICK_REMOVER *cr, DUMB_CLICK *cl)
{
cl->next = cr->free_clicks;
cr->free_clicks = cl;
}
DUMB_CLICK_REMOVER *DUMBEXPORT dumb_create_click_remover(void)
{
DUMB_CLICK_REMOVER *cr = malloc(sizeof(*cr));
if (!cr) return NULL;
cr->click = NULL;
cr->n_clicks = 0;
cr->offset = 0;
cr->free_clicks = NULL;
return cr;
}
void DUMBEXPORT dumb_record_click(DUMB_CLICK_REMOVER *cr, int32 pos, sample_t step)
{
DUMB_CLICK *click;
ASSERT(pos >= 0);
if (!cr || !step) return;
if (pos == 0) {
cr->offset -= step;
return;
}
click = alloc_click(cr);
if (!click) return;
click->pos = pos;
click->step = step;
click->next = cr->click;
cr->click = click;
cr->n_clicks++;
}
static DUMB_CLICK *dumb_click_mergesort(DUMB_CLICK *click, int n_clicks)
{
int i;
DUMB_CLICK *c1, *c2, **cp;
if (n_clicks <= 1) return click;
/* Split the list into two */
c1 = click;
cp = &c1;
for (i = 0; i < n_clicks; i += 2) cp = &(*cp)->next;
c2 = *cp;
*cp = NULL;
/* Sort the sublists */
c1 = dumb_click_mergesort(c1, (n_clicks + 1) >> 1);
c2 = dumb_click_mergesort(c2, n_clicks >> 1);
/* Merge them */
cp = &click;
while (c1 && c2) {
if (c1->pos > c2->pos) {
*cp = c2;
c2 = c2->next;
} else {
*cp = c1;
c1 = c1->next;
}
cp = &(*cp)->next;
}
if (c2)
*cp = c2;
else
*cp = c1;
return click;
}
void DUMBEXPORT dumb_remove_clicks(DUMB_CLICK_REMOVER *cr, sample_t *samples, int32 length, int step, double halflife)
{
DUMB_CLICK *click;
int32 pos = 0;
int offset;
int factor;
if (!cr) return;
factor = (int)floor(pow(0.5, 1.0/halflife) * (1U << 31));
click = dumb_click_mergesort(cr->click, cr->n_clicks);
cr->click = NULL;
cr->n_clicks = 0;
length *= step;
while (click) {
DUMB_CLICK *next = click->next;
int end = click->pos * step;
ASSERT(end <= length);
offset = cr->offset;
if (offset < 0) {
offset = -offset;
while (pos < end) {
samples[pos] -= offset;
offset = (int)(((LONG_LONG)(offset << 1) * factor) >> 32);
pos += step;
}
offset = -offset;
} else {
while (pos < end) {
samples[pos] += offset;
offset = (int)(((LONG_LONG)(offset << 1) * factor) >> 32);
pos += step;
}
}
cr->offset = offset - click->step;
free_click(cr, click);
click = next;
}
offset = cr->offset;
if (offset < 0) {
offset = -offset;
while (pos < length) {
samples[pos] -= offset;
offset = (int)((LONG_LONG)(offset << 1) * factor >> 32);
pos += step;
}
offset = -offset;
} else {
while (pos < length) {
samples[pos] += offset;
offset = (int)((LONG_LONG)(offset << 1) * factor >> 32);
pos += step;
}
}
cr->offset = offset;
}
sample_t DUMBEXPORT dumb_click_remover_get_offset(DUMB_CLICK_REMOVER *cr)
{
return cr ? cr->offset : 0;
}
void DUMBEXPORT dumb_destroy_click_remover(DUMB_CLICK_REMOVER *cr)
{
if (cr) {
DUMB_CLICK *click = cr->click;
while (click) {
DUMB_CLICK *next = click->next;
free(click);
click = next;
}
click = cr->free_clicks;
while (click) {
DUMB_CLICK *next = click->next;
free(click);
click = next;
}
free(cr);
}
}
DUMB_CLICK_REMOVER **DUMBEXPORT dumb_create_click_remover_array(int n)
{
int i;
DUMB_CLICK_REMOVER **cr;
if (n <= 0) return NULL;
cr = malloc(n * sizeof(*cr));
if (!cr) return NULL;
for (i = 0; i < n; i++) cr[i] = dumb_create_click_remover();
return cr;
}
void DUMBEXPORT dumb_record_click_array(int n, DUMB_CLICK_REMOVER **cr, int32 pos, sample_t *step)
{
if (cr) {
int i;
for (i = 0; i < n; i++)
dumb_record_click(cr[i], pos, step[i]);
}
}
void DUMBEXPORT dumb_record_click_negative_array(int n, DUMB_CLICK_REMOVER **cr, int32 pos, sample_t *step)
{
if (cr) {
int i;
for (i = 0; i < n; i++)
dumb_record_click(cr[i], pos, -step[i]);
}
}
void DUMBEXPORT dumb_remove_clicks_array(int n, DUMB_CLICK_REMOVER **cr, sample_t **samples, int32 length, double halflife)
{
if (cr) {
int i;
for (i = 0; i < n >> 1; i++) {
dumb_remove_clicks(cr[i << 1], samples[i], length, 2, halflife);
dumb_remove_clicks(cr[(i << 1) + 1], samples[i] + 1, length, 2, halflife);
}
if (n & 1)
dumb_remove_clicks(cr[i << 1], samples[i], length, 1, halflife);
}
}
void DUMBEXPORT dumb_click_remover_get_offset_array(int n, DUMB_CLICK_REMOVER **cr, sample_t *offset)
{
if (cr) {
int i;
for (i = 0; i < n; i++)
if (cr[i]) offset[i] += cr[i]->offset;
}
}
void DUMBEXPORT dumb_destroy_click_remover_array(int n, DUMB_CLICK_REMOVER **cr)
{
if (cr) {
int i;
for (i = 0; i < n; i++) dumb_destroy_click_remover(cr[i]);
free(cr);
}
}

View file

@ -0,0 +1,96 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* memfile.c - Module for reading data from / / \ \
* memory using a DUMBFILE. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <string.h>
#include "dumb.h"
typedef struct MEMFILE MEMFILE;
struct MEMFILE
{
const char *ptr;
int32 left;
};
static int dumb_memfile_skip(void *f, int32 n)
{
MEMFILE *m = f;
if (n > m->left) return -1;
m->ptr += n;
m->left -= n;
return 0;
}
static int dumb_memfile_getc(void *f)
{
MEMFILE *m = f;
if (m->left <= 0) return -1;
m->left--;
return *(const unsigned char *)m->ptr++;
}
static int32 dumb_memfile_getnc(char *ptr, int32 n, void *f)
{
MEMFILE *m = f;
if (n > m->left) n = m->left;
memcpy(ptr, m->ptr, n);
m->ptr += n;
m->left -= n;
return n;
}
static void dumb_memfile_close(void *f)
{
free(f);
}
static const DUMBFILE_SYSTEM memfile_dfs = {
NULL,
&dumb_memfile_skip,
&dumb_memfile_getc,
&dumb_memfile_getnc,
&dumb_memfile_close
};
DUMBFILE *DUMBEXPORT dumbfile_open_memory(const char *data, int32 size)
{
MEMFILE *m = malloc(sizeof(*m));
if (!m) return NULL;
m->ptr = data;
m->left = size;
return dumbfile_open_ex(m, &memfile_dfs);
}

View file

@ -0,0 +1,160 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* resamp2.inc - Resampling helper template. / / \ \
* | < / \_
* By Bob and entheh. | \/ /\ /
* \_ / > /
* In order to find a good trade-off between | \ / /
* speed and accuracy in this code, some tests | ' /
* were carried out regarding the behaviour of \__/
* long long ints with gcc. The following code
* was tested:
*
* int a, b, c;
* c = ((long long)a * b) >> 16;
*
* DJGPP GCC Version 3.0.3 generated the following assembly language code for
* the multiplication and scaling, leaving the 32-bit result in EAX.
*
* movl -8(%ebp), %eax ; read one int into EAX
* imull -4(%ebp) ; multiply by the other; result goes in EDX:EAX
* shrdl $16, %edx, %eax ; shift EAX right 16, shifting bits in from EDX
*
* Note that a 32*32->64 multiplication is performed, allowing for high
* accuracy. On the Pentium 2 and above, shrdl takes two cycles (generally),
* so it is a minor concern when four multiplications are being performed
* (the cubic resampler). On the Pentium MMX and earlier, it takes four or
* more cycles, so this method is unsuitable for use in the low-quality
* resamplers.
*
* Since "long long" is a gcc-specific extension, we use LONG_LONG instead,
* defined in dumb.h. We may investigate later what code MSVC generates, but
* if it seems too slow then we suggest you use a good compiler.
*
* FIXME: these comments are somewhat out of date now.
*/
#define SUFFIX3 _1
/* For convenience, returns nonzero on stop. */
static int process_pickup(DUMB_RESAMPLER *resampler)
{
if (resampler->overshot < 0) {
resampler->overshot = 0;
dumb_resample(resampler, NULL, 2, MONO_DEST_VOLUME_ZEROS, 1.0f); /* Doesn't matter which SUFFIX3. */
COPYSRC(resampler->X, 0, resampler->X, 1);
}
for (;;) {
SRCTYPE *src = resampler->src;
if (resampler->dir < 0) {
if (resampler->overshot >= 3 && resampler->pos+3 >= resampler->start) COPYSRC(resampler->X, 0, src, resampler->pos+3);
if (resampler->overshot >= 2 && resampler->pos+2 >= resampler->start) COPYSRC(resampler->X, 1, src, resampler->pos+2);
if (resampler->overshot >= 1 && resampler->pos+1 >= resampler->start) COPYSRC(resampler->X, 2, src, resampler->pos+1);
resampler->overshot = resampler->start - resampler->pos - 1;
} else {
if (resampler->overshot >= 3 && resampler->pos-3 < resampler->end) COPYSRC(resampler->X, 0, src, resampler->pos-3);
if (resampler->overshot >= 2 && resampler->pos-2 < resampler->end) COPYSRC(resampler->X, 1, src, resampler->pos-2);
if (resampler->overshot >= 1 && resampler->pos-1 < resampler->end) COPYSRC(resampler->X, 2, src, resampler->pos-1);
resampler->overshot = resampler->pos - resampler->end;
}
if (resampler->overshot < 0) {
resampler->overshot = 0;
return 0;
}
if (!resampler->pickup) {
resampler->dir = 0;
return 1;
}
(*resampler->pickup)(resampler, resampler->pickup_data);
if (resampler->dir == 0) return 1;
ASSERT(resampler->dir == -1 || resampler->dir == 1);
}
}
/* Create mono destination resampler. */
/* SUFFIX3 was set above. */
#define VOLUME_PARAMETERS MONO_DEST_VOLUME_PARAMETERS
#define VOLUME_VARIABLES MONO_DEST_VOLUME_VARIABLES
#define SET_VOLUME_VARIABLES SET_MONO_DEST_VOLUME_VARIABLES
#define RETURN_VOLUME_VARIABLES RETURN_MONO_DEST_VOLUME_VARIABLES
#define VOLUMES_ARE_ZERO MONO_DEST_VOLUMES_ARE_ZERO
#define MIX_ALIAS(op, upd, offset) MONO_DEST_MIX_ALIAS(op, upd, offset)
#define MIX_LINEAR(op, upd, o0, o1) MONO_DEST_MIX_LINEAR(op, upd, o0, o1)
#define MIX_CUBIC(op, upd, x0, x3, o0, o1, o2, o3) MONO_DEST_MIX_CUBIC(op, upd, x0, x3, o0, o1, o2, o3)
#define MIX_ZEROS(op) *dst++ op 0
#include "resamp3.inc"
/* Create stereo destination resampler. */
#define SUFFIX3 _2
#define VOLUME_PARAMETERS DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right
#define VOLUME_VARIABLES lvol, lvolr, lvold, lvolt, lvolm, rvol, rvolr, rvold, rvolt, rvolm
#define SET_VOLUME_VARIABLES { \
if ( volume_left ) { \
lvolr = (int)(volume_left->volume * 16777216.0); \
lvold = (int)(volume_left->delta * 16777216.0); \
lvolt = (int)(volume_left->target * 16777216.0); \
lvolm = (int)(volume_left->mix * 16777216.0); \
lvol = MULSCV( lvolr, lvolm ); \
if ( lvolr == lvolt ) volume_left = NULL; \
} else { \
lvol = 0; \
lvolt = 0; \
} \
if ( volume_right ) { \
rvolr = (int)(volume_right->volume * 16777216.0); \
rvold = (int)(volume_right->delta * 16777216.0); \
rvolt = (int)(volume_right->target * 16777216.0); \
rvolm = (int)(volume_right->mix * 16777216.0); \
rvol = MULSCV( rvolr, rvolm ); \
if ( rvolr == rvolt ) volume_right = NULL; \
} else { \
rvol = 0; \
rvolt = 0; \
} \
}
#define RETURN_VOLUME_VARIABLES { \
if ( volume_left ) volume_left->volume = (float)lvolr / 16777216.0f; \
if ( volume_right ) volume_right->volume = (float)rvolr / 16777216.0f; \
}
#define VOLUMES_ARE_ZERO (lvol == 0 && lvolt == 0 && rvol == 0 && rvolt == 0)
#define MIX_ALIAS(op, upd, offset) STEREO_DEST_MIX_ALIAS(op, upd, offset)
#define MIX_LINEAR(op, upd, o0, o1) STEREO_DEST_MIX_LINEAR(op, upd, o0, o1)
#define MIX_CUBIC(op, upd, x0, x3, o0, o1, o2, o3) STEREO_DEST_MIX_CUBIC(op, upd, x0, x3, o0, o1, o2, o3)
#define MIX_ZEROS(op) { *dst++ op 0; *dst++ op 0; }
#include "resamp3.inc"
#undef STEREO_DEST_MIX_CUBIC
#undef MONO_DEST_MIX_CUBIC
#undef STEREO_DEST_MIX_LINEAR
#undef MONO_DEST_MIX_LINEAR
#undef STEREO_DEST_MIX_ALIAS
#undef MONO_DEST_MIX_ALIAS
#undef MONO_DEST_VOLUMES_ARE_ZERO
#undef SET_MONO_DEST_VOLUME_VARIABLES
#undef RETURN_MONO_DEST_VOLUME_VARIABLES
#undef MONO_DEST_VOLUME_ZEROS
#undef MONO_DEST_VOLUME_VARIABLES
#undef MONO_DEST_VOLUME_PARAMETERS
#undef COPYSRC2
#undef COPYSRC
#undef DIVIDE_BY_SRC_CHANNELS
#undef SRC_CHANNELS
#undef SUFFIX2

View file

@ -0,0 +1,376 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* resamp3.inc - Resampling helper template. / / \ \
* | < / \_
* By Bob and entheh. | \/ /\ /
* \_ / > /
* In order to find a good trade-off between | \ / /
* speed and accuracy in this code, some tests | ' /
* were carried out regarding the behaviour of \__/
* long long ints with gcc. The following code
* was tested:
*
* int a, b, c;
* c = ((long long)a * b) >> 16;
*
* DJGPP GCC Version 3.0.3 generated the following assembly language code for
* the multiplication and scaling, leaving the 32-bit result in EAX.
*
* movl -8(%ebp), %eax ; read one int into EAX
* imull -4(%ebp) ; multiply by the other; result goes in EDX:EAX
* shrdl $16, %edx, %eax ; shift EAX right 16, shifting bits in from EDX
*
* Note that a 32*32->64 multiplication is performed, allowing for high
* accuracy. On the Pentium 2 and above, shrdl takes two cycles (generally),
* so it is a minor concern when four multiplications are being performed
* (the cubic resampler). On the Pentium MMX and earlier, it takes four or
* more cycles, so this method is unsuitable for use in the low-quality
* resamplers.
*
* Since "long long" is a gcc-specific extension, we use LONG_LONG instead,
* defined in dumb.h. We may investigate later what code MSVC generates, but
* if it seems too slow then we suggest you use a good compiler.
*
* FIXME: these comments are somewhat out of date now.
*/
int32 dumb_resample(DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, VOLUME_PARAMETERS, double delta)
{
int dt;
int VOLUME_VARIABLES;
long done;
long todo;
LONG_LONG todo64;
int quality;
if (!resampler || resampler->dir == 0) return 0;
ASSERT(resampler->dir == -1 || resampler->dir == 1);
done = 0;
dt = (int)(delta * 65536.0 + 0.5);
if (dt == 0) return 0;
SET_VOLUME_VARIABLES;
if (VOLUMES_ARE_ZERO) dst = NULL;
init_cubic();
quality = resampler->quality;
while (done < dst_size) {
if (process_pickup(resampler)) {
RETURN_VOLUME_VARIABLES;
return done;
}
if ((resampler->dir ^ dt) < 0)
dt = -dt;
if (resampler->dir < 0)
todo64 = ((((LONG_LONG)(resampler->pos - resampler->start) << 16) + resampler->subpos - dt) / -dt);
else
todo64 = ((((LONG_LONG)(resampler->end - resampler->pos) << 16) - resampler->subpos - 1 + dt) / dt);
if (todo64 < 0)
todo = 0;
else if (todo64 > dst_size - done)
todo = dst_size - done;
else
todo = (long) todo64;
done += todo;
{
SRCTYPE *src = resampler->src;
long pos = resampler->pos;
int subpos = resampler->subpos;
long diff = pos;
long overshot;
if (resampler->dir < 0) {
if (!dst) {
/* Silence or simulation */
LONG_LONG new_subpos = subpos + (LONG_LONG)dt * todo;
pos += (long)(new_subpos >> 16);
subpos = (long)new_subpos & 65535;
} else if (quality <= DUMB_RQ_ALIASING) {
/* Aliasing, backwards */
SRCTYPE xbuf[2*SRC_CHANNELS];
SRCTYPE *x = &xbuf[0];
SRCTYPE *xstart;
COPYSRC(xbuf, 0, resampler->X, 1);
COPYSRC(xbuf, 1, resampler->X, 2);
while (todo && x < &xbuf[2*SRC_CHANNELS]) {
// TODO: check what happens when multiple tempo slides occur per row
HEAVYASSERT(pos >= resampler->start);
MIX_ALIAS(+=, 1, 0);
subpos += dt;
pos += subpos >> 16;
x -= (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
todo--;
}
x = xstart = &src[pos*SRC_CHANNELS];
LOOP4(todo,
MIX_ALIAS(+=, 1, 2);
subpos += dt;
x += (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
);
pos += DIVIDE_BY_SRC_CHANNELS((long)(x - xstart));
} else if (quality <= DUMB_RQ_LINEAR) {
/* Linear interpolation, backwards */
SRCTYPE xbuf[3*SRC_CHANNELS];
SRCTYPE *x = &xbuf[1*SRC_CHANNELS];
COPYSRC(xbuf, 0, resampler->X, 1);
COPYSRC(xbuf, 1, resampler->X, 2);
COPYSRC(xbuf, 2, src, pos);
while (todo && x < &xbuf[3*SRC_CHANNELS]) {
HEAVYASSERT(pos >= resampler->start);
MIX_LINEAR(+=, 1, 0, -1);
subpos += dt;
pos += subpos >> 16;
x -= (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
todo--;
}
// TODO: use xstart for others too
x = &src[pos*SRC_CHANNELS];
LOOP4(todo,
HEAVYASSERT(pos >= resampler->start);
MIX_LINEAR(+=, 1, 1, 2);
subpos += dt;
pos += subpos >> 16;
x += (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
);
} else {
/* Cubic interpolation, backwards */
SRCTYPE xbuf[6*SRC_CHANNELS];
SRCTYPE *x = &xbuf[3*SRC_CHANNELS];
COPYSRC(xbuf, 0, resampler->X, 0);
COPYSRC(xbuf, 1, resampler->X, 1);
COPYSRC(xbuf, 2, resampler->X, 2);
COPYSRC(xbuf, 3, src, pos);
if (pos-1 >= resampler->start) COPYSRC(xbuf, 4, src, pos-1);
if (pos-2 >= resampler->start) COPYSRC(xbuf, 5, src, pos-2);
while (todo && x < &xbuf[6*SRC_CHANNELS]) {
HEAVYASSERT(pos >= resampler->start);
MIX_CUBIC(+=, 1, x, x, 0, -1, -2, -3);
subpos += dt;
pos += subpos >> 16;
x -= (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
todo--;
}
x = &src[pos*SRC_CHANNELS];
LOOP4(todo,
HEAVYASSERT(pos >= resampler->start);
MIX_CUBIC(+=, 1, x, x, 0, 1, 2, 3);
subpos += dt;
pos += subpos >> 16;
x += (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
);
}
diff = diff - pos;
overshot = resampler->start - pos - 1;
if (diff >= 3) {
COPYSRC2(resampler->X, 0, overshot < 3, src, pos+3);
COPYSRC2(resampler->X, 1, overshot < 2, src, pos+2);
COPYSRC2(resampler->X, 2, overshot < 1, src, pos+1);
} else if (diff >= 2) {
COPYSRC(resampler->X, 0, resampler->X, 2);
COPYSRC2(resampler->X, 1, overshot < 2, src, pos+2);
COPYSRC2(resampler->X, 2, overshot < 1, src, pos+1);
} else if (diff >= 1) {
COPYSRC(resampler->X, 0, resampler->X, 1);
COPYSRC(resampler->X, 1, resampler->X, 2);
COPYSRC2(resampler->X, 2, overshot < 1, src, pos+1);
}
} else {
if (!dst) {
/* Silence or simulation */
LONG_LONG new_subpos = subpos + (LONG_LONG)dt * todo;
pos += (long)(new_subpos >> 16);
subpos = (long)new_subpos & 65535;
} else if (quality <= DUMB_RQ_ALIASING) {
/* Aliasing, forwards */
SRCTYPE xbuf[2*SRC_CHANNELS];
SRCTYPE *x = &xbuf[0];
SRCTYPE *xstart;
COPYSRC(xbuf, 0, resampler->X, 1);
COPYSRC(xbuf, 1, resampler->X, 2);
while (todo && x < &xbuf[2*SRC_CHANNELS]) {
HEAVYASSERT(pos < resampler->end);
MIX_ALIAS(+=, 1, 0);
subpos += dt;
pos += subpos >> 16;
x += (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
todo--;
}
x = xstart = &src[pos*SRC_CHANNELS];
LOOP4(todo,
MIX_ALIAS(+=, 1, -2);
subpos += dt;
x += (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
);
pos += DIVIDE_BY_SRC_CHANNELS((long)(x - xstart));
} else if (quality <= DUMB_RQ_LINEAR) {
/* Linear interpolation, forwards */
SRCTYPE xbuf[3*SRC_CHANNELS];
SRCTYPE *x = &xbuf[1*SRC_CHANNELS];
COPYSRC(xbuf, 0, resampler->X, 1);
COPYSRC(xbuf, 1, resampler->X, 2);
COPYSRC(xbuf, 2, src, pos);
while (todo && x < &xbuf[3*SRC_CHANNELS]) {
HEAVYASSERT(pos < resampler->end);
MIX_LINEAR(+=, 1, -1, 0);
subpos += dt;
pos += subpos >> 16;
x += (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
todo--;
}
x = &src[pos*SRC_CHANNELS];
LOOP4(todo,
HEAVYASSERT(pos < resampler->end);
MIX_LINEAR(+=, 1, -2, -1);
subpos += dt;
pos += subpos >> 16;
x += (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
);
} else {
/* Cubic interpolation, forwards */
SRCTYPE xbuf[6*SRC_CHANNELS];
SRCTYPE *x = &xbuf[3*SRC_CHANNELS];
COPYSRC(xbuf, 0, resampler->X, 0);
COPYSRC(xbuf, 1, resampler->X, 1);
COPYSRC(xbuf, 2, resampler->X, 2);
COPYSRC(xbuf, 3, src, pos);
if (pos+1 < resampler->end) COPYSRC(xbuf, 4, src, pos+1);
if (pos+2 < resampler->end) COPYSRC(xbuf, 5, src, pos+2);
while (todo && x < &xbuf[6*SRC_CHANNELS]) {
HEAVYASSERT(pos < resampler->end);
MIX_CUBIC(+=, 1, x, x, -3, -2, -1, 0);
subpos += dt;
pos += subpos >> 16;
x += (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
todo--;
}
x = &src[pos*SRC_CHANNELS];
LOOP4(todo,
HEAVYASSERT(pos < resampler->end);
MIX_CUBIC(+=, 1, x, x, -3, -2, -1, 0);
subpos += dt;
pos += subpos >> 16;
x += (subpos >> 16) * SRC_CHANNELS;
subpos &= 65535;
);
}
diff = pos - diff;
overshot = pos - resampler->end;
if (diff >= 3) {
COPYSRC2(resampler->X, 0, overshot < 3, src, pos-3);
COPYSRC2(resampler->X, 1, overshot < 2, src, pos-2);
COPYSRC2(resampler->X, 2, overshot < 1, src, pos-1);
} else if (diff >= 2) {
COPYSRC(resampler->X, 0, resampler->X, 2);
COPYSRC2(resampler->X, 1, overshot < 2, src, pos-2);
COPYSRC2(resampler->X, 2, overshot < 1, src, pos-1);
} else if (diff >= 1) {
COPYSRC(resampler->X, 0, resampler->X, 1);
COPYSRC(resampler->X, 1, resampler->X, 2);
COPYSRC2(resampler->X, 2, overshot < 1, src, pos-1);
}
}
resampler->pos = pos;
resampler->subpos = subpos;
}
}
RETURN_VOLUME_VARIABLES;
return done;
}
void dumb_resample_get_current_sample(DUMB_RESAMPLER *resampler, VOLUME_PARAMETERS, sample_t *dst)
{
int VOLUME_VARIABLES;
SRCTYPE *src;
long pos;
int subpos;
int quality;
SRCTYPE *x;
if (!resampler || resampler->dir == 0) { MIX_ZEROS(=); return; }
ASSERT(resampler->dir == -1 || resampler->dir == 1);
if (process_pickup(resampler)) { MIX_ZEROS(=); return; }
SET_VOLUME_VARIABLES;
if (VOLUMES_ARE_ZERO) { MIX_ZEROS(=); return; }
init_cubic();
quality = resampler->quality;
src = resampler->src;
pos = resampler->pos;
subpos = resampler->subpos;
x = resampler->X;
if (resampler->dir < 0) {
HEAVYASSERT(pos >= resampler->start);
if (dumb_resampling_quality <= DUMB_RQ_ALIASING) {
/* Aliasing, backwards */
MIX_ALIAS(=, 0, 1);
} else if (quality <= DUMB_RQ_LINEAR) {
/* Linear interpolation, backwards */
MIX_LINEAR(=, 0, 2, 1);
} else {
/* Cubic interpolation, backwards */
MIX_CUBIC(=, 0, src, x, pos, 2, 1, 0);
}
} else {
HEAVYASSERT(pos < resampler->end);
if (dumb_resampling_quality <= DUMB_RQ_ALIASING) {
/* Aliasing */
MIX_ALIAS(=, 0, 1);
} else if (dumb_resampling_quality <= DUMB_RQ_LINEAR) {
/* Linear interpolation, forwards */
MIX_LINEAR(=, 0, 1, 2);
} else {
/* Cubic interpolation, forwards */
MIX_CUBIC(=, 0, x, src, 0, 1, 2, pos);
}
}
}
#undef MIX_ZEROS
#undef MIX_CUBIC
#undef MIX_LINEAR
#undef MIX_ALIAS
#undef VOLUMES_ARE_ZERO
#undef SET_VOLUME_VARIABLES
#undef RETURN_VOLUME_VARIABLES
#undef VOLUME_VARIABLES
#undef VOLUME_PARAMETERS
#undef SUFFIX3

406
dumb/src/helpers/resample.c Normal file
View file

@ -0,0 +1,406 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* resample.c - Resampling helpers. / / \ \
* | < / \_
* By Bob and entheh. | \/ /\ /
* \_ / > /
* In order to find a good trade-off between | \ / /
* speed and accuracy in this code, some tests | ' /
* were carried out regarding the behaviour of \__/
* int32 int32 ints with gcc. The following code
* was tested:
*
* int a, b, c;
* c = ((int32 int32)a * b) >> 16;
*
* DJGPP GCC Version 3.0.3 generated the following assembly language code for
* the multiplication and scaling, leaving the 32-bit result in EAX.
*
* movl -8(%ebp), %eax ; read one int into EAX
* imull -4(%ebp) ; multiply by the other; result goes in EDX:EAX
* shrdl $16, %edx, %eax ; shift EAX right 16, shifting bits in from EDX
*
* Note that a 32*32->64 multiplication is performed, allowing for high
* accuracy. On the Pentium 2 and above, shrdl takes two cycles (generally),
* so it is a minor concern when four multiplications are being performed
* (the cubic resampler). On the Pentium MMX and earlier, it takes four or
* more cycles, so this method is unsuitable for use in the low-quality
* resamplers.
*
* Since "int32 int32" is a gcc-specific extension, we use LONG_LONG instead,
* defined in dumb.h. We may investigate later what code MSVC generates, but
* if it seems too slow then we suggest you use a good compiler.
*
* FIXME: these comments are somewhat out of date now.
*/
#include <math.h>
#include "dumb.h"
/* Compile with -DHEAVYDEBUG if you want to make sure the pick-up function is
* called when it should be. There will be a considerable performance hit,
* since at least one condition has to be tested for every sample generated.
*/
#ifdef HEAVYDEBUG
#define HEAVYASSERT(cond) ASSERT(cond)
#else
#define HEAVYASSERT(cond)
#endif
/* Make MSVC shut the hell up about if ( upd ) UPDATE_VOLUME() conditions being constant */
#ifdef _MSC_VER
#pragma warning(disable:4127 4701)
#endif
/* A global variable for controlling resampling quality wherever a local
* specification doesn't override it. The following values are valid:
*
* 0 - DUMB_RQ_ALIASING - fastest
* 1 - DUMB_RQ_LINEAR
* 2 - DUMB_RQ_CUBIC - nicest
*
* Values outside the range 0-2 will behave the same as the nearest
* value within the range.
*/
int dumb_resampling_quality = DUMB_RQ_CUBIC;
//#define MULSC(a, b) ((int)((LONG_LONG)(a) * (b) >> 16))
//#define MULSC(a, b) ((a) * ((b) >> 2) >> 14)
#define MULSCV(a, b) ((int)((LONG_LONG)(a) * (b) >> 32))
#define MULSC(a, b) ((int)((LONG_LONG)((a) << 4) * ((b) << 12) >> 32))
#define MULSC16(a, b) ((int)((LONG_LONG)((a) << 12) * ((b) << 12) >> 32))
/* Executes the content 'iterator' times.
* Clobbers the 'iterator' variable.
* The loop is unrolled by four.
*/
#if 0
#define LOOP4(iterator, CONTENT) \
{ \
if ((iterator) & 2) { \
CONTENT; \
CONTENT; \
} \
if ((iterator) & 1) { \
CONTENT; \
} \
(iterator) >>= 2; \
while (iterator) { \
CONTENT; \
CONTENT; \
CONTENT; \
CONTENT; \
(iterator)--; \
} \
}
#else
/* [RH] Unrolling this makes the object code ~2.5x larger with
* marginal, if any, improvement in performance.
*/
#define LOOP4(iterator, CONTENT) \
{ \
while (iterator) { \
CONTENT; \
(iterator)--; \
} \
}
#endif
#define PASTERAW(a, b) a ## b /* This does not expand macros in b ... */
#define PASTE(a, b) PASTERAW(a, b) /* ... but b is expanded during this substitution. */
#define X PASTE(x.x, SRCBITS)
/* Cubic resampler: look-up tables
*
* a = 1.5*x1 - 1.5*x2 + 0.5*x3 - 0.5*x0
* b = 2*x2 + x0 - 2.5*x1 - 0.5*x3
* c = 0.5*x2 - 0.5*x0
* d = x1
*
* x = a*t*t*t + b*t*t + c*t + d
* = (-0.5*x0 + 1.5*x1 - 1.5*x2 + 0.5*x3) * t*t*t +
* ( 1*x0 - 2.5*x1 + 2 *x2 - 0.5*x3) * t*t +
* (-0.5*x0 + 0.5*x2 ) * t +
* ( 1*x1 )
* = (-0.5*t*t*t + 1 *t*t - 0.5*t ) * x0 +
* ( 1.5*t*t*t - 2.5*t*t + 1) * x1 +
* (-1.5*t*t*t + 2 *t*t + 0.5*t ) * x2 +
* ( 0.5*t*t*t - 0.5*t*t ) * x3
* = A0(t) * x0 + A1(t) * x1 + A2(t) * x2 + A3(t) * x3
*
* A0, A1, A2 and A3 stay within the range [-1,1].
* In the tables, they are scaled with 14 fractional bits.
*
* Turns out we don't need to store A2 and A3; they are symmetrical to A1 and A0.
*
* TODO: A0 and A3 stay very small indeed. Consider different scale/resolution?
*/
static short cubicA0[1025], cubicA1[1025];
static void init_cubic(void)
{
unsigned int t; /* 3*1024*1024*1024 is within range if it's unsigned */
static int done = 0;
if (done) return;
done = 1;
for (t = 0; t < 1025; t++) {
/* int casts to pacify warnings about negating unsigned values */
cubicA0[t] = -(int)( t*t*t >> 17) + (int)( t*t >> 6) - (int)(t << 3);
cubicA1[t] = (int)(3*t*t*t >> 17) - (int)(5*t*t >> 7) + (int)(1 << 14);
}
}
/* Create resamplers for 24-in-32-bit source samples. */
/* #define SUFFIX
* MSVC warns if we try to paste a null SUFFIX, so instead we define
* special macros for the function names that don't bother doing the
* corresponding paste. The more generic definitions are further down.
*/
#define process_pickup PASTE(process_pickup, SUFFIX2)
#define dumb_resample PASTE(PASTE(dumb_resample, SUFFIX2), SUFFIX3)
#define dumb_resample_get_current_sample PASTE(PASTE(dumb_resample_get_current_sample, SUFFIX2), SUFFIX3)
#define SRCTYPE sample_t
#define SRCBITS 24
#define ALIAS(x, vol) MULSC(x, vol)
#define LINEAR(x0, x1) (x0 + MULSC(x1 - x0, subpos))
/*
#define SET_CUBIC_COEFFICIENTS(x0, x1, x2, x3) { \
a = (3 * (x1 - x2) + (x3 - x0)) >> 1; \
b = ((x2 << 2) + (x0 << 1) - (5 * x1 + x3)) >> 1; \
c = (x2 - x0) >> 1; \
}
#define CUBIC(d) MULSC(MULSC(MULSC(MULSC(a, subpos) + b, subpos) + c, subpos) + d, vol)
*/
#define CUBIC(x0, x1, x2, x3) ( \
MULSC(x0, cubicA0[subpos >> 6] << 2) + \
MULSC(x1, cubicA1[subpos >> 6] << 2) + \
MULSC(x2, cubicA1[1 + (subpos >> 6 ^ 1023)] << 2) + \
MULSC(x3, cubicA0[1 + (subpos >> 6 ^ 1023)] << 2))
#define CUBICVOL(x, vol) MULSC(x, vol)
#include "resample.inc"
/* Undefine the simplified macros. */
#undef dumb_resample_get_current_sample
#undef dumb_resample
#undef process_pickup
/* Now define the proper ones that use SUFFIX. */
#define dumb_reset_resampler PASTE(dumb_reset_resampler, SUFFIX)
#define dumb_start_resampler PASTE(dumb_start_resampler, SUFFIX)
#define process_pickup PASTE(PASTE(process_pickup, SUFFIX), SUFFIX2)
#define dumb_resample PASTE(PASTE(PASTE(dumb_resample, SUFFIX), SUFFIX2), SUFFIX3)
#define dumb_resample_get_current_sample PASTE(PASTE(PASTE(dumb_resample_get_current_sample, SUFFIX), SUFFIX2), SUFFIX3)
#define dumb_end_resampler PASTE(dumb_end_resampler, SUFFIX)
/* Create resamplers for 16-bit source samples. */
#define SUFFIX _16
#define SRCTYPE short
#define SRCBITS 16
#define ALIAS(x, vol) (x * vol >> 8)
#define LINEAR(x0, x1) ((x0 << 8) + MULSC16(x1 - x0, subpos))
/*
#define SET_CUBIC_COEFFICIENTS(x0, x1, x2, x3) { \
a = (3 * (x1 - x2) + (x3 - x0)) << 7; \
b = ((x2 << 2) + (x0 << 1) - (5 * x1 + x3)) << 7; \
c = (x2 - x0) << 7; \
}
#define CUBIC(d) MULSC(MULSC(MULSC(MULSC(a, subpos) + b, subpos) + c, subpos) + (d << 8), vol)
*/
#define CUBIC(x0, x1, x2, x3) ( \
x0 * cubicA0[subpos >> 6] + \
x1 * cubicA1[subpos >> 6] + \
x2 * cubicA1[1 + (subpos >> 6 ^ 1023)] + \
x3 * cubicA0[1 + (subpos >> 6 ^ 1023)])
#define CUBICVOL(x, vol) (int)((LONG_LONG)(x) * (vol << 10) >> 32)
#include "resample.inc"
/* Create resamplers for 8-bit source samples. */
#define SUFFIX _8
#define SRCTYPE signed char
#define SRCBITS 8
#define ALIAS(x, vol) (x * vol)
#define LINEAR(x0, x1) ((x0 << 16) + (x1 - x0) * subpos)
/*
#define SET_CUBIC_COEFFICIENTS(x0, x1, x2, x3) { \
a = 3 * (x1 - x2) + (x3 - x0); \
b = ((x2 << 2) + (x0 << 1) - (5 * x1 + x3)) << 15; \
c = (x2 - x0) << 15; \
}
#define CUBIC(d) MULSC(MULSC(MULSC((a * subpos >> 1) + b, subpos) + c, subpos) + (d << 16), vol)
*/
#define CUBIC(x0, x1, x2, x3) (( \
x0 * cubicA0[subpos >> 6] + \
x1 * cubicA1[subpos >> 6] + \
x2 * cubicA1[1 + (subpos >> 6 ^ 1023)] + \
x3 * cubicA0[1 + (subpos >> 6 ^ 1023)]) << 6)
#define CUBICVOL(x, vol) (int)((LONG_LONG)(x) * (vol << 12) >> 32)
#include "resample.inc"
#undef dumb_reset_resampler
#undef dumb_start_resampler
#undef process_pickup
#undef dumb_resample
#undef dumb_resample_get_current_sample
#undef dumb_end_resampler
void dumb_reset_resampler_n(int n, DUMB_RESAMPLER *resampler, void *src, int src_channels, int32 pos, int32 start, int32 end, int quality)
{
if (n == 8)
dumb_reset_resampler_8(resampler, src, src_channels, pos, start, end, quality);
else if (n == 16)
dumb_reset_resampler_16(resampler, src, src_channels, pos, start, end, quality);
else
dumb_reset_resampler(resampler, src, src_channels, pos, start, end, quality);
}
DUMB_RESAMPLER *dumb_start_resampler_n(int n, void *src, int src_channels, int32 pos, int32 start, int32 end, int quality)
{
if (n == 8)
return dumb_start_resampler_8(src, src_channels, pos, start, end, quality);
else if (n == 16)
return dumb_start_resampler_16(src, src_channels, pos, start, end, quality);
else
return dumb_start_resampler(src, src_channels, pos, start, end, quality);
}
int32 dumb_resample_n_1_1(int n, DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume, double delta)
{
if (n == 8)
return dumb_resample_8_1_1(resampler, dst, dst_size, volume, delta);
else if (n == 16)
return dumb_resample_16_1_1(resampler, dst, dst_size, volume, delta);
else
return dumb_resample_1_1(resampler, dst, dst_size, volume, delta);
}
int32 dumb_resample_n_1_2(int n, DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta)
{
if (n == 8)
return dumb_resample_8_1_2(resampler, dst, dst_size, volume_left, volume_right, delta);
else if (n == 16)
return dumb_resample_16_1_2(resampler, dst, dst_size, volume_left, volume_right, delta);
else
return dumb_resample_1_2(resampler, dst, dst_size, volume_left, volume_right, delta);
}
int32 dumb_resample_n_2_1(int n, DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta)
{
if (n == 8)
return dumb_resample_8_2_1(resampler, dst, dst_size, volume_left, volume_right, delta);
else if (n == 16)
return dumb_resample_16_2_1(resampler, dst, dst_size, volume_left, volume_right, delta);
else
return dumb_resample_2_1(resampler, dst, dst_size, volume_left, volume_right, delta);
}
int32 dumb_resample_n_2_2(int n, DUMB_RESAMPLER *resampler, sample_t *dst, int32 dst_size, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, double delta)
{
if (n == 8)
return dumb_resample_8_2_2(resampler, dst, dst_size, volume_left, volume_right, delta);
else if (n == 16)
return dumb_resample_16_2_2(resampler, dst, dst_size, volume_left, volume_right, delta);
else
return dumb_resample_2_2(resampler, dst, dst_size, volume_left, volume_right, delta);
}
void dumb_resample_get_current_sample_n_1_1(int n, DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume, sample_t *dst)
{
if (n == 8)
dumb_resample_get_current_sample_8_1_1(resampler, volume, dst);
else if (n == 16)
dumb_resample_get_current_sample_16_1_1(resampler, volume, dst);
else
dumb_resample_get_current_sample_1_1(resampler, volume, dst);
}
void dumb_resample_get_current_sample_n_1_2(int n, DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst)
{
if (n == 8)
dumb_resample_get_current_sample_8_1_2(resampler, volume_left, volume_right, dst);
else if (n == 16)
dumb_resample_get_current_sample_16_1_2(resampler, volume_left, volume_right, dst);
else
dumb_resample_get_current_sample_1_2(resampler, volume_left, volume_right, dst);
}
void dumb_resample_get_current_sample_n_2_1(int n, DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst)
{
if (n == 8)
dumb_resample_get_current_sample_8_2_1(resampler, volume_left, volume_right, dst);
else if (n == 16)
dumb_resample_get_current_sample_16_2_1(resampler, volume_left, volume_right, dst);
else
dumb_resample_get_current_sample_2_1(resampler, volume_left, volume_right, dst);
}
void dumb_resample_get_current_sample_n_2_2(int n, DUMB_RESAMPLER *resampler, DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right, sample_t *dst)
{
if (n == 8)
dumb_resample_get_current_sample_8_2_2(resampler, volume_left, volume_right, dst);
else if (n == 16)
dumb_resample_get_current_sample_16_2_2(resampler, volume_left, volume_right, dst);
else
dumb_resample_get_current_sample_2_2(resampler, volume_left, volume_right, dst);
}
void dumb_end_resampler_n(int n, DUMB_RESAMPLER *resampler)
{
if (n == 8)
dumb_end_resampler_8(resampler);
else if (n == 16)
dumb_end_resampler_16(resampler);
else
dumb_end_resampler(resampler);
}

View file

@ -0,0 +1,264 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* resample.inc - Resampling helper template. / / \ \
* | < / \_
* By Bob and entheh. | \/ /\ /
* \_ / > /
* In order to find a good trade-off between | \ / /
* speed and accuracy in this code, some tests | ' /
* were carried out regarding the behaviour of \__/
* long long ints with gcc. The following code
* was tested:
*
* int a, b, c;
* c = ((long long)a * b) >> 16;
*
* DJGPP GCC Version 3.0.3 generated the following assembly language code for
* the multiplication and scaling, leaving the 32-bit result in EAX.
*
* movl -8(%ebp), %eax ; read one int into EAX
* imull -4(%ebp) ; multiply by the other; result goes in EDX:EAX
* shrdl $16, %edx, %eax ; shift EAX right 16, shifting bits in from EDX
*
* Note that a 32*32->64 multiplication is performed, allowing for high
* accuracy. On the Pentium 2 and above, shrdl takes two cycles (generally),
* so it is a minor concern when four multiplications are being performed
* (the cubic resampler). On the Pentium MMX and earlier, it takes four or
* more cycles, so this method is unsuitable for use in the low-quality
* resamplers.
*
* Since "long long" is a gcc-specific extension, we use LONG_LONG instead,
* defined in dumb.h. We may investigate later what code MSVC generates, but
* if it seems too slow then we suggest you use a good compiler.
*
* FIXME: these comments are somewhat out of date now.
*/
void dumb_reset_resampler(DUMB_RESAMPLER *resampler, SRCTYPE *src, int src_channels, int32 pos, int32 start, int32 end, int quality)
{
int i;
resampler->src = src;
resampler->pos = pos;
resampler->subpos = 0;
resampler->start = start;
resampler->end = end;
resampler->dir = 1;
resampler->pickup = NULL;
resampler->pickup_data = NULL;
if (quality < 0)
{
resampler->quality = 0;
}
else if (quality > DUMB_RQ_N_LEVELS - 1)
{
resampler->quality = DUMB_RQ_N_LEVELS - 1;
}
else
{
resampler->quality = quality;
}
for (i = 0; i < src_channels*3; i++) resampler->X[i] = 0;
resampler->overshot = -1;
}
DUMB_RESAMPLER *dumb_start_resampler(SRCTYPE *src, int src_channels, int32 pos, int32 start, int32 end, int quality)
{
DUMB_RESAMPLER *resampler = malloc(sizeof(*resampler));
if (!resampler) return NULL;
dumb_reset_resampler(resampler, src, src_channels, pos, start, end, quality);
return resampler;
}
#define UPDATE_VOLUME( pvol, vol ) { \
if (pvol) { \
vol##r += vol##d; \
if ((vol##d < 0 && vol##r <= vol##t) || \
(vol##d > 0 && vol##r >= vol##t)) { \
pvol->volume = pvol->target; \
pvol = NULL; \
vol = MULSCV( vol##t, vol##m ); \
} else { \
vol = MULSCV( vol##r, vol##m ); \
} \
} \
}
/* Create mono source resampler. */
#define SUFFIX2 _1
#define SRC_CHANNELS 1
#define DIVIDE_BY_SRC_CHANNELS(x) (x)
#define COPYSRC(dstarray, dstindex, srcarray, srcindex) (dstarray)[dstindex] = (srcarray)[srcindex]
#define COPYSRC2(dstarray, dstindex, condition, srcarray, srcindex) (dstarray)[dstindex] = condition ? (srcarray)[srcindex] : 0
#define MONO_DEST_VOLUME_PARAMETERS DUMB_VOLUME_RAMP_INFO * volume
#define MONO_DEST_VOLUME_VARIABLES vol, volr, vold, volt, volm
#define MONO_DEST_VOLUME_ZEROS 0
#define SET_MONO_DEST_VOLUME_VARIABLES { \
if ( volume ) { \
volr = (int)(volume->volume * 16777216.0); \
vold = (int)(volume->delta * 16777216.0); \
volt = (int)(volume->target * 16777216.0); \
volm = (int)(volume->mix * 16777216.0); \
vol = MULSCV( volr, volm ); \
if ( volr == volt ) volume = NULL; \
} else { \
vol = 0; \
volt = 0; \
} \
}
#define RETURN_MONO_DEST_VOLUME_VARIABLES if ( volume ) volume->volume = (float)volr / 16777216.0f
#define MONO_DEST_VOLUMES_ARE_ZERO (vol == 0 && volt == 0)
#define MONO_DEST_MIX_ALIAS(op, upd, offset) { \
*dst++ op ALIAS(x[offset], vol); \
if ( upd ) UPDATE_VOLUME( volume, vol ); \
}
#define STEREO_DEST_MIX_ALIAS(op, upd, offset) { \
int xm = x[offset]; \
*dst++ op ALIAS(xm, lvol); \
*dst++ op ALIAS(xm, rvol); \
if ( upd ) UPDATE_VOLUME( volume_left, lvol ); \
if ( upd ) UPDATE_VOLUME( volume_right, rvol ); \
}
#define MONO_DEST_MIX_LINEAR(op, upd, o0, o1) { \
*dst++ op MULSC(LINEAR(x[o0], x[o1]), vol); \
if ( upd ) UPDATE_VOLUME( volume, vol ); \
}
#define STEREO_DEST_MIX_LINEAR(op, upd, o0, o1) { \
int xm = LINEAR(x[o0], x[o1]); \
*dst++ op MULSC(xm, lvol); \
*dst++ op MULSC(xm, rvol); \
if ( upd ) UPDATE_VOLUME( volume_left, lvol ); \
if ( upd ) UPDATE_VOLUME( volume_right, rvol ); \
}
#define MONO_DEST_MIX_CUBIC(op, upd, x0, x3, o0, o1, o2, o3) { \
*dst++ op CUBICVOL(CUBIC(x0[o0], x[o1], x[o2], x3[o3]), vol); \
if ( upd ) UPDATE_VOLUME( volume, vol ); \
}
#define STEREO_DEST_MIX_CUBIC(op, upd, x0, x3, o0, o1, o2, o3) { \
int xm = CUBIC(x0[o0], x[o1], x[o2], x3[o3]); \
*dst++ op CUBICVOL(xm, lvol); \
*dst++ op CUBICVOL(xm, rvol); \
if ( upd ) UPDATE_VOLUME( volume_left, lvol ); \
if ( upd ) UPDATE_VOLUME( volume_right, rvol ); \
}
#include "resamp2.inc"
/* Create stereo source resampler. */
#define SUFFIX2 _2
#define SRC_CHANNELS 2
#define DIVIDE_BY_SRC_CHANNELS(x) ((x) >> 1)
#define COPYSRC(dstarray, dstindex, srcarray, srcindex) { \
(dstarray)[(dstindex)*2] = (srcarray)[(srcindex)*2]; \
(dstarray)[(dstindex)*2+1] = (srcarray)[(srcindex)*2+1]; \
}
#define COPYSRC2(dstarray, dstindex, condition, srcarray, srcindex) { \
if (condition) { \
(dstarray)[(dstindex)*2] = (srcarray)[(srcindex)*2]; \
(dstarray)[(dstindex)*2+1] = (srcarray)[(srcindex)*2+1]; \
} else { \
(dstarray)[(dstindex)*2] = 0; \
(dstarray)[(dstindex)*2+1] = 0; \
} \
}
#define MONO_DEST_VOLUME_PARAMETERS DUMB_VOLUME_RAMP_INFO * volume_left, DUMB_VOLUME_RAMP_INFO * volume_right
#define MONO_DEST_VOLUME_VARIABLES lvol, lvolr, lvold, lvolt, lvolm, rvol, rvolr, rvold, rvolt, rvolm
#define MONO_DEST_VOLUME_ZEROS 0, 0
#define SET_MONO_DEST_VOLUME_VARIABLES { \
if ( volume_left ) { \
lvolr = (int)(volume_left->volume * 16777216.0); \
lvold = (int)(volume_left->delta * 16777216.0); \
lvolt = (int)(volume_left->target * 16777216.0); \
lvolm = (int)(volume_left->mix * 16777216.0); \
lvol = MULSCV( lvolr, lvolm ); \
if ( lvolr == lvolt ) volume_left = NULL; \
} else { \
lvol = 0; \
lvolt = 0; \
} \
if ( volume_right ) { \
rvolr = (int)(volume_right->volume * 16777216.0); \
rvold = (int)(volume_right->delta * 16777216.0); \
rvolt = (int)(volume_right->target * 16777216.0); \
rvolm = (int)(volume_right->mix * 16777216.0); \
rvol = MULSCV( rvolr, rvolm ); \
if ( rvolr == rvolt ) volume_right = NULL; \
} else { \
rvol = 0; \
rvolt = 0; \
} \
}
#define RETURN_MONO_DEST_VOLUME_VARIABLES { \
if ( volume_left ) volume_left->volume = (float)lvolr / 16777216.0f; \
if ( volume_right ) volume_right->volume = (float)rvolr / 16777216.0f; \
}
#define MONO_DEST_VOLUMES_ARE_ZERO (lvol == 0 && lvolt == 0 && rvol == 0 && rvolt == 0)
#define MONO_DEST_MIX_ALIAS(op, upd, offset) { \
*dst++ op ALIAS(x[(offset)*2], lvol) + ALIAS(x[(offset)*2+1], rvol); \
if ( upd ) UPDATE_VOLUME( volume_left, lvol ); \
if ( upd ) UPDATE_VOLUME( volume_right, rvol ); \
}
#define STEREO_DEST_MIX_ALIAS(op, upd, offset) { \
*dst++ op ALIAS(x[(offset)*2], lvol); \
*dst++ op ALIAS(x[(offset)*2+1], rvol); \
if ( upd ) UPDATE_VOLUME( volume_left, lvol ); \
if ( upd ) UPDATE_VOLUME( volume_right, rvol ); \
}
#define MONO_DEST_MIX_LINEAR(op, upd, o0, o1) { \
*dst++ op MULSC(LINEAR(x[(o0)*2], x[(o1)*2]), lvol) + MULSC(LINEAR(x[(o0)*2+1], x[(o1)*2+1]), rvol); \
if ( upd ) UPDATE_VOLUME( volume_left, lvol ); \
if ( upd ) UPDATE_VOLUME( volume_right, rvol ); \
}
#define STEREO_DEST_MIX_LINEAR(op, upd, o0, o1) { \
*dst++ op MULSC(LINEAR(x[(o0)*2], x[(o1)*2]), lvol); \
*dst++ op MULSC(LINEAR(x[(o0)*2+1], x[(o1)*2+1]), rvol); \
if ( upd ) UPDATE_VOLUME( volume_left, lvol ); \
if ( upd ) UPDATE_VOLUME( volume_right, rvol ); \
}
#define MONO_DEST_MIX_CUBIC(op, upd, x0, x3, o0, o1, o2, o3) { \
*dst++ op \
CUBICVOL(CUBIC(x0[(o0)*2], x[(o1)*2], x[(o2)*2], x3[(o3)*2]), lvol) + \
CUBICVOL(CUBIC(x0[(o0)*2+1], x[(o1)*2+1], x[(o2)*2+1], x3[(o3)*2+1]), rvol); \
if ( upd ) UPDATE_VOLUME( volume_left, lvol ); \
if ( upd ) UPDATE_VOLUME( volume_right, rvol ); \
}
#define STEREO_DEST_MIX_CUBIC(op, upd, x0, x3, o0, o1, o2, o3) { \
*dst++ op CUBICVOL(CUBIC(x0[(o0)*2], x[(o1)*2], x[(o2)*2], x3[(o3)*2]), lvol); \
*dst++ op CUBICVOL(CUBIC(x0[(o0)*2+1], x[(o1)*2+1], x[(o2)*2+1], x3[(o3)*2+1]), rvol); \
if ( upd ) UPDATE_VOLUME( volume_left, lvol ); \
if ( upd ) UPDATE_VOLUME( volume_right, rvol ); \
}
#include "resamp2.inc"
void dumb_end_resampler(DUMB_RESAMPLER *resampler)
{
if (resampler)
free(resampler);
}
#undef CUBICVOL
#undef CUBIC
#undef LINEAR
#undef ALIAS
#undef SRCBITS
#undef SRCTYPE
#undef SUFFIX

90
dumb/src/helpers/riff.c Normal file
View file

@ -0,0 +1,90 @@
#include "dumb.h"
#include "internal/riff.h"
#include "internal/dumb.h"
#include <stdlib.h>
#include <string.h>
struct riff * riff_parse( unsigned char * ptr, unsigned size, unsigned proper )
{
unsigned stream_size;
struct riff * stream;
if ( size < 8 ) return 0;
if ( ptr[0] != 'R' || ptr[1] != 'I' || ptr[2] != 'F' || ptr[3] != 'F' ) return 0;
stream_size = ptr[4] | ( ptr[5] << 8 ) | ( ptr[6] << 16 ) | ( ptr[7] << 24 );
if ( stream_size + 8 > size ) return 0;
if ( stream_size < 4 ) return 0;
stream = malloc( sizeof( struct riff ) );
if ( ! stream ) return 0;
stream->type = ( ptr[8] << 24 ) | ( ptr[9] << 16 ) | ( ptr[10] << 8 ) | ptr[11];
stream->chunk_count = 0;
stream->chunks = 0;
ptr += 12;
stream_size -= 4;
while ( stream_size )
{
struct riff_chunk * chunk;
if ( stream_size < 8 ) break;
stream->chunks = realloc( stream->chunks, ( stream->chunk_count + 1 ) * sizeof( struct riff_chunk ) );
if ( ! stream->chunks ) break;
chunk = stream->chunks + stream->chunk_count;
chunk->type = ( ptr[0] << 24 ) | ( ptr[1] << 16 ) | ( ptr[2] << 8 ) | ptr[3];
chunk->size = ptr[4] | ( ptr[5] << 8 ) | ( ptr[6] << 16 ) | ( ptr[7] << 24 );
ptr += 8;
stream_size -= 8;
if ( stream_size < chunk->size ) break;
if ( chunk->type == DUMB_ID('R','I','F','F') )
{
chunk->data = riff_parse( ptr - 8, chunk->size + 8, proper );
if ( ! chunk->data ) break;
}
else
{
chunk->data = malloc( chunk->size );
if ( ! chunk->data ) break;
memcpy( chunk->data, ptr, chunk->size );
}
ptr += chunk->size;
stream_size -= chunk->size;
if ( proper && ( chunk->size & 1 ) )
{
++ ptr;
-- stream_size;
}
++stream->chunk_count;
}
if ( stream_size )
{
riff_free( stream );
stream = 0;
}
return stream;
}
void riff_free( struct riff * stream )
{
if ( stream )
{
if ( stream->chunks )
{
unsigned i;
for ( i = 0; i < stream->chunk_count; ++i )
{
struct riff_chunk * chunk = stream->chunks + i;
if ( chunk->type == DUMB_ID('R','I','F','F') ) riff_free( ( struct riff * ) chunk->data );
else free( chunk->data );
}
free( stream->chunks );
}
free( stream );
}
}

View file

@ -0,0 +1,64 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* sampbuf.c - Helper for allocating sample / / \ \
* buffers. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include "dumb.h"
/* DEPRECATED */
sample_t **create_sample_buffer(int n_channels, int32 length)
{
int i;
sample_t **samples = malloc(n_channels * sizeof(*samples));
if (!samples) return NULL;
samples[0] = malloc(n_channels * length * sizeof(*samples[0]));
if (!samples[0]) {
free(samples);
return NULL;
}
for (i = 1; i < n_channels; i++) samples[i] = samples[i-1] + length;
return samples;
}
sample_t **DUMBEXPORT allocate_sample_buffer(int n_channels, int32 length)
{
int i;
sample_t **samples = malloc(((n_channels + 1) >> 1) * sizeof(*samples));
if (!samples) return NULL;
samples[0] = malloc(n_channels * length * sizeof(*samples[0]));
if (!samples[0]) {
free(samples);
return NULL;
}
for (i = 1; i < (n_channels + 1) >> 1; i++) samples[i] = samples[i-1] + length*2;
return samples;
}
void DUMBEXPORT destroy_sample_buffer(sample_t **samples)
{
if (samples) {
free(samples[0]);
free(samples);
}
}

View file

@ -0,0 +1,29 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* silence.c - Silencing helper. / / \ \
* | < / \_
* By entheh. | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <string.h>
#include "dumb.h"
void DUMBEXPORT dumb_silence(sample_t *samples, int32 length)
{
memset(samples, 0, length * sizeof(*samples));
}

View file

@ -0,0 +1,93 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* stdfile.c - stdio file input module. / / \ \
* | < / \_
* By entheh. | \/ /\ /
* \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdio.h>
#include "dumb.h"
static void *dumb_stdfile_open(const char *filename)
{
return fopen(filename, "rb");
}
static int dumb_stdfile_skip(void *f, int32 n)
{
return fseek(f, n, SEEK_CUR);
}
static int dumb_stdfile_getc(void *f)
{
return fgetc(f);
}
static int32 dumb_stdfile_getnc(char *ptr, int32 n, void *f)
{
return (int32)fread(ptr, 1, n, f);
}
static void dumb_stdfile_close(void *f)
{
fclose(f);
}
static const DUMBFILE_SYSTEM stdfile_dfs = {
&dumb_stdfile_open,
&dumb_stdfile_skip,
&dumb_stdfile_getc,
&dumb_stdfile_getnc,
&dumb_stdfile_close
};
void DUMBEXPORT dumb_register_stdfiles(void)
{
register_dumbfile_system(&stdfile_dfs);
}
static const DUMBFILE_SYSTEM stdfile_dfs_leave_open = {
NULL,
&dumb_stdfile_skip,
&dumb_stdfile_getc,
&dumb_stdfile_getnc,
NULL
};
DUMBFILE *DUMBEXPORT dumbfile_open_stdfile(FILE *p)
{
DUMBFILE *d = dumbfile_open_ex(p, &stdfile_dfs_leave_open);
return d;
}

279
dumb/src/it/filter.cpp Normal file
View file

@ -0,0 +1,279 @@
#if defined(_DEBUG) && defined(_MSC_VER)
#include <crtdbg.h>
#endif
#ifdef _MSC_VER
#include <intrin.h>
#endif
#include <math.h>
#include <xmmintrin.h>
#include "dumb.h"
extern "C" {
#include "internal/it.h"
}
#ifdef _M_IX86
enum {
CPU_HAVE_3DNOW = 1 << 0,
CPU_HAVE_3DNOW_EX = 1 << 1,
CPU_HAVE_SSE = 1 << 2,
CPU_HAVE_SSE2 = 1 << 3,
CPU_HAVE_SSE3 = 1 << 4,
};
static bool query_cpu_feature_set(/*unsigned p_value*/) {
__try {
/*if (p_value & (CPU_HAVE_SSE | CPU_HAVE_SSE2 | CPU_HAVE_SSE2))*/ {
int buffer[4];
__cpuid(buffer,1);
/*if (p_value & CPU_HAVE_SSE)*/ {
if ((buffer[3]&(1<<25)) == 0) return false;
}
/*if (p_value & CPU_HAVE_SSE2) {
if ((buffer[3]&(1<<26)) == 0) return false;
}
if (p_value & CPU_HAVE_SSE3) {
if ((buffer[2]&(1<<0)) == 0) return false;
}*/
}
#ifdef _M_IX86
/*if (p_value & (CPU_HAVE_3DNOW_EX | CPU_HAVE_3DNOW)) {
int buffer_amd[4];
__cpuid(buffer_amd,0x80000000);
if ((unsigned)buffer_amd[0] < 0x80000001) return false;
__cpuid(buffer_amd,0x80000001);
if (p_value & CPU_HAVE_3DNOW) {
if ((buffer_amd[3]&(1<<22)) == 0) return false;
}
if (p_value & CPU_HAVE_3DNOW_EX) {
if ((buffer_amd[3]&(1<<30)) == 0) return false;
}
}*/
#endif
return true;
} __except(1) {
return false;
}
}
static const bool g_have_sse = query_cpu_feature_set(/*CPU_HAVE_SSE*/);
#elif defined(_M_X64)
enum {g_have_sse2 = true, g_have_sse = true, g_have_3dnow = false};
#else
enum {g_have_sse2 = false, g_have_sse = false, g_have_3dnow = false};
#endif
static void it_filter_int(DUMB_CLICK_REMOVER *cr, IT_FILTER_STATE *state, sample_t *dst, int32 pos, sample_t *src, int32 size, int step, int sampfreq, int cutoff, int resonance);
static void it_filter_sse(DUMB_CLICK_REMOVER *cr, IT_FILTER_STATE *state, sample_t *dst, int32 pos, sample_t *src, int32 size, int step, int sampfreq, int cutoff, int resonance);
extern "C" void it_filter(DUMB_CLICK_REMOVER *cr, IT_FILTER_STATE *state, sample_t *dst, int32 pos, sample_t *src, int32 size, int step, int sampfreq, int cutoff, int resonance)
{
if ( g_have_sse ) it_filter_sse(cr, state, dst, pos, src, size, step, sampfreq, cutoff, resonance);
else it_filter_int(cr, state, dst, pos, src, size, step, sampfreq, cutoff, resonance);
}
#define LOG10 2.30258509299
#define MULSCA(a, b) ((int)((LONG_LONG)((a) << 4) * (b) >> 32))
#define SCALEB 12
static void it_filter_int(DUMB_CLICK_REMOVER *cr, IT_FILTER_STATE *state, sample_t *dst, int32 pos, sample_t *src, int32 size, int step, int sampfreq, int cutoff, int resonance)
{
//profiler( filter );
sample_t currsample = state->currsample;
sample_t prevsample = state->prevsample;
float a, b, c;
int32 datasize;
{
float inv_angle = (float)(sampfreq * pow(0.5, 0.25 + cutoff*(1.0/(24<<IT_ENVELOPE_SHIFT))) * (1.0/(2*3.14159265358979323846*110.0)));
float loss = (float)exp(resonance*(-LOG10*1.2/128.0));
float d, e;
#if 0
loss *= 2; // This is the mistake most players seem to make!
#endif
#if 1
d = (1.0f - loss) / inv_angle;
if (d > 2.0f) d = 2.0f;
d = (loss - d) * inv_angle;
e = inv_angle * inv_angle;
a = 1.0f / (1.0f + d + e);
c = -e * a;
b = 1.0f - a - c;
#else
a = 1.0f / (inv_angle*inv_angle + inv_angle*loss + loss);
c = -(inv_angle*inv_angle) * a;
b = 1.0f - a - c;
#endif
}
dst += pos * step;
datasize = size * step;
#define INT_FILTERS
#ifdef INT_FILTERS
{
int ai = (int)(a * (1 << (16+SCALEB)));
int bi = (int)(b * (1 << (16+SCALEB)));
int ci = (int)(c * (1 << (16+SCALEB)));
int i;
if (cr) {
sample_t startstep = MULSCA(src[0], ai) + MULSCA(currsample, bi) + MULSCA(prevsample, ci);
dumb_record_click(cr, pos, startstep);
}
for (i = 0; i < datasize; i += step) {
{
sample_t newsample = MULSCA(src[i], ai) + MULSCA(currsample, bi) + MULSCA(prevsample, ci);
prevsample = currsample;
currsample = newsample;
}
dst[i] += currsample;
}
if (cr) {
sample_t endstep = MULSCA(src[datasize], ai) + MULSCA(currsample, bi) + MULSCA(prevsample, ci);
dumb_record_click(cr, pos + size, -endstep);
}
}
#else
if (cr) {
float startstep = src[0]*a + currsample*b + prevsample*c;
dumb_record_click(cr, pos, (sample_t)startstep);
}
{
int i = size % 3;
while (i > 0) {
{
float newsample = *src*a + currsample*b + prevsample*c;
src += step;
prevsample = currsample;
currsample = newsample;
}
*dst += (sample_t)currsample;
dst += step;
i--;
}
i = size / 3;
while (i > 0) {
float newsample;
/* Gotta love unrolled loops! */
*dst += (sample_t)(newsample = *src*a + currsample*b + prevsample*c);
src += step; dst += step;
*dst += (sample_t)(prevsample = *src*a + newsample*b + currsample*c);
src += step; dst += step;
*dst += (sample_t)(currsample = *src*a + prevsample*b + newsample*c);
src += step; dst += step;
i--;
}
}
if (cr) {
float endstep = src[datasize]*a + currsample*b + prevsample*c;
dumb_record_click(cr, pos + size, -(sample_t)endstep);
}
#endif
state->currsample = currsample;
state->prevsample = prevsample;
}
static void it_filter_sse(DUMB_CLICK_REMOVER *cr, IT_FILTER_STATE *state, sample_t *dst, int32 pos, sample_t *src, int32 size, int step, int sampfreq, int cutoff, int resonance)
{
__m128 data, impulse;
__m128 temp1, temp2;
sample_t currsample = state->currsample;
sample_t prevsample = state->prevsample;
float imp[4];
//profiler( filter_sse ); On ClawHammer Athlon64 3200+, ~12000 cycles, ~500 for that x87 setup code (as opposed to ~25500 for the original integer code)
int32 datasize;
{
float inv_angle = (float)(sampfreq * pow(0.5, 0.25 + cutoff*(1.0/(24<<IT_ENVELOPE_SHIFT))) * (1.0/(2*3.14159265358979323846*110.0)));
float loss = (float)exp(resonance*(-LOG10*1.2/128.0));
float d, e;
#if 0
loss *= 2; // This is the mistake most players seem to make!
#endif
#if 1
d = (1.0f - loss) / inv_angle;
if (d > 2.0f) d = 2.0f;
d = (loss - d) * inv_angle;
e = inv_angle * inv_angle;
imp[0] = 1.0f / (1.0f + d + e);
imp[2] = -e * imp[0];
imp[1] = 1.0f - imp[0] - imp[2];
#else
imp[0] = 1.0f / (inv_angle*inv_angle + inv_angle*loss + loss);
imp[2] = -(inv_angle*inv_angle) * imp[0];
imp[1] = 1.0f - imp[0] - imp[2];
#endif
imp[3] = 0;
}
dst += pos * step;
datasize = size * step;
{
int ai, bi, ci, i;
if (cr) {
sample_t startstep;
ai = (int)(imp[0] * (1 << (16+SCALEB)));
bi = (int)(imp[1] * (1 << (16+SCALEB)));
ci = (int)(imp[2] * (1 << (16+SCALEB)));
startstep = MULSCA(src[0], ai) + MULSCA(currsample, bi) + MULSCA(prevsample, ci);
dumb_record_click(cr, pos, startstep);
}
data = _mm_cvtsi32_ss( data, prevsample );
data = _mm_shuffle_ps( data, data, _MM_SHUFFLE(0, 0, 0, 0) );
data = _mm_cvtsi32_ss( data, currsample );
impulse = _mm_loadu_ps( (const float *) &imp );
temp1 = _mm_shuffle_ps( data, data, _MM_SHUFFLE(0, 1, 0, 0) );
for (i = 0; i < datasize; i += step) {
data = _mm_cvtsi32_ss( temp1, src [i] );
temp1 = _mm_mul_ps( data, impulse );
temp2 = _mm_shuffle_ps( temp1, temp1, _MM_SHUFFLE(0, 0, 3, 2) );
temp1 = _mm_add_ps( temp1, temp2 );
temp2 = _mm_shuffle_ps( temp1, temp1, _MM_SHUFFLE(0, 0, 0, 1) );
temp1 = _mm_add_ps( temp1, temp2 );
temp1 = _mm_shuffle_ps( temp1, data, _MM_SHUFFLE(0, 1, 0, 0) );
dst [i] += _mm_cvtss_si32( temp1 );
}
currsample = _mm_cvtss_si32( temp1 );
temp1 = _mm_shuffle_ps( temp1, temp1, _MM_SHUFFLE(0, 0, 0, 2) );
prevsample = _mm_cvtss_si32( temp1 );
#ifndef _M_X64
_mm_empty();
#endif
if (cr) {
sample_t endstep = MULSCA(src[datasize], ai) + MULSCA(currsample, bi) + MULSCA(prevsample, ci);
dumb_record_click(cr, pos + size, -endstep);
}
}
state->currsample = currsample;
state->prevsample = prevsample;
}

43
dumb/src/it/itload.c Normal file
View file

@ -0,0 +1,43 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* itload.c - Code to read an Impulse Tracker / / \ \
* file, opening and closing it for | < / \_
* you. | \/ /\ /
* \_ / > /
* By entheh. Don't worry Bob, you're credited | \ / /
* in itread.c! | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_it_quick(): loads an IT file into a DUH struct, returning a
* pointer to the DUH struct. When you have finished with it, you must pass
* the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_it_quick(const char *filename)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = dumb_read_it_quick(f);
dumbfile_close(f);
return duh;
}

29
dumb/src/it/itload2.c Normal file
View file

@ -0,0 +1,29 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* itload2.c - Function to read an Impulse Tracker / / \ \
* file, opening and closing it for | < / \_
* you, and do an initial run-through. | \/ /\ /
* \_ / > /
* Split off from itload.c by entheh. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
DUH *DUMBEXPORT dumb_load_it(const char *filename)
{
DUH *duh = dumb_load_it_quick(filename);
dumb_it_do_initial_runthrough(duh);
return duh;
}

247
dumb/src/it/itmisc.c Normal file
View file

@ -0,0 +1,247 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* itmisc.c - Miscellaneous functions relating / / \ \
* to module files. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
DUMB_IT_SIGDATA *DUMBEXPORT duh_get_it_sigdata(DUH *duh)
{
return duh_get_raw_sigdata(duh, 0, SIGTYPE_IT);
}
const unsigned char *DUMBEXPORT dumb_it_sd_get_song_message(DUMB_IT_SIGDATA *sd)
{
return sd ? sd->song_message : NULL;
}
int DUMBEXPORT dumb_it_sd_get_n_orders(DUMB_IT_SIGDATA *sd)
{
return sd ? sd->n_orders : 0;
}
int DUMBEXPORT dumb_it_sd_get_n_samples(DUMB_IT_SIGDATA *sd)
{
return sd ? sd->n_samples : 0;
}
int DUMBEXPORT dumb_it_sd_get_n_instruments(DUMB_IT_SIGDATA *sd)
{
return sd ? sd->n_instruments : 0;
}
const unsigned char *DUMBEXPORT dumb_it_sd_get_sample_name(DUMB_IT_SIGDATA *sd, int i)
{
ASSERT(sd && sd->sample && i >= 0 && i < sd->n_samples);
return sd->sample[i].name;
}
const unsigned char *DUMBEXPORT dumb_it_sd_get_sample_filename(DUMB_IT_SIGDATA *sd, int i)
{
ASSERT(sd && sd->sample && i >= 0 && i < sd->n_samples);
return sd->sample[i].filename;
}
const unsigned char *DUMBEXPORT dumb_it_sd_get_instrument_name(DUMB_IT_SIGDATA *sd, int i)
{
ASSERT(sd && sd->instrument && i >= 0 && i < sd->n_instruments);
return sd->instrument[i].name;
}
const unsigned char *DUMBEXPORT dumb_it_sd_get_instrument_filename(DUMB_IT_SIGDATA *sd, int i)
{
ASSERT(sd && sd->instrument && i >= 0 && i < sd->n_instruments);
return sd->instrument[i].filename;
}
int DUMBEXPORT dumb_it_sd_get_initial_global_volume(DUMB_IT_SIGDATA *sd)
{
return sd ? sd->global_volume : 0;
}
void DUMBEXPORT dumb_it_sd_set_initial_global_volume(DUMB_IT_SIGDATA *sd, int gv)
{
if (sd) sd->global_volume = gv;
}
int DUMBEXPORT dumb_it_sd_get_mixing_volume(DUMB_IT_SIGDATA *sd)
{
return sd ? sd->mixing_volume : 0;
}
void DUMBEXPORT dumb_it_sd_set_mixing_volume(DUMB_IT_SIGDATA *sd, int mv)
{
if (sd) sd->mixing_volume = mv;
}
int DUMBEXPORT dumb_it_sd_get_initial_speed(DUMB_IT_SIGDATA *sd)
{
return sd ? sd->speed : 0;
}
void DUMBEXPORT dumb_it_sd_set_initial_speed(DUMB_IT_SIGDATA *sd, int speed)
{
if (sd) sd->speed = speed;
}
int DUMBEXPORT dumb_it_sd_get_initial_tempo(DUMB_IT_SIGDATA *sd)
{
return sd ? sd->tempo : 0;
}
void DUMBEXPORT dumb_it_sd_set_initial_tempo(DUMB_IT_SIGDATA *sd, int tempo)
{
if (sd) sd->tempo = tempo;
}
int DUMBEXPORT dumb_it_sd_get_initial_channel_volume(DUMB_IT_SIGDATA *sd, int channel)
{
ASSERT(channel >= 0 && channel < DUMB_IT_N_CHANNELS);
return sd ? sd->channel_volume[channel] : 0;
}
void DUMBEXPORT dumb_it_sd_set_initial_channel_volume(DUMB_IT_SIGDATA *sd, int channel, int volume)
{
ASSERT(channel >= 0 && channel < DUMB_IT_N_CHANNELS);
if (sd) sd->channel_volume[channel] = volume;
}
int DUMBEXPORT dumb_it_sr_get_current_order(DUMB_IT_SIGRENDERER *sr)
{
return sr ? sr->order : -1;
}
int DUMBEXPORT dumb_it_sr_get_current_row(DUMB_IT_SIGRENDERER *sr)
{
return sr ? sr->row : -1;
}
int DUMBEXPORT dumb_it_sr_get_global_volume(DUMB_IT_SIGRENDERER *sr)
{
return sr ? sr->globalvolume : 0;
}
void DUMBEXPORT dumb_it_sr_set_global_volume(DUMB_IT_SIGRENDERER *sr, int gv)
{
if (sr) sr->globalvolume = gv;
}
int DUMBEXPORT dumb_it_sr_get_tempo(DUMB_IT_SIGRENDERER *sr)
{
return sr ? sr->tempo : 0;
}
void DUMBEXPORT dumb_it_sr_set_tempo(DUMB_IT_SIGRENDERER *sr, int tempo)
{
if (sr) sr->tempo = tempo;
}
int DUMBEXPORT dumb_it_sr_get_speed(DUMB_IT_SIGRENDERER *sr)
{
return sr ? sr->speed : 0;
}
void DUMBEXPORT dumb_it_sr_set_speed(DUMB_IT_SIGRENDERER *sr, int speed)
{
if (sr) sr->speed = speed;
}
int DUMBEXPORT dumb_it_sr_get_channel_volume(DUMB_IT_SIGRENDERER *sr, int channel)
{
return sr ? sr->channel[channel].channelvolume : 0;
}
void DUMBEXPORT dumb_it_sr_set_channel_volume(DUMB_IT_SIGRENDERER *sr, int channel, int volume)
{
if (sr) sr->channel[channel].channelvolume = volume;
}
void DUMBEXPORT dumb_it_sr_set_channel_muted(DUMB_IT_SIGRENDERER *sr, int channel, int muted)
{
if (sr) {
if (muted)
sr->channel[channel].flags |= IT_CHANNEL_MUTED;
else
sr->channel[channel].flags &= ~IT_CHANNEL_MUTED;
}
}
int DUMBEXPORT dumb_it_sr_get_channel_muted(DUMB_IT_SIGRENDERER *sr, int channel)
{
return sr ? (sr->channel[channel].flags & IT_CHANNEL_MUTED) != 0 : 0;
}

63
dumb/src/it/itorder.c Normal file
View file

@ -0,0 +1,63 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* itorder.c - Code to fix invalid patterns in / / \ \
* the pattern table. | < / \_
* | \/ /\ /
* By Julien Cugniere. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include "dumb.h"
#include "internal/it.h"
/* This function ensures that any pattern mentioned in the order table but
* not present in the pattern table is treated as an empty 64 rows pattern.
* This is done by adding such a dummy pattern at the end of the pattern
* table, and redirect invalid orders to it.
* Patterns 254 and 255 are left untouched, unless the signal is an XM.
*/
int _dumb_it_fix_invalid_orders(DUMB_IT_SIGDATA *sigdata)
{
int i;
int found_some = 0;
int first_invalid = sigdata->n_patterns;
int last_invalid = (sigdata->flags & IT_WAS_AN_XM) ? 255 : 253;
for (i = 0; i < sigdata->n_orders; i++) {
if (sigdata->order[i] >= first_invalid && sigdata->order[i] <= last_invalid) {
sigdata->order[i] = sigdata->n_patterns;
found_some = 1;
}
}
if (found_some) {
IT_PATTERN *new_pattern = realloc(sigdata->pattern, sizeof(*sigdata->pattern) * (sigdata->n_patterns + 1));
if (!new_pattern)
return -1;
new_pattern[sigdata->n_patterns].n_rows = 64;
new_pattern[sigdata->n_patterns].n_entries = 0;
new_pattern[sigdata->n_patterns].entry = NULL;
sigdata->pattern = new_pattern;
sigdata->n_patterns++;
}
return 0;
}

1335
dumb/src/it/itread.c Normal file

File diff suppressed because it is too large Load diff

29
dumb/src/it/itread2.c Normal file
View file

@ -0,0 +1,29 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* itread2.c - Function to read an Impulse Tracker / / \ \
* module from an open file and do an | < / \_
* initial run-through. | \/ /\ /
* \_ / > /
* Split off from itread.c by entheh. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
DUH *DUMBEXPORT dumb_read_it(DUMBFILE *f)
{
DUH *duh = dumb_read_it_quick(f);
dumb_it_do_initial_runthrough(duh);
return duh;
}

5557
dumb/src/it/itrender.c Normal file

File diff suppressed because it is too large Load diff

72
dumb/src/it/itunload.c Normal file
View file

@ -0,0 +1,72 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* itunload.c - Code to free an Impulse Tracker / / \ \
* module from memory. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include "dumb.h"
#include "internal/it.h"
void _dumb_it_unload_sigdata(sigdata_t *vsigdata)
{
if (vsigdata) {
DUMB_IT_SIGDATA *sigdata = vsigdata;
int n;
if (sigdata->song_message)
free(sigdata->song_message);
if (sigdata->order)
free(sigdata->order);
if (sigdata->instrument)
free(sigdata->instrument);
if (sigdata->sample) {
for (n = 0; n < sigdata->n_samples; n++)
if (sigdata->sample[n].data)
free(sigdata->sample[n].data);
free(sigdata->sample);
}
if (sigdata->pattern) {
for (n = 0; n < sigdata->n_patterns; n++)
if (sigdata->pattern[n].entry)
free(sigdata->pattern[n].entry);
free(sigdata->pattern);
}
if (sigdata->midi)
free(sigdata->midi);
{
IT_CHECKPOINT *checkpoint = sigdata->checkpoint;
while (checkpoint) {
IT_CHECKPOINT *next = checkpoint->next;
_dumb_it_end_sigrenderer(checkpoint->sigrenderer);
free(checkpoint);
checkpoint = next;
}
}
free(vsigdata);
}
}

42
dumb/src/it/load669.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadmod.c - Code to read a 669 Composer module / / \ \
* file, opening and closing it for | < / \_
* you. | \/ /\ /
* \_ / > /
* By Chris Moeller | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_669_quick(): loads a 669 file into a DUH struct, returning a
* pointer to the DUH struct. When you have finished with it, you must
* pass the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_669_quick(const char *filename)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = dumb_read_669_quick(f);
dumbfile_close(f);
return duh;
}

34
dumb/src/it/load6692.c Normal file
View file

@ -0,0 +1,34 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadmod2.c - Code to read a 669 Composer module / / \ \
* file, opening and closing it for | < / \_
* you, and do an initial run-through. | \/ /\ /
* \_ / > /
* By Chris Moeller | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_669(): loads a 669 file into a DUH struct, returning a pointer
* to the DUH struct. When you have finished with it, you must pass the
* pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_669(const char *filename)
{
DUH *duh = dumb_load_669_quick(filename);
dumb_it_do_initial_runthrough(duh);
return duh;
}

42
dumb/src/it/loadasy.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadasy.c - Code to read an ASYLUM Music Format / / \ \
* module file, opening and closing it | < / \_
* for you. | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_asy_quick(): loads a AMF file into a DUH struct, returning a
* pointer to the DUH struct. When you have finished with it, you must
* pass the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_asy_quick(const char *filename)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = dumb_read_asy_quick(f);
dumbfile_close(f);
return duh;
}

34
dumb/src/it/loadasy2.c Normal file
View file

@ -0,0 +1,34 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadasy2.c - Code to read an ASYLUM Music Format / / \ \
* module file, opening and closing it | < / \_
* for you, and do an initial run- | \/ /\ /
* through. \_ / > /
* | \ / /
* By Chris Moeller. | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_asy(): loads a AMF file into a DUH struct, returning a pointer
* to the DUH struct. When you have finished with it, you must pass the
* pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_asy(const char *filename)
{
DUH *duh = dumb_load_asy_quick(filename);
dumb_it_do_initial_runthrough(duh);
return duh;
}

42
dumb/src/it/loadmod.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadmod.c - Code to read a good old-fashioned / / \ \
* Amiga module file, opening and | < / \_
* closing it for you. | \/ /\ /
* \_ / > /
* By entheh. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_mod_quick(): loads a MOD file into a DUH struct, returning a
* pointer to the DUH struct. When you have finished with it, you must
* pass the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_mod_quick(const char *filename, int restrict)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = dumb_read_mod_quick(f, restrict);
dumbfile_close(f);
return duh;
}

29
dumb/src/it/loadmod2.c Normal file
View file

@ -0,0 +1,29 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadmod2.c - Function to read a good old- / / \ \
* fashioned Amiga module file, | < / \_
* opening and closing it for you, | \/ /\ /
* and do an initial run-through. \_ / > /
* | \ / /
* Split off from loadmod.c by entheh. | ' /
* \__/
*/
#include "dumb.h"
DUH *DUMBEXPORT dumb_load_mod(const char *filename, int restrict)
{
DUH *duh = dumb_load_mod_quick(filename, restrict);
dumb_it_do_initial_runthrough(duh);
return duh;
}

42
dumb/src/it/loadmtm.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadmtm.c - Code to read a MultiTracker Module / / \ \
* file, opening and closing it for | < / \_
* you. | \/ /\ /
* \_ / > /
* By Chris Moeller | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_mtm_quick(): loads a MTM file into a DUH struct, returning a
* pointer to the DUH struct. When you have finished with it, you must
* pass the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_mtm_quick(const char *filename)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = dumb_read_mtm_quick(f);
dumbfile_close(f);
return duh;
}

34
dumb/src/it/loadmtm2.c Normal file
View file

@ -0,0 +1,34 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadmtm2.c - Code to read a MultiTracker Module / / \ \
* file, opening and closing it for | < / \_
* you, and do an initial run-through. | \/ /\ /
* \_ / > /
* By Chris Moeller | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_mtm(): loads a MTM file into a DUH struct, returning a pointer
* to the DUH struct. When you have finished with it, you must pass the
* pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_mtm(const char *filename)
{
DUH *duh = dumb_load_mtm_quick(filename);
dumb_it_do_initial_runthrough(duh);
return duh;
}

43
dumb/src/it/loadoldpsm.c Normal file
View file

@ -0,0 +1,43 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadoldpsm.c - Code to read a ProTracker Studio / / \ \
* file, opening and closing it for | < / \_
* you. | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_old_psm_quick(): loads an old PSM file into a DUH struct,
* returning a pointer to the DUH struct. When you have finished with it,
* you must pass the pointer to unload_duh() so that the memory can be
* freed.
*/
DUH *DUMBEXPORT dumb_load_old_psm_quick(const char *filename)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = dumb_read_old_psm_quick(f);
dumbfile_close(f);
return duh;
}

34
dumb/src/it/loadoldpsm2.c Normal file
View file

@ -0,0 +1,34 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadoldpsm2.c - Code to read a ProTracker Studio / / \ \
* file, opening and closing it for | < / \_
* you, and do an initial run- | \/ /\ /
* through. \_ / > /
* | \ / /
* By Chris Moeller. | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_old_psm(): loads an old PSM file into a DUH struct, returning
* a pointer to the DUH struct. When you have finished with it, you must
* pass the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_old_psm(const char *filename)
{
DUH *duh = dumb_load_old_psm_quick(filename);
dumb_it_do_initial_runthrough(duh);
return duh;
}

42
dumb/src/it/loadpsm.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadpsm.c - Code to read a ProTracker Studio / / \ \
* file, opening and closing it for | < / \_
* you. | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_psm_quick(): loads a PSM file into a DUH struct, returning a
* pointer to the DUH struct. When you have finished with it, you must
* pass the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_psm_quick(const char *filename, int subsong)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = dumb_read_psm_quick(f, subsong);
dumbfile_close(f);
return duh;
}

34
dumb/src/it/loadpsm2.c Normal file
View file

@ -0,0 +1,34 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadpsm2.c - Code to read a ProTracker Studio / / \ \
* file, opening and closing it for | < / \_
* you, and do an initial run-through. | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_psm(): loads a PSM file into a DUH struct, returning a pointer
* to the DUH struct. When you have finished with it, you must pass the
* pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_psm(const char *filename, int subsong)
{
DUH *duh = dumb_load_psm_quick(filename, subsong);
dumb_it_do_initial_runthrough(duh);
return duh;
}

42
dumb/src/it/loadptm.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadptm.c - Code to read a Poly Tracker v2.03 / / \ \
* file, opening and closing it for | < / \_
* you. | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_ptm_quick(): loads a PTM file into a DUH struct, returning a
* pointer to the DUH struct. When you have finished with it, you must
* pass the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_ptm_quick(const char *filename)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = dumb_read_ptm_quick(f);
dumbfile_close(f);
return duh;
}

34
dumb/src/it/loadptm2.c Normal file
View file

@ -0,0 +1,34 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadptm2.c - Code to read a Poly Tracker v2.03 / / \ \
* file, opening and closing it for | < / \_
* you, and do an initial run-through. | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_ptm(): loads a PTM file into a DUH struct, returning a pointer
* to the DUH struct. When you have finished with it, you must pass the
* pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_ptm(const char *filename)
{
DUH *duh = dumb_load_ptm_quick(filename);
dumb_it_do_initial_runthrough(duh);
return duh;
}

42
dumb/src/it/loadriff.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadriff.c - Code to read a RIFF module file / / \ \
* opening and closing it for you. | < / \_
* | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_riff_quick(): loads a RIFF file into a DUH struct, returning
* a pointer to the DUH struct. When you have finished with it, you must
* pass the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_riff_quick( const char *filename )
{
DUH * duh;
DUMBFILE * f = dumbfile_open( filename );
if ( ! f )
return NULL;
duh = dumb_read_riff_quick( f );
dumbfile_close( f );
return duh;
}

29
dumb/src/it/loadriff2.c Normal file
View file

@ -0,0 +1,29 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadriff2.c - Code to read a RIFF module file / / \ \
* opening and closing it for you, | < / \_
* and do an initial run-through. | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
DUH *DUMBEXPORT dumb_load_riff(const char *filename)
{
DUH *duh = dumb_load_riff_quick(filename);
dumb_it_do_initial_runthrough(duh);
return duh;
}

42
dumb/src/it/loads3m.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loads3m.c - Code to read a ScreamTracker 3 / / \ \
* file, opening and closing it for | < / \_
* you. | \/ /\ /
* \_ / > /
* By entheh. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_s3m_quick(): loads an S3M file into a DUH struct, returning
* a pointer to the DUH struct. When you have finished with it, you must
* pass the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_s3m_quick(const char *filename)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = dumb_read_s3m_quick(f);
dumbfile_close(f);
return duh;
}

29
dumb/src/it/loads3m2.c Normal file
View file

@ -0,0 +1,29 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loads3m2.c - Function to read a ScreamTracker 3 / / \ \
* file, opening and closing it for | < / \_
* you, and do an initial run-through. | \/ /\ /
* \_ / > /
* Split off from loads3m.c by entheh. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
DUH *DUMBEXPORT dumb_load_s3m(const char *filename)
{
DUH *duh = dumb_load_s3m_quick(filename);
dumb_it_do_initial_runthrough(duh);
return duh;
}

42
dumb/src/it/loadstm.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadstm.c - Code to read a ScreamTracker 2 / / \ \
* file, opening and closing it for | < / \_
* you. | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_stm_quick(): loads an STM file into a DUH struct, returning a
* pointer to the DUH struct. When you have finished with it, you must
* pass the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_stm_quick(const char *filename)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = dumb_read_stm_quick(f);
dumbfile_close(f);
return duh;
}

29
dumb/src/it/loadstm2.c Normal file
View file

@ -0,0 +1,29 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadstm2.c - Function to read a ScreamTracker 2 / / \ \
* file, opening and closing it for | < / \_
* you, and do an initial run-through. | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
DUH *DUMBEXPORT dumb_load_stm(const char *filename)
{
DUH *duh = dumb_load_stm_quick(filename);
dumb_it_do_initial_runthrough(duh);
return duh;
}

42
dumb/src/it/loadxm.c Normal file
View file

@ -0,0 +1,42 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadxm.c - Code to read a Fast Tracker II / / \ \
* file, opening and closing it for | < / \_
* you. | \/ /\ /
* \_ / > /
* By entheh. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
/* dumb_load_xm_quick(): loads an XM file into a DUH struct, returning a
* pointer to the DUH struct. When you have finished with it, you must
* pass the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_load_xm_quick(const char *filename)
{
DUH *duh;
DUMBFILE *f = dumbfile_open(filename);
if (!f)
return NULL;
duh = dumb_read_xm_quick(f);
dumbfile_close(f);
return duh;
}

29
dumb/src/it/loadxm2.c Normal file
View file

@ -0,0 +1,29 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* loadxm2.c - Function to read a Fast Tracker II / / \ \
* file, opening and closing it for | < / \_
* you, and do an initial run-through. | \/ /\ /
* \_ / > /
* Split off from loadxm.c by entheh. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
DUH *DUMBEXPORT dumb_load_xm(const char *filename)
{
DUH *duh = dumb_load_xm_quick(filename);
dumb_it_do_initial_runthrough(duh);
return duh;
}

125
dumb/src/it/ptmeffect.c Normal file
View file

@ -0,0 +1,125 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* ptmeffect.c - Code for converting PTM / / \ \
* effects to IT effects. | < / \_
* | \/ /\ /
* By Chris Moeller. Based on xmeffect.c \_ / > /
* by Julien Cugniere. | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <string.h>
#include "dumb.h"
#include "internal/it.h"
void _dumb_it_ptm_convert_effect(int effect, int value, IT_ENTRY *entry)
{
if (effect >= PTM_N_EFFECTS)
return;
/* Linearisation of the effect number... */
if (effect == PTM_E) {
effect = PTM_EBASE + HIGH(value);
value = LOW(value);
}
/* convert effect */
entry->mask |= IT_ENTRY_EFFECT;
switch (effect) {
case PTM_APPREGIO: effect = IT_ARPEGGIO; break;
case PTM_PORTAMENTO_UP: effect = IT_PORTAMENTO_UP; break;
case PTM_PORTAMENTO_DOWN: effect = IT_PORTAMENTO_DOWN; break;
case PTM_TONE_PORTAMENTO: effect = IT_TONE_PORTAMENTO; break;
case PTM_VIBRATO: effect = IT_VIBRATO; break;
case PTM_VOLSLIDE_TONEPORTA: effect = IT_VOLSLIDE_TONEPORTA; break;
case PTM_VOLSLIDE_VIBRATO: effect = IT_VOLSLIDE_VIBRATO; break;
case PTM_TREMOLO: effect = IT_TREMOLO; break;
case PTM_SAMPLE_OFFSET: effect = IT_SET_SAMPLE_OFFSET; break;
case PTM_VOLUME_SLIDE: effect = IT_VOLUME_SLIDE; break;
case PTM_POSITION_JUMP: effect = IT_JUMP_TO_ORDER; break;
case PTM_SET_CHANNEL_VOLUME: effect = IT_SET_CHANNEL_VOLUME; break;
case PTM_PATTERN_BREAK: effect = IT_BREAK_TO_ROW; break;
case PTM_SET_GLOBAL_VOLUME: effect = IT_SET_GLOBAL_VOLUME; break;
case PTM_RETRIGGER: effect = IT_RETRIGGER_NOTE; break;
case PTM_FINE_VIBRATO: effect = IT_FINE_VIBRATO; break;
/* TODO properly */
case PTM_NOTE_SLIDE_UP: effect = IT_PTM_NOTE_SLIDE_UP; break;
case PTM_NOTE_SLIDE_DOWN: effect = IT_PTM_NOTE_SLIDE_DOWN; break;
case PTM_NOTE_SLIDE_UP_RETRIG: effect = IT_PTM_NOTE_SLIDE_UP_RETRIG; break;
case PTM_NOTE_SLIDE_DOWN_RETRIG: effect = IT_PTM_NOTE_SLIDE_DOWN_RETRIG; break;
case PTM_SET_TEMPO_BPM:
effect = (value < 0x20) ? (IT_SET_SPEED) : (IT_SET_SONG_TEMPO);
break;
case PTM_EBASE+PTM_E_SET_FINETUNE: effect = SBASE+IT_S_FINETUNE; break; /** TODO */
case PTM_EBASE+PTM_E_SET_LOOP: effect = SBASE+IT_S_PATTERN_LOOP; break;
case PTM_EBASE+PTM_E_NOTE_CUT: effect = SBASE+IT_S_DELAYED_NOTE_CUT; break;
case PTM_EBASE+PTM_E_NOTE_DELAY: effect = SBASE+IT_S_NOTE_DELAY; break;
case PTM_EBASE+PTM_E_PATTERN_DELAY: effect = SBASE+IT_S_PATTERN_DELAY; break;
case PTM_EBASE+PTM_E_SET_PANNING: effect = SBASE+IT_S_SET_PAN; break;
case PTM_EBASE+PTM_E_FINE_VOLSLIDE_UP:
effect = IT_VOLUME_SLIDE;
value = EFFECT_VALUE(value, 0xF);
break;
case PTM_EBASE + PTM_E_FINE_VOLSLIDE_DOWN:
effect = IT_VOLUME_SLIDE;
value = EFFECT_VALUE(0xF, value);
break;
case PTM_EBASE + PTM_E_FINE_PORTA_UP:
effect = IT_PORTAMENTO_UP;
value = EFFECT_VALUE(0xF, value);
break;
case PTM_EBASE + PTM_E_FINE_PORTA_DOWN:
effect = IT_PORTAMENTO_DOWN;
value = EFFECT_VALUE(0xF, value);
break;
case PTM_EBASE + PTM_E_RETRIG_NOTE:
effect = IT_XM_RETRIGGER_NOTE;
value = EFFECT_VALUE(0, value);
break;
case PTM_EBASE + PTM_E_SET_VIBRATO_CONTROL:
effect = SBASE+IT_S_SET_VIBRATO_WAVEFORM;
value &= ~4; /** TODO: value&4 -> don't retrig wave */
break;
case PTM_EBASE + PTM_E_SET_TREMOLO_CONTROL:
effect = SBASE+IT_S_SET_TREMOLO_WAVEFORM;
value &= ~4; /** TODO: value&4 -> don't retrig wave */
break;
default:
/* user effect (often used in demos for synchronisation) */
entry->mask &= ~IT_ENTRY_EFFECT;
}
/* Inverse linearisation... */
if (effect >= SBASE && effect < SBASE+16) {
value = EFFECT_VALUE(effect-SBASE, value);
effect = IT_S;
}
entry->effect = effect;
entry->effectvalue = value;
}

447
dumb/src/it/read669.c Normal file
View file

@ -0,0 +1,447 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* read669.c - Code to read a 669 Composer module / / \ \
* from an open file. | < / \_
* | \/ /\ /
* By Chris Moeller. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "dumb.h"
#include "internal/it.h"
static int it_669_read_pattern(IT_PATTERN *pattern, DUMBFILE *f, int tempo, int breakpoint, unsigned char *buffer, int * used_channels)
{
int pos;
int channel;
int row;
IT_ENTRY *entry;
pattern->n_rows = 64;
if (dumbfile_getnc(buffer, 64 * 3 * 8, f) < 64 * 3 * 8)
return -1;
/* compute number of entries */
pattern->n_entries = 64 + 1; /* Account for the row end markers, speed command */
if (breakpoint < 63) pattern->n_entries++; /* and break to row 0 */
pos = 0;
for (row = 0; row < 64; row++) {
for (channel = 0; channel < 8; channel++) {
if (buffer[pos+0] != 0xFF || buffer[pos+2] != 0xFF)
pattern->n_entries++;
pos += 3;
}
}
pattern->entry = malloc(pattern->n_entries * sizeof(*pattern->entry));
if (!pattern->entry)
return -1;
if (breakpoint == 63) breakpoint++;
entry = pattern->entry;
entry->channel = 8;
entry->mask = IT_ENTRY_EFFECT;
entry->effect = IT_SET_SPEED;
entry->effectvalue = tempo;
entry++;
pos = 0;
for (row = 0; row < 64; row++) {
if (row == breakpoint) {
entry->channel = 8;
entry->mask = IT_ENTRY_EFFECT;
entry->effect = IT_BREAK_TO_ROW;
entry->effectvalue = 0;
entry++;
}
for (channel = 0; channel < 8; channel++) {
if (buffer[pos+0] != 0xFF || buffer[pos+2] != 0xFF) {
entry->channel = channel;
entry->mask = 0;
if (buffer[pos+0] < 0xFE) {
entry->mask |= IT_ENTRY_NOTE | IT_ENTRY_INSTRUMENT;
entry->note = (buffer[pos+0] >> 2) + 36;
entry->instrument = (((buffer[pos+0] << 4) | (buffer[pos+1] >> 4)) & 0x3F) + 1;
}
if (buffer[pos+0] <= 0xFE) {
entry->mask |= IT_ENTRY_VOLPAN;
entry->volpan = ((buffer[pos+1] & 15) << 6) / 15;
if (*used_channels < channel + 1) *used_channels = channel + 1;
}
if (buffer[pos+2] != 0xFF) {
entry->mask |= IT_ENTRY_EFFECT;
entry->effectvalue = buffer[pos+2] & 15;
switch (buffer[pos+2] >> 4) {
case 0:
entry->effect = IT_PORTAMENTO_UP;
break;
case 1:
entry->effect = IT_PORTAMENTO_DOWN;
break;
case 2:
entry->effect = IT_TONE_PORTAMENTO;
break;
case 3:
entry->effect = IT_S;
entry->effectvalue += IT_S_FINETUNE * 16 + 8;
break;
case 4:
entry->effect = IT_VIBRATO;
// XXX speed unknown
entry->effectvalue |= 0x10;
break;
case 5:
if (entry->effectvalue) {
entry->effect = IT_SET_SPEED;
} else {
entry->mask &= ~IT_ENTRY_EFFECT;
}
break;
#if 0
/* dunno about this, really... */
case 6:
if (entry->effectvalue == 0) {
entry->effect = IT_PANNING_SLIDE;
entry->effectvalue = 0xFE;
} else if (entry->effectvalue == 1) {
entry->effect = IT_PANNING_SLIDE;
entry->effectvalue = 0xEF;
} else {
entry->mask &= ~IT_ENTRY_EFFECT;
}
break;
#endif
default:
entry->mask &= ~IT_ENTRY_EFFECT;
break;
}
if (*used_channels < channel + 1) *used_channels = channel + 1;
}
entry++;
}
pos += 3;
}
IT_SET_END_ROW(entry);
entry++;
}
return 0;
}
static int it_669_read_sample_header(IT_SAMPLE *sample, DUMBFILE *f)
{
dumbfile_getnc(sample->name, 13, f);
sample->name[13] = 0;
sample->filename[0] = 0;
sample->length = dumbfile_igetl(f);
sample->loop_start = dumbfile_igetl(f);
sample->loop_end = dumbfile_igetl(f);
if (dumbfile_error(f))
return -1;
if (sample->length <= 0) {
sample->flags = 0;
return 0;
}
sample->flags = IT_SAMPLE_EXISTS;
sample->global_volume = 64;
sample->default_volume = 64;
sample->default_pan = 0;
sample->C5_speed = 8363;
// the above line might be wrong
if ((sample->loop_end > sample->length) && !(sample->loop_start))
sample->loop_end = 0;
if (sample->loop_end > sample->length)
sample->loop_end = sample->length;
if (sample->loop_end - sample->loop_start > 2)
sample->flags |= IT_SAMPLE_LOOP;
sample->vibrato_speed = 0;
sample->vibrato_depth = 0;
sample->vibrato_rate = 0;
sample->vibrato_waveform = 0; // do we have to set _all_ these?
sample->finetune = 0;
sample->max_resampling_quality = -1;
return 0;
}
static int it_669_read_sample_data(IT_SAMPLE *sample, DUMBFILE *f)
{
int32 i;
int32 truncated_size;
/* let's get rid of the sample data coming after the end of the loop */
if ((sample->flags & IT_SAMPLE_LOOP) && sample->loop_end < sample->length) {
truncated_size = sample->length - sample->loop_end;
sample->length = sample->loop_end;
} else {
truncated_size = 0;
}
sample->data = malloc(sample->length);
if (!sample->data)
return -1;
if (sample->length)
{
i = dumbfile_getnc(sample->data, sample->length, f);
if (i < sample->length) {
//return -1;
// ficking truncated files
if (i <= 0) {
sample->flags = 0;
return 0;
}
sample->length = i;
if (sample->loop_end > i) sample->loop_end = i;
} else {
/* skip truncated data */
dumbfile_skip(f, truncated_size);
// Should we be truncating it?
if (dumbfile_error(f))
return -1;
}
for (i = 0; i < sample->length; i++)
((signed char *)sample->data)[i] ^= 0x80;
}
return 0;
}
static DUMB_IT_SIGDATA *it_669_load_sigdata(DUMBFILE *f, int * ext)
{
DUMB_IT_SIGDATA *sigdata;
int n_channels;
int i;
unsigned char tempolist[128];
unsigned char breaklist[128];
i = dumbfile_igetw(f);
if (i != 0x6669 && i != 0x4E4A) return NULL;
*ext = (i == 0x4E4A);
sigdata = malloc(sizeof(*sigdata));
if (!sigdata) {
return NULL;
}
if (dumbfile_getnc(sigdata->name, 36, f) < 36) {
free(sigdata);
return NULL;
}
sigdata->name[36] = 0;
sigdata->order = NULL;
sigdata->instrument = NULL;
sigdata->pattern = NULL;
sigdata->midi = NULL;
sigdata->checkpoint = NULL;
sigdata->sample = NULL;
sigdata->n_instruments = 0;
sigdata->song_message = malloc(72 + 2 + 1);
if (!sigdata->song_message) {
free(sigdata);
return NULL;
}
if (dumbfile_getnc(sigdata->song_message, 36, f) < 36) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
sigdata->song_message[36] = 13;
sigdata->song_message[36 + 1] = 10;
if (dumbfile_getnc(sigdata->song_message + 38, 36, f) < 36) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
sigdata->song_message[38 + 36] = 0;
sigdata->n_samples = dumbfile_getc(f);
sigdata->n_patterns = dumbfile_getc(f);
sigdata->restart_position = dumbfile_getc(f);
if ((sigdata->n_samples) > 64 || (sigdata->n_patterns > 128)) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
sigdata->order = malloc(128); /* We may need to scan the extra ones! */
if (!sigdata->order) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
if (dumbfile_getnc(sigdata->order, 128, f) < 128) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (i = 0; i < 128; i++) {
if (sigdata->order[i] == 255) break;
if (sigdata->order[i] >= sigdata->n_patterns) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
}
if (!i) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
sigdata->n_orders = i;
if (dumbfile_getnc(tempolist, 128, f) < 128) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
if (dumbfile_getnc(breaklist, 128, f) < 128) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
sigdata->sample = malloc(sigdata->n_samples * sizeof(*sigdata->sample));
if (!sigdata->sample) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (i = 0; i < sigdata->n_samples; i++)
sigdata->sample[i].data = NULL;
for (i = 0; i < sigdata->n_samples; i++) {
if (it_669_read_sample_header(&sigdata->sample[i], f)) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
}
/* May as well try to save a tiny bit of memory. */
if (sigdata->n_orders < 128) {
unsigned char *order = realloc(sigdata->order, sigdata->n_orders);
if (order) sigdata->order = order;
}
sigdata->pattern = malloc(sigdata->n_patterns * sizeof(*sigdata->pattern));
if (!sigdata->pattern) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (i = 0; i < sigdata->n_patterns; i++)
sigdata->pattern[i].entry = NULL;
n_channels = 0;
/* Read in the patterns */
{
unsigned char *buffer = malloc(64 * 3 * 8); /* 64 rows * 3 bytes * 8 channels */
if (!buffer) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (i = 0; i < sigdata->n_patterns; i++) {
if (it_669_read_pattern(&sigdata->pattern[i], f, tempolist[i], breaklist[i], buffer, &n_channels) != 0) {
free(buffer);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
}
free(buffer);
}
sigdata->n_pchannels = n_channels;
/* And finally, the sample data */
for (i = 0; i < sigdata->n_samples; i++) {
if (it_669_read_sample_data(&sigdata->sample[i], f)) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
}
/* Now let's initialise the remaining variables, and we're done! */
sigdata->flags = IT_OLD_EFFECTS | IT_LINEAR_SLIDES | IT_STEREO | IT_WAS_A_669;
sigdata->global_volume = 128;
sigdata->mixing_volume = 48;
sigdata->speed = 4;
sigdata->tempo = 78;
sigdata->pan_separation = 128;
memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS);
for (i = 0; i < DUMB_IT_N_CHANNELS; i += 2) {
sigdata->channel_pan[i+0] = 48;
sigdata->channel_pan[i+1] = 16;
}
_dumb_it_fix_invalid_orders(sigdata);
return sigdata;
}
DUH *DUMBEXPORT dumb_read_669_quick(DUMBFILE *f)
{
sigdata_t *sigdata;
int ext;
DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
sigdata = it_669_load_sigdata(f, &ext);
if (!sigdata)
return NULL;
{
const char *tag[2][2];
tag[0][0] = "TITLE";
tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
tag[1][0] = "FORMAT";
tag[1][1] = ext ? "669 Extended" : "669";
return make_duh(-1, 2, (const char *const (*)[2])tag, 1, &descptr, &sigdata);
}
}

0
dumb/src/it/read6692.c Normal file
View file

757
dumb/src/it/readam.c Normal file
View file

@ -0,0 +1,757 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readam.c - Code to read a RIFF AM module / / \ \
* from a parsed RIFF structure. | < / \_
* | \/ /\ /
* By Chris Moeller. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <string.h>
#include "dumb.h"
#include "internal/it.h"
#include "internal/riff.h"
DUH *dumb_read_riff_amff( struct riff * stream );
DUH *dumb_read_riff_am( struct riff * stream );
static int it_riff_am_process_sample( IT_SAMPLE * sample, const unsigned char * data, int len, int ver )
{
int header_length;
int default_pan;
int default_volume;
int flags;
int length;
int length_bytes;
int loop_start;
int loop_end;
int sample_rate;
if ( ver == 0 )
{
if ( len < 0x38 )
return -1;
header_length = 0x38;
memcpy( sample->name, data, 28 );
sample->name[ 28 ] = 0;
default_pan = data[ 0x1C ];
default_volume = data[ 0x1D ];
flags = data[ 0x1E ] | ( data[ 0x1F ] << 8 );
length = data[ 0x20 ] | ( data[ 0x21 ] << 8 ) | ( data[ 0x22 ] << 16 ) | ( data[ 0x23 ] << 24 );
loop_start = data[ 0x24 ] | ( data[ 0x25 ] << 8 ) | ( data[ 0x26 ] << 16 ) | ( data[ 0x27 ] << 24 );
loop_end = data[ 0x28 ] | ( data[ 0x29 ] << 8 ) | ( data[ 0x2A ] << 16 ) | ( data[ 0x2B ] << 24 );
sample_rate = data[ 0x2C ] | ( data[ 0x2D ] << 8 ) | ( data[ 0x2E ] << 16 ) | ( data[ 0x2F ] << 24 );
}
else
{
if (len < 4) return -1;
header_length = data[ 0 ] | ( data[ 1 ] << 8 ) | ( data[ 2 ] << 16 ) | ( data[ 3 ] << 24 );
if ( header_length < 0x40 )
return -1;
if ( header_length + 4 > len )
return -1;
data += 4;
len -= 4;
memcpy( sample->name, data, 32 );
sample->name[ 32 ] = 0;
default_pan = data[ 0x20 ] | ( data[ 0x21 ] << 8 );
default_volume = data[ 0x22 ] | ( data[ 0x23 ] << 8 );
flags = data[ 0x24 ] | ( data[ 0x25 ] << 8 ); /* | ( data[ 0x26 ] << 16 ) | ( data[ 0x27 ] << 24 );*/
length = data[ 0x28 ] | ( data[ 0x29 ] << 8 ) | ( data[ 0x2A ] << 16 ) | ( data[ 0x2B ] << 24 );
loop_start = data[ 0x2C ] | ( data[ 0x2D ] << 8 ) | ( data[ 0x2E ] << 16 ) | ( data[ 0x2F ] << 24 );
loop_end = data[ 0x30 ] | ( data[ 0x31 ] << 8 ) | ( data[ 0x32 ] << 16 ) | ( data[ 0x33 ] << 24 );
sample_rate = data[ 0x34 ] | ( data[ 0x35 ] << 8 ) | ( data[ 0x36 ] << 16 ) | ( data[ 0x37 ] << 24 );
if ( default_pan > 0x7FFF || default_volume > 0x7FFF )
return -1;
default_pan = default_pan * 64 / 32767;
default_volume = default_volume * 64 / 32767;
}
/*if ( data[ 0x38 ] || data[ 0x39 ] || data[ 0x3A ] || data[ 0x3B ] )
return -1;*/
if ( ! length ) {
sample->flags &= ~IT_SAMPLE_EXISTS;
return 0;
}
if ( flags & ~( 0x8000 | 0x80 | 0x20 | 0x10 | 0x08 | 0x04 ) )
return -1;
length_bytes = length << ( ( flags & 0x04 ) >> 2 );
if ( length_bytes + header_length > len )
return -1;
sample->flags = 0;
if ( flags & 0x80 ) sample->flags |= IT_SAMPLE_EXISTS;
if ( flags & 0x04 ) sample->flags |= IT_SAMPLE_16BIT;
sample->length = length;
sample->loop_start = loop_start;
sample->loop_end = loop_end;
sample->C5_speed = sample_rate;
sample->default_volume = default_volume;
sample->default_pan = default_pan | ( ( flags & 0x20 ) << 2 );
sample->filename[0] = 0;
sample->global_volume = 64;
sample->vibrato_speed = 0;
sample->vibrato_depth = 0;
sample->vibrato_rate = 0;
sample->vibrato_waveform = IT_VIBRATO_SINE;
sample->finetune = 0;
sample->max_resampling_quality = -1;
if ( flags & 0x08 )
{
if (((unsigned int)sample->loop_end <= (unsigned int)sample->length) &&
((unsigned int)sample->loop_start < (unsigned int)sample->loop_end))
{
sample->length = sample->loop_end;
sample->flags |= IT_SAMPLE_LOOP;
if ( flags & 0x10 ) sample->flags |= IT_SAMPLE_PINGPONG_LOOP;
}
}
length_bytes = sample->length << ( ( flags & 0x04 ) >> 2 );
sample->data = malloc( length_bytes );
if ( ! sample->data )
return -1;
memcpy( sample->data, data + header_length, length_bytes );
return 0;
}
static int it_riff_am_process_pattern( IT_PATTERN * pattern, const unsigned char * data, int len, int ver )
{
int nrows, row, pos;
unsigned flags;
IT_ENTRY * entry;
nrows = data[0] + 1;
pattern->n_rows = nrows;
data += 1;
len -= 1;
pattern->n_entries = 0;
row = 0;
pos = 0;
while ( (row < nrows) && (pos < len) ) {
if ( ! data[ pos ] ) {
++ row;
++ pos;
continue;
}
flags = data[ pos++ ] & 0xE0;
if (flags) {
++ pattern->n_entries;
if (flags & 0x80) pos += 2;
if (flags & 0x40) pos += 2;
if (flags & 0x20) pos ++;
}
}
if ( ! pattern->n_entries ) return 0;
pattern->n_entries += nrows;
pattern->entry = malloc( pattern->n_entries * sizeof( * pattern->entry ) );
if ( ! pattern->entry ) return -1;
entry = pattern->entry;
row = 0;
pos = 0;
while ( ( row < nrows ) && ( pos < len ) )
{
if ( ! data[ pos ] )
{
IT_SET_END_ROW( entry );
++ entry;
++ row;
++ pos;
continue;
}
flags = data[ pos++ ];
entry->channel = flags & 0x1F;
entry->mask = 0;
if (flags & 0xE0)
{
if ( flags & 0x80 )
{
_dumb_it_xm_convert_effect( data[ pos + 1 ], data[ pos ], entry, 0 );
pos += 2;
}
if ( flags & 0x40 )
{
if ( data[ pos ] )
{
entry->mask |= IT_ENTRY_INSTRUMENT;
entry->instrument = data[ pos ];
}
if ( data[ pos + 1 ] )
{
entry->mask |= IT_ENTRY_NOTE;
entry->note = data[ pos + 1 ] - 1;
}
pos += 2;
}
if ( flags & 0x20 )
{
entry->mask |= IT_ENTRY_VOLPAN;
if ( ver == 0 ) entry->volpan = data[ pos ];
else entry->volpan = data[ pos ] * 64 / 127;
++ pos;
}
if (entry->mask) entry++;
}
}
while ( row < nrows )
{
IT_SET_END_ROW( entry );
++ entry;
++ row;
}
pattern->n_entries = (int)(entry - pattern->entry);
if ( ! pattern->n_entries ) return -1;
return 0;
}
static DUMB_IT_SIGDATA *it_riff_amff_load_sigdata( struct riff * stream )
{
DUMB_IT_SIGDATA *sigdata;
int n, found;
unsigned int o;
unsigned char * ptr;
if ( ! stream ) goto error;
if ( stream->type != DUMB_ID( 'A', 'M', 'F', 'F' ) ) goto error;
sigdata = malloc( sizeof( *sigdata ) );
if ( ! sigdata ) goto error;
sigdata->n_patterns = 0;
sigdata->n_samples = 0;
sigdata->name[0] = 0;
found = 0;
for ( n = 0; (unsigned int)n < stream->chunk_count; ++n )
{
struct riff_chunk * c = stream->chunks + n;
switch( c->type )
{
case DUMB_ID( 'M', 'A', 'I', 'N' ):
/* initialization data */
if ( ( found & 1 ) || ( c->size < 0x48 ) ) goto error_sd;
found |= 1;
break;
case DUMB_ID( 'O', 'R', 'D', 'R' ):
if ( ( found & 2 ) || ( c->size < 1 ) ) goto error_sd;
found |= 2;
break;
case DUMB_ID( 'P', 'A', 'T', 'T' ):
ptr = ( unsigned char * ) c->data;
if ( ptr[ 0 ] >= sigdata->n_patterns ) sigdata->n_patterns = ptr[ 0 ] + 1;
o = ptr[ 1 ] | ( ptr[ 2 ] << 8 ) | ( ptr[ 3 ] << 16 ) | ( ptr[ 4 ] << 24 );
if ( o + 5 > c->size ) goto error_sd;
break;
case DUMB_ID( 'I', 'N', 'S', 'T' ):
{
if ( c->size < 0xE1 ) goto error;
ptr = ( unsigned char * ) c->data;
if ( ptr[ 1 ] >= sigdata->n_samples ) sigdata->n_samples = ptr[ 1 ] + 1;
if ( c->size >= 0x121 && ( ptr[ 0xE1 ] == 'S' && ptr[ 0xE2 ] == 'A' &&
ptr[ 0xE3 ] == 'M' && ptr[ 0xE4 ] == 'P' ) )
{
unsigned size = ptr[ 0xE5 ] | ( ptr[ 0xE6 ] << 8 ) | ( ptr[ 0xE7 ] << 16 ) | ( ptr[ 0xE8 ] << 24 );
if ( size + 0xE1 + 8 > c->size ) goto error;
}
}
break;
}
}
if ( found != 3 || !sigdata->n_samples || !sigdata->n_patterns ) goto error_sd;
if ( sigdata->n_samples > 255 || sigdata->n_patterns > 255 ) goto error_sd;
sigdata->song_message = NULL;
sigdata->order = NULL;
sigdata->instrument = NULL;
sigdata->sample = NULL;
sigdata->pattern = NULL;
sigdata->midi = NULL;
sigdata->checkpoint = NULL;
sigdata->mixing_volume = 48;
sigdata->pan_separation = 128;
sigdata->n_instruments = 0;
sigdata->n_orders = 0;
memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS);
for (n = 0; n < DUMB_IT_N_CHANNELS; n += 4) {
sigdata->channel_pan[n ] = 16;
sigdata->channel_pan[n+1] = 48;
sigdata->channel_pan[n+2] = 48;
sigdata->channel_pan[n+3] = 16;
}
for ( n = 0; (unsigned int)n < stream->chunk_count; ++n )
{
struct riff_chunk * c = stream->chunks + n;
switch ( c->type )
{
case DUMB_ID( 'M', 'A', 'I', 'N' ):
ptr = ( unsigned char * ) c->data;
memcpy( sigdata->name, c->data, 64 );
sigdata->name[ 64 ] = 0;
sigdata->flags = IT_STEREO | IT_OLD_EFFECTS | IT_COMPATIBLE_GXX | IT_WAS_AN_S3M;
if ( ! ( ptr[ 0x40 ] & 1 ) ) sigdata->flags |= IT_LINEAR_SLIDES;
if ( ( ptr[ 0x40 ] & ~3 ) || ! ( ptr[ 0x40 ] & 2 ) ) goto error_usd; // unknown flags
sigdata->n_pchannels = ptr[ 0x41 ];
sigdata->speed = ptr[ 0x42 ];
sigdata->tempo = ptr[ 0x43 ];
sigdata->global_volume = ptr[ 0x48 ];
if ( (int)c->size < 0x48 + sigdata->n_pchannels ) goto error_usd;
for ( o = 0; (int)o < sigdata->n_pchannels; ++o )
{
sigdata->channel_pan[ o ] = ptr[ 0x49 + o ];
if ( ptr[ 0x49 + o ] >= 128 )
{
sigdata->channel_volume[ o ] = 0;
}
}
break;
}
}
sigdata->pattern = malloc( sigdata->n_patterns * sizeof( *sigdata->pattern ) );
if ( ! sigdata->pattern ) goto error_usd;
for ( n = 0; n < sigdata->n_patterns; ++n )
sigdata->pattern[ n ].entry = NULL;
sigdata->sample = malloc( sigdata->n_samples * sizeof( *sigdata->sample ) );
if ( ! sigdata->sample ) goto error_usd;
for ( n = 0; n < sigdata->n_samples; ++n )
{
IT_SAMPLE * sample = sigdata->sample + n;
sample->data = NULL;
sample->flags = 0;
sample->name[ 0 ] = 0;
}
for ( n = 0; (unsigned int)n < stream->chunk_count; ++n )
{
struct riff_chunk * c = stream->chunks + n;
switch ( c->type )
{
case DUMB_ID( 'O', 'R', 'D', 'R' ):
ptr = ( unsigned char * ) c->data;
sigdata->n_orders = ptr[ 0 ] + 1;
if ( sigdata->n_orders + 1 > (int)c->size ) goto error_usd;
sigdata->order = malloc( sigdata->n_orders );
if ( ! sigdata->order ) goto error_usd;
memcpy( sigdata->order, ptr + 1, sigdata->n_orders );
break;
case DUMB_ID( 'P', 'A', 'T', 'T' ):
ptr = ( unsigned char * ) c->data;
o = ptr[ 1 ] | ( ptr[ 2 ] << 8 ) | ( ptr[ 3 ] << 16 ) | ( ptr[ 4 ] << 24 );
if ( it_riff_am_process_pattern( sigdata->pattern + ptr[ 0 ], ptr + 5, o, 0 ) ) goto error_usd;
break;
case DUMB_ID( 'I', 'N', 'S', 'T' ):
{
IT_SAMPLE * sample;
ptr = ( unsigned char * ) c->data;
sample = sigdata->sample + ptr[ 1 ];
if ( c->size >= 0x121 && ( ptr[ 0xE1 ] == 'S' && ptr[ 0xE2 ] == 'A' &&
ptr[ 0xE3 ] == 'M' && ptr[ 0xE4 ] == 'P' ) )
{
unsigned size = ptr[ 0xE5 ] | ( ptr[ 0xE6 ] << 8 ) | ( ptr[ 0xE7 ] << 16 ) | ( ptr[ 0xE8 ] << 24 );
if ( it_riff_am_process_sample( sample, ptr + 0xE1 + 8, size, 0 ) ) goto error_usd;
}
else
{
memcpy( sample->name, ptr + 2, 28 );
sample->name[ 28 ] = 0;
}
}
break;
}
}
_dumb_it_fix_invalid_orders( sigdata );
return sigdata;
error_usd:
_dumb_it_unload_sigdata( sigdata );
goto error;
error_sd:
free( sigdata );
error:
return NULL;
}
static DUMB_IT_SIGDATA *it_riff_am_load_sigdata( struct riff * stream )
{
DUMB_IT_SIGDATA *sigdata;
int n, o, p, found;
unsigned char * ptr;
if ( ! stream ) goto error;
if ( stream->type != DUMB_ID( 'A', 'M', ' ', ' ' ) ) goto error;
sigdata = malloc(sizeof(*sigdata));
if ( ! sigdata ) goto error;
sigdata->n_patterns = 0;
sigdata->n_samples = 0;
sigdata->name[0] = 0;
found = 0;
for ( n = 0; (unsigned int)n < stream->chunk_count; ++n )
{
struct riff_chunk * c = stream->chunks + n;
switch( c->type )
{
case DUMB_ID( 'I' ,'N' ,'I' ,'T' ):
/* initialization data */
if ( ( found & 1 ) || ( c->size < 0x48 ) ) goto error_sd;
found |= 1;
break;
case DUMB_ID( 'O', 'R', 'D', 'R' ):
if ( ( found & 2 ) || ( c->size < 1 ) ) goto error_sd;
found |= 2;
break;
case DUMB_ID( 'P', 'A', 'T', 'T' ):
ptr = ( unsigned char * ) c->data;
if ( ptr[ 0 ] >= sigdata->n_patterns ) sigdata->n_patterns = ptr[ 0 ] + 1;
o = ptr[ 1 ] | ( ptr[ 2 ] << 8 ) | ( ptr[ 3 ] << 16 ) | ( ptr[ 4 ] << 24 );
if ( o + 5 > (int)c->size ) goto error_sd;
break;
case DUMB_ID( 'R', 'I', 'F', 'F' ):
{
struct riff * str = ( struct riff * ) c->data;
switch ( str->type )
{
case DUMB_ID( 'A', 'I', ' ', ' ' ):
for ( o = 0; (unsigned int)o < str->chunk_count; ++o )
{
struct riff_chunk * chk = str->chunks + o;
switch( chk->type )
{
case DUMB_ID( 'I', 'N', 'S', 'T' ):
{
struct riff * temp;
unsigned size;
unsigned sample_found;
ptr = ( unsigned char * ) chk->data;
size = ptr[ 0 ] | ( ptr[ 1 ] << 8 ) | ( ptr[ 2 ] << 16 ) | ( ptr[ 3 ] << 24 );
if ( size < 0x142 ) goto error;
sample_found = 0;
if ( ptr[ 5 ] >= sigdata->n_samples ) sigdata->n_samples = ptr[ 5 ] + 1;
temp = riff_parse( ptr + 4 + size, chk->size - size - 4, 1 );
if ( temp )
{
if ( temp->type == DUMB_ID( 'A', 'S', ' ', ' ' ) )
{
for ( p = 0; (unsigned int)p < temp->chunk_count; ++p )
{
if ( temp->chunks[ p ].type == DUMB_ID( 'S', 'A', 'M', 'P' ) )
{
if ( sample_found )
{
riff_free( temp );
goto error;
}
sample_found = 1;
}
}
}
riff_free( temp );
}
}
}
}
}
}
break;
}
}
if ( found != 3 || !sigdata->n_samples || !sigdata->n_patterns ) goto error_sd;
if ( sigdata->n_samples > 255 || sigdata->n_patterns > 255 ) goto error_sd;
sigdata->song_message = NULL;
sigdata->order = NULL;
sigdata->instrument = NULL;
sigdata->sample = NULL;
sigdata->pattern = NULL;
sigdata->midi = NULL;
sigdata->checkpoint = NULL;
sigdata->mixing_volume = 48;
sigdata->pan_separation = 128;
sigdata->n_instruments = 0;
sigdata->n_orders = 0;
memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS);
for (n = 0; n < DUMB_IT_N_CHANNELS; n += 4) {
sigdata->channel_pan[n ] = 16;
sigdata->channel_pan[n+1] = 48;
sigdata->channel_pan[n+2] = 48;
sigdata->channel_pan[n+3] = 16;
}
for ( n = 0; (unsigned int)n < stream->chunk_count; ++n )
{
struct riff_chunk * c = stream->chunks + n;
switch ( c->type )
{
case DUMB_ID( 'I', 'N', 'I', 'T' ):
ptr = ( unsigned char * ) c->data;
memcpy( sigdata->name, c->data, 64 );
sigdata->name[ 64 ] = 0;
sigdata->flags = IT_STEREO | IT_OLD_EFFECTS | IT_COMPATIBLE_GXX | IT_WAS_AN_S3M;
if ( ! ( ptr[ 0x40 ] & 1 ) ) sigdata->flags |= IT_LINEAR_SLIDES;
if ( ( ptr[ 0x40 ] & ~3 ) || ! ( ptr[ 0x40 ] & 2 ) ) goto error_usd; // unknown flags
sigdata->n_pchannels = ptr[ 0x41 ];
sigdata->speed = ptr[ 0x42 ];
sigdata->tempo = ptr[ 0x43 ];
sigdata->global_volume = ptr[ 0x48 ];
if ( (int)c->size < 0x48 + sigdata->n_pchannels ) goto error_usd;
for ( o = 0; o < sigdata->n_pchannels; ++o )
{
if ( ptr[ 0x49 + o ] <= 128 )
{
sigdata->channel_pan[ o ] = ptr[ 0x49 + o ] / 2;
}
else
{
sigdata->channel_volume[ o ] = 0;
}
}
break;
}
}
sigdata->pattern = malloc( sigdata->n_patterns * sizeof( *sigdata->pattern ) );
if ( ! sigdata->pattern ) goto error_usd;
for ( n = 0; n < sigdata->n_patterns; ++n )
sigdata->pattern[ n ].entry = NULL;
sigdata->sample = malloc( sigdata->n_samples * sizeof( *sigdata->sample ) );
if ( ! sigdata->sample ) goto error_usd;
for ( n = 0; n < sigdata->n_samples; ++n )
{
IT_SAMPLE * sample = sigdata->sample + n;
sample->data = NULL;
sample->flags = 0;
sample->name[ 0 ] = 0;
}
for ( n = 0; (unsigned int)n < stream->chunk_count; ++n )
{
struct riff_chunk * c = stream->chunks + n;
switch ( c->type )
{
case DUMB_ID( 'O', 'R', 'D', 'R' ):
ptr = ( unsigned char * ) c->data;
sigdata->n_orders = ptr[ 0 ] + 1;
if ( sigdata->n_orders + 1 > (int)c->size ) goto error_usd;
sigdata->order = malloc( sigdata->n_orders );
if ( ! sigdata->order ) goto error_usd;
memcpy( sigdata->order, ptr + 1, sigdata->n_orders );
break;
case DUMB_ID( 'P', 'A', 'T', 'T' ):
ptr = ( unsigned char * ) c->data;
o = ptr[ 1 ] | ( ptr[ 2 ] << 8 ) | ( ptr[ 3 ] << 16 ) | ( ptr[ 4 ] << 24 );
if ( it_riff_am_process_pattern( sigdata->pattern + ptr[ 0 ], ptr + 5, o, 1 ) ) goto error_usd;
break;
case DUMB_ID( 'R', 'I', 'F', 'F' ):
{
struct riff * str = ( struct riff * ) c->data;
switch ( str->type )
{
case DUMB_ID('A', 'I', ' ', ' '):
for ( o = 0; (unsigned int)o < str->chunk_count; ++o )
{
struct riff_chunk * chk = str->chunks + o;
switch( chk->type )
{
case DUMB_ID( 'I', 'N', 'S', 'T' ):
{
struct riff * temp;
unsigned size;
unsigned sample_found;
IT_SAMPLE * sample;
ptr = ( unsigned char * ) chk->data;
size = ptr[ 0 ] | ( ptr[ 1 ] << 8 ) | ( ptr[ 2 ] << 16 ) | ( ptr[ 3 ] << 24 );
temp = riff_parse( ptr + 4 + size, chk->size - size - 4, 1 );
sample_found = 0;
sample = sigdata->sample + ptr[ 5 ];
if ( temp )
{
if ( temp->type == DUMB_ID( 'A', 'S', ' ', ' ' ) )
{
for ( p = 0; (unsigned int)p < temp->chunk_count; ++p )
{
struct riff_chunk * c = temp->chunks + p;
if ( c->type == DUMB_ID( 'S', 'A', 'M', 'P' ) )
{
if ( sample_found )
{
riff_free( temp );
goto error_usd;
}
if ( it_riff_am_process_sample( sigdata->sample + ptr[ 5 ], ( unsigned char * ) c->data, c->size, 1 ) )
{
riff_free( temp );
goto error_usd;
}
sample_found = 1;
}
}
}
riff_free( temp );
}
if ( ! sample_found )
{
memcpy( sample->name, ptr + 6, 32 );
sample->name[ 32 ] = 0;
}
}
}
}
}
}
break;
}
}
_dumb_it_fix_invalid_orders( sigdata );
return sigdata;
error_usd:
_dumb_it_unload_sigdata( sigdata );
goto error;
error_sd:
free( sigdata );
error:
return NULL;
}
DUH *dumb_read_riff_amff( struct riff * stream )
{
sigdata_t *sigdata;
int32 length;
DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
sigdata = it_riff_amff_load_sigdata( stream );
if (!sigdata)
return NULL;
length = 0;/*_dumb_it_build_checkpoints(sigdata, 0);*/
{
const char *tag[2][2];
tag[0][0] = "TITLE";
tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
tag[1][0] = "FORMAT";
tag[1][1] = "RIFF AMFF";
return make_duh( length, 2, ( const char * const (*) [ 2 ] ) tag, 1, & descptr, & sigdata );
}
}
DUH *dumb_read_riff_am( struct riff * stream )
{
sigdata_t *sigdata;
DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
sigdata = it_riff_am_load_sigdata( stream );
if (!sigdata)
return NULL;
{
const char *tag[2][2];
tag[0][0] = "TITLE";
tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
tag[1][0] = "FORMAT";
tag[1][1] = "RIFF AM";
return make_duh( -1, 2, ( const char * const (*) [ 2 ] ) tag, 1, & descptr, & sigdata );
}
}

331
dumb/src/it/readasy.c Normal file
View file

@ -0,0 +1,331 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readasy.c - Code to read an ASYLUM Music Format / / \ \
* module from an open file. | < / \_
* | \/ /\ /
* By Chris Moeller. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "dumb.h"
#include "internal/it.h"
static int it_asy_read_pattern( IT_PATTERN *pattern, DUMBFILE *f, unsigned char *buffer )
{
int pos;
int channel;
int row;
IT_ENTRY *entry;
pattern->n_rows = 64;
if ( dumbfile_getnc( buffer, 64 * 8 * 4, f ) != 64 * 8 * 4 )
return -1;
/* compute number of entries */
pattern->n_entries = 64; /* Account for the row end markers */
pos = 0;
for ( row = 0; row < 64; ++row ) {
for ( channel = 0; channel < 8; ++channel ) {
if ( buffer[ pos + 0 ] | buffer[ pos + 1 ] | buffer[ pos + 2 ] | buffer[ pos + 3 ] )
++pattern->n_entries;
pos += 4;
}
}
pattern->entry = malloc( pattern->n_entries * sizeof( *pattern->entry ) );
if ( !pattern->entry )
return -1;
entry = pattern->entry;
pos = 0;
for ( row = 0; row < 64; ++row ) {
for ( channel = 0; channel < 8; ++channel ) {
if ( buffer[ pos + 0 ] | buffer[ pos + 1 ] | buffer[ pos + 2 ] | buffer[ pos + 3 ] ) {
entry->channel = channel;
entry->mask = 0;
if ( buffer[ pos + 0 ] && buffer[ pos + 0 ] < 96 ) {
entry->note = buffer[ pos + 0 ];
entry->mask |= IT_ENTRY_NOTE;
}
if ( buffer[ pos + 1 ] && buffer[ pos + 1 ] <= 64 ) {
entry->instrument = buffer[ pos + 1 ];
entry->mask |= IT_ENTRY_INSTRUMENT;
}
_dumb_it_xm_convert_effect( buffer[ pos + 2 ] & 0x0F, buffer[ pos + 3 ], entry, 1 );
if ( entry->mask ) ++entry;
}
pos += 4;
}
IT_SET_END_ROW( entry );
++entry;
}
pattern->n_entries = (int)(entry - pattern->entry);
return 0;
}
static int it_asy_read_sample_header( IT_SAMPLE *sample, DUMBFILE *f )
{
int finetune;
/**
21 22 Chars Sample 1 name. If the name is not a full
22 chars in length, it will be null
terminated.
If
the sample name begins with a '#' character (ASCII $23 (35)) then this is
assumed not to be an instrument name, and is probably a message.
*/
dumbfile_getnc( sample->name, 22, f );
sample->name[22] = 0;
sample->filename[0] = 0;
/** Each finetune step changes the note 1/8th of a semitone. */
finetune = ( signed char ) ( dumbfile_getc( f ) << 4 ) >> 4; /* signed nibble */
sample->default_volume = dumbfile_getc( f ); // Should we be setting global_volume to this instead?
sample->global_volume = 64;
if ( sample->default_volume > 64 ) sample->default_volume = 64;
dumbfile_skip( f, 1 ); /* XXX unknown */
sample->length = dumbfile_igetl( f );
sample->loop_start = dumbfile_igetl( f );
sample->loop_end = sample->loop_start + dumbfile_igetl( f );
if ( sample->length <= 0 ) {
sample->flags = 0;
return 0;
}
sample->flags = IT_SAMPLE_EXISTS;
sample->default_pan = 0;
sample->C5_speed = (int)( AMIGA_CLOCK / 214.0 );//( int32 )( 16726.0 * pow( DUMB_PITCH_BASE, finetune * 32 ) );
sample->finetune = finetune * 32;
// the above line might be wrong
if ( ( sample->loop_end - sample->loop_start > 2 ) && ( sample->loop_end <= sample->length ) )
sample->flags |= IT_SAMPLE_LOOP;
sample->vibrato_speed = 0;
sample->vibrato_depth = 0;
sample->vibrato_rate = 0;
sample->vibrato_waveform = 0; // do we have to set _all_ these?
sample->max_resampling_quality = -1;
return dumbfile_error(f);
}
static int it_asy_read_sample_data( IT_SAMPLE *sample, DUMBFILE *f )
{
int32 truncated_size;
/* let's get rid of the sample data coming after the end of the loop */
if ( ( sample->flags & IT_SAMPLE_LOOP ) && sample->loop_end < sample->length ) {
truncated_size = sample->length - sample->loop_end;
sample->length = sample->loop_end;
} else {
truncated_size = 0;
}
sample->data = malloc( sample->length );
if ( !sample->data )
return -1;
if ( sample->length )
dumbfile_getnc( sample->data, sample->length, f );
dumbfile_skip( f, truncated_size );
return dumbfile_error( f );
}
static DUMB_IT_SIGDATA *it_asy_load_sigdata(DUMBFILE *f)
{
DUMB_IT_SIGDATA *sigdata;
int i;
static const char sig_part[] = "ASYLUM Music Format";
static const char sig_rest[] = " V1.0"; /* whee, string space optimization with format type below */
char signature [32];
if ( dumbfile_getnc( signature, 32, f ) != 32 ||
memcmp( signature, sig_part, 19 ) ||
memcmp( signature + 19, sig_rest, 5 ) ) {
return NULL;
}
sigdata = malloc(sizeof(*sigdata));
if (!sigdata) {
return NULL;
}
sigdata->speed = dumbfile_getc( f ); /* XXX seems to fit the files I have */
sigdata->tempo = dumbfile_getc( f ); /* ditto */
sigdata->n_samples = dumbfile_getc( f ); /* ditto */
sigdata->n_patterns = dumbfile_getc( f );
sigdata->n_orders = dumbfile_getc( f );
sigdata->restart_position = dumbfile_getc( f );
if ( dumbfile_error( f ) || !sigdata->n_samples || sigdata->n_samples > 64 || !sigdata->n_patterns ||
!sigdata->n_orders ) {
free( sigdata );
return NULL;
}
if ( sigdata->restart_position > sigdata->n_orders ) /* XXX */
sigdata->restart_position = 0;
sigdata->order = malloc( sigdata->n_orders );
if ( !sigdata->order ) {
free( sigdata );
return NULL;
}
if ( dumbfile_getnc( sigdata->order, sigdata->n_orders, f ) != sigdata->n_orders ||
dumbfile_skip( f, 256 - sigdata->n_orders ) ) {
free( sigdata->order );
free( sigdata );
return NULL;
}
sigdata->sample = malloc( sigdata->n_samples * sizeof( *sigdata->sample ) );
if ( !sigdata->sample ) {
free( sigdata->order );
free( sigdata );
return NULL;
}
sigdata->song_message = NULL;
sigdata->instrument = NULL;
sigdata->pattern = NULL;
sigdata->midi = NULL;
sigdata->checkpoint = NULL;
sigdata->n_instruments = 0;
for ( i = 0; i < sigdata->n_samples; ++i )
sigdata->sample[i].data = NULL;
for ( i = 0; i < sigdata->n_samples; ++i ) {
if ( it_asy_read_sample_header( &sigdata->sample[i], f ) ) {
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
}
if ( dumbfile_skip( f, 37 * ( 64 - sigdata->n_samples ) ) ) {
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
sigdata->pattern = malloc( sigdata->n_patterns * sizeof( *sigdata->pattern ) );
if ( !sigdata->pattern ) {
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
for (i = 0; i < sigdata->n_patterns; ++i)
sigdata->pattern[i].entry = NULL;
/* Read in the patterns */
{
unsigned char *buffer = malloc( 64 * 8 * 4 ); /* 64 rows * 8 channels * 4 bytes */
if ( !buffer ) {
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
for ( i = 0; i < sigdata->n_patterns; ++i ) {
if ( it_asy_read_pattern( &sigdata->pattern[i], f, buffer ) != 0 ) {
free( buffer );
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
}
free( buffer );
}
/* And finally, the sample data */
for ( i = 0; i < sigdata->n_samples; ++i ) {
if ( it_asy_read_sample_data( &sigdata->sample[i], f ) ) {
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
}
/* Now let's initialise the remaining variables, and we're done! */
sigdata->flags = IT_WAS_AN_XM | IT_WAS_A_MOD | IT_OLD_EFFECTS | IT_COMPATIBLE_GXX | IT_STEREO;
sigdata->global_volume = 128;
sigdata->mixing_volume = 48;
sigdata->pan_separation = 128;
sigdata->n_pchannels = 8;
sigdata->name[0] = 0;
memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS);
for (i = 0; i < DUMB_IT_N_CHANNELS; i += 4) {
sigdata->channel_pan[i+0] = 16;
sigdata->channel_pan[i+1] = 48;
sigdata->channel_pan[i+2] = 48;
sigdata->channel_pan[i+3] = 16;
}
_dumb_it_fix_invalid_orders(sigdata);
return sigdata;
}
DUH *DUMBEXPORT dumb_read_asy_quick(DUMBFILE *f)
{
sigdata_t *sigdata;
DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
sigdata = it_asy_load_sigdata(f);
if (!sigdata)
return NULL;
{
const char *tag[2][2];
tag[0][0] = "TITLE";
tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
tag[1][0] = "FORMAT";
tag[1][1] = "ASYLUM Music Format";
return make_duh(-1, 2, (const char *const (*)[2])tag, 1, &descptr, &sigdata);
}
}

375
dumb/src/it/readdsmf.c Normal file
View file

@ -0,0 +1,375 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readam.c - Code to read a RIFF DSMF module / / \ \
* from a parsed RIFF structure. | < / \_
* | \/ /\ /
* By Chris Moeller. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <string.h>
#include "dumb.h"
#include "internal/it.h"
#include "internal/riff.h"
DUH *dumb_read_riff_dsmf( struct riff * stream );
static int it_riff_dsmf_process_sample( IT_SAMPLE * sample, const unsigned char * data, int len )
{
int flags;
memcpy( sample->filename, data, 13 );
sample->filename[ 14 ] = 0;
flags = data[ 13 ] | ( data[ 14 ] << 8 );
sample->default_volume = data[ 15 ];
sample->length = data[ 16 ] | ( data[ 17 ] << 8 ) | ( data[ 18 ] << 16 ) | ( data[ 19 ] << 24 );
sample->loop_start = data[ 20 ] | ( data[ 21 ] << 8 ) | ( data[ 22 ] << 16 ) | ( data[ 23 ] << 24 );
sample->loop_end = data[ 24 ] | ( data[ 25 ] << 8 ) | ( data[ 26 ] << 16 ) | ( data[ 27 ] << 24 );
sample->C5_speed = ( data[ 32 ] | ( data[ 33 ] << 8 ) ) * 2;
memcpy( sample->name, data + 36, 28 );
sample->name[ 28 ] = 0;
/*if ( data[ 0x38 ] || data[ 0x39 ] || data[ 0x3A ] || data[ 0x3B ] )
return -1;*/
if ( ! sample->length ) {
sample->flags &= ~IT_SAMPLE_EXISTS;
return 0;
}
/*if ( flags & ~( 2 | 1 ) )
return -1;*/
if ( sample->length + 64 > len )
return -1;
sample->flags = IT_SAMPLE_EXISTS;
sample->default_pan = 0;
sample->global_volume = 64;
sample->vibrato_speed = 0;
sample->vibrato_depth = 0;
sample->vibrato_rate = 0;
sample->vibrato_waveform = IT_VIBRATO_SINE;
sample->finetune = 0;
sample->max_resampling_quality = -1;
if ( flags & 1 )
{
if (((unsigned int)sample->loop_end <= (unsigned int)sample->length) &&
((unsigned int)sample->loop_start < (unsigned int)sample->loop_end))
{
sample->length = sample->loop_end;
sample->flags |= IT_SAMPLE_LOOP;
if ( flags & 0x10 ) sample->flags |= IT_SAMPLE_PINGPONG_LOOP;
}
}
sample->data = malloc( sample->length );
if ( ! sample->data )
return -1;
memcpy( sample->data, data + 64, sample->length );
if ( ! ( flags & 2 ) )
{
for ( flags = 0; flags < sample->length; ++flags )
( ( signed char * ) sample->data ) [ flags ] ^= 0x80;
}
return 0;
}
static int it_riff_dsmf_process_pattern( IT_PATTERN * pattern, const unsigned char * data, int len )
{
int length, row, pos;
unsigned flags;
IT_ENTRY * entry;
length = data[ 0 ] | ( data[ 1 ] << 8 );
if ( length > len ) return -1;
data += 2;
len = length - 2;
pattern->n_rows = 64;
pattern->n_entries = 64;
row = 0;
pos = 0;
while ( (row < 64) && (pos < len) ) {
if ( ! data[ pos ] ) {
++ row;
++ pos;
continue;
}
flags = data[ pos++ ] & 0xF0;
if (flags) {
++ pattern->n_entries;
if (flags & 0x80) pos ++;
if (flags & 0x40) pos ++;
if (flags & 0x20) pos ++;
if (flags & 0x10) pos += 2;
}
}
if ( pattern->n_entries == 64 ) return 0;
pattern->entry = malloc( pattern->n_entries * sizeof( * pattern->entry ) );
if ( ! pattern->entry ) return -1;
entry = pattern->entry;
row = 0;
pos = 0;
while ( ( row < 64 ) && ( pos < len ) )
{
if ( ! data[ pos ] )
{
IT_SET_END_ROW( entry );
++ entry;
++ row;
++ pos;
continue;
}
flags = data[ pos++ ];
entry->channel = flags & 0x0F;
entry->mask = 0;
if ( flags & 0xF0 )
{
if ( flags & 0x80 )
{
if ( data[ pos ] )
{
entry->mask |= IT_ENTRY_NOTE;
entry->note = data[ pos ] - 1;
}
++ pos;
}
if ( flags & 0x40 )
{
if ( data[ pos ] )
{
entry->mask |= IT_ENTRY_INSTRUMENT;
entry->instrument = data[ pos ];
}
++ pos;
}
if ( flags & 0x20 )
{
entry->mask |= IT_ENTRY_VOLPAN;
entry->volpan = data[ pos ];
++ pos;
}
if ( flags & 0x10 )
{
_dumb_it_xm_convert_effect( data[ pos ], data[ pos + 1 ], entry, 0 );
pos += 2;
}
if (entry->mask) entry++;
}
}
while ( row < 64 )
{
IT_SET_END_ROW( entry );
++ entry;
++ row;
}
pattern->n_entries = (int)(entry - pattern->entry);
if ( ! pattern->n_entries ) return -1;
return 0;
}
static DUMB_IT_SIGDATA *it_riff_dsmf_load_sigdata( struct riff * stream )
{
DUMB_IT_SIGDATA *sigdata;
int n, o, found;
unsigned char * ptr;
if ( ! stream ) goto error;
if ( stream->type != DUMB_ID( 'D', 'S', 'M', 'F' ) ) goto error;
sigdata = malloc(sizeof(*sigdata));
if ( ! sigdata ) goto error;
sigdata->n_patterns = 0;
sigdata->n_samples = 0;
sigdata->name[0] = 0;
found = 0;
for ( n = 0; (unsigned int)n < stream->chunk_count; ++n )
{
struct riff_chunk * c = stream->chunks + n;
switch( c->type )
{
case DUMB_ID( 'S' ,'O' ,'N' ,'G' ):
/* initialization data */
if ( ( found ) || ( c->size < 192 ) ) goto error_sd;
found = 1;
break;
case DUMB_ID( 'P', 'A', 'T', 'T' ):
++ sigdata->n_patterns;
break;
case DUMB_ID( 'I', 'N', 'S', 'T' ):
++ sigdata->n_samples;
break;
}
}
if ( !found || !sigdata->n_samples || !sigdata->n_patterns ) goto error_sd;
if ( sigdata->n_samples > 255 || sigdata->n_patterns > 255 ) goto error_sd;
sigdata->song_message = NULL;
sigdata->order = NULL;
sigdata->instrument = NULL;
sigdata->sample = NULL;
sigdata->pattern = NULL;
sigdata->midi = NULL;
sigdata->checkpoint = NULL;
sigdata->mixing_volume = 48;
sigdata->pan_separation = 128;
sigdata->n_instruments = 0;
sigdata->n_orders = 0;
memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS);
for (n = 0; n < DUMB_IT_N_CHANNELS; n += 4) {
sigdata->channel_pan[n ] = 16;
sigdata->channel_pan[n+1] = 48;
sigdata->channel_pan[n+2] = 48;
sigdata->channel_pan[n+3] = 16;
}
for ( n = 0; (unsigned int)n < stream->chunk_count; ++n )
{
struct riff_chunk * c = stream->chunks + n;
switch ( c->type )
{
case DUMB_ID( 'S', 'O', 'N', 'G' ):
ptr = ( unsigned char * ) c->data;
memcpy( sigdata->name, c->data, 28 );
sigdata->name[ 28 ] = 0;
sigdata->flags = IT_WAS_AN_XM | IT_WAS_A_MOD | IT_STEREO | IT_OLD_EFFECTS | IT_COMPATIBLE_GXX;
sigdata->n_orders = ptr[ 36 ] | ( ptr[ 37 ] << 8 );
//sigdata->n_samples = ptr[ 38 ] | ( ptr[ 39 ] << 8 ); // whatever
//sigdata->n_patterns = ptr[ 40 ] | ( ptr[ 41 ] << 8 );
sigdata->n_pchannels = ptr[ 42 ] | ( ptr[ 43 ] << 8 );
sigdata->global_volume = ptr[ 44 ];
sigdata->mixing_volume = ptr[ 45 ];
sigdata->speed = ptr[ 46 ];
sigdata->tempo = ptr[ 47 ];
for ( o = 0; o < 16; ++o )
{
sigdata->channel_pan[ o ] = ptr[ 48 + o ] / 2;
}
sigdata->order = malloc( 128 );
if ( ! sigdata->order ) goto error_usd;
memcpy( sigdata->order, ptr + 64, 128 );
break;
}
}
sigdata->pattern = malloc( sigdata->n_patterns * sizeof( *sigdata->pattern ) );
if ( ! sigdata->pattern ) goto error_usd;
for ( n = 0; n < sigdata->n_patterns; ++n )
sigdata->pattern[ n ].entry = NULL;
sigdata->sample = malloc( sigdata->n_samples * sizeof( *sigdata->sample ) );
if ( ! sigdata->sample ) goto error_usd;
for ( n = 0; n < sigdata->n_samples; ++n )
{
IT_SAMPLE * sample = sigdata->sample + n;
sample->data = NULL;
}
sigdata->n_samples = 0;
sigdata->n_patterns = 0;
for ( n = 0; (unsigned int)n < stream->chunk_count; ++n )
{
struct riff_chunk * c = stream->chunks + n;
switch ( c->type )
{
case DUMB_ID( 'P', 'A', 'T', 'T' ):
if ( it_riff_dsmf_process_pattern( sigdata->pattern + sigdata->n_patterns, ( unsigned char * ) c->data, c->size ) ) goto error_usd;
++ sigdata->n_patterns;
break;
case DUMB_ID( 'I', 'N', 'S', 'T' ):
if ( it_riff_dsmf_process_sample( sigdata->sample + sigdata->n_samples, ( unsigned char * ) c->data, c->size ) ) goto error_usd;
++ sigdata->n_samples;
break;
}
}
_dumb_it_fix_invalid_orders( sigdata );
return sigdata;
error_usd:
_dumb_it_unload_sigdata( sigdata );
goto error;
error_sd:
free( sigdata );
error:
return NULL;
}
DUH *dumb_read_riff_dsmf( struct riff * stream )
{
sigdata_t *sigdata;
DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
sigdata = it_riff_dsmf_load_sigdata( stream );
if (!sigdata)
return NULL;
{
const char *tag[2][2];
tag[0][0] = "TITLE";
tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
tag[1][0] = "FORMAT";
tag[1][1] = "RIFF DSMF";
return make_duh( -1, 2, ( const char * const (*) [ 2 ] ) tag, 1, & descptr, & sigdata );
}
}

780
dumb/src/it/readmod.c Normal file
View file

@ -0,0 +1,780 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readmod.c - Code to read a good old-fashioned / / \ \
* Amiga module from an open file. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "dumb.h"
#include "internal/it.h"
static int it_mod_read_pattern(IT_PATTERN *pattern, DUMBFILE *f, int n_channels, unsigned char *buffer)
{
int pos;
int channel;
int row;
IT_ENTRY *entry;
pattern->n_rows = 64;
if (n_channels == 0) {
/* Read the first four channels, leaving gaps for the rest. */
for (pos = 0; pos < 64*8*4; pos += 8*4)
dumbfile_getnc(buffer + pos, 4*4, f);
/* Read the other channels into the gaps we left. */
for (pos = 4*4; pos < 64*8*4; pos += 8*4)
dumbfile_getnc(buffer + pos, 4*4, f);
n_channels = 8;
} else
dumbfile_getnc(buffer, 64 * n_channels * 4, f);
if (dumbfile_error(f))
return -1;
/* compute number of entries */
pattern->n_entries = 64; /* Account for the row end markers */
pos = 0;
for (row = 0; row < 64; row++) {
for (channel = 0; channel < n_channels; channel++) {
if (buffer[pos+0] | buffer[pos+1] | buffer[pos+2] | buffer[pos+3])
pattern->n_entries++;
pos += 4;
}
}
pattern->entry = malloc(pattern->n_entries * sizeof(*pattern->entry));
if (!pattern->entry)
return -1;
entry = pattern->entry;
pos = 0;
for (row = 0; row < 64; row++) {
for (channel = 0; channel < n_channels; channel++) {
if (buffer[pos+0] | buffer[pos+1] | buffer[pos+2] | buffer[pos+3]) {
unsigned char sample = (buffer[pos+0] & 0xF0) | (buffer[pos+2] >> 4);
int period = ((int)(buffer[pos+0] & 0x0F) << 8) | buffer[pos+1];
entry->channel = channel;
entry->mask = 0;
if (period) {
int note;
entry->mask |= IT_ENTRY_NOTE;
/* frequency = (AMIGA_DIVISOR / 8) / (period * 2)
* C-1: period = 214 -> frequency = 16726
* so, set C5_speed to 16726
* and period = 214 should translate to C5 aka 60
* halve the period, go up an octive
*
* period = 214 / pow(DUMB_SEMITONE_BASE, note - 60)
* pow(DUMB_SEMITONE_BASE, note - 60) = 214 / period
* note - 60 = log(214/period) / log(DUMB_SEMITONE_BASE)
*/
note = (int)floor(log(214.0/period) / log(DUMB_SEMITONE_BASE) + 60.5);
entry->note = MID(0, note, 119);
// or should we preserve the period?
//entry->note = buffer[pos+0] & 0x0F; /* High nibble */
//entry->volpan = buffer[pos+1]; /* Low byte */
// and what about finetune?
}
if (sample) {
entry->mask |= IT_ENTRY_INSTRUMENT;
entry->instrument = sample;
}
_dumb_it_xm_convert_effect(buffer[pos+2] & 0x0F, buffer[pos+3], entry, 1);
entry++;
}
pos += 4;
}
IT_SET_END_ROW(entry);
entry++;
}
return 0;
}
static int it_mod_read_sample_header(IT_SAMPLE *sample, DUMBFILE *f)
{
int finetune, loop_start, loop_length;
/**
21 22 Chars Sample 1 name. If the name is not a full
22 chars in length, it will be null
terminated.
If
the sample name begins with a '#' character (ASCII $23 (35)) then this is
assumed not to be an instrument name, and is probably a message.
*/
dumbfile_getnc(sample->name, 22, f);
sample->name[22] = 0;
sample->filename[0] = 0;
sample->length = dumbfile_mgetw(f) << 1;
finetune = (signed char)(dumbfile_getc(f) << 4) >> 4; /* signed nibble */
/** Each finetune step changes the note 1/8th of a semitone. */
sample->global_volume = 64;
sample->default_volume = dumbfile_getc(f); // Should we be setting global_volume to this instead?
loop_start = dumbfile_mgetw(f) << 1;
loop_length = dumbfile_mgetw(f) << 1;
if ( loop_length > 2 && loop_start + loop_length > sample->length && loop_start / 2 + loop_length <= sample->length )
loop_start /= 2;
sample->loop_start = loop_start;
sample->loop_end = loop_start + loop_length;
/**
Once this sample has been played completely from beginning
to end, if the repeat length (next field) is greater than two bytes it
will loop back to this position in the sample and continue playing. Once
it has played for the repeat length, it continues to loop back to the
repeat start offset. This means the sample continues playing until it is
told to stop.
*/
if (sample->length <= 0) {
sample->flags = 0;
return 0;
}
sample->flags = IT_SAMPLE_EXISTS;
sample->default_pan = 0;
sample->C5_speed = (int)( AMIGA_CLOCK / 214.0 ); //(int32)(16726.0*pow(DUMB_PITCH_BASE, finetune*32));
sample->finetune = finetune * 32;
// the above line might be wrong
if (sample->loop_end > sample->length)
sample->loop_end = sample->length;
if (sample->loop_end - sample->loop_start > 2)
sample->flags |= IT_SAMPLE_LOOP;
sample->vibrato_speed = 0;
sample->vibrato_depth = 0;
sample->vibrato_rate = 0;
sample->vibrato_waveform = 0; // do we have to set _all_ these?
sample->max_resampling_quality = -1;
return dumbfile_error(f);
}
static int it_mod_read_sample_data(IT_SAMPLE *sample, DUMBFILE *f, uint32 fft)
{
int32 i;
int32 truncated_size;
/* let's get rid of the sample data coming after the end of the loop */
if ((sample->flags & IT_SAMPLE_LOOP) && sample->loop_end < sample->length) {
truncated_size = sample->length - sample->loop_end;
sample->length = sample->loop_end;
} else {
truncated_size = 0;
}
if (sample->length) {
sample->data = malloc(sample->length);
if (!sample->data)
return -1;
/* Sample data are stored in "8-bit two's compliment format" (sic). */
/*
for (i = 0; i < sample->length; i++)
((signed char *)sample->left)[i] = dumbfile_getc(f);
*/
/* F U Olivier Lapicque */
if (sample->length >= 5)
{
i = dumbfile_getnc(sample->data, 5, f);
if (i == 5)
{
if (!memcmp(sample->data, "ADPCM", 5))
{
if (_dumb_it_read_sample_data_adpcm4(sample, f) < 0)
return -1;
return 0;
}
else
{
i += dumbfile_getnc(((char *)sample->data) + 5, sample->length - 5, f);
}
}
}
else
{
i = dumbfile_getnc(sample->data, sample->length, f);
}
if (i < sample->length)
{
if (i <= 0)
{
sample->flags = 0;
return 0;
}
sample->length = i;
if (sample->loop_end > i) sample->loop_end = i;
// holy crap!
if (sample->loop_start > i) sample->flags &= ~IT_SAMPLE_LOOP;
}
else
{
/* skip truncated data */
int feh = dumbfile_error(f);
if (truncated_size) dumbfile_skip(f, truncated_size);
// Should we be truncating it?
if (feh)
return -1;
}
if (fft == DUMB_ID('M',0,0,0) || fft == DUMB_ID('8',0,0,0)) {
int delta = 0;
for (i = 0; i < sample->length; i++) {
delta += ((signed char *)sample->data)[i];
((signed char *)sample->data)[i] = delta;
}
}
}
return 0;
}
typedef struct BUFFERED_MOD BUFFERED_MOD;
struct BUFFERED_MOD
{
unsigned char *buffered;
int32 ptr, len;
DUMBFILE *remaining;
};
static int buffer_mod_skip(void *f, int32 n)
{
BUFFERED_MOD *bm = f;
if (bm->buffered) {
bm->ptr += n;
if (bm->ptr >= bm->len) {
free(bm->buffered);
bm->buffered = NULL;
return dumbfile_skip(bm->remaining, bm->ptr - bm->len);
}
return 0;
}
return dumbfile_skip(bm->remaining, n);
}
static int buffer_mod_getc(void *f)
{
BUFFERED_MOD *bm = f;
if (bm->buffered) {
int rv = bm->buffered[bm->ptr++];
if (bm->ptr >= bm->len) {
free(bm->buffered);
bm->buffered = NULL;
}
return rv;
}
return dumbfile_getc(bm->remaining);
}
static int32 buffer_mod_getnc(char *ptr, int32 n, void *f)
{
BUFFERED_MOD *bm = f;
if (bm->buffered) {
int left = bm->len - bm->ptr;
if (n >= left) {
memcpy(ptr, bm->buffered + bm->ptr, left);
free(bm->buffered);
bm->buffered = NULL;
if (n - left) {
int rv = dumbfile_getnc(ptr + left, n - left, bm->remaining);
return left + MAX(rv, 0);
} else {
return left;
}
}
memcpy(ptr, bm->buffered + bm->ptr, n);
bm->ptr += n;
return n;
}
return dumbfile_getnc(ptr, n, bm->remaining);
}
static void buffer_mod_close(void *f)
{
BUFFERED_MOD *bm = f;
if (bm->buffered) free(bm->buffered);
/* Do NOT close bm->remaining */
free(f);
}
DUMBFILE_SYSTEM buffer_mod_dfs = {
NULL,
&buffer_mod_skip,
&buffer_mod_getc,
&buffer_mod_getnc,
&buffer_mod_close
};
#define MOD_FFT_OFFSET (20 + 31*(22+2+1+1+2+2) + 1 + 1 + 128)
static DUMBFILE *dumbfile_buffer_mod(DUMBFILE *f, uint32 *fft)
{
BUFFERED_MOD *bm = malloc(sizeof(*bm));
if (!bm) return NULL;
bm->buffered = malloc(MOD_FFT_OFFSET + 4);
if (!bm->buffered) {
free(bm);
return NULL;
}
bm->len = dumbfile_getnc(bm->buffered, MOD_FFT_OFFSET + 4, f);
if (bm->len > 0) {
if (bm->len >= MOD_FFT_OFFSET + 4)
*fft = (uint32)bm->buffered[MOD_FFT_OFFSET ] << 24
| (uint32)bm->buffered[MOD_FFT_OFFSET+1] << 16
| (uint32)bm->buffered[MOD_FFT_OFFSET+2] << 8
| (uint32)bm->buffered[MOD_FFT_OFFSET+3];
else
*fft = 0;
bm->ptr = 0;
} else {
free(bm->buffered);
bm->buffered = NULL;
}
bm->remaining = f;
return dumbfile_open_ex(bm, &buffer_mod_dfs);
}
static DUMBFILE *dumbfile_buffer_mod_2(DUMBFILE *f, int32 *remain)
{
int32 read;
BUFFERED_MOD *bm = malloc(sizeof(*bm));
if (!bm) return NULL;
bm->buffered = malloc(32768);
if (!bm->buffered) {
free(bm);
return NULL;
}
bm->len = 0;
*remain = 0;
read = dumbfile_getnc(bm->buffered, 32768, f);
if (read >= 0) {
bm->len += read;
*remain += read;
while (read >= 32768) {
bm->buffered = realloc(bm->buffered, *remain + 32768);
if (!bm->buffered) {
free(bm);
return 0;
}
read = dumbfile_getnc(bm->buffered + *remain, 32768, f);
if (read >= 0) {
bm->len += read;
*remain += read;
}
}
}
if (*remain) {
bm->ptr = 0;
} else {
free(bm->buffered);
bm->buffered = NULL;
}
bm->remaining = f;
return dumbfile_open_ex(bm, &buffer_mod_dfs);
}
static DUMB_IT_SIGDATA *it_mod_load_sigdata(DUMBFILE *f, int restrict)
{
DUMB_IT_SIGDATA *sigdata;
int n_channels;
int i;
uint32 fft = 0;
DUMBFILE *rem;
f = dumbfile_buffer_mod(f, &fft);
if (!f)
return NULL;
sigdata = malloc(sizeof(*sigdata));
if (!sigdata) {
dumbfile_close(f);
return NULL;
}
/**
1 20 Chars Title of the song. If the title is not a
full 20 chars in length, it will be null-
terminated.
*/
if (dumbfile_getnc(sigdata->name, 20, f) < 20) {
free(sigdata);
dumbfile_close(f);
return NULL;
}
sigdata->name[20] = 0;
sigdata->n_samples = 31;
switch (fft) {
case DUMB_ID('M','.','K','.'):
case DUMB_ID('M','!','K','!'):
case DUMB_ID('M','&','K','!'):
case DUMB_ID('N','.','T','.'):
case DUMB_ID('N','S','M','S'):
case DUMB_ID('F','L','T','4'):
case DUMB_ID('M',0,0,0):
case DUMB_ID('8',0,0,0):
n_channels = 4;
break;
case DUMB_ID('F','L','T','8'):
n_channels = 0;
/* 0 indicates a special case; two four-channel patterns must be
* combined into one eight-channel pattern. Pattern indexes must
* be halved. Why oh why do they obfuscate so?
*/
/*for (i = 0; i < 128; i++)
sigdata->order[i] >>= 1;*/
break;
case DUMB_ID('C','D','8','1'):
case DUMB_ID('O','C','T','A'):
case DUMB_ID('O','K','T','A'):
n_channels = 8;
break;
case DUMB_ID('1','6','C','N'):
n_channels = 16;
break;
case DUMB_ID('3','2','C','N'):
n_channels = 32;
break;
default:
/* If we get an illegal tag, assume 4 channels 15 samples. */
if ((fft & 0x0000FFFFL) == DUMB_ID(0,0,'C','H')) {
if (fft >= '1' << 24 && fft < '4' << 24) {
n_channels = ((fft & 0x00FF0000L) >> 16) - '0';
if ((unsigned int)n_channels >= 10) {
/* Rightmost character wasn't a digit. */
n_channels = 4;
sigdata->n_samples = 15;
} else {
n_channels += (((fft & 0xFF000000L) >> 24) - '0') * 10;
/* MODs should really only go up to 32 channels, but we're lenient. */
if ((unsigned int)(n_channels - 1) >= DUMB_IT_N_CHANNELS - 1) {
/* No channels or too many? Can't be right... */
n_channels = 4;
sigdata->n_samples = 15;
}
}
} else {
n_channels = 4;
sigdata->n_samples = 15;
}
} else if ((fft & 0x00FFFFFFL) == DUMB_ID(0,'C','H','N')) {
n_channels = (fft >> 24) - '0';
if ((unsigned int)(n_channels - 1) >= 9) {
/* Character was '0' or it wasn't a digit */
n_channels = 4;
sigdata->n_samples = 15;
}
} else if ((fft & 0xFFFFFF00L) == DUMB_ID('T','D','Z',0)) {
n_channels = (fft & 0x000000FFL) - '0';
if ((unsigned int)(n_channels - 1) >= 9) {
/* We've been very lenient, given that it should have
* been 1, 2 or 3, but this MOD has been very naughty and
* must be punished.
*/
n_channels = 4;
sigdata->n_samples = 15;
}
} else {
n_channels = 4;
sigdata->n_samples = 15;
}
}
// moo
if ( restrict && sigdata->n_samples == 15 )
{
free(sigdata);
dumbfile_close(f);
return NULL;
}
sigdata->n_pchannels = n_channels ? n_channels : 8; /* special case for 0, see above */
sigdata->sample = malloc(sigdata->n_samples * sizeof(*sigdata->sample));
if (!sigdata->sample) {
free(sigdata);
dumbfile_close(f);
return NULL;
}
sigdata->song_message = NULL;
sigdata->order = NULL;
sigdata->instrument = NULL;
sigdata->pattern = NULL;
sigdata->midi = NULL;
sigdata->checkpoint = NULL;
sigdata->n_instruments = 0;
for (i = 0; i < sigdata->n_samples; i++)
sigdata->sample[i].data = NULL;
for (i = 0; i < sigdata->n_samples; i++) {
if (it_mod_read_sample_header(&sigdata->sample[i], f)) {
_dumb_it_unload_sigdata(sigdata);
dumbfile_close(f);
return NULL;
}
}
sigdata->n_orders = dumbfile_getc(f);
sigdata->restart_position = dumbfile_getc(f);
// what if this is >= 127? what about with Fast Tracker II?
/* if (sigdata->n_orders <= 0 || sigdata->n_orders > 128) { // is this right?
_dumb_it_unload_sigdata(sigdata);
dumbfile_close(f);
return NULL;
}*/
//if (sigdata->restart_position >= sigdata->n_orders)
//sigdata->restart_position = 0;
sigdata->order = malloc(128); /* We may need to scan the extra ones! */
if (!sigdata->order) {
_dumb_it_unload_sigdata(sigdata);
dumbfile_close(f);
return NULL;
}
if (dumbfile_getnc(sigdata->order, 128, f) < 128) {
_dumb_it_unload_sigdata(sigdata);
dumbfile_close(f);
return NULL;
}
if (sigdata->n_orders <= 0 || sigdata->n_orders > 128) { // is this right?
sigdata->n_orders = 128;
//while (sigdata->n_orders > 1 && !sigdata->order[sigdata->n_orders - 1]) sigdata->n_orders--;
}
if ( ! n_channels )
for (i = 0; i < 128; i++)
sigdata->order[i] >>= 1;
/* "The old NST format contains only 15 samples (instead of 31). Further
* it doesn't contain a file format tag (id). So Pattern data offset is
* at 20+15*30+1+1+128."
* - Then I shall assume the File Format Tag never exists if there are
* only 15 samples. I hope this isn't a faulty assumption...
*/
if (sigdata->n_samples == 31)
dumbfile_skip(f, 4);
/* Work out how many patterns there are. */
sigdata->n_patterns = -1;
for (i = 0; i < 128; i++)
if (sigdata->n_patterns < sigdata->order[i])
sigdata->n_patterns = sigdata->order[i];
sigdata->n_patterns++;
/* May as well try to save a tiny bit of memory. */
if (sigdata->n_orders < 128) {
unsigned char *order = realloc(sigdata->order, sigdata->n_orders);
if (order) sigdata->order = order;
}
sigdata->pattern = malloc(sigdata->n_patterns * sizeof(*sigdata->pattern));
if (!sigdata->pattern) {
_dumb_it_unload_sigdata(sigdata);
dumbfile_close(f);
return NULL;
}
for (i = 0; i < sigdata->n_patterns; i++)
sigdata->pattern[i].entry = NULL;
/* Read in the patterns */
{
unsigned char *buffer = malloc(256 * n_channels); /* 64 rows * 4 bytes */
if (!buffer) {
_dumb_it_unload_sigdata(sigdata);
dumbfile_close(f);
return NULL;
}
for (i = 0; i < sigdata->n_patterns; i++) {
if (it_mod_read_pattern(&sigdata->pattern[i], f, n_channels, buffer) != 0) {
free(buffer);
_dumb_it_unload_sigdata(sigdata);
dumbfile_close(f);
return NULL;
}
}
free(buffer);
}
rem = NULL;
/* uggly */
if (fft == DUMB_ID('M',0,0,0) || fft == DUMB_ID('8',0,0,0)) {
int32 skip;
int32 remain;
rem = f;
f = dumbfile_buffer_mod_2(rem, &remain);
if (!f) {
_dumb_it_unload_sigdata(sigdata);
dumbfile_close(rem);
return NULL;
}
for (skip = 0, i = 0; i < sigdata->n_samples; i++) {
if (sigdata->sample[i].flags & IT_SAMPLE_EXISTS) {
skip += sigdata->sample[i].length;
}
}
if (remain - skip) {
if (dumbfile_skip(f, remain - skip)) {
_dumb_it_unload_sigdata(sigdata);
dumbfile_close(f);
dumbfile_close(rem);
return NULL;
}
}
}
/* And finally, the sample data */
for (i = 0; i < sigdata->n_samples; i++) {
if (it_mod_read_sample_data(&sigdata->sample[i], f, fft)) {
_dumb_it_unload_sigdata(sigdata);
dumbfile_close(f);
if (rem) dumbfile_close(rem);
return NULL;
}
}
/* w00t! */
/*if ( n_channels == 4 &&
( sigdata->n_samples == 15 ||
( ( fft & 240 ) != DUMB_ID( 0, 0, 'C', 0 ) &&
( fft & 240 ) != DUMB_ID( 0, 0, 'H', 0 ) &&
( fft & 240 ) != 0 ) ) ) {
for ( i = 0; i < sigdata->n_samples; ++i ) {
IT_SAMPLE * sample = &sigdata->sample [i];
if ( sample && ( sample->flags & IT_SAMPLE_EXISTS ) ) {
int n, o;
o = sample->length;
if ( o > 4 ) o = 4;
for ( n = 0; n < o; ++n )
( ( char * ) sample->data ) [n] = 0;
}
}
}*/
dumbfile_close(f); /* Destroy the BUFFERED_MOD DUMBFILE we were using. */
/* The DUMBFILE originally passed to our function is intact. */
if (rem) dumbfile_close(rem);
/* Now let's initialise the remaining variables, and we're done! */
sigdata->flags = IT_WAS_AN_XM | IT_WAS_A_MOD | IT_OLD_EFFECTS | IT_COMPATIBLE_GXX | IT_STEREO;
sigdata->global_volume = 128;
sigdata->mixing_volume = 48;
/* We want 50 ticks per second; 50/6 row advances per second;
* 50*10=500 row advances per minute; 500/4=125 beats per minute.
*/
sigdata->speed = 6;
sigdata->tempo = 125;
sigdata->pan_separation = 128;
memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS);
for (i = 0; i < DUMB_IT_N_CHANNELS; i += 4) {
sigdata->channel_pan[i+0] = 16;
sigdata->channel_pan[i+1] = 48;
sigdata->channel_pan[i+2] = 48;
sigdata->channel_pan[i+3] = 16;
}
_dumb_it_fix_invalid_orders(sigdata);
return sigdata;
}
DUH *DUMBEXPORT dumb_read_mod_quick(DUMBFILE *f, int restrict)
{
sigdata_t *sigdata;
DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
sigdata = it_mod_load_sigdata(f, restrict);
if (!sigdata)
return NULL;
{
const char *tag[2][2];
tag[0][0] = "TITLE";
tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
tag[1][0] = "FORMAT";
tag[1][1] = "MOD";
return make_duh(-1, 2, (const char *const (*)[2])tag, 1, &descptr, &sigdata);
}
}

29
dumb/src/it/readmod2.c Normal file
View file

@ -0,0 +1,29 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readmod2.c - Function to read a good old- / / \ \
* fashioned Amiga module from an | < / \_
* open file and do an initial | \/ /\ /
* run-through. \_ / > /
* | \ / /
* Split off from readmod.c by entheh. | ' /
* \__/
*/
#include "dumb.h"
DUH *DUMBEXPORT dumb_read_mod(DUMBFILE *f, int restrict)
{
DUH *duh = dumb_read_mod_quick(f, restrict);
dumb_it_do_initial_runthrough(duh);
return duh;
}

413
dumb/src/it/readmtm.c Normal file
View file

@ -0,0 +1,413 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readmtm.c - Code to read a MultiTracker Module / / \ \
* from an open file. | < / \_
* | \/ /\ /
* By Chris Moeller. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "dumb.h"
#include "internal/it.h"
static size_t strlen_max(const char * ptr, size_t max)
{
const char * end, * start;
if (ptr==0) return 0;
start = ptr;
end = ptr + max;
while(*ptr && ptr < end) ptr++;
return ptr - start;
}
static int it_mtm_assemble_pattern(IT_PATTERN *pattern, const unsigned char * track, const unsigned short * sequence, int n_rows)
{
int n, o, note, sample;
const unsigned char * t;
IT_ENTRY * entry;
pattern->n_rows = n_rows;
pattern->n_entries = n_rows;
for (n = 0; n < 32; n++) {
if (sequence[n]) {
t = &track[192 * (sequence[n] - 1)];
for (o = 0; o < n_rows; o++) {
if (t[0] || t[1] || t[2]) pattern->n_entries++;
t += 3;
}
}
}
entry = malloc(pattern->n_entries * sizeof(*entry));
if (!entry) return -1;
pattern->entry = entry;
for (n = 0; n < n_rows; n++) {
for (o = 0; o < 32; o++) {
if (sequence[o]) {
t = &track[192 * (sequence[o] - 1) + (n * 3)];
if (t[0] || t[1] || t[2]) {
entry->channel = o;
entry->mask = 0;
note = t[0] >> 2;
sample = ((t[0] << 4) | (t[1] >> 4)) & 0x3F;
if (note) {
entry->mask |= IT_ENTRY_NOTE;
entry->note = note + 24;
}
if (sample) {
entry->mask |= IT_ENTRY_INSTRUMENT;
entry->instrument = sample;
}
_dumb_it_xm_convert_effect(t[1] & 0xF, t[2], entry, 1);
if (entry->mask) entry++;
}
}
}
IT_SET_END_ROW(entry);
entry++;
}
pattern->n_entries = (int)(entry - pattern->entry);
return 0;
}
static int it_mtm_read_sample_header(IT_SAMPLE *sample, DUMBFILE *f)
{
int finetune, flags;
dumbfile_getnc(sample->name, 22, f);
sample->name[22] = 0;
sample->filename[0] = 0;
sample->length = dumbfile_igetl(f);
sample->loop_start = dumbfile_igetl(f);
sample->loop_end = dumbfile_igetl(f);
finetune = (signed char)(dumbfile_getc(f) << 4) >> 4; /* signed nibble */
sample->global_volume = 64;
sample->default_volume = dumbfile_getc(f);
flags = dumbfile_getc(f);
if (sample->length <= 0) {
sample->flags = 0;
return 0;
}
sample->flags = IT_SAMPLE_EXISTS;
if (flags & 1) {
sample->flags |= IT_SAMPLE_16BIT;
sample->length >>= 1;
sample->loop_start >>= 1;
sample->loop_end >>= 1;
}
sample->default_pan = 0;
sample->C5_speed = (int)( AMIGA_CLOCK / 214.0 );//(int32)(16726.0*pow(DUMB_PITCH_BASE, finetune*32));
sample->finetune = finetune * 32;
// the above line might be wrong
if (sample->loop_end > sample->length)
sample->loop_end = sample->length;
if (sample->loop_end - sample->loop_start > 2)
sample->flags |= IT_SAMPLE_LOOP;
sample->vibrato_speed = 0;
sample->vibrato_depth = 0;
sample->vibrato_rate = 0;
sample->vibrato_waveform = 0; // do we have to set _all_ these?
sample->max_resampling_quality = -1;
return dumbfile_error(f);
}
static int it_mtm_read_sample_data(IT_SAMPLE *sample, DUMBFILE *f)
{
int32 i;
int32 truncated_size;
/* let's get rid of the sample data coming after the end of the loop */
if ((sample->flags & IT_SAMPLE_LOOP) && sample->loop_end < sample->length) {
truncated_size = sample->length - sample->loop_end;
sample->length = sample->loop_end;
} else {
truncated_size = 0;
}
sample->data = malloc(sample->length);
if (!sample->data)
return -1;
dumbfile_getnc((char *)sample->data, sample->length, f);
dumbfile_skip(f, truncated_size);
if (dumbfile_error(f))
return -1;
for (i = 0; i < sample->length; i++)
((signed char *)sample->data)[i] ^= 0x80;
return 0;
}
static DUMB_IT_SIGDATA *it_mtm_load_sigdata(DUMBFILE *f, int * version)
{
DUMB_IT_SIGDATA *sigdata;
int n, o, n_tracks, l_comment, n_rows, n_channels;
unsigned char * track;
unsigned short * sequence;
char * comment;
if (dumbfile_getc(f) != 'M' ||
dumbfile_getc(f) != 'T' ||
dumbfile_getc(f) != 'M') goto error;
*version = dumbfile_getc(f);
sigdata = malloc(sizeof(*sigdata));
if (!sigdata) goto error;
dumbfile_getnc(sigdata->name, 20, f);
sigdata->name[20] = 0;
n_tracks = dumbfile_igetw(f);
sigdata->n_patterns = dumbfile_getc(f) + 1;
sigdata->n_orders = dumbfile_getc(f) + 1;
l_comment = dumbfile_igetw(f);
sigdata->n_samples = dumbfile_getc(f);
//if (dumbfile_getc(f)) goto error_sd;
dumbfile_getc(f);
n_rows = dumbfile_getc(f);
n_channels = dumbfile_getc(f);
if (dumbfile_error(f) ||
(n_tracks <= 0) ||
(sigdata->n_samples <= 0) ||
(n_rows <= 0 || n_rows > 64) ||
(n_channels <= 0 || n_channels > 32)) goto error_sd;
memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS);
if (dumbfile_getnc(sigdata->channel_pan, 32, f) < 32) goto error_sd;
for (n = 0; n < 32; n++) {
if (sigdata->channel_pan[n] <= 15) {
sigdata->channel_pan[n] -= (sigdata->channel_pan[n] & 8) >> 3;
sigdata->channel_pan[n] = (sigdata->channel_pan[n] * 32) / 7;
} else {
sigdata->channel_volume[n] = 0;
sigdata->channel_pan[n] = 7;
}
}
for (n = 32; n < DUMB_IT_N_CHANNELS; n += 4) {
sigdata->channel_pan[n ] = 16;
sigdata->channel_pan[n+1] = 48;
sigdata->channel_pan[n+2] = 48;
sigdata->channel_pan[n+3] = 16;
}
sigdata->sample = malloc(sigdata->n_samples * sizeof(*sigdata->sample));
if (!sigdata->sample) goto error_sd;
sigdata->flags = IT_WAS_AN_XM | IT_WAS_A_MOD | IT_STEREO | IT_OLD_EFFECTS | IT_COMPATIBLE_GXX;
sigdata->global_volume = 128;
sigdata->mixing_volume = 48;
sigdata->speed = 6;
sigdata->tempo = 125;
sigdata->pan_separation = 128;
sigdata->song_message = NULL;
sigdata->order = NULL;
sigdata->instrument = NULL;
sigdata->pattern = NULL;
sigdata->midi = NULL;
sigdata->checkpoint = NULL;
sigdata->n_instruments = 0;
sigdata->restart_position = 0;
sigdata->n_pchannels = n_channels;
for (n = 0; n < sigdata->n_samples; n++)
sigdata->sample[n].data = NULL;
for (n = 0; n < sigdata->n_samples; n++) {
if (it_mtm_read_sample_header(&sigdata->sample[n], f)) goto error_usd;
}
sigdata->order = malloc(sigdata->n_orders);
if (!sigdata->order) goto error_usd;
if (dumbfile_getnc(sigdata->order, sigdata->n_orders, f) < sigdata->n_orders) goto error_usd;
if (sigdata->n_orders < 128)
if (dumbfile_skip(f, 128 - sigdata->n_orders)) goto error_usd;
track = malloc(192 * n_tracks);
if (!track) goto error_usd;
if (dumbfile_getnc(track, 192 * n_tracks, f) < 192 * n_tracks) goto error_ft;
sigdata->pattern = malloc(sigdata->n_patterns * sizeof(*sigdata->pattern));
if (!sigdata->pattern) goto error_ft;
for (n = 0; n < sigdata->n_patterns; n++)
sigdata->pattern[n].entry = NULL;
sequence = malloc(sigdata->n_patterns * 32 * sizeof(*sequence));
if (!sequence) goto error_ft;
for (n = 0; n < sigdata->n_patterns; n++) {
for (o = 0; o < 32; o++) {
sequence[(n * 32) + o] = dumbfile_igetw(f);
if (sequence[(n * 32) + o] > n_tracks)
{
//goto error_fs;
// illegal track number, silence instead of rejecting the file
sequence[(n * 32) + o] = 0;
}
}
}
for (n = 0; n < sigdata->n_patterns; n++) {
if (it_mtm_assemble_pattern(&sigdata->pattern[n], track, &sequence[n * 32], n_rows)) goto error_fs;
}
if (l_comment) {
comment = malloc(l_comment);
if (!comment) goto error_fs;
if (dumbfile_getnc(comment, l_comment, f) < l_comment) goto error_fc;
/* Time for annoying "logic", yes. We want each line which has text,
* and each blank line in between all the valid lines.
*/
/* Find last actual line. */
for (o = -1, n = 0; n < l_comment; n += 40) {
if (comment[n]) o = n;
}
if (o >= 0) {
size_t l;
int m;
for (l = 0, n = 0; n <= o; n += 40) {
l += strlen_max(&comment[n], 40) + 2;
}
l -= 1;
sigdata->song_message = malloc(l);
if (!sigdata->song_message) goto error_fc;
for (m = 0, n = 0; n <= o; n += 40) {
int p = (int)strlen_max(&comment[n], 40);
if (p) {
memcpy(sigdata->song_message + m, &comment[n], p);
m += p;
}
if (l - m > 1) {
sigdata->song_message[m++] = 13;
sigdata->song_message[m++] = 10;
}
}
sigdata->song_message[m] = 0;
}
free(comment);
}
for (n = 0; n < sigdata->n_samples; n++) {
if (it_mtm_read_sample_data(&sigdata->sample[n], f)) goto error_fs;
}
_dumb_it_fix_invalid_orders(sigdata);
free(sequence);
free(track);
return sigdata;
error_fc:
free(comment);
error_fs:
free(sequence);
error_ft:
free(track);
error_usd:
_dumb_it_unload_sigdata(sigdata);
return NULL;
error_sd:
free(sigdata);
error:
return NULL;
}
static char hexdigit(int in)
{
if (in < 10) return in + '0';
else return in + 'A' - 10;
}
DUH *DUMBEXPORT dumb_read_mtm_quick(DUMBFILE *f)
{
sigdata_t *sigdata;
int ver;
DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
sigdata = it_mtm_load_sigdata(f, &ver);
if (!sigdata)
return NULL;
{
char version[16];
const char *tag[2][2];
tag[0][0] = "TITLE";
tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
tag[1][0] = "FORMAT";
version[0] = 'M';
version[1] = 'T';
version[2] = 'M';
version[3] = ' ';
version[4] = 'v';
version[5] = hexdigit(ver >> 4);
version[6] = '.';
version[7] = hexdigit(ver & 15);
version[8] = 0;
tag[1][1] = (const char *) &version;
return make_duh(-1, 2, (const char *const (*)[2])tag, 1, &descptr, &sigdata);
}
}

729
dumb/src/it/readoldpsm.c Normal file
View file

@ -0,0 +1,729 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readpsm.c - Code to read an old Protracker / / \ \
* Studio module from an open file. | < / \_
* | \/ /\ /
* By Chris Moeller. \_ / > /
* | \ / /
* | ' /
* \__/
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "dumb.h"
#include "internal/it.h"
static int CDECL psm_sample_compare(const void *e1, const void *e2)
{
const unsigned char * pa = e1;
const unsigned char * pb = e2;
int a = pa[37] | (pa[38] << 8) | (pa[39] << 16) | (pa[40] << 24);
int b = pb[37] | (pb[38] << 8) | (pb[39] << 16) | (pb[40] << 24);
return a - b;
}
static int it_old_psm_read_samples(IT_SAMPLE ** sample, DUMBFILE * f, int * num, const unsigned char * prebuffer, int32 data_pos, int32 data_size)
{
int n, o, pos, count = *num, true_num, snum, offset, flags, finetune, delta;
unsigned char * buffer, * sbuffer = 0;
const unsigned char * sdata;
buffer = malloc(count * 64);
if (!buffer) goto error;
if (dumbfile_getnc(buffer, count * 64, f) < count * 64) goto error_fb;
pos = dumbfile_pos(f);
true_num = 0;
for (n = 0; n < count; n++) {
snum = buffer[(n * 64) + 45] | (buffer[(n * 64) + 46] << 8);
if ((snum < 1) || (snum > 255)) goto error_fb;
if (true_num < snum) true_num = snum;
}
if (true_num > count) {
IT_SAMPLE * meh = realloc(*sample, true_num * sizeof(*meh));
if (!meh) goto error_fb;
for (n = count; n < true_num; n++) {
meh[n].data = NULL;
}
*sample = meh;
*num = true_num;
}
qsort(buffer, count, 64, &psm_sample_compare);
for (n = 0; n < true_num; n++) {
(*sample)[n].flags = 0;
}
for (n = 0; n < count; n++) {
IT_SAMPLE * s;
snum = buffer[(n * 64) + 45] | (buffer[(n * 64) + 46] << 8);
s = &((*sample)[snum - 1]);
memcpy(s->filename, buffer + (n * 64), 13);
s->filename[13] = 0;
memcpy(s->name, buffer + (n * 64) + 13, 24);
s->name[24] = 0;
offset = buffer[(n * 64) + 37] | (buffer[(n * 64) + 38] << 8) |
(buffer[(n * 64) + 39] << 16) | (buffer[(n * 64) + 40] << 24);
flags = buffer[(n * 64) + 47];
s->length = buffer[(n * 64) + 48] | (buffer[(n * 64) + 49] << 8) |
(buffer[(n * 64) + 50] << 16) | (buffer[(n * 64) + 51] << 24);
s->loop_start = buffer[(n * 64) + 52] | (buffer[(n * 64) + 53] << 8) |
(buffer[(n * 64) + 54] << 16) | (buffer[(n * 64) + 55] << 24);
s->loop_end = buffer[(n * 64) + 56] | (buffer[(n * 64) + 57] << 8) |
(buffer[(n * 64) + 58] << 16) | (buffer[(n * 64) + 59] << 24);
if (s->length <= 0) continue;
finetune = buffer[(n * 64) + 60];
s->default_volume = buffer[(n * 64) + 61];
s->C5_speed = buffer[(n * 64) + 62] | (buffer[(n * 64) + 63] << 8);
if (finetune < 16) {
if (finetune >= 8) finetune -= 16;
//s->C5_speed = (int32)((double)s->C5_speed * pow(DUMB_PITCH_BASE, finetune*32));
s->finetune = finetune * 32;
}
else s->finetune = 0;
s->flags |= IT_SAMPLE_EXISTS;
if (flags & 0x41) {
s->flags &= ~IT_SAMPLE_EXISTS;
continue;
}
if (flags & 0x20) s->flags |= IT_SAMPLE_PINGPONG_LOOP;
if (flags & 4) s->flags |= IT_SAMPLE_16BIT;
if (flags & 0x80) {
s->flags |= IT_SAMPLE_LOOP;
if ((unsigned int)s->loop_end > (unsigned int)s->length)
s->loop_end = s->length;
else if ((unsigned int)s->loop_start >= (unsigned int)s->loop_end)
s->flags &= ~IT_SAMPLE_LOOP;
else
s->length = s->loop_end;
}
s->global_volume = 64;
s->vibrato_speed = 0;
s->vibrato_depth = 0;
s->vibrato_rate = 0;
s->vibrato_waveform = IT_VIBRATO_SINE;
s->max_resampling_quality = -1;
s->data = malloc(s->length * ((flags & 4) ? 2 : 1));
if (!s->data) goto error_fb;
if ((offset >= data_pos) &&
((offset + s->length * ((flags & 4) ? 2 : 1)) <= (data_pos + data_size))) {
sdata = prebuffer + offset - data_pos;
} else if (offset >= pos) {
if (dumbfile_skip(f, offset - pos)) goto error_fb;
pos = offset;
offset = s->length * ((flags & 4) ? 2 : 1);
sbuffer = malloc(offset);
if (!sbuffer) goto error_fb;
if (dumbfile_getnc(sbuffer, offset, f) < offset) goto error_fsb;
sdata = sbuffer;
} else
goto error_fb;
if (flags & 0x10) {
if (flags & 8) {
if (flags & 4) {
for (o = 0; o < s->length; o++)
((short *)s->data)[o] = (sdata[o * 2] | (sdata[(o * 2) + 1] << 8)) ^ 0x8000;
} else {
for (o = 0; o < s->length; o++)
((signed char *)s->data)[o] = sdata[o] ^ 0x80;
}
} else {
if (flags & 4) {
for (o = 0; o < s->length; o++)
((short *)s->data)[o] = sdata[o * 2] | (sdata[(o * 2) + 1] << 8);
} else {
memcpy(s->data, sdata, s->length);
}
}
} else {
delta = 0;
if (flags & 8) {
/* unsigned delta? mehhh, does anything even use this? */
if (flags & 4) {
for (o = 0; o < s->length; o++) {
delta += (short)(sdata[o * 2] | (sdata[(o * 2) + 1] << 8));
((short *)s->data)[o] = delta ^ 0x8000;
}
} else {
for (o = 0; o < s->length; o++) {
delta += (signed char)sdata[o];
((signed char *)s->data)[o] = delta ^ 0x80;
}
}
} else {
if (flags & 4) {
for (o = 0; o < s->length; o++) {
delta += (short)(sdata[o * 2] | (sdata[(o * 2) + 1] << 8));
((short *)s->data)[o] = delta;
}
} else {
for (o = 0; o < s->length; o++) {
delta += (signed char)sdata[o];
((signed char *)s->data)[o] = delta;
}
}
}
}
if (sbuffer) {
free(sbuffer);
sbuffer = 0;
}
}
free(buffer);
return 0;
error_fsb:
if (sbuffer) free(sbuffer);
error_fb:
free(buffer);
error:
return -1;
}
static int it_old_psm_read_patterns(IT_PATTERN * pattern, DUMBFILE * f, int num, int size, int pchans, int sflags)
{
int n, offset, psize, rows, chans, row, flags, channel;
unsigned char * buffer, * ptr, * end;
IT_ENTRY * entry;
(void)sflags; /* Avoid unused parameter warning from GCC */
buffer = malloc(size);
if (!buffer) goto error;
if (dumbfile_getnc(buffer, size, f) < size) goto error_fb;
offset = 0;
for (n = 0; n < num; n++) {
IT_PATTERN * p = &pattern[n];
if (offset >= size) goto error_fb;
ptr = buffer + offset;
psize = ptr[0] | (ptr[1] << 8);
rows = ptr[2];
chans = ptr[3];
if (!rows || !chans) {
p->n_rows = 1;
p->n_entries = 0;
continue;
}
psize = (psize + 15) & ~15;
end = ptr + psize;
ptr += 4;
p->n_rows = rows;
p->n_entries = rows;
row = 0;
while ((row < rows) && (ptr < end)) {
flags = *ptr++;
if (!flags) {
row++;
continue;
}
if (flags & 0xE0) {
p->n_entries++;
if (flags & 0x80) ptr += 2;
if (flags & 0x40) ptr++;
if (flags & 0x20) {
ptr++;
if (*ptr == 40) ptr += 3;
else ptr++;
}
}
}
entry = malloc(p->n_entries * sizeof(*p->entry));
if (!entry) goto error_fb;
p->entry = entry;
ptr = buffer + offset + 4;
row = 0;
while ((row < rows) && (ptr < end)) {
flags = *ptr++;
if (!flags) {
IT_SET_END_ROW(entry);
entry++;
row++;
continue;
}
if (flags & 0xE0) {
entry->mask = 0;
entry->channel = channel = flags & 0x1F;
if (channel >= chans)
{
//channel = 0;
goto error_fb;
}
if (flags & 0x80) {
if ((*ptr < 60) && (channel < pchans)) {
entry->mask |= IT_ENTRY_NOTE;
entry->note = *ptr + 36;
}
ptr++;
if (*ptr) {
entry->mask |= IT_ENTRY_INSTRUMENT;
entry->instrument = *ptr;
}
ptr++;
}
if (flags & 0x40) {
if (*ptr <= 64) {
entry->mask |= IT_ENTRY_VOLPAN;
entry->volpan = *ptr;
}
ptr++;
}
if (flags & 0x20) {
entry->mask |= IT_ENTRY_EFFECT;
switch (*ptr) {
case 1:
entry->effect = IT_XM_FINE_VOLSLIDE_UP;
entry->effectvalue = ptr[1];
break;
case 2:
entry->effect = IT_VOLUME_SLIDE;
entry->effectvalue = (ptr[1] << 4) & 0xF0;
break;
case 3:
entry->effect = IT_XM_FINE_VOLSLIDE_DOWN;
entry->effectvalue = ptr[1];
break;
case 4:
entry->effect = IT_VOLUME_SLIDE;
entry->effectvalue = ptr[1] & 0xF;
break;
case 10:
entry->effect = IT_PORTAMENTO_UP;
entry->effectvalue = EFFECT_VALUE(0xF, ptr[1]);
break;
case 11:
entry->effect = IT_PORTAMENTO_UP;
entry->effectvalue = ptr[1];
break;
case 12:
entry->effect = IT_PORTAMENTO_DOWN;
entry->effectvalue = EFFECT_VALUE(ptr[1], 0xF);
break;
case 13:
entry->effect = IT_PORTAMENTO_DOWN;
entry->effectvalue = ptr[1];
break;
case 14:
entry->effect = IT_TONE_PORTAMENTO;
entry->effectvalue = ptr[1];
break;
case 15:
entry->effect = IT_S;
entry->effectvalue = EFFECT_VALUE(IT_S_SET_GLISSANDO_CONTROL, ptr[1] & 15);
break;
case 16:
entry->effect = IT_VOLSLIDE_TONEPORTA;
entry->effectvalue = ptr[1] << 4;
break;
case 17:
entry->effect = IT_VOLSLIDE_TONEPORTA;
entry->effectvalue = ptr[1] & 0xF;
break;
case 20:
entry->effect = IT_VIBRATO;
entry->effectvalue = ptr[1];
break;
case 21:
entry->effect = IT_S;
entry->effectvalue = EFFECT_VALUE(IT_S_SET_VIBRATO_WAVEFORM, ptr[1] & 11);
break;
case 22:
entry->effect = IT_VOLSLIDE_VIBRATO;
entry->effectvalue = ptr[1] << 4;
break;
case 23:
entry->effect = IT_VOLSLIDE_VIBRATO;
entry->effectvalue = ptr[1] & 0xF;
break;
case 30:
entry->effect = IT_TREMOLO;
entry->effectvalue = ptr[1];
break;
case 31:
entry->effect = IT_S;
entry->effectvalue = EFFECT_VALUE(IT_S_SET_TREMOLO_WAVEFORM, ptr[1] & 11);
break;
case 40:
entry->effect = IT_SET_SAMPLE_OFFSET;
entry->effectvalue = ptr[2];
ptr += 2;
break;
case 41:
entry->effect = IT_XM_RETRIGGER_NOTE;
entry->effectvalue = ptr[1];
break;
case 42:
entry->effect = IT_S;
entry->effectvalue = EFFECT_VALUE(IT_S_DELAYED_NOTE_CUT, ptr[1] & 0xF);
break;
case 43:
entry->effect = IT_S;
entry->effectvalue = EFFECT_VALUE(IT_S_NOTE_DELAY, ptr[1] & 0xF);
break;
case 50:
entry->effect = IT_JUMP_TO_ORDER;
entry->effectvalue = ptr[1];
break;
case 51:
entry->effect = IT_BREAK_TO_ROW;
entry->effectvalue = ptr[1];
break;
case 52:
entry->effect = IT_S;
entry->effectvalue = EFFECT_VALUE(IT_S_PATTERN_LOOP, ptr[1] & 0xF);
break;
case 53:
entry->effect = IT_S;
entry->effectvalue = EFFECT_VALUE(IT_S_PATTERN_DELAY, ptr[1] & 0xF);
break;
case 60:
entry->effect = IT_SET_SPEED;
entry->effectvalue = ptr[1];
break;
case 61:
entry->effect = IT_SET_SONG_TEMPO;
entry->effectvalue = ptr[1];
break;
case 70:
entry->effect = IT_ARPEGGIO;
entry->effectvalue = ptr[1];
break;
case 71:
entry->effect = IT_S;
entry->effectvalue = EFFECT_VALUE(IT_S_FINETUNE, ptr[1] & 0xF);
break;
case 72:
/* "balance" ... panning? */
entry->effect = IT_SET_PANNING;
entry->effectvalue = ((ptr[1] - ((ptr[1] & 8) >> 3)) << 5) / 7;
break;
default:
entry->mask &= ~IT_ENTRY_EFFECT;
}
ptr += 2;
}
if (entry->mask) entry++;
}
}
p->n_entries = (int)(entry - p->entry);
offset += psize;
}
free(buffer);
return 0;
error_fb:
free(buffer);
error:
return -1;
}
#define PSM_COMPONENT_ORDERS 0
#define PSM_COMPONENT_PANPOS 1
#define PSM_COMPONENT_PATTERNS 2
#define PSM_COMPONENT_SAMPLE_HEADERS 3
#define PSM_COMPONENT_COMMENTS 4
typedef struct PSM_COMPONENT
{
unsigned char type;
int32 offset;
}
PSM_COMPONENT;
static int CDECL psm_component_compare(const void *e1, const void *e2)
{
return ((const PSM_COMPONENT *)e1)->offset -
((const PSM_COMPONENT *)e2)->offset;
}
static DUMB_IT_SIGDATA *it_old_psm_load_sigdata(DUMBFILE *f)
{
DUMB_IT_SIGDATA *sigdata;
unsigned char * ptr = 0;
PSM_COMPONENT *component;
int n_components = 0;
int n, flags, version, pver, n_orders, n_channels, total_pattern_size;
if (dumbfile_mgetl(f) != DUMB_ID('P','S','M',254)) goto error;
sigdata = malloc(sizeof(*sigdata));
if (!sigdata) goto error;
if (dumbfile_getnc(sigdata->name, 60, f) < 60 ||
sigdata->name[59] != 0x1A) goto error_sd;
sigdata->name[59] = 0;
flags = dumbfile_getc(f);
version = dumbfile_getc(f);
pver = dumbfile_getc(f);
sigdata->speed = dumbfile_getc(f);
sigdata->tempo = dumbfile_getc(f);
sigdata->mixing_volume = dumbfile_getc(f);
sigdata->n_orders = dumbfile_igetw(f);
n_orders = dumbfile_igetw(f);
sigdata->n_patterns = dumbfile_igetw(f);
sigdata->n_samples = dumbfile_igetw(f);
sigdata->n_pchannels = dumbfile_igetw(f);
n_channels = dumbfile_igetw(f);
if (dumbfile_error(f) ||
(flags & 1) ||
(version != 1 && version != 0x10) ||
(pver) ||
(sigdata->n_orders <= 0) ||
(sigdata->n_orders > 255) ||
(n_orders > 255) ||
(n_orders < sigdata->n_orders) ||
(sigdata->n_patterns > 255) ||
(sigdata->n_samples > 255) ||
(sigdata->n_pchannels > DUMB_IT_N_CHANNELS) ||
(sigdata->n_pchannels > n_channels) ||
(n_channels > DUMB_IT_N_CHANNELS))
goto error_sd;
sigdata->flags = IT_STEREO | IT_OLD_EFFECTS | IT_COMPATIBLE_GXX;
sigdata->global_volume = 128;
sigdata->pan_separation = 128;
sigdata->song_message = NULL;
sigdata->order = NULL;
sigdata->instrument = NULL;
sigdata->sample = NULL;
sigdata->pattern = NULL;
sigdata->midi = NULL;
sigdata->checkpoint = NULL;
sigdata->n_instruments = 0;
sigdata->restart_position = 0;
sigdata->order = malloc(sigdata->n_orders);
if (!sigdata->order) goto error_usd;
if (sigdata->n_samples) {
sigdata->sample = malloc(sigdata->n_samples * sizeof(*sigdata->sample));
if (!sigdata->sample) goto error_usd;
for (n = 0; n < sigdata->n_samples; n++)
sigdata->sample[n].data = NULL;
}
if (sigdata->n_patterns) {
sigdata->pattern = malloc(sigdata->n_patterns * sizeof(*sigdata->pattern));
if (!sigdata->pattern) goto error_usd;
for (n = 0; n < sigdata->n_patterns; n++)
sigdata->pattern[n].entry = NULL;
}
component = malloc(5 * sizeof(*component));
if (!component) goto error_usd;
for (n = 0; n < 5; n++) {
component[n_components].offset = dumbfile_igetl(f);
if (component[n_components].offset) {
component[n_components].type = n;
n_components++;
}
}
if (!n_components) goto error_fc;
total_pattern_size = dumbfile_igetl(f);
if (!total_pattern_size) goto error_fc;
qsort(component, n_components, sizeof(PSM_COMPONENT), &psm_component_compare);
memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS);
for (n = 0; n < DUMB_IT_N_CHANNELS; n += 4) {
sigdata->channel_pan[n ] = 16;
sigdata->channel_pan[n+1] = 48;
sigdata->channel_pan[n+2] = 48;
sigdata->channel_pan[n+3] = 16;
}
for (n = 0; n < n_components; n++)
{
int o;
int32 data_pos, data_size;
/* Whee, sample data may be before the sample headers */
data_pos = dumbfile_pos(f);
if (data_pos > component[n].offset) goto error_fc;
data_size = component[n].offset - data_pos;
if (data_size) {
ptr = malloc(data_size);
if (!ptr) goto error_fc;
if (dumbfile_getnc(ptr, data_size, f) < data_size) goto error_fp;
}
switch (component[n].type) {
case PSM_COMPONENT_ORDERS:
if (dumbfile_getnc(sigdata->order, sigdata->n_orders, f) < sigdata->n_orders) goto error_fp;
if (n_orders > sigdata->n_orders)
if (dumbfile_skip(f, n_orders - sigdata->n_orders))
goto error_fp;
if (dumbfile_igetw(f)) goto error_fp;
break;
case PSM_COMPONENT_PANPOS:
if (dumbfile_getnc(sigdata->channel_pan, sigdata->n_pchannels, f) < sigdata->n_pchannels) goto error_fp;
for (o = 0; o < sigdata->n_pchannels; o++) {
sigdata->channel_pan[o] -= (sigdata->channel_pan[o] & 8) >> 3;
sigdata->channel_pan[o] = ((int)sigdata->channel_pan[o] << 5) / 7;
}
break;
case PSM_COMPONENT_PATTERNS:
if (it_old_psm_read_patterns(sigdata->pattern, f, sigdata->n_patterns, total_pattern_size, sigdata->n_pchannels, flags)) goto error_fp;
break;
case PSM_COMPONENT_SAMPLE_HEADERS:
if (it_old_psm_read_samples(&sigdata->sample, f, &sigdata->n_samples, ptr, data_pos, data_size)) goto error_fp;
break;
case PSM_COMPONENT_COMMENTS:
if (dumbfile_mgetl(f) == DUMB_ID('T','E','X','T')) {
o = dumbfile_igetw(f);
if (o > 0) {
sigdata->song_message = malloc(o + 1);
if (dumbfile_getnc(sigdata->song_message, o, f) < o) goto error_fp;
sigdata->song_message[o] = 0;
}
}
break;
}
if (ptr) {
free(ptr);
ptr = 0;
}
}
_dumb_it_fix_invalid_orders(sigdata);
free(component);
return sigdata;
error_fp:
if (ptr) free(ptr);
error_fc:
free(component);
error_usd:
_dumb_it_unload_sigdata(sigdata);
return NULL;
error_sd:
free(sigdata);
error:
return NULL;
}
DUH *DUMBEXPORT dumb_read_old_psm_quick(DUMBFILE *f)
{
sigdata_t *sigdata;
DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
sigdata = it_old_psm_load_sigdata(f);
if (!sigdata)
return NULL;
{
const char *tag[2][2];
tag[0][0] = "TITLE";
tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
tag[1][0] = "FORMAT";
tag[1][1] = "PSM (old)";
return make_duh(-1, 2, (const char *const (*)[2])tag, 1, &descptr, &sigdata);
}
}

1280
dumb/src/it/readpsm.c Normal file

File diff suppressed because it is too large Load diff

568
dumb/src/it/readptm.c Normal file
View file

@ -0,0 +1,568 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readptm.c - Code to read a Poly Tracker v2.03 / / \ \
* module from an open file. | < / \_
* | \/ /\ /
* By Chris Moeller. Based on reads3m.c \_ / > /
* by entheh. | \ / /
* | ' /
* \__/
*/
// IT_STEREO... :o
#include <stdlib.h>
#include <string.h>
#include "dumb.h"
#include "internal/it.h"
/** WARNING: this is duplicated in itread.c */
static int it_seek(DUMBFILE *f, int32 offset)
{
int32 pos = dumbfile_pos(f);
if (pos > offset)
return -1;
if (pos < offset)
if (dumbfile_skip(f, offset - pos))
return -1;
return 0;
}
static int it_ptm_read_sample_header(IT_SAMPLE *sample, int32 *offset, DUMBFILE *f)
{
int flags;
flags = dumbfile_getc(f);
dumbfile_getnc(sample->filename, 12, f);
sample->filename[12] = 0;
sample->default_volume = dumbfile_getc(f);
sample->C5_speed = dumbfile_igetw(f) << 1;
dumbfile_skip(f, 2); /* segment */
*offset = dumbfile_igetl(f);
sample->length = dumbfile_igetl(f);
sample->loop_start = dumbfile_igetl(f);
sample->loop_end = dumbfile_igetl(f);
/* GUSBegin, GUSLStart, GUSLEnd, GUSLoop, reserverd */
dumbfile_skip(f, 4+4+4+1+1);
dumbfile_getnc(sample->name, 28, f);
sample->name[28] = 0;
/*
if (dumbfile_mgetl(f) != DUMB_ID('P','T','M','S'))
return -1;
*/
/* BLAH! Shit likes to have broken or missing sample IDs */
dumbfile_skip(f, 4);
if ((flags & 3) == 0) {
/* Looks like no sample */
sample->flags &= ~IT_SAMPLE_EXISTS;
return dumbfile_error(f);
}
sample->global_volume = 64;
sample->flags = IT_SAMPLE_EXISTS;
if (flags & 4) sample->flags |= IT_SAMPLE_LOOP;
if (flags & 8) sample->flags |= IT_SAMPLE_PINGPONG_LOOP;
if (flags & 16) {
sample->flags |= IT_SAMPLE_16BIT;
sample->length >>= 1;
sample->loop_start >>= 1;
sample->loop_end >>= 1;
}
if (sample->loop_end) sample->loop_end--;
sample->default_pan = 0; // 0 = don't use, or 160 = centre?
if (sample->length <= 0)
sample->flags &= ~IT_SAMPLE_EXISTS;
else if (sample->flags & IT_SAMPLE_LOOP) {
if ((unsigned int)sample->loop_end > (unsigned int)sample->length)
sample->flags &= ~IT_SAMPLE_LOOP;
else if ((unsigned int)sample->loop_start >= (unsigned int)sample->loop_end)
sample->flags &= ~IT_SAMPLE_LOOP;
else
sample->length = sample->loop_end;
}
//Do we need to set all these?
sample->vibrato_speed = 0;
sample->vibrato_depth = 0;
sample->vibrato_rate = 0;
sample->vibrato_waveform = IT_VIBRATO_SINE;
sample->finetune = 0;
sample->max_resampling_quality = -1;
return dumbfile_error(f);
}
static int it_ptm_read_byte(DUMBFILE *f)
{
int meh = dumbfile_getc(f);
if (meh < 0) return 0;
return meh;
}
static int it_ptm_read_sample_data(IT_SAMPLE *sample, int last, DUMBFILE *f)
{
int32 n;
int s;
sample->data = malloc(sample->length * (sample->flags & IT_SAMPLE_16BIT ? 2 : 1));
if (!sample->data)
return -1;
s = 0;
if (sample->flags & IT_SAMPLE_16BIT) {
unsigned char a, b;
for (n = 0; n < sample->length; n++) {
a = s += (signed char) it_ptm_read_byte(f);
b = s += (signed char) it_ptm_read_byte(f);
((short *)sample->data)[n] = a | (b << 8);
}
} else {
for (n = 0; n < sample->length; n++) {
s += (signed char) it_ptm_read_byte(f);
((signed char *)sample->data)[n] = s;
}
}
if (dumbfile_error(f) && !last)
return -1;
return 0;
}
static int it_ptm_read_pattern(IT_PATTERN *pattern, DUMBFILE *f, unsigned char *buffer, int length)
{
int buflen = 0;
int bufpos = 0;
int effect, effectvalue;
IT_ENTRY *entry;
unsigned char channel;
if (!length)
return -1;
pattern->n_rows = 0;
pattern->n_entries = 0;
/* Read in the pattern data, little by little, and work out how many
* entries we need room for. Sorry, but this is just so funny...
*/
for (;;) {
unsigned char b = buffer[buflen++] = dumbfile_getc(f);
#if 1
static const unsigned char used[8] = {0, 2, 2, 4, 1, 3, 3, 5};
channel = b & 31;
b >>= 5;
pattern->n_entries++;
if (b) {
if (buflen + used[b] >= 65536) return -1;
dumbfile_getnc(buffer + buflen, used[b], f);
buflen += used[b];
} else {
/* End of row */
if (++pattern->n_rows == 64) break;
if (buflen >= 65536) return -1;
}
#else
if (b == 0) {
/* End of row */
pattern->n_entries++;
if (++pattern->n_rows == 64) break;
if (buflen >= 65536) return -1;
} else {
static const unsigned char used[8] = {0, 2, 2, 4, 1, 3, 3, 5};
channel = b & 31;
b >>= 5;
if (b) {
pattern->n_entries++;
if (buflen + used[b] >= 65536) return -1;
dumbfile_getnc(buffer + buflen, used[b], f);
buflen += used[b];
}
}
#endif
/* We have ensured that buflen < 65536 at this point, so it is safe
* to iterate and read at least one more byte without checking.
* However, now would be a good time to check for errors reading from
* the file.
*/
if (dumbfile_error(f))
return -1;
/* Great. We ran out of data, but there should be data for more rows.
* Fill the rest with null data...
*/
if (buflen >= length && pattern->n_rows < 64)
{
while (pattern->n_rows < 64)
{
if (buflen >= 65536) return -1;
buffer[buflen++] = 0;
pattern->n_entries++;
pattern->n_rows++;
}
break;
}
}
pattern->entry = malloc(pattern->n_entries * sizeof(*pattern->entry));
if (!pattern->entry)
return -1;
entry = pattern->entry;
while (bufpos < buflen) {
unsigned char b = buffer[bufpos++];
if (b == 0)
{
/* End of row */
IT_SET_END_ROW(entry);
entry++;
continue;
}
channel = b & 31;
if (b & 224) {
entry->mask = 0;
entry->channel = channel;
if (b & 32) {
unsigned char n = buffer[bufpos++];
if (n == 254 || (n >= 1 && n <= 120)) {
if (n == 254)
entry->note = IT_NOTE_CUT;
else
entry->note = n - 1;
entry->mask |= IT_ENTRY_NOTE;
}
entry->instrument = buffer[bufpos++];
if (entry->instrument)
entry->mask |= IT_ENTRY_INSTRUMENT;
}
if (b & 64) {
effect = buffer[bufpos++];
effectvalue = buffer[bufpos++];
_dumb_it_ptm_convert_effect(effect, effectvalue, entry);
}
if (b & 128) {
entry->volpan = buffer[bufpos++];
if (entry->volpan <= 64)
entry->mask |= IT_ENTRY_VOLPAN;
}
entry++;
}
}
ASSERT(entry == pattern->entry + pattern->n_entries);
return 0;
}
/** WARNING: this is duplicated in itread.c - also bad practice to use the same struct name unless they are unified in a header */
/* Currently we assume the sample data are stored after the sample headers in
* module files. This assumption may be unjustified; let me know if you have
* trouble.
*/
#define PTM_COMPONENT_INSTRUMENT 1
#define PTM_COMPONENT_PATTERN 2
#define PTM_COMPONENT_SAMPLE 3
typedef struct PTM_COMPONENT
{
unsigned char type;
unsigned char n;
int32 offset;
}
PTM_COMPONENT;
static int CDECL ptm_component_compare(const void *e1, const void *e2)
{
return ((const PTM_COMPONENT *)e1)->offset -
((const PTM_COMPONENT *)e2)->offset;
}
static DUMB_IT_SIGDATA *it_ptm_load_sigdata(DUMBFILE *f)
{
DUMB_IT_SIGDATA *sigdata;
PTM_COMPONENT *component;
int n_components = 0;
int n;
unsigned char *buffer;
sigdata = malloc(sizeof(*sigdata));
if (!sigdata) return NULL;
/* Skip song name. */
dumbfile_getnc(sigdata->name, 28, f);
sigdata->name[28] = 0;
if (dumbfile_getc(f) != 0x1A || dumbfile_igetw(f) != 0x203) {
free(sigdata);
return NULL;
}
dumbfile_skip(f, 1);
sigdata->song_message = NULL;
sigdata->order = NULL;
sigdata->instrument = NULL;
sigdata->sample = NULL;
sigdata->pattern = NULL;
sigdata->midi = NULL;
sigdata->checkpoint = NULL;
sigdata->n_orders = dumbfile_igetw(f);
sigdata->n_instruments = 0;
sigdata->n_samples = dumbfile_igetw(f);
sigdata->n_patterns = dumbfile_igetw(f);
if (dumbfile_error(f) || sigdata->n_orders <= 0 || sigdata->n_samples > 255 || sigdata->n_patterns > 128) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
sigdata->n_pchannels = dumbfile_igetw(f);
if (dumbfile_igetw(f) != 0) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
dumbfile_skip(f, 2);
if (dumbfile_mgetl(f) != DUMB_ID('P','T','M','F')) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
dumbfile_skip(f, 16);
sigdata->order = malloc(sigdata->n_orders);
if (!sigdata->order) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
if (sigdata->n_samples) {
sigdata->sample = malloc(sigdata->n_samples * sizeof(*sigdata->sample));
if (!sigdata->sample) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (n = 0; n < sigdata->n_samples; n++)
sigdata->sample[n].data = NULL;
}
if (sigdata->n_patterns) {
sigdata->pattern = malloc(sigdata->n_patterns * sizeof(*sigdata->pattern));
if (!sigdata->pattern) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (n = 0; n < sigdata->n_patterns; n++)
sigdata->pattern[n].entry = NULL;
}
/** WARNING: which ones? */
sigdata->flags = IT_STEREO | IT_OLD_EFFECTS | IT_COMPATIBLE_GXX | IT_WAS_A_PTM;
sigdata->global_volume = 128;
sigdata->speed = 6;
sigdata->tempo = 125;
sigdata->mixing_volume = 48;
/* Panning positions for 32 channels */
{
int i;
for (i = 0; i < 32; i++) {
int c = dumbfile_getc(f);
if (c <= 15) {
sigdata->channel_volume[i] = 64;
sigdata->channel_pan[i] = c;
} else {
/** WARNING: this could be improved if we support channel muting... */
sigdata->channel_volume[i] = 0;
sigdata->channel_pan[i] = 7;
}
}
}
/* Orders, byte each, length = sigdata->n_orders (should be even) */
dumbfile_getnc(sigdata->order, sigdata->n_orders, f);
sigdata->restart_position = 0;
component = malloc(768*sizeof(*component));
if (!component) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
if (it_seek(f, 352)) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (n = 0; n < sigdata->n_patterns; n++) {
component[n_components].type = PTM_COMPONENT_PATTERN;
component[n_components].n = n;
component[n_components].offset = dumbfile_igetw(f) << 4;
n_components++;
}
if (it_seek(f, 608)) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (n = 0; n < sigdata->n_samples; n++) {
if (it_ptm_read_sample_header(&sigdata->sample[n], &component[n_components].offset, f)) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
if (!(sigdata->sample[n].flags & IT_SAMPLE_EXISTS)) continue;
component[n_components].type = PTM_COMPONENT_SAMPLE;
component[n_components].n = n;
n_components++;
}
qsort(component, n_components, sizeof(PTM_COMPONENT), &ptm_component_compare);
{
int i;
for (i = 0; i < 32; i++) {
sigdata->channel_pan[i] -= (sigdata->channel_pan[i] & 8) >> 3;
sigdata->channel_pan[i] = ((int)sigdata->channel_pan[i] << 5) / 7;
if (sigdata->channel_pan[i] > 64) sigdata->channel_pan[i] = 64;
}
}
sigdata->pan_separation = 128;
if (dumbfile_error(f)) {
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
buffer = malloc(65536);
if (!buffer) {
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (n = 0; n < n_components; n++) {
if (it_seek(f, component[n].offset)) {
free(buffer);
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
switch (component[n].type) {
case PTM_COMPONENT_PATTERN:
if (it_ptm_read_pattern(&sigdata->pattern[component[n].n], f, buffer, (n + 1 < n_components) ? (component[n+1].offset - component[n].offset) : 0)) {
free(buffer);
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
break;
case PTM_COMPONENT_SAMPLE:
if (it_ptm_read_sample_data(&sigdata->sample[component[n].n], (n + 1 == n_components), f)) {
free(buffer);
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
}
}
free(buffer);
free(component);
_dumb_it_fix_invalid_orders(sigdata);
return sigdata;
}
DUH *DUMBEXPORT dumb_read_ptm_quick(DUMBFILE *f)
{
sigdata_t *sigdata;
DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
sigdata = it_ptm_load_sigdata(f);
if (!sigdata)
return NULL;
{
const char *tag[2][2];
tag[0][0] = "TITLE";
tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
tag[1][0] = "FORMAT";
tag[1][1] = "PTM";
return make_duh(-1, 2, (const char *const (*)[2])tag, 1, &descptr, &sigdata);
}
}

73
dumb/src/it/readriff.c Normal file
View file

@ -0,0 +1,73 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readriff.c - Code to read a RIFF module file / / \ \
* from memory. | < / \_
* | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
#include "internal/it.h"
#include "internal/riff.h"
DUH *dumb_read_riff_amff( struct riff * stream );
DUH *dumb_read_riff_am( struct riff * stream );
DUH *dumb_read_riff_dsmf( struct riff * stream );
/* dumb_read_riff_quick(): reads a RIFF file into a DUH struct, returning a
* pointer to the DUH struct. When you have finished with it, you must pass
* the pointer to unload_duh() so that the memory can be freed.
*/
DUH *DUMBEXPORT dumb_read_riff_quick( DUMBFILE * f )
{
DUH * duh;
struct riff * stream;
{
unsigned char * buffer = 0;
int32 size = 0;
int32 read;
do
{
buffer = realloc( buffer, 32768 + size );
if ( ! buffer ) return 0;
read = dumbfile_getnc( buffer + size, 32768, f );
if ( read < 0 )
{
free( buffer );
return 0;
}
size += read;
}
while ( read == 32768 );
stream = riff_parse( buffer, size, 1 );
if ( ! stream ) stream = riff_parse( buffer, size, 0 );
free( buffer );
}
if ( ! stream ) return 0;
if ( stream->type == DUMB_ID( 'A', 'M', ' ', ' ' ) )
duh = dumb_read_riff_am( stream );
else if ( stream->type == DUMB_ID( 'A', 'M', 'F', 'F' ) )
duh = dumb_read_riff_amff( stream );
else if ( stream->type == DUMB_ID( 'D', 'S', 'M', 'F' ) )
duh = dumb_read_riff_dsmf( stream );
else duh = 0;
riff_free( stream );
return duh;
}

861
dumb/src/it/reads3m.c Normal file
View file

@ -0,0 +1,861 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* reads3m.c - Code to read a ScreamTracker 3 / / \ \
* module from an open file. | < / \_
* | \/ /\ /
* By entheh. \_ / > /
* | \ / /
* | ' /
* \__/
*/
// IT_STEREO... :o
#include <stdlib.h>
#include <string.h>
#include "dumb.h"
#include "internal/it.h"
//#define S3M_BROKEN_OVERLAPPED_SAMPLES
/** WARNING: this is duplicated in itread.c */
static int it_seek(DUMBFILE *f, int32 offset)
{
int32 pos = dumbfile_pos(f);
if (pos > offset) {
return -1;
}
if (pos < offset)
if (dumbfile_skip(f, offset - pos))
return -1;
return 0;
}
static int it_s3m_read_sample_header(IT_SAMPLE *sample, int32 *offset, unsigned char *pack, int cwtv, DUMBFILE *f)
{
unsigned char type;
int flags;
type = dumbfile_getc(f);
dumbfile_getnc(sample->filename, 12, f);
sample->filename[12] = 0;
if (type > 1) {
/** WARNING: no adlib support */
dumbfile_skip(f, 3 + 12 + 1 + 1 + 2 + 2 + 2 + 12);
dumbfile_getnc(sample->name, 28, f);
sample->name[28] = 0;
dumbfile_skip(f, 4);
sample->flags &= ~IT_SAMPLE_EXISTS;
return dumbfile_error(f);
}
*offset = dumbfile_getc(f) << 20;
*offset += dumbfile_igetw(f) << 4;
sample->length = dumbfile_igetl(f);
sample->loop_start = dumbfile_igetl(f);
sample->loop_end = dumbfile_igetl(f);
sample->default_volume = dumbfile_getc(f);
dumbfile_skip(f, 1);
flags = dumbfile_getc(f);
if (flags < 0 || (flags != 0 && flags != 4))
/* Sample is packed apparently (or error reading from file). We don't
* know how to read packed samples.
*/
return -1;
*pack = flags;
flags = dumbfile_getc(f);
sample->C5_speed = dumbfile_igetl(f) << 1;
/* Skip four unused bytes and three internal variables. */
dumbfile_skip(f, 4+2+2+4);
dumbfile_getnc(sample->name, 28, f);
sample->name[28] = 0;
if (type == 0 || sample->length <= 0) {
/* Looks like no-existy. Anyway, there's for sure no 'SCRS' ... */
sample->flags &= ~IT_SAMPLE_EXISTS;
return dumbfile_error(f);
}
if (dumbfile_mgetl(f) != DUMB_ID('S','C','R','S'))
return -1;
sample->global_volume = 64;
sample->flags = IT_SAMPLE_EXISTS;
if (flags & 1) sample->flags |= IT_SAMPLE_LOOP;
/* The ST3 TECH.DOC is unclear on this, but IMAGO Orpheus is not. Piece of crap. */
if (flags & 2) {
sample->flags |= IT_SAMPLE_STEREO;
if ((cwtv & 0xF000) == 0x2000) {
sample->length >>= 1;
sample->loop_start >>= 1;
sample->loop_end >>= 1;
}
}
if (flags & 4) {
sample->flags |= IT_SAMPLE_16BIT;
if ((cwtv & 0xF000) == 0x2000) {
sample->length >>= 1;
sample->loop_start >>= 1;
sample->loop_end >>= 1;
}
}
sample->default_pan = 0; // 0 = don't use, or 160 = centre?
if (sample->flags & IT_SAMPLE_LOOP) {
if ((unsigned int)sample->loop_end > (unsigned int)sample->length)
/*sample->flags &= ~IT_SAMPLE_LOOP;*/
sample->loop_end = sample->length;
else if ((unsigned int)sample->loop_start >= (unsigned int)sample->loop_end)
sample->flags &= ~IT_SAMPLE_LOOP;
else
/* ScreamTracker seems not to save what comes after the loop end
* point, but rather to assume it is a duplicate of what comes at
* the loop start point. I am not completely sure of this though.
* It is easy to evade; simply truncate the sample.
*/
sample->length = sample->loop_end;
}
//Do we need to set all these?
sample->vibrato_speed = 0;
sample->vibrato_depth = 0;
sample->vibrato_rate = 0;
sample->vibrato_waveform = IT_VIBRATO_SINE;
sample->finetune = 0;
sample->max_resampling_quality = -1;
return dumbfile_error(f);
}
static int it_s3m_read_sample_data(IT_SAMPLE *sample, int ffi, unsigned char pack, DUMBFILE *f)
{
int32 n;
int32 datasize = sample->length;
if (sample->flags & IT_SAMPLE_STEREO) datasize <<= 1;
sample->data = malloc(datasize * (sample->flags & IT_SAMPLE_16BIT ? 2 : 1));
if (!sample->data)
return -1;
if (pack == 4) {
if (_dumb_it_read_sample_data_adpcm4(sample, f) < 0)
return -1;
}
else if (sample->flags & IT_SAMPLE_STEREO) {
if (sample->flags & IT_SAMPLE_16BIT) {
for (n = 0; n < datasize; n += 2)
((short *)sample->data)[n] = dumbfile_igetw(f);
for (n = 1; n < datasize; n += 2)
((short *)sample->data)[n] = dumbfile_igetw(f);
} else {
for (n = 0; n < datasize; n += 2)
((signed char *)sample->data)[n] = dumbfile_getc(f);
for (n = 1; n < datasize; n += 2)
((signed char *)sample->data)[n] = dumbfile_getc(f);
}
} else if (sample->flags & IT_SAMPLE_16BIT)
for (n = 0; n < sample->length; n++)
((short *)sample->data)[n] = dumbfile_igetw(f);
else
for (n = 0; n < sample->length; n++)
((signed char *)sample->data)[n] = dumbfile_getc(f);
if (dumbfile_error(f))
return -1;
if (ffi != 1) {
/* Convert to signed. */
if (sample->flags & IT_SAMPLE_16BIT)
for (n = 0; n < datasize; n++)
((short *)sample->data)[n] ^= 0x8000;
else
for (n = 0; n < datasize; n++)
((signed char *)sample->data)[n] ^= 0x80;
}
return 0;
}
static int it_s3m_read_pattern(IT_PATTERN *pattern, DUMBFILE *f, unsigned char *buffer, int maxlen)
{
int length;
int buflen = 0;
int bufpos = 0;
IT_ENTRY *entry;
unsigned char channel;
/* Haha, this is hilarious!
*
* Well, after some experimentation, it seems that different S3M writers
* define the format in different ways. The S3M docs say that the first
* two bytes hold the "length of [the] packed pattern", and the packed
* pattern data follow. Judging by the contents of ARMANI.S3M, packaged
* with ScreamTracker itself, the measure of length _includes_ the two
* bytes used to store the length; in other words, we should read
* (length - 2) more bytes. However, aryx.s3m, packaged with ModPlug
* Tracker, excludes these two bytes, so (length) more bytes must be
* read.
*
* Call me crazy, but I just find it insanely funny that the format was
* misunderstood in this way :D
*
* Now we can't just risk reading two extra bytes, because then we
* overshoot, and DUMBFILEs don't support backward seeking (for a good
* reason). Luckily, there is a way. We can read the data little by
* little, and stop when we have 64 rows in memory. Provided we protect
* against buffer overflow, this method should work with all sensibly
* written S3M files. If you find one for which it does not work, please
* let me know at entheh@users.sf.net so I can look at it.
*/
/* Discard the length. */
/* read at most length bytes, in case of retarded crap */
length = dumbfile_igetw(f);
if (maxlen)
{
maxlen -= 2;
if (length > maxlen) length = maxlen;
}
if (dumbfile_error(f) || !length)
return -1;
pattern->n_rows = 0;
pattern->n_entries = 0;
/* Read in the pattern data, little by little, and work out how many
* entries we need room for. Sorry, but this is just so funny...
*/
for (;;) {
unsigned char b = buffer[buflen++] = dumbfile_getc(f);
#if 1
static const unsigned char used[8] = {0, 2, 1, 3, 2, 4, 3, 5};
channel = b & 31;
b >>= 5;
pattern->n_entries++;
if (b) {
if (buflen + used[b] >= 65536) return -1;
if (buflen + used[b] <= length)
dumbfile_getnc(buffer + buflen, used[b], f);
else
memset(buffer + buflen, 0, used[b]);
buflen += used[b];
} else {
/* End of row */
if (++pattern->n_rows == 64) break;
if (buflen >= 65536) return -1;
}
#else
if (b == 0) {
/* End of row */
pattern->n_entries++;
if (++pattern->n_rows == 64) break;
if (buflen >= 65536) return -1;
} else {
static const unsigned char used[8] = {0, 2, 1, 3, 2, 4, 3, 5};
channel = b & 31;
b >>= 5;
if (b) {
pattern->n_entries++;
if (buflen + used[b] >= 65536) return -1;
dumbfile_getnc(buffer + buflen, used[b], f);
buflen += used[b];
}
}
#endif
/* We have ensured that buflen < 65536 at this point, so it is safe
* to iterate and read at least one more byte without checking.
* However, now would be a good time to check for errors reading from
* the file.
*/
if (dumbfile_error(f))
return -1;
/* Great. We ran out of data, but there should be data for more rows.
* Fill the rest with null data...
*/
if (buflen >= length && pattern->n_rows < 64)
{
while (pattern->n_rows < 64)
{
if (buflen >= 65536) return -1;
buffer[buflen++] = 0;
pattern->n_entries++;
pattern->n_rows++;
}
break;
}
}
pattern->entry = malloc(pattern->n_entries * sizeof(*pattern->entry));
if (!pattern->entry)
return -1;
entry = pattern->entry;
while (bufpos < buflen) {
unsigned char b = buffer[bufpos++];
#if 1
if (!(b & ~31))
#else
if (b == 0)
#endif
{
/* End of row */
IT_SET_END_ROW(entry);
entry++;
continue;
}
channel = b & 31;
if (b & 224) {
entry->mask = 0;
entry->channel = channel;
if (b & 32) {
unsigned char n = buffer[bufpos++];
if (n != 255) {
if (n == 254)
entry->note = IT_NOTE_CUT;
else
entry->note = (n >> 4) * 12 + (n & 15);
entry->mask |= IT_ENTRY_NOTE;
}
entry->instrument = buffer[bufpos++];
if (entry->instrument)
entry->mask |= IT_ENTRY_INSTRUMENT;
}
if (b & 64) {
entry->volpan = buffer[bufpos++];
if (entry->volpan != 255)
entry->mask |= IT_ENTRY_VOLPAN;
}
if (b & 128) {
entry->effect = buffer[bufpos++];
entry->effectvalue = buffer[bufpos++];
// XXX woot
if (entry->effect && entry->effect < IT_MIDI_MACRO /*!= 255*/) {
entry->mask |= IT_ENTRY_EFFECT;
switch (entry->effect) {
case IT_BREAK_TO_ROW:
entry->effectvalue -= (entry->effectvalue >> 4) * 6;
break;
case IT_SET_CHANNEL_VOLUME:
case IT_CHANNEL_VOLUME_SLIDE:
case IT_PANNING_SLIDE:
case IT_GLOBAL_VOLUME_SLIDE:
case IT_PANBRELLO:
case IT_MIDI_MACRO:
entry->mask &= ~IT_ENTRY_EFFECT;
break;
case IT_S:
switch (entry->effectvalue >> 4) {
case IT_S_SET_PANBRELLO_WAVEFORM:
case IT_S_FINE_PATTERN_DELAY:
case IT_S7:
case IT_S_SET_SURROUND_SOUND:
case IT_S_SET_MIDI_MACRO:
entry->mask &= ~IT_ENTRY_EFFECT;
break;
}
break;
}
}
/** WARNING: ARGH! CONVERT TEH EFFECTS!@~ */
}
entry++;
}
}
ASSERT(entry == pattern->entry + pattern->n_entries);
return 0;
}
/** WARNING: this is duplicated in itread.c - also bad practice to use the same struct name unless they are unified in a header */
/* Currently we assume the sample data are stored after the sample headers in
* module files. This assumption may be unjustified; let me know if you have
* trouble.
*/
#define S3M_COMPONENT_INSTRUMENT 1
#define S3M_COMPONENT_PATTERN 2
#define S3M_COMPONENT_SAMPLE 3
typedef struct S3M_COMPONENT
{
unsigned char type;
unsigned char n;
int32 offset;
short sampfirst; /* component[sampfirst] = first sample data after this */
short sampnext; /* sampnext is used to create linked lists of sample data */
}
S3M_COMPONENT;
static int CDECL s3m_component_compare(const void *e1, const void *e2)
{
return ((const S3M_COMPONENT *)e1)->offset -
((const S3M_COMPONENT *)e2)->offset;
}
static DUMB_IT_SIGDATA *it_s3m_load_sigdata(DUMBFILE *f, int * cwtv)
{
DUMB_IT_SIGDATA *sigdata;
int flags, ffi;
int default_pan_present;
int master_volume;
unsigned char sample_pack[256];
S3M_COMPONENT *component;
int n_components = 0;
int n;
unsigned char *buffer;
sigdata = malloc(sizeof(*sigdata));
if (!sigdata) return NULL;
dumbfile_getnc(sigdata->name, 28, f);
sigdata->name[28] = 0;
n = dumbfile_getc(f);
if (n != 0x1A && n != 0) {
free(sigdata);
return NULL;
}
if (dumbfile_getc(f) != 16) {
free(sigdata);
return NULL;
}
dumbfile_skip(f, 2);
sigdata->song_message = NULL;
sigdata->order = NULL;
sigdata->instrument = NULL;
sigdata->sample = NULL;
sigdata->pattern = NULL;
sigdata->midi = NULL;
sigdata->checkpoint = NULL;
sigdata->n_orders = dumbfile_igetw(f);
sigdata->n_instruments = 0;
sigdata->n_samples = dumbfile_igetw(f);
sigdata->n_patterns = dumbfile_igetw(f);
if (dumbfile_error(f) || sigdata->n_orders <= 0 || sigdata->n_samples > 256 || sigdata->n_patterns > 256) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
sigdata->order = malloc(sigdata->n_orders);
if (!sigdata->order) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
if (sigdata->n_samples) {
sigdata->sample = malloc(sigdata->n_samples * sizeof(*sigdata->sample));
if (!sigdata->sample) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (n = 0; n < sigdata->n_samples; n++)
sigdata->sample[n].data = NULL;
}
if (sigdata->n_patterns) {
sigdata->pattern = malloc(sigdata->n_patterns * sizeof(*sigdata->pattern));
if (!sigdata->pattern) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (n = 0; n < sigdata->n_patterns; n++)
sigdata->pattern[n].entry = NULL;
}
flags = dumbfile_igetw(f);
*cwtv = dumbfile_igetw(f);
if (*cwtv == 0x1300) {
/** WARNING: volume slides on every frame */
}
ffi = dumbfile_igetw(f);
/** WARNING: which ones? */
sigdata->flags = IT_OLD_EFFECTS | IT_COMPATIBLE_GXX | IT_WAS_AN_S3M;
if (dumbfile_mgetl(f) != DUMB_ID('S','C','R','M')) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
sigdata->global_volume = dumbfile_getc(f) << 1;
if ( !sigdata->global_volume || sigdata->global_volume > 128 ) sigdata->global_volume = 128;
sigdata->speed = dumbfile_getc(f);
if (sigdata->speed == 0) sigdata->speed = 6; // Should we? What about tempo?
sigdata->tempo = dumbfile_getc(f);
master_volume = dumbfile_getc(f); // 7 bits; +128 for stereo
//what do we do with master_volume? it's not the same as mixing volume...
sigdata->mixing_volume = 48;
if (master_volume & 128) sigdata->flags |= IT_STEREO;
/* Skip GUS Ultra Click Removal byte. */
dumbfile_getc(f);
default_pan_present = dumbfile_getc(f);
dumbfile_skip(f, 8);
/* Skip Special Custom Data Pointer. */
/** WARNING: investigate this? */
dumbfile_igetw(f);
sigdata->n_pchannels = 0;
/* Channel settings for 32 channels, 255=unused, +128=disabled */
{
int i;
for (i = 0; i < 32; i++) {
int c = dumbfile_getc(f);
if (!(c & (128 | 16))) { /* +128=disabled, +16=Adlib */
if (sigdata->n_pchannels < i + 1) sigdata->n_pchannels = i + 1;
sigdata->channel_volume[i] = 64;
sigdata->channel_pan[i] = c & 8 ? 12 : 3;
/** WARNING: ah, but it should be 7 for mono... */
} else {
/** WARNING: this could be improved if we support channel muting... */
sigdata->channel_volume[i] = 0;
sigdata->channel_pan[i] = 7;
}
}
}
/* Orders, byte each, length = sigdata->n_orders (should be even) */
dumbfile_getnc(sigdata->order, sigdata->n_orders, f);
sigdata->restart_position = 0;
component = malloc(768*sizeof(*component));
if (!component) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (n = 0; n < sigdata->n_samples; n++) {
component[n_components].type = S3M_COMPONENT_SAMPLE;
component[n_components].n = n;
component[n_components].offset = dumbfile_igetw(f) << 4;
component[n_components].sampfirst = -1;
n_components++;
}
for (n = 0; n < sigdata->n_patterns; n++) {
int32 offset = dumbfile_igetw(f) << 4;
if (offset) {
component[n_components].type = S3M_COMPONENT_PATTERN;
component[n_components].n = n;
component[n_components].offset = offset;
component[n_components].sampfirst = -1;
n_components++;
} else {
/** WARNING: Empty 64-row pattern ... ? (this does happen!) */
sigdata->pattern[n].n_rows = 64;
sigdata->pattern[n].n_entries = 0;
}
}
qsort(component, n_components, sizeof(S3M_COMPONENT), &s3m_component_compare);
/* I found a really dumb S3M file that claimed to contain default pan
* data but didn't contain any. Programs would load it by reading part of
* the first instrument header, assuming the data to be default pan
* positions, and then rereading the instrument module. We cannot do this
* without obfuscating the file input model, so we insert an extra check
* here that we won't overrun the start of the first component.
*/
if (default_pan_present == 252 && component[0].offset >= dumbfile_pos(f) + 32) {
/* Channel default pan positions */
int i;
for (i = 0; i < 32; i++) {
int c = dumbfile_getc(f);
if (c & 32)
sigdata->channel_pan[i] = c & 15;
}
}
{
int i;
for (i = 0; i < 32; i++) {
sigdata->channel_pan[i] -= (sigdata->channel_pan[i] & 8) >> 3;
sigdata->channel_pan[i] = ((int)sigdata->channel_pan[i] << 5) / 7;
}
}
sigdata->pan_separation = 128;
if (dumbfile_error(f)) {
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
buffer = malloc(65536);
if (!buffer) {
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
/* Voila, I must deal with a very dumb S3M myself. This file refers to the same file offset twice
* for two different patterns. Solution: Eliminate it.
*/
for (n = 0; n < n_components; n++) {
if (component[n].type == S3M_COMPONENT_PATTERN) {
int m;
for (m = n + 1; m < n_components; m++) {
if (component[m].type == S3M_COMPONENT_PATTERN) {
if (component[n].offset == component[m].offset) {
int o, pattern;
pattern = component[m].n;
n_components--;
for (o = m; o < n_components; o++) {
component[o] = component[o + 1];
}
for (o = 0; o < sigdata->n_orders; o++) {
if (sigdata->order[o] == pattern) {
sigdata->order[o] = component[n].n;
}
}
sigdata->pattern[pattern].n_rows = 64;
sigdata->pattern[pattern].n_entries = 0;
m--;
} else
break;
}
}
}
}
for (n = 0; n < n_components; n++) {
int32 offset = 0;
int m;
#ifdef S3M_BROKEN_OVERLAPPED_SAMPLES
int last;
#endif
if (it_seek(f, component[n].offset)) {
free(buffer);
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
switch (component[n].type) {
case S3M_COMPONENT_PATTERN:
if (it_s3m_read_pattern(&sigdata->pattern[component[n].n], f, buffer, (n + 1 < n_components) ? (component[n+1].offset - component[n].offset) : 0)) {
free(buffer);
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
break;
case S3M_COMPONENT_SAMPLE:
if (it_s3m_read_sample_header(&sigdata->sample[component[n].n], &offset, &sample_pack[component[n].n], *cwtv, f)) {
free(buffer);
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
if (sigdata->sample[component[n].n].flags & IT_SAMPLE_EXISTS) {
short *sample;
for (m = n + 1; m < n_components; m++)
if (component[m].offset > offset)
break;
m--;
sample = &component[m].sampfirst;
while (*sample >= 0 && component[*sample].offset <= offset)
sample = &component[*sample].sampnext;
component[n].sampnext = *sample;
*sample = n;
component[n].offset = offset;
}
}
m = component[n].sampfirst;
#ifdef S3M_BROKEN_OVERLAPPED_SAMPLES
last = -1;
#endif
while (m >= 0) {
// XXX
#ifdef S3M_BROKEN_OVERLAPPED_SAMPLES
if ( last >= 0 ) {
if ( dumbfile_pos( f ) > component[m].offset ) {
IT_SAMPLE * s1 = &sigdata->sample[component[last].n];
IT_SAMPLE * s2 = &sigdata->sample[component[m].n];
if ( ( s1->flags | s2->flags ) & ( IT_SAMPLE_16BIT | IT_SAMPLE_STEREO ) ) {
free(buffer);
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
if ( component[m].offset >= component[last].offset &&
component[m].offset + s2->length <= component[last].offset + s1->length ) {
s2->left = malloc( s2->length );
if ( ! s2->left ) {
free(buffer);
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
memcpy( s2->left, ( const char * ) s1->left + component[m].offset - component[last].offset, s2->length );
last = -1;
}
}
} else last = 0;
if ( last >= 0 ) {
#endif
if (it_seek(f, component[m].offset)) {
free(buffer);
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
if (it_s3m_read_sample_data(&sigdata->sample[component[m].n], ffi, sample_pack[component[m].n], f)) {
free(buffer);
free(component);
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
#ifdef S3M_BROKEN_OVERLAPPED_SAMPLES
last = m;
}
#endif
m = component[m].sampnext;
}
}
free(buffer);
free(component);
_dumb_it_fix_invalid_orders(sigdata);
return sigdata;
}
static char hexdigit(int in)
{
if (in < 10) return in + '0';
else return in + 'A' - 10;
}
DUH *DUMBEXPORT dumb_read_s3m_quick(DUMBFILE *f)
{
sigdata_t *sigdata;
int cwtv;
DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
sigdata = it_s3m_load_sigdata(f, &cwtv);
if (!sigdata)
return NULL;
{
char version[8];
const char *tag[3][2];
tag[0][0] = "TITLE";
tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
tag[1][0] = "FORMAT";
tag[1][1] = "S3M";
tag[2][0] = "TRACKERVERSION";
version[0] = hexdigit((cwtv >> 8) & 15);
version[1] = '.';
version[2] = hexdigit((cwtv >> 4) & 15);
version[3] = hexdigit(cwtv & 15);
version[4] = 0;
tag[2][1] = (const char *) &version;
return make_duh(-1, 3, (const char *const (*)[2])tag, 1, &descptr, &sigdata);
}
}

29
dumb/src/it/reads3m2.c Normal file
View file

@ -0,0 +1,29 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* reads3m2.c - Function to read a ScreamTracker 3 / / \ \
* module from an open file and do an | < / \_
* initial run-through. | \/ /\ /
* \_ / > /
* Split off from reads3m.c by entheh. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
DUH *DUMBEXPORT dumb_read_s3m(DUMBFILE *f)
{
DUH *duh = dumb_read_s3m_quick(f);
dumb_it_do_initial_runthrough(duh);
return duh;
}

381
dumb/src/it/readstm.c Normal file
View file

@ -0,0 +1,381 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readstm.c - Code to read a ScreamTracker 2 / / \ \
* module from an open file. | < / \_
* | \/ /\ /
* By Chris Moeller. \_ / > /
* | \ / /
* | ' /
* \__/
*/
// IT_STEREO... :o
#include <stdlib.h>
#include <string.h>
#include "dumb.h"
#include "internal/it.h"
static int it_stm_read_sample_header( IT_SAMPLE *sample, DUMBFILE *f )
{
dumbfile_getnc( sample->filename, 12, f );
sample->filename[12] = 0;
memcpy( sample->name, sample->filename, 13 );
dumbfile_skip( f, 2 + 2 );
sample->length = dumbfile_igetw( f );
sample->loop_start = dumbfile_igetw( f );
sample->loop_end = dumbfile_igetw( f );
sample->default_volume = dumbfile_getc( f );
dumbfile_skip( f, 1 );
sample->C5_speed = dumbfile_igetw( f ) << 3;
dumbfile_skip( f, 6 );
if ( sample->length < 4 || !sample->default_volume ) {
/* Looks like no-existy. */
sample->flags &= ~IT_SAMPLE_EXISTS;
sample->length = 0;
return dumbfile_error( f );
}
sample->flags = IT_SAMPLE_EXISTS;
sample->global_volume = 64;
sample->default_pan = 0; // 0 = don't use, or 160 = centre?
if ( ( sample->loop_start < sample->length ) &&
( sample->loop_end > sample->loop_start ) &&
( sample->loop_end != 0xFFFF ) ) {
sample->flags |= IT_SAMPLE_LOOP;
if ( sample->loop_end > sample->length ) sample->loop_end = sample->length;
}
//Do we need to set all these?
sample->vibrato_speed = 0;
sample->vibrato_depth = 0;
sample->vibrato_rate = 0;
sample->vibrato_waveform = IT_VIBRATO_SINE;
sample->finetune = 0;
sample->max_resampling_quality = -1;
return dumbfile_error(f);
}
static int it_stm_read_sample_data( IT_SAMPLE *sample, DUMBFILE *f )
{
int32 n;
if ( ! sample->length ) return 0;
n = dumbfile_pos( f );
if ( n & 15 ) {
if ( dumbfile_skip( f, 16 - ( n & 15 ) ) )
return -1;
}
sample->data = malloc( sample->length );
if (!sample->data)
return -1;
if ( dumbfile_getnc( sample->data, sample->length, f ) != sample->length )
return -1;
return 0;
}
static int it_stm_read_pattern( IT_PATTERN *pattern, DUMBFILE *f, unsigned char *buffer )
{
int pos;
int channel;
int row;
IT_ENTRY *entry;
pattern->n_rows = 64;
if ( dumbfile_getnc( buffer, 64 * 4 * 4, f ) != 64 * 4 * 4 )
return -1;
pattern->n_entries = 64;
pos = 0;
for ( row = 0; row < 64; ++row ) {
for ( channel = 0; channel < 4; ++channel ) {
if ( buffer[ pos + 0 ] | buffer[ pos + 1 ] | buffer[ pos + 2 ] | buffer[ pos + 3 ] )
++pattern->n_entries;
pos += 4;
}
}
pattern->entry = malloc( pattern->n_entries * sizeof( *pattern->entry ) );
if ( !pattern->entry )
return -1;
entry = pattern->entry;
pos = 0;
for ( row = 0; row < 64; ++row ) {
for ( channel = 0; channel < 4; ++channel ) {
if ( buffer[ pos + 0 ] | buffer[ pos + 1 ] | buffer[ pos + 2 ] | buffer[ pos + 3 ] ) {
unsigned note;
note = buffer[ pos + 0 ];
entry->channel = channel;
entry->mask = 0;
entry->instrument = buffer[ pos + 1 ] >> 3;
entry->volpan = ( buffer[ pos + 1 ] & 0x07 ) + ( buffer[ pos + 2 ] >> 1 );
entry->effect = buffer[ pos + 2 ] & 0x0F;
entry->effectvalue = buffer[ pos + 3 ];
if ( entry->instrument && entry->instrument < 32 )
entry->mask |= IT_ENTRY_INSTRUMENT;
if ( note == 0xFC || note == 0xFE ) {
entry->mask |= IT_ENTRY_NOTE;
entry->note = IT_NOTE_CUT;
}
if ( note < 251 ) {
entry->mask |= IT_ENTRY_NOTE;
entry->note = ( note >> 4 ) * 12 + ( note & 0x0F );
}
if ( entry->volpan <= 64 )
entry->mask |= IT_ENTRY_VOLPAN;
entry->mask |= IT_ENTRY_EFFECT;
switch ( entry->effect ) {
case IT_SET_SPEED:
entry->effectvalue >>= 4;
break;
case IT_BREAK_TO_ROW:
entry->effectvalue -= (entry->effectvalue >> 4) * 6;
break;
case IT_JUMP_TO_ORDER:
case IT_VOLUME_SLIDE:
case IT_PORTAMENTO_DOWN:
case IT_PORTAMENTO_UP:
case IT_TONE_PORTAMENTO:
case IT_VIBRATO:
case IT_TREMOR:
case IT_ARPEGGIO:
case IT_VOLSLIDE_VIBRATO:
case IT_VOLSLIDE_TONEPORTA:
break;
default:
entry->mask &= ~IT_ENTRY_EFFECT;
break;
}
if ( entry->mask ) ++entry;
}
pos += 4;
}
IT_SET_END_ROW(entry);
++entry;
}
pattern->n_entries = (int)(entry - pattern->entry);
return 0;
}
static DUMB_IT_SIGDATA *it_stm_load_sigdata(DUMBFILE *f /*, int * version*/)
{
DUMB_IT_SIGDATA *sigdata;
char tracker_name[ 8 ];
int n;
sigdata = malloc(sizeof(*sigdata));
if (!sigdata) return NULL;
/* Skip song name. */
dumbfile_getnc(sigdata->name, 20, f);
sigdata->name[20] = 0;
dumbfile_getnc(tracker_name, 8, f);
n = dumbfile_getc(f);
if ( n != 0x02 && n != 0x1A && n != 0x1B )
{
free( sigdata );
return NULL;
}
if ( dumbfile_getc(f) != 2 ) /* only support modules */
{
free( sigdata );
return NULL;
}
if ( strnicmp( tracker_name, "!Scream!", 8 ) &&
strnicmp( tracker_name, "BMOD2STM", 8 ) &&
strnicmp( tracker_name, "WUZAMOD!", 8 ) )
{
free( sigdata );
return NULL;
}
/* *version = dumbfile_mgetw(f); */
dumbfile_skip( f, 2 );
sigdata->song_message = NULL;
sigdata->order = NULL;
sigdata->instrument = NULL;
sigdata->sample = NULL;
sigdata->pattern = NULL;
sigdata->midi = NULL;
sigdata->checkpoint = NULL;
sigdata->n_instruments = 0;
sigdata->n_samples = 31;
sigdata->n_pchannels = 4;
sigdata->tempo = 125;
sigdata->mixing_volume = 48;
sigdata->pan_separation = 128;
/** WARNING: which ones? */
sigdata->flags = IT_OLD_EFFECTS | IT_COMPATIBLE_GXX | IT_WAS_AN_S3M;
sigdata->speed = dumbfile_getc(f) >> 4;
if ( sigdata->speed < 1 ) sigdata->speed = 1;
sigdata->n_patterns = dumbfile_getc(f);
sigdata->global_volume = dumbfile_getc(f) << 1;
if ( sigdata->global_volume > 128 ) sigdata->global_volume = 128;
dumbfile_skip(f, 13);
if ( dumbfile_error(f) || sigdata->n_patterns < 1 || sigdata->n_patterns > 99 ) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
sigdata->sample = malloc(sigdata->n_samples * sizeof(*sigdata->sample));
if (!sigdata->sample) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (n = 0; n < sigdata->n_samples; n++)
sigdata->sample[n].data = NULL;
if (sigdata->n_patterns) {
sigdata->pattern = malloc(sigdata->n_patterns * sizeof(*sigdata->pattern));
if (!sigdata->pattern) {
_dumb_it_unload_sigdata(sigdata);
return NULL;
}
for (n = 0; n < sigdata->n_patterns; n++)
sigdata->pattern[n].entry = NULL;
}
memset( sigdata->channel_volume, 64, 4 );
sigdata->channel_pan[ 0 ] = 48;
sigdata->channel_pan[ 1 ] = 16;
sigdata->channel_pan[ 2 ] = 48;
sigdata->channel_pan[ 3 ] = 16;
for ( n = 0; n < sigdata->n_samples; ++n ) {
if ( it_stm_read_sample_header( &sigdata->sample[ n ], f ) ) {
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
}
sigdata->order = malloc( 128 );
if ( !sigdata->order ) {
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
/* Orders, byte each, length = sigdata->n_orders (should be even) */
dumbfile_getnc( sigdata->order, 128, f );
sigdata->restart_position = 0;
for ( n = 127; n >= 0; --n ) {
if ( sigdata->order[ n ] < sigdata->n_patterns ) break;
}
if ( n < 0 ) {
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
sigdata->n_orders = n + 1;
for ( n = 0; n < 128; ++n ) {
if ( sigdata->order[ n ] >= 99 ) sigdata->order[ n ] = 0xFF;
}
if ( sigdata->n_patterns ) {
unsigned char * buffer = malloc( 64 * 4 * 4 );
if ( ! buffer ) {
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
for ( n = 0; n < sigdata->n_patterns; ++n ) {
if ( it_stm_read_pattern( &sigdata->pattern[ n ], f, buffer ) ) {
free( buffer );
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
}
free( buffer );
}
for ( n = 0; n < sigdata->n_samples; ++n ) {
if ( it_stm_read_sample_data( &sigdata->sample[ n ], f ) ) {
_dumb_it_unload_sigdata( sigdata );
return NULL;
}
}
_dumb_it_fix_invalid_orders(sigdata);
return sigdata;
}
/*static char hexdigit(int in)
{
if (in < 10) return in + '0';
else return in + 'A' - 10;
}*/
DUH *DUMBEXPORT dumb_read_stm_quick(DUMBFILE *f)
{
sigdata_t *sigdata;
/*int ver;*/
DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
sigdata = it_stm_load_sigdata(f /*, &ver*/);
if (!sigdata)
return NULL;
{
/*char version[16];*/
const char *tag[2][2];
tag[0][0] = "TITLE";
tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
tag[1][0] = "FORMAT";
tag[1][1] = "STM";
/*version[0] = 'S';
version[1] = 'T';
version[2] = 'M';
version[3] = ' ';
version[4] = 'v';
version[5] = hexdigit((ver >> 8) & 15);
version[6] = '.';
version[7] = hexdigit((ver >> 4) & 15);
version[8] = hexdigit(ver & 15);
version[9] = 0;
tag[1][1] = (const char *) &version;*/
return make_duh(-1, 2, (const char *const (*)[2])tag, 1, &descptr, &sigdata);
}
}

29
dumb/src/it/readstm2.c Normal file
View file

@ -0,0 +1,29 @@
/* _______ ____ __ ___ ___
* \ _ \ \ / \ / \ \ / / ' ' '
* | | \ \ | | || | \/ | . .
* | | | | | | || ||\ /| |
* | | | | | | || || \/ | | ' ' '
* | | | | | | || || | | . .
* | |_/ / \ \__// || | |
* /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
* / \
* / . \
* readstm2.c - Function to read a ScreamTracker 2 / / \ \
* module from an open file and do an | < / \_
* initial run-through. | \/ /\ /
* \_ / > /
* By Chris Moeller. | \ / /
* | ' /
* \__/
*/
#include "dumb.h"
DUH *DUMBEXPORT dumb_read_stm(DUMBFILE *f)
{
DUH *duh = dumb_read_stm_quick(f);
dumb_it_do_initial_runthrough(duh);
return duh;
}

1295
dumb/src/it/readxm.c Normal file

File diff suppressed because it is too large Load diff

Some files were not shown because too many files have changed in this diff Show more