use PhysicsFS to write MIDI file, make hmp2mid use PhysicsFS exclusively

This commit is contained in:
kreatordxx 2006-12-30 05:29:21 +00:00
parent 72a4f98889
commit 96ee4e1082
5 changed files with 41 additions and 42 deletions

View file

@ -9,10 +9,7 @@
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "args.h"
#include "hmp2mid.h"
@ -22,8 +19,6 @@
#define MUSIC_FADE_TIME 500 //milliseconds
#define MUSIC_EXTENSION_ARG "-music_ext"
#define DESCENT_DATA_PATH "" //FIXME: we need PhysFS
Mix_Music *current_music = NULL;
void music_done() {
@ -34,34 +29,35 @@ void music_done() {
void convert_hmp(char *filename, char *mid_filename) {
if (access(mid_filename, R_OK) != 0) {
if (MIX_MUSIC_DEBUG) printf("convert_hmp: converting %s to %s\n", filename, mid_filename);
if (!PHYSFS_exists(mid_filename))
{
const char *err;
CFILE *hmp_in;
FILE *mid_out = fopen(mid_filename, "w");
PHYSFS_File *hmp_in;
PHYSFS_File *mid_out = PHYSFSX_openWriteBuffered(mid_filename);
if (!mid_out) {
fprintf(stderr, "Error could not open: %s for writing: %s\n", mid_filename, strerror(errno));
fprintf(stderr, "Error could not open: %s for writing: %s\n", mid_filename, PHYSFS_getLastError());
return;
}
hmp_in = cfopen(filename, "rb");
if (MIX_MUSIC_DEBUG) printf("convert_hmp: converting %s to %s\n", filename, mid_filename);
hmp_in = PHYSFSX_openReadBuffered(filename);
if (!hmp_in) {
fprintf(stderr, "Error could not open: %s\n", filename);
fclose(mid_out);
PHYSFS_close(mid_out);
return;
}
err = hmp2mid((hmp2mid_read_func_t) cfread, hmp_in, mid_out);
err = hmp2mid(hmp_in, mid_out);
fclose(mid_out);
cfclose(hmp_in);
PHYSFS_close(mid_out);
PHYSFS_close(hmp_in);
if (err) {
fprintf(stderr, "%s\n", err);
unlink(mid_filename);
PHYSFS_delete(mid_filename);
return;
}
}
@ -74,10 +70,9 @@ void mix_play_music(char *filename, int loop) {
loop *= -1;
int i, t, got_end=0;
int fn_buf_len = strlen(DESCENT_DATA_PATH) + strlen(filename) + 16;
char real_filename[fn_buf_len];
char midi_filename[16];
char real_filename[PATH_MAX];
char rel_filename[16]; // just the filename of the actual music file used
char music_title[16];
char music_file_extension[8];
@ -98,18 +93,16 @@ void mix_play_music(char *filename, int loop) {
t = FindArg(MUSIC_EXTENSION_ARG);
if (t > 0) {
sprintf(music_file_extension, "%.3s", Args[t+1]);
// Building absolute path to the file
sprintf(real_filename, "%s%s.%s", DESCENT_DATA_PATH, music_title, music_file_extension);
sprintf(rel_filename, "%s.%s", music_title, music_file_extension);
}
else {
sprintf(midi_filename, "%s.mid", music_title);
convert_hmp(filename, midi_filename);
// Relative path for now (we're using $HOME/.d1x-rebirth/ as working dir)
strcpy(real_filename, midi_filename);
sprintf(rel_filename, "%s.mid", music_title);
convert_hmp(filename, rel_filename);
}
PHYSFSX_getRealPath(rel_filename, real_filename);
if ((current_music = Mix_LoadMUS(real_filename))) {
printf("Now playing: %s\n", real_filename);
printf("Now playing: %s\n", rel_filename);
if (Mix_PlayingMusic()) {
// Fade-in effect sounds cleaner if we're already playing something
Mix_FadeInMusic(current_music, loop, MUSIC_FADE_TIME);
@ -120,7 +113,7 @@ void mix_play_music(char *filename, int loop) {
Mix_HookMusicFinished(music_done);
}
else {
printf("Music %s could not be loaded\n", real_filename);
printf("Music %s could not be loaded\n", rel_filename);
Mix_HaltMusic();
}
}

View file

@ -1,5 +1,9 @@
D2X-Rebirth Changelog
20061230
--------
arch/sdl/mixmusic.c, include/cfile.h, include/hmp2mid.h, misc/hmp2mid.c: use PhysicsFS to write MIDI file, make hmp2mid use PhysicsFS exclusively
20061229
--------
arch/ogl/gr.c, arch/ogl/ogl.c: allocate 'pixels' and 'texbuf' according to current screen resolution, saving over a hundred megabytes of memory

View file

@ -33,17 +33,17 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#define CFILE PHYSFS_file
#define cfopen(f,m) PHYSFSX_openReadBuffered(f)
//killed by MD2211 - see cfread() below
//#define cfread(p,s,n,fp) PHYSFS_read(fp,p,s,n)
#define cfread(p,s,n,fp) PHYSFS_read(fp,p,s,n)
#define cfclose PHYSFS_close
#define cftell PHYSFS_tell
#define cfexist PHYSFS_exists
#define cfilelength PHYSFS_fileLength
//MD2211: hmp2mid needs a function pointer, so a macro won't do the job
static inline size_t cfread(void *p, size_t s, size_t n, CFILE *fp) {
return PHYSFS_read(fp,p,s,n);
}
//Kreator: no longer needed, hmp2mid now uses PhysicsFS implicitly
//static inline size_t cfread(void *p, size_t s, size_t n, CFILE *fp) {
// return PHYSFS_read(fp,p,s,n);
//}
//Specify the name of the hogfile. Returns 1 if hogfile found & had files
static inline int cfile_init(char *hogname)

View file

@ -20,12 +20,13 @@
*/
#ifndef __HMP2MID_H
#define __HMP2MID_H
#include <stdio.h>
//#include <stdio.h>
#include <physfs.h>
typedef size_t (*hmp2mid_read_func_t)(void *ptr, size_t size, size_t nmemb,
void *stream);
//typedef size_t (*hmp2mid_read_func_t)(void *ptr, size_t size, size_t nmemb,
// void *stream);
/* Returns NULL on success, otherwise a c-string with an error message */
const char *hmp2mid(hmp2mid_read_func_t read_func, void *hmp_in, FILE* mid_out);
const char *hmp2mid(/*hmp2mid_read_func_t read_func, */PHYSFS_File *hmp_in, PHYSFS_File* mid_out);
#endif

View file

@ -19,15 +19,16 @@
Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <physfs.h>
#include "hmp2mid.h"
/* Some convience macros to keep the code below more readable */
#define HMP_READ(buf, count) \
{\
if (read_func(buf, 1, count, hmp_in) != (count)) \
if (PHYSFS_read(hmp_in, buf, 1, count) != (count)) \
{ \
free(mid_track_buf); \
return hmp_read_error; \
@ -45,11 +46,11 @@
}
#define MID_WRITE(buf, count) \
if (fwrite(buf, 1, count, mid_out) != (count)) \
if (PHYSFS_write(mid_out, buf, 1, count) != (count)) \
{ \
free(mid_track_buf); \
snprintf(hmp2mid_error, sizeof(hmp2mid_error), mid_write_error_templ, \
strerror(errno)); \
PHYSFS_getLastError); \
return hmp2mid_error; \
}
@ -79,7 +80,7 @@ static const char midi_header1[10] = { 'M', 'T', 'h', 'd', 0, 0, 0, 6, 0, 1 };
static const char midi_header2[21] = { 0, 0xC0, 'M', 'T', 'r', 'k', 0, 0, 0,
0x0B, 0, 0xFF, 0x51, 0x03, 0x18, 0x80, 0, 0, 0xFF, 0x2F, 0 };
const char *hmp2mid(hmp2mid_read_func_t read_func, void *hmp_in, FILE* mid_out)
const char *hmp2mid(PHYSFS_File *hmp_in, PHYSFS_File *mid_out)
{
unsigned char last_com = 0, buf[0x300];
unsigned int num_tracks, track_length = 0, i, t;