* SConstruct enhancements

* Work in progress: SDL_mixer backend for sound
This commit is contained in:
md2211 2006-10-17 20:52:09 +00:00
parent c5d137adac
commit 74f4d94349
4 changed files with 429 additions and 29 deletions

View file

@ -1,24 +1,38 @@
#SConstruct
print "D1X-Rebirth"
# needed imports
import sys
import os
PROGRAM_NAME = 'D1X-Rebirth'
#SVN_REVISION = os.popen('echo -n `LANG=C svn info | grep Revision | cut -d\ -f2`').read()
# version number
D1XMAJOR = 0
D1XMINOR = 50
#D1XMICRO = int(SVN_REVISION)
VERSION_STRING = ' v' + str(D1XMAJOR) + '.' + str(D1XMINOR)
print '\n===== ' + PROGRAM_NAME + VERSION_STRING + ' =====\n'
# installation path
PREFIX = '/usr/local/'
BIN_SUBDIR = 'games/'
DATA_SUBDIR = 'share/games/d1x-rebirth/'
BIN_DIR = PREFIX + BIN_SUBDIR
DATA_DIR = PREFIX + DATA_SUBDIR
# command-line parms
sharepath = str(ARGUMENTS.get('sharepath', '/usr/local/share/games/d1x-rebirth/'))
sharepath = str(ARGUMENTS.get('sharepath', DATA_DIR))
debug = int(ARGUMENTS.get('debug', 0))
profiler = int(ARGUMENTS.get('profiler', 0))
sdl_only = int(ARGUMENTS.get('sdl_only', 0))
no_asm = int(ARGUMENTS.get('no_asm', 0))
editor = int(ARGUMENTS.get('editor', 0))
shareware = int(ARGUMENTS.get('shareware', 0))
sdlmixer = int(ARGUMENTS.get('sdlmixer', 0))
# general source files
common_sources = [
@ -82,6 +96,7 @@ common_sources = [
'main/gameseq.c',
'main/gauges.c',
'main/hash.c',
'main/hmp2mid.c',
'main/hostage.c',
'main/hud.c',
'main/hudlog.c',
@ -220,7 +235,6 @@ editor_sources = [
# for linux
arch_linux_sources = [
'arch/linux/arch_ip.cpp',
'arch/linux/hmiplay.c',
'arch/linux/init.c',
'arch/linux/ipx_bsd.c',
'arch/linux/ipx_kali.c',
@ -230,7 +244,6 @@ arch_linux_sources = [
'arch/linux/timer.c',
'arch/linux/ukali.c',
'arch/sdl/clipboard.c',
'arch/sdl/digi.c',
'arch/sdl/event.c',
'arch/sdl/init.c',
'arch/sdl/joy.c',
@ -239,6 +252,16 @@ arch_linux_sources = [
'arch/sdl/mouse.c'
]
# choosing a sound implementation for linux
arch_linux_sound_sdlmixer = [ 'arch/sdl/mixdigi.c', 'arch/sdl/mixmusic.c' ]
arch_linux_sound_old = [ 'arch/sdl/digi.c', 'arch/linux/hmiplay.c' ]
if (sdlmixer == 1):
arch_linux_sources += arch_linux_sound_sdlmixer
else:
arch_linux_sources += arch_linux_sound_old
# for windows
arch_win32_sources = [
'arch/win32/arch_ip.cpp',
@ -263,7 +286,7 @@ arch_win32_sources = [
arch_ogl_sources = [
'arch/ogl/gr.c',
'arch/ogl/ogl.c',
'arch/ogl/sdlgl.c',
'arch/ogl/sdlgl.c'
]
# for sdl
@ -298,9 +321,12 @@ noasm_sources = [
env = Environment(ENV = os.environ)
env.Append(CPPFLAGS = '-O2 -Wall -funsigned-char')
env.Append(CPPDEFINES = [('D1XMAJOR', '\\"' + str(D1XMAJOR) + '\\"'), ('D1XMINOR', '\\"' + str(D1XMINOR) + '\\"')])
env.Append(CPPPATH = ['include', 'main', 'arch/sdl/include'])
#env.Append(CPPDEFINES = [('D1XMICRO', '\\"' + str(D1XMICRO) + '\\"')])
env.Append(CPPDEFINES = [('USE_SDLMIXER', sdlmixer)])
env.Append(CPPDEFINES = ['NMONO', 'NETWORK', 'HAVE_NETIPX_IPX_H', 'SUPPORTS_NET_IP', '__SDL__', 'SDL_INPUT', 'SDL_AUDIO', '_REENTRANT'])
env.Append(CPPPATH = ['include', 'main', 'arch/sdl/include'])
sdllibs = ['SDL']
sdlmixerlib = ['SDL_mixer']
# windows or *nix?
if sys.platform == 'win32':
@ -342,7 +368,12 @@ else:
env.Append(CPPDEFINES = ogldefines)
env.Append(CPPPATH = ['arch/ogl/include'])
common_sources = arch_ogl_sources + common_sources
alllibs = ogllibs + alllibs
alllibs += ogllibs
# SDL_mixer for sound? (*NIX only)
if (sdlmixer == 1):
print "including SDL_mixer"
alllibs += sdlmixerlib
# debug?
if (debug == 1):
@ -352,8 +383,8 @@ else:
# profiler?
if (profiler == 1):
env.Append(CPPFLAGS = ' -pg ')
lflags = ' -pg '
lflags = ' -g -pg '
env.Append(CPPFLAGS = lflags)
# assembler code?
if (no_asm == 0) and (sdl_only == 1):
@ -383,24 +414,29 @@ if (shareware == 0) and (editor == 0):
# finally building program...
env.Program(target=str(target), source = common_sources, LIBS = alllibs, LINKFLAGS = str(lflags))
env.Install('/usr/local/bin', str(target))
env.Alias('install', '/usr/local/bin')
env.Install(BIN_DIR, str(target))
env.Alias('install', BIN_DIR)
# show some help when running scons -h
Help("""
D1X-Rebirth, SConstruct file help:
Type 'scons' to build the binary.
Type 'scons install' to build and install to /usr/local/bin.
Type 'scons -c' to clean up.
Extra options (add them to command line, like 'scons extraoption=value'):
'sharepath=DIR' Use DIR for shared game data (*NIX only). Must end with a slash.
Default: /usr/local/share/games/d1x-rebirth/
'sdl_only=1' don't include OpenGL, use SDL-only instead
'shareware=1' build SHAREWARE version
'no_asm=1' don't use ASSEMBLER (only with sdl_only=1)
'debug=1' build DEBUG binary which includes asserts, debugging output, cheats and more output
'profiler=1' do profiler build
'editor=1' build editor !EXPERIMENTAL!
""")
Help(PROGRAM_NAME + ', SConstruct file help:' +
"""
#EOF
Type 'scons' to build the binary.
Type 'scons install' to build and install.
Type 'scons -c' to clean up.
Extra options (add them to command line, like 'scons extraoption=value'):
'sharepath=DIR' (*NIX only) use DIR for shared game data. Must end with a slash.
'sdl_only=1' don't include OpenGL, use SDL-only instead
'sdlmixer=1' (*NIX only) use SDL_Mixer for sound (includes external music support)
'shareware=1' build SHAREWARE version
'no_asm=1' don't use ASSEMBLER (only with sdl_only=1)
'debug=1' build DEBUG binary which includes asserts, debugging output, cheats and more output
'profiler=1' do profiler build
'editor=1' build editor !EXPERIMENTAL!
Default values:
""" + ' sharepath = ' + DATA_DIR + '\n')
#EOF

View file

@ -0,0 +1,14 @@
/*
* Header file for music playback through SDL_mixer
*
* -- MD2211 (2006-04-24)
*/
#ifndef _SDLMIXER_MUSIC_H
#define _SDLMIXER_MUSIC_H
void mix_play_music(char *filename, int loop);
void mix_set_music_volume(int vol);
void mix_stop_music();
#endif

226
arch/sdl/mixdigi.c Normal file
View file

@ -0,0 +1,226 @@
/*
* This is an alternate backend for the sound effect system.
* It uses SDL_mixer to provide a more reliable playback,
* and allow processing of multiple audio formats.
*
* This file is based on the original D1X arch/sdl/digi.c
*
* -- MD2211 (2006-10-12)
*/
#ifdef HAVE_CONFIG_H
#include <conf.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <SDL/SDL.h>
#include <SDL/SDL_audio.h>
#include <SDL/SDL_mixer.h>
#include "types.h"
#include "error.h"
#include "mono.h"
#include "fix.h"
#include "digi.h"
#include "sounds.h"
#include "mixmusic.h"
#define MIX_DIGI_DEBUG 0
#define MIX_OUTPUT_FREQUENCY 44100 // in 2006, we can afford it!
#define MIX_OUTPUT_FORMAT AUDIO_S16
#define MIX_OUTPUT_CHANNELS 2
int digi_sample_rate = 11025; //SAMPLE_RATE_11K;
//edited 05/17/99 Matt Mueller - added ifndef NO_ASM
//added on 980905 by adb to add inline fixmul for mixer on i386
#ifndef NO_ASM
#ifdef __i386__
#define do_fixmul(x,y) \
({ \
int _ax, _dx; \
asm("imull %2\n\tshrdl %3,%1,%0" \
: "=a"(_ax), "=d"(_dx) \
: "rm"(y), "i"(16), "0"(x)); \
_ax; \
})
extern inline fix fixmul(fix x, fix y) { return do_fixmul(x,y); }
#endif
#endif
//end edit by adb
//end edit -MM
#define MAX_SOUND_SLOTS 32
#define SOUND_BUFFER_SIZE 512
#define MIN_VOLUME 10
//added/changed on 980905 by adb to make sfx volume work, on 990221 by adb changed F1_0 to F1_0 / 2
#define SOUND_MAX_VOLUME (F1_0 / 2)
int digi_volume = SOUND_MAX_VOLUME;
//end edit by adb
static int digi_initialised = 0;
static int digi_max_channels = MAX_SOUND_SLOTS;
inline int fix2byte(fix f) { return (f / 256) % 256; }
Mix_Chunk SoundChunks[MAX_SOUNDS];
/* Initialise audio */
int digi_init() {
if (MIX_DIGI_DEBUG) printf("digi_init %d (SDL_Mixer)\n", MAX_SOUNDS);
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) Error("SDL audio initialisation failed: %s.", SDL_GetError());
if (Mix_OpenAudio(MIX_OUTPUT_FREQUENCY, MIX_OUTPUT_FORMAT, MIX_OUTPUT_CHANNELS, SOUND_BUFFER_SIZE)) {
//edited on 10/05/98 by Matt Mueller - should keep running, just with no sound.
printf("\nError: Couldn't open audio: %s\n", SDL_GetError());
return 1;
}
Mix_AllocateChannels(digi_max_channels);
Mix_Pause(0);
atexit(digi_close);
digi_initialised = 1;
return 0;
}
/* Shut down audio */
void digi_close() {
if (!digi_initialised) return;
digi_initialised = 0;
Mix_CloseAudio();
}
/*
* Play-time conversion. Performs output conversion only once per sound effect used.
* Once the sound sample has been converted, it is cached in SoundChunks[]
*/
void mixdigi_convert_sound(int i) {
SDL_AudioCVT cvt;
Uint8 *data = GameSounds[i].data;
Uint32 dlen = GameSounds[i].length;
int freq = GameSounds[i].freq;
//int bits = GameSounds[i].bits;
if (SoundChunks[i].abuf) return; //proceed only if not converted yet
if (data) {
if (MIX_DIGI_DEBUG) printf("converting %d (%d)\n", i, dlen);
SDL_BuildAudioCVT(&cvt, AUDIO_U8, 1, freq, MIX_OUTPUT_FORMAT, MIX_OUTPUT_CHANNELS, MIX_OUTPUT_FREQUENCY);
cvt.buf = malloc(dlen * cvt.len_mult);
cvt.len = dlen;
memcpy(cvt.buf, data, dlen);
if (SDL_ConvertAudio(&cvt)) printf("conversion of %d failed\n", i);
SoundChunks[i].abuf = cvt.buf;
SoundChunks[i].alen = dlen * cvt.len_mult;
SoundChunks[i].allocated = 1;
SoundChunks[i].volume = 128; // Max volume = 128
}
}
// Volume 0-F1_0
int digi_start_sound(short soundnum, fix volume, int pan, int looping, int loop_start, int loop_end, int soundobj)
{
if (!digi_initialised) return -1;
Assert(GameSounds[soundnum].data != (void *)-1);
mixdigi_convert_sound(soundnum);
int mix_vol = fix2byte(fixmul(digi_volume, volume));
int mix_pan = fix2byte(pan);
int mix_loop = looping * -1;
if (MIX_DIGI_DEBUG) printf("digi_start_sound %d, volume %d, pan %d (start=%d, end=%d)\n", soundnum, mix_vol, mix_pan, loop_start, loop_end);
int channel = Mix_PlayChannel(-1, &(SoundChunks[soundnum]), mix_loop);
Mix_SetPanning(channel, 255-mix_pan, mix_pan);
Mix_SetDistance(channel, 255-mix_vol);
return channel;
}
void digi_set_channel_volume(int channel, int volume) {
int mix_vol = fix2byte(volume);
Mix_SetDistance(channel, 255-mix_vol);
}
void digi_set_channel_pan(int channel, int pan) {
int mix_pan = fix2byte(pan);
Mix_SetPanning(channel, 255-mix_pan, mix_pan);
}
void digi_stop_sound(int channel) {
if (MIX_DIGI_DEBUG) printf("digi_stop_sound %d\n", channel);
Mix_HaltChannel(channel);
}
void digi_end_sound(int channel) {
digi_stop_sound(channel);
}
//added on 980905 by adb from original source to make sfx volume work
void digi_set_digi_volume( int dvolume )
{
dvolume = fixmuldiv( dvolume, SOUND_MAX_VOLUME, 0x7fff);
if ( dvolume > SOUND_MAX_VOLUME )
digi_volume = SOUND_MAX_VOLUME;
else if ( dvolume < 0 )
digi_volume = 0;
else
digi_volume = dvolume;
if ( !digi_initialised ) return;
digi_sync_sounds();
}
//end edit by adb
void digi_set_volume( int dvolume, int mvolume ) {
digi_set_digi_volume(dvolume);
digi_set_midi_volume(mvolume);
}
int digi_find_channel(int soundno) { return 0; }
int digi_is_sound_playing(int soundno) { return 0; }
int digi_is_channel_playing(int channel) { return 0; }
void digi_reset() {}
void digi_stop_all_channels() {}
extern void digi_end_soundobj(int channel);
int verify_sound_channel_free(int channel);
//added on 980905 by adb to make sound channel setting work
void digi_set_max_channels(int n) { }
int digi_get_max_channels() { return digi_max_channels; }
// end edit by adb
// MIDI stuff follows.
#ifndef _WIN32
void digi_set_midi_volume( int mvolume ) {
mix_set_music_volume(mvolume);
}
void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop ) {
mix_play_music(filename, loop);
}
void digi_stop_current_song() {
mix_stop_music();
}
#endif
void digi_pause_midi() {}
void digi_resume_midi() {}
#ifndef NDEBUG
void digi_debug() {}
#endif

124
arch/sdl/mixmusic.c Normal file
View file

@ -0,0 +1,124 @@
/*
* This is an alternate backend for the music system.
* It uses SDL_mixer to provide a more reliable playback,
* and allow processing of multiple audio formats.
*
* -- MD2211 (2006-04-24)
*/
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <string.h>
#include "args.h"
#include "hmp2mid.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "cfile.h"
#define MUSIC_FADE_TIME 500 //milliseconds
#define MUSIC_EXTENSION_ARG "-music_ext"
Mix_Music *current_music = NULL;
void music_done() {
Mix_HaltMusic();
Mix_FreeMusic(current_music);
current_music = NULL;
}
void convert_hmp(char * filename) {
char *mid_filename = "tmp.mid";
if (access(mid_filename, R_OK) != 0) {
const char *err;
CFILE *hmp_in;
FILE *mid_out = fopen(mid_filename, "w");
if (!mid_out) {
fprintf(stderr, "Error could not open: %s for writing: %s\n", mid_filename, strerror(errno));
return;
}
hmp_in = cfopen(filename, "rb");
if (!hmp_in) {
fprintf(stderr, "Error could not open: %s\n", filename);
fclose(mid_out);
return;
}
err = hmp2mid((hmp2mid_read_func_t) cfread, hmp_in, mid_out);
fclose(mid_out);
cfclose(hmp_in);
if (err) {
fprintf(stderr, "%s\n", err);
unlink(mid_filename);
return;
}
}
}
void mix_play_music(char *filename, int loop) {
int got_end=0, i, t;
loop *= -1;
int fn_buf_len = strlen(DESCENT_DATA_PATH) + strlen(filename) + 16;
char real_filename[fn_buf_len];
char music_file_extension[8] = "mid";
// Quick hack to suppress the .hmp extension
for (i=0; !got_end; i++) {
switch (filename[i]) {
case '.':
// prematurely end the string when we find the dot
filename[i] = '\0';
case '\0':
got_end = 1;
}
}
// What is the extension of external files? If none, default to internal MIDI
t = FindArg(MUSIC_EXTENSION_ARG);
if (t > 0) {
sprintf(music_file_extension,"%.3s", Args[t+1]);
}
else {
convert_hmp(filename);
//hmp2mid((hmp2mid_read_func_t)cfread, hmp_in, mid_out);
}
// Building absolute path to the file
sprintf(real_filename, "%s%s.%s", DESCENT_DATA_PATH, filename, music_file_extension);
current_music = Mix_LoadMUS(real_filename);
if (current_music) {
printf("Now playing: %s\n", real_filename);
if (Mix_PlayingMusic()) {
// Fade-in effect sounds cleaner if we're already playing something
Mix_FadeInMusic(current_music, loop, MUSIC_FADE_TIME);
}
else {
Mix_PlayMusic(current_music, loop);
}
Mix_HookMusicFinished(music_done);
}
else {
printf("Music %s could not be loaded\n", real_filename);
Mix_HaltMusic();
}
}
void mix_set_music_volume(int vol) {
Mix_VolumeMusic(vol);
}
void mix_stop_music() {
Mix_HaltMusic();
}