From 997d2ee86ef77422ce7ddd08859bd5a4aef66145 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Wed, 4 Mar 2009 22:07:27 +0000 Subject: Add initial stub for OPL backend. Subversion-branch: /branches/opl-branch Subversion-revision: 1447 --- src/Makefile.am | 2 + src/i_oplmusic.c | 214 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/i_sdlmusic.c | 2 - src/s_sound.c | 2 + 4 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 src/i_oplmusic.c (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 63ddc98f..66909dd1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -155,6 +155,7 @@ FEATURE_SOUND_SOURCE_FILES = \ i_pcsound.c \ i_sdlsound.c \ i_sdlmusic.c \ +i_oplmusic.c \ mus2mid.c mus2mid.h SOURCE_FILES = $(MAIN_SOURCE_FILES) \ @@ -172,6 +173,7 @@ endif chocolate_doom_LDADD = \ ../textscreen/libtextscreen.a \ ../pcsound/libpcsound.a \ + ../opl/libopl.a \ @LDFLAGS@ \ @SDLMIXER_LIBS@ \ @SDLNET_LIBS@ diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c new file mode 100644 index 00000000..ee909193 --- /dev/null +++ b/src/i_oplmusic.c @@ -0,0 +1,214 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 1993-1996 Id Software, Inc. +// Copyright(C) 2005 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// +// DESCRIPTION: +// System interface for music. +// +//----------------------------------------------------------------------------- + + +#include +#include + +#include "doomdef.h" +#include "memio.h" +#include "mus2mid.h" + +#include "deh_main.h" +#include "m_misc.h" +#include "s_sound.h" +#include "w_wad.h" +#include "z_zone.h" + +#define MAXMIDLENGTH (96 * 1024) + +static boolean music_initialised = false; + +//static boolean musicpaused = false; +static int current_music_volume; + +// Shutdown music + +static void I_OPL_ShutdownMusic(void) +{ +} + +// Initialise music subsystem + +static boolean I_OPL_InitMusic(void) +{ + music_initialised = true; + + return true; +} + +// Set music volume (0 - 127) + +static void I_OPL_SetMusicVolume(int volume) +{ + // Internal state variable. + current_music_volume = volume; +} + +// Start playing a mid + +static void I_OPL_PlaySong(void *handle, int looping) +{ + if (!music_initialised) + { + return; + } +} + +static void I_OPL_PauseSong(void) +{ + if (!music_initialised) + { + return; + } +} + +static void I_OPL_ResumeSong(void) +{ + if (!music_initialised) + { + return; + } +} + +static void I_OPL_StopSong(void) +{ + if (!music_initialised) + { + return; + } +} + +static void I_OPL_UnRegisterSong(void *handle) +{ + if (!music_initialised) + { + return; + } +} + +// Determine whether memory block is a .mid file + +static boolean IsMid(byte *mem, int len) +{ + return len > 4 && !memcmp(mem, "MThd", 4); +} + +static boolean ConvertMus(byte *musdata, int len, char *filename) +{ + MEMFILE *instream; + MEMFILE *outstream; + void *outbuf; + size_t outbuf_len; + int result; + + instream = mem_fopen_read(musdata, len); + outstream = mem_fopen_write(); + + result = mus2mid(instream, outstream); + + if (result == 0) + { + mem_get_buf(outstream, &outbuf, &outbuf_len); + + M_WriteFile(filename, outbuf, outbuf_len); + } + + mem_fclose(instream); + mem_fclose(outstream); + + return result; +} + +static void *I_OPL_RegisterSong(void *data, int len) +{ + char *filename; + + if (!music_initialised) + { + return NULL; + } + + // MUS files begin with "MUS" + // Reject anything which doesnt have this signature + + filename = M_TempFile("doom.mid"); + + if (IsMid(data, len) && len < MAXMIDLENGTH) + { + M_WriteFile(filename, data, len); + } + else + { + // Assume a MUS file and try to convert + + ConvertMus(data, len, filename); + } + + // .... + + // remove file now + + remove(filename); + + Z_Free(filename); + + return NULL; +} + +// Is the song playing? +static boolean I_OPL_MusicIsPlaying(void) +{ + if (!music_initialised) + { + return false; + } + + return false; +} + +static snddevice_t music_opl_devices[] = +{ + SNDDEVICE_ADLIB, + SNDDEVICE_SB, +}; + +music_module_t music_opl_module = +{ + music_opl_devices, + arrlen(music_opl_devices), + I_OPL_InitMusic, + I_OPL_ShutdownMusic, + I_OPL_SetMusicVolume, + I_OPL_PauseSong, + I_OPL_ResumeSong, + I_OPL_RegisterSong, + I_OPL_UnRegisterSong, + I_OPL_PlaySong, + I_OPL_StopSong, + I_OPL_MusicIsPlaying, +}; + diff --git a/src/i_sdlmusic.c b/src/i_sdlmusic.c index 313e2a58..c1ab340b 100644 --- a/src/i_sdlmusic.c +++ b/src/i_sdlmusic.c @@ -324,8 +324,6 @@ static boolean I_SDL_MusicIsPlaying(void) static snddevice_t music_sdl_devices[] = { - SNDDEVICE_ADLIB, - SNDDEVICE_SB, SNDDEVICE_PAS, SNDDEVICE_GUS, SNDDEVICE_WAVEBLASTER, diff --git a/src/s_sound.c b/src/s_sound.c index 70fa75f3..f038e9cd 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -136,6 +136,7 @@ int snd_sfxdevice = SNDDEVICE_SB; extern sound_module_t sound_sdl_module; extern sound_module_t sound_pcsound_module; extern music_module_t music_sdl_module; +extern music_module_t music_opl_module; // Compiled-in sound modules: @@ -154,6 +155,7 @@ static music_module_t *music_modules[] = { #ifdef FEATURE_SOUND &music_sdl_module, + &music_opl_module, #endif NULL, }; -- cgit v1.2.3 From 3efee3d5e2b662df97aabc4c8fd275b60b6f08f4 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Tue, 10 Mar 2009 00:03:54 +0000 Subject: Add initial GENMIDI lump loading, OPL detection. Subversion-branch: /branches/opl-branch Subversion-revision: 1456 --- src/Makefile.am | 2 +- src/i_oplmusic.c | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/m_config.c | 2 +- 3 files changed, 172 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 66909dd1..406d0af8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,7 +3,7 @@ gamesdir = $(prefix)/games games_PROGRAMS = chocolate-doom chocolate-server -AM_CFLAGS = -I../textscreen -I../pcsound @SDLMIXER_CFLAGS@ @SDLNET_CFLAGS@ +AM_CFLAGS = -I../opl -I../textscreen -I../pcsound @SDLMIXER_CFLAGS@ @SDLNET_CFLAGS@ DEDSERV_FILES=\ d_dedicated.c \ diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index ee909193..98170284 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -27,6 +27,7 @@ #include #include +#include #include "doomdef.h" #include "memio.h" @@ -38,23 +39,191 @@ #include "w_wad.h" #include "z_zone.h" +#include "opl.h" + #define MAXMIDLENGTH (96 * 1024) +#define GENMIDI_NUM_INSTRS 128 + +#define GENMIDI_HEADER "#OPL_II#" +#define GENMIDI_FLAG_FIXED 0x0000 /* fixed pitch */ +#define GENMIDI_FLAG_2VOICE 0x0002 /* double voice (OPL3) */ + +typedef struct +{ + byte tremolo; + byte attack; + byte sustain; + byte waveform; + byte scale; + byte level; +} PACKEDATTR genmidi_op_t; + +typedef struct +{ + genmidi_op_t modulator; + byte feedback; + genmidi_op_t carrier; + byte unused; + byte base_note_offset; +} PACKEDATTR genmidi_voice_t; + +typedef struct +{ + unsigned short flags; + byte fine_tuning; + byte fixed_note; + + genmidi_voice_t opl2_voice; + genmidi_voice_t opl3_voice; +} PACKEDATTR genmidi_instr_t; static boolean music_initialised = false; //static boolean musicpaused = false; static int current_music_volume; +static genmidi_instr_t *main_instrs; +static genmidi_instr_t *percussion_instrs; + +// Configuration file variable, containing the port number for the +// adlib chip. + +int snd_mport = 0x388; + +static unsigned int GetStatus(void) +{ + return OPL_ReadPort(OPL_REGISTER_PORT); +} + +// Write an OPL register value + +static void WriteRegister(int reg, int value) +{ + int i; + + OPL_WritePort(OPL_REGISTER_PORT, reg); + + // For timing, read the register port six times after writing the + // register number to cause the appropriate delay + + for (i=0; i<6; ++i) + { + GetStatus(); + } + + OPL_WritePort(OPL_DATA_PORT, value); + + // Read the register port 25 times after writing the value to + // cause the appropriate delay + + for (i=0; i<25; ++i) + { + GetStatus(); + } +} + +// Detect the presence of an OPL chip + +static boolean DetectOPL(void) +{ + int result1, result2; + + // Reset both timers: + WriteRegister(OPL_REG_TIMER_CTRL, 0x60); + + // Enable interrupts: + WriteRegister(OPL_REG_TIMER_CTRL, 0x80); + + // Read status + result1 = GetStatus(); + + // Set timer: + WriteRegister(OPL_REG_TIMER1, 0xff); + + // Start timer 1: + WriteRegister(OPL_REG_TIMER_CTRL, 0x21); + + // Wait for 80 microseconds + + // Read status + result2 = GetStatus(); + + // Reset both timers: + WriteRegister(OPL_REG_TIMER_CTRL, 0x60); + + // Enable interrupts: + WriteRegister(OPL_REG_TIMER_CTRL, 0x80); + + return (result1 & 0xe0) == 0x00 + && (result2 & 0xe0) == 0xc0; +} + + +// Load instrument table from GENMIDI lump: + +static boolean LoadInstrumentTable(void) +{ + byte *lump; + + lump = W_CacheLumpName("GENMIDI", PU_STATIC); + + // Check header + + if (strncmp((char *) lump, GENMIDI_HEADER, strlen(GENMIDI_HEADER)) != 0) + { + W_ReleaseLumpName("GENMIDI"); + + return false; + } + + main_instrs = (genmidi_instr_t *) (lump + strlen(GENMIDI_HEADER)); + percussion_instrs = main_instrs + GENMIDI_NUM_INSTRS; + + return true; +} + // Shutdown music static void I_OPL_ShutdownMusic(void) { + if (music_initialised) + { + OPL_Shutdown(); + + // Release GENMIDI lump + + W_ReleaseLumpName("GENMIDI"); + + music_initialised = false; + } } // Initialise music subsystem static boolean I_OPL_InitMusic(void) -{ +{ + if (!OPL_Init(snd_mport)) + { + return false; + } + + // Doom does the detection sequence twice, for some reason: + + if (!DetectOPL() || !DetectOPL()) + { + printf("Dude. The Adlib isn't responding.\n"); + OPL_Shutdown(); + return false; + } + + // Load instruments from GENMIDI lump: + + if (!LoadInstrumentTable()) + { + OPL_Shutdown(); + return false; + } + music_initialised = true; return true; diff --git a/src/m_config.c b/src/m_config.c index 4066cb3c..c54b3774 100644 --- a/src/m_config.c +++ b/src/m_config.c @@ -124,6 +124,7 @@ extern int vanilla_demo_limit; extern int snd_musicdevice; extern int snd_sfxdevice; extern int snd_samplerate; +extern int snd_mport; // controls whether to use libsamplerate for sample rate conversions @@ -136,7 +137,6 @@ extern int use_libsamplerate; static int snd_sbport = 0; static int snd_sbirq = 0; static int snd_sbdma = 0; -static int snd_mport = 0; typedef enum { -- cgit v1.2.3 From ca7f823d48bb78eda9048750909cdb315836125c Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Tue, 10 Mar 2009 21:21:16 +0000 Subject: Initialise OPL registers on startup, initialise voices. Subversion-branch: /branches/opl-branch Subversion-revision: 1457 --- src/i_oplmusic.c | 178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 98170284..5b3ae041 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -77,14 +77,45 @@ typedef struct genmidi_voice_t opl3_voice; } PACKEDATTR genmidi_instr_t; +typedef struct opl_voice_s opl_voice_t; + +struct opl_voice_s +{ + // Index of this voice: + int index; + + // The operators used by this voice: + int op1, op2; + + // Currently-loaded instrument data + genmidi_instr_t *current_instr; + + // Next in freelist + opl_voice_t *next; +}; + +// Operators used by the different voices. + +static const int voice_operators[2][OPL_NUM_VOICES] = { + { 0x00, 0x01, 0x02, 0x08, 0x09, 0x0a, 0x10, 0x11, 0x12 }, + { 0x03, 0x04, 0x05, 0x0b, 0x0c, 0x0d, 0x13, 0x14, 0x15 } +}; + static boolean music_initialised = false; //static boolean musicpaused = false; static int current_music_volume; +// GENMIDI lump instrument data: + static genmidi_instr_t *main_instrs; static genmidi_instr_t *percussion_instrs; +// Voices: + +static opl_voice_t voices[OPL_NUM_VOICES]; +static opl_voice_t *voice_free_list; + // Configuration file variable, containing the port number for the // adlib chip. @@ -158,6 +189,47 @@ static boolean DetectOPL(void) && (result2 & 0xe0) == 0xc0; } +// Initialise registers on startup + +static void InitRegisters(void) +{ + int r; + + // Initialise level registers + + for (r=OPL_REGS_LEVEL; r < OPL_REGS_LEVEL + OPL_NUM_OPERATORS; ++r) + { + WriteRegister(r, 0x3f); + } + + // Initialise other registers + // These two loops write to registers that actually don't exist, + // but this is what Doom does ... + + for (r=OPL_REGS_ATTACK; r < OPL_REGS_WAVEFORM + OPL_NUM_OPERATORS; ++r) + { + WriteRegister(r, 0x00); + } + + // More registers ... + + for (r=0; r < OPL_REGS_LEVEL; ++r) + { + WriteRegister(r, 0x00); + } + + // Re-initialise the low registers: + + // Reset both timers and enable interrupts: + WriteRegister(OPL_REG_TIMER_CTRL, 0x60); + WriteRegister(OPL_REG_TIMER_CTRL, 0x80); + + // "Allow FM chips to control the waveform of each operator": + WriteRegister(OPL_REG_WAVEFORM_ENABLE, 0x20); + + // Keyboard split point on (?) + WriteRegister(OPL_REG_FM_MODE, 0x40); +} // Load instrument table from GENMIDI lump: @@ -182,6 +254,109 @@ static boolean LoadInstrumentTable(void) return true; } +// Get the next available voice from the freelist. + +static opl_voice_t *GetFreeVoice(void) +{ + opl_voice_t *result; + + // None available? + + if (voice_free_list == NULL) + { + return NULL; + } + + result = voice_free_list; + voice_free_list = voice_free_list->next; + + return result; +} + +// Release a voice back to the freelist. + +static void ReleaseVoice(opl_voice_t *voice) +{ + opl_voice_t **rover; + + // Search to the end of the freelist (This is how Doom behaves!) + + rover = &voice_free_list; + + while (*rover != NULL) + { + rover = &(*rover)->next; + } + + *rover = voice; +} + +// Load data to the specified operator + +static void LoadOperatorData(int operator, genmidi_op_t *data, + boolean max_level) +{ + int level; + + // The scale and level fields must be combined for the level register. + // For the carrier wave we always set the maximum level. + + level = (data->scale & 0xc0) | (data->level & 0x3f); + + if (max_level) + { + level |= 0x3f; + } + + WriteRegister(OPL_REGS_LEVEL + operator, level); + WriteRegister(OPL_REGS_TREMOLO + operator, data->tremolo); + WriteRegister(OPL_REGS_ATTACK + operator, data->attack); + WriteRegister(OPL_REGS_SUSTAIN + operator, data->sustain); + WriteRegister(OPL_REGS_WAVEFORM + operator, data->waveform); +} + +// Set the instrument for a particular voice. + +static void SetVoiceInstrument(opl_voice_t *voice, genmidi_voice_t *data) +{ + // Doom loads the second operator first, then the first. + + LoadOperatorData(voice->op2, &data->carrier, true); + LoadOperatorData(voice->op1, &data->modulator, false); + + // Set feedback register that control the connection between the + // two operators. Turn on bits in the upper nybble; I think this + // is for OPL3, where it turns on channel A/B. + + WriteRegister(OPL_REGS_FEEDBACK + voice->index, + data->feedback | 0x30); +} + +// Initialise the voice table and freelist + +static void InitVoices(void) +{ + int i; + + // Start with an empty free list. + + voice_free_list = NULL; + + // Initialise each voice. + + for (i=0; iop2, 0x94); + + // Note on: + WriteRegister(OPL_REGS_FREQ_1 + voice->index, 0x65); + WriteRegister(OPL_REGS_FREQ_2 + voice->index, 0x2b); + } +#endif + music_initialised = true; return true; -- cgit v1.2.3 From 90fd85b32dd85884fb9a9cf0a7063c4509147d40 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Thu, 12 Mar 2009 18:50:15 +0000 Subject: Read from register port when doing register writes during startup phase; afterwards, read from the data port. Subversion-branch: /branches/opl-branch Subversion-revision: 1464 --- src/i_oplmusic.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index baf17bd6..0543be2e 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -116,6 +116,13 @@ static genmidi_instr_t *percussion_instrs; static opl_voice_t voices[OPL_NUM_VOICES]; static opl_voice_t *voice_free_list; +// In the initialisation stage, register writes are spaced by reading +// from the register port (0). After initialisation, spacing is +// peformed by reading from the data port instead. I have no idea +// why. + +static boolean init_stage_reg_writes = false; + // Configuration file variable, containing the port number for the // adlib chip. @@ -139,7 +146,18 @@ static void WriteRegister(int reg, int value) for (i=0; i<6; ++i) { - GetStatus(); + // An oddity of the Doom OPL code: at startup initialisation, + // the spacing here is performed by reading from the register + // port; after initialisation, the data port is read, instead. + + if (init_stage_reg_writes) + { + OPL_ReadPort(OPL_REGISTER_PORT); + } + else + { + OPL_ReadPort(OPL_DATA_PORT); + } } OPL_WritePort(OPL_DATA_PORT, value); @@ -393,6 +411,8 @@ static boolean I_OPL_InitMusic(void) return false; } + init_stage_reg_writes = true; + // Doom does the detection sequence twice, for some reason: if (!DetectOPL() || !DetectOPL()) @@ -413,6 +433,11 @@ static boolean I_OPL_InitMusic(void) InitRegisters(); InitVoices(); + // Now that initialisation has finished, switch the + // register writing mode: + + init_stage_reg_writes = false; + #ifdef TEST { opl_voice_t *voice; -- cgit v1.2.3 From 543b9bdc85dfeb84ed855084595407d6bf49d8f6 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Thu, 12 Mar 2009 18:52:12 +0000 Subject: Make base_note_offset a 16-bit integer rather than an 8-bit integer. Subversion-branch: /branches/opl-branch Subversion-revision: 1465 --- src/i_oplmusic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 0543be2e..f50c3322 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -64,7 +64,7 @@ typedef struct byte feedback; genmidi_op_t carrier; byte unused; - byte base_note_offset; + short base_note_offset; } PACKEDATTR genmidi_voice_t; typedef struct -- cgit v1.2.3 From b96cdbe82fa6cb51ac91072ad86e084d739fd9be Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 28 Mar 2009 00:24:50 +0000 Subject: Initial MIDI file parsing code. Subversion-branch: /branches/opl-branch Subversion-revision: 1489 --- src/Makefile.am | 1 + src/midifile.c | 287 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/midifile.h | 135 ++++++++++++++++++++++++++ 3 files changed, 423 insertions(+) create mode 100644 src/midifile.c create mode 100644 src/midifile.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 406d0af8..92095b5c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -156,6 +156,7 @@ i_pcsound.c \ i_sdlsound.c \ i_sdlmusic.c \ i_oplmusic.c \ +midifile.c midifile.h \ mus2mid.c mus2mid.h SOURCE_FILES = $(MAIN_SOURCE_FILES) \ diff --git a/src/midifile.c b/src/midifile.c new file mode 100644 index 00000000..e74cf17f --- /dev/null +++ b/src/midifile.c @@ -0,0 +1,287 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2009 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// +// DESCRIPTION: +// Reading of MIDI files. +// +//----------------------------------------------------------------------------- + +#include +#include +#include + +#include "doomtype.h" +#include "i_swap.h" +#include "midifile.h" + +#define HEADER_CHUNK_ID "MThd" +#define TRACK_CHUNK_ID "MTrk" + +typedef struct +{ + byte chunk_id[4]; + unsigned int chunk_size; +} chunk_header_t; + +typedef struct +{ + chunk_header_t chunk_header; + unsigned short format_type; + unsigned short num_tracks; + unsigned int time_division; +} midi_header_t; + +struct midi_file_s +{ + FILE *stream; + midi_header_t header; + unsigned int data_len; +}; + +static boolean CheckChunkHeader(chunk_header_t *chunk, + char *expected_id) +{ + boolean result; + + result = (strcmp((char *) chunk->chunk_id, expected_id) == 0); + + if (!result) + { + fprintf(stderr, "CheckChunkHeader: Expected '%s' chunk header!\n", + expected_id); + } + + return result; +} + +// Read and check the header chunk. + +static boolean ReadHeaderChunk(midi_file_t *file) +{ + size_t bytes_read; + + bytes_read = fread(&file->header, sizeof(midi_header_t), 1, file->stream); + + if (bytes_read < sizeof(midi_header_t)) + { + return false; + } + + if (!CheckChunkHeader(&file->header.chunk_header, HEADER_CHUNK_ID) + || LONG(file->header.chunk_header.chunk_size) != 6) + { + fprintf(stderr, "ReadHeaderChunk: Invalid MIDI chunk header!\n"); + return false; + } + + if (SHORT(file->header.format_type) != 0 + || SHORT(file->header.num_tracks) != 1) + { + fprintf(stderr, "ReadHeaderChunk: Only single track, " + "type 0 MIDI files supported!\n"); + return false; + } + + return true; +} + +// Read and check the track chunk header + +static boolean ReadTrackChunk(midi_file_t *file) +{ + size_t bytes_read; + chunk_header_t chunk_header; + + bytes_read = fread(&chunk_header, sizeof(chunk_header_t), 1, file->stream); + + if (bytes_read < sizeof(chunk_header)) + { + return false; + } + + if (!CheckChunkHeader(&chunk_header, TRACK_CHUNK_ID)) + { + return false; + } + + file->data_len = LONG(chunk_header.chunk_size); + + return true; +} + +midi_file_t *MIDI_OpenFile(char *filename) +{ + midi_file_t *file; + + file = malloc(sizeof(midi_file_t)); + + if (file == NULL) + { + return NULL; + } + + // Open file + + file->stream = fopen(filename, "rb"); + + if (file->stream == NULL) + { + fprintf(stderr, "Failed to open '%s'\n", filename); + free(file); + return NULL; + } + + // Read MIDI file header + + if (!ReadHeaderChunk(file)) + { + fclose(file->stream); + free(file); + return NULL; + } + + // Read track header + + if (!ReadTrackChunk(file)) + { + fclose(file->stream); + free(file); + return NULL; + } + + return file; +} + +void MIDI_CloseFile(midi_file_t *file) +{ + fclose(file->stream); + free(file); +} + +// Read a MIDI channel event. +// two_param indicates that the event type takes two parameters +// (three byte) otherwise it is single parameter (two byte) + +static boolean ReadChannelEvent(midi_file_t *file, midi_event_t *event, + int event_type, boolean two_param) +{ + int c; + + // Set basics: + + event->event_type = event_type >> 4; + event->data.channel.channel = event_type & 0xf; + + // Read parameters: + + c = fgetc(file->stream); + + if (c == EOF) + { + return false; + } + + event->data.channel.param1 = c; + + // Second parameter: + + if (two_param) + { + c = fgetc(file->stream); + + if (c == EOF) + { + return false; + } + + event->data.channel.param2 = c; + } + + return true; +} + +// Read sysex event: + +static boolean ReadSysExEvent(midi_file_t *file, midi_event_t *event, + int event_type) +{ + // TODO + return false; +} + +// Read meta event: + +static boolean ReadMetaEvent(midi_file_t *file, midi_event_t *event) +{ + // TODO + return false; +} + +boolean MIDI_ReadEvent(midi_file_t *file, midi_event_t *event) +{ + int event_type; + + event_type = fgetc(file->stream); + + if (event_type == EOF) + { + return false; + } + + // Check event type: + + switch (event_type >> 4) + { + // Two parameter channel events: + + case MIDI_EVENT_NOTE_OFF: + case MIDI_EVENT_NOTE_ON: + case MIDI_EVENT_AFTERTOUCH: + case MIDI_EVENT_CONTROLLER: + case MIDI_EVENT_PITCH_BEND: + return ReadChannelEvent(file, event, event_type, true); + + // Single parameter channel events: + + case MIDI_EVENT_PROGRAM_CHANGE: + case MIDI_EVENT_CHAN_AFTERTOUCH: + return ReadChannelEvent(file, event, event_type, false); + + // Other event types: + + case 0xf: + if (event_type == MIDI_EVENT_SYSEX + || event_type == MIDI_EVENT_SYSEX_SPLIT) + { + return ReadSysExEvent(file, event, event_type); + } + else if (event_type == MIDI_EVENT_META) + { + return ReadMetaEvent(file, event); + } + + // Fall-through deliberate - + + default: + fprintf(stderr, "Unknown MIDI event type: 0x%x\n", event_type); + return false; + } +} + diff --git a/src/midifile.h b/src/midifile.h new file mode 100644 index 00000000..d7df8b86 --- /dev/null +++ b/src/midifile.h @@ -0,0 +1,135 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2009 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// +// DESCRIPTION: +// MIDI file parsing. +// +//----------------------------------------------------------------------------- + +#ifndef MIDIFILE_H +#define MIDIFILE_H + +typedef struct midi_file_s midi_file_t; + +typedef enum +{ + MIDI_EVENT_NOTE_OFF = 0x8, + MIDI_EVENT_NOTE_ON = 0x9, + MIDI_EVENT_AFTERTOUCH = 0xa, + MIDI_EVENT_CONTROLLER = 0xb, + MIDI_EVENT_PROGRAM_CHANGE = 0xc, + MIDI_EVENT_CHAN_AFTERTOUCH = 0xd, + MIDI_EVENT_PITCH_BEND = 0xe, + + MIDI_EVENT_SYSEX = 0xf0, + MIDI_EVENT_SYSEX_SPLIT = 0xf7, + MIDI_EVENT_META = 0xff, +} midi_event_type_t; + +typedef enum +{ + MIDI_CONTROLLER_BANK_SELECT = 0x0, + MIDI_CONTROLLER_MODULATION = 0x1, + MIDI_CONTROLLER_BREATH_CONTROL = 0x2, + MIDI_CONTROLLER_FOOT_CONTROL = 0x3, + MIDI_CONTROLLER_PORTAMENTO = 0x4, + MIDI_CONTROLLER_DATA_ENTRY = 0x5, +} midi_controller_t; + +typedef enum +{ + MIDI_META_SEQUENCE_NUMBER = 0x0, + + MIDI_META_TEXT = 0x1, + MIDI_META_COPYRIGHT = 0x2, + MIDI_META_TRACK_NAME = 0x3, + MIDI_META_INSTR_NAME = 0x4, + MIDI_META_LYRICS = 0x5, + MIDI_META_MARKER = 0x6, + MIDI_META_CUE_POINT = 0x7, + + MIDI_META_CHANNEL_PREFIX = 0x8, + MIDI_META_END_OF_TRACK = 0x9, + MIDI_META_SET_TEMPO = 0xa, + MIDI_META_SMPTE_OFFSET = 0xb, + MIDI_META_TIME_SIGNATURE = 0xc, + MIDI_META_KEY_SIGNATURE = 0xd, + MIDI_META_SEQUENCER_SPECIFIC = 0xe, +} midi_meta_event_type_t; + +typedef struct +{ + // Meta event type: + + unsigned int type; + + // Length: + + unsigned int length; + + // Meta event data: + + byte *data; +} midi_meta_event_data_t; + +typedef struct +{ + // Length: + + unsigned int length; + + // Event data: + + byte *data; +} midi_sysex_event_data_t; + +typedef struct +{ + // The channel number to which this applies: + + unsigned int channel; + + // Extra parameters: + + unsigned int param1; + unsigned int param2; +} midi_channel_event_data_t; + +typedef struct +{ + // Time between the previous event and this event. + unsigned int delta_time; + + // Type of event: + midi_event_type_t event_type; + + union + { + midi_channel_event_data_t channel; + midi_meta_event_data_t meta; + midi_sysex_event_data_t sysex; + } data; +} midi_event_t; + +midi_file_t *MIDI_OpenFile(char *filename); +void MIDI_CloseFile(midi_file_t *file); + +#endif /* #ifndef MIDIFILE_H */ + -- cgit v1.2.3 From 2b64dfc84981dbbd9de1a0d9cb11995bf0ba9379 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 29 Mar 2009 22:39:25 +0000 Subject: Parse SysEx, meta events Subversion-branch: /branches/opl-branch Subversion-revision: 1490 --- src/midifile.c | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 180 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/midifile.c b/src/midifile.c index e74cf17f..1b8a8786 100644 --- a/src/midifile.c +++ b/src/midifile.c @@ -53,6 +53,10 @@ struct midi_file_s FILE *stream; midi_header_t header; unsigned int data_len; + + // Data buffer used to store data read for SysEx or meta events: + byte *buffer; + unsigned int buffer_size; }; static boolean CheckChunkHeader(chunk_header_t *chunk, @@ -137,6 +141,9 @@ midi_file_t *MIDI_OpenFile(char *filename) return NULL; } + file->buffer = NULL; + file->buffer_size = 0; + // Open file file->stream = fopen(filename, "rb"); @@ -172,9 +179,116 @@ midi_file_t *MIDI_OpenFile(char *filename) void MIDI_CloseFile(midi_file_t *file) { fclose(file->stream); + free(file->buffer); free(file); } +// Read a single byte. Returns false on error. + +static boolean ReadByte(midi_file_t *file, byte *result) +{ + int c; + + c = fgetc(file->stream); + + if (c == EOF) + { + return false; + } + else + { + *result = (byte) c; + + return true; + } +} + +// Read a variable-length value. + +static boolean ReadVariableLength(midi_file_t *file, unsigned int *result) +{ + int i; + byte b; + + *result = 0; + + for (i=0; i<4; ++i) + { + if (!ReadByte(file, &b)) + { + fprintf(stderr, "Error while reading variable-length value\n"); + return false; + } + + // Insert the bottom seven bits from this byte. + + *result <<= 7; + *result |= b & 0x7f; + + // If the top bit is not set, this is the end. + + if ((b & 0x80) == 0) + { + return true; + } + } + + fprintf(stderr, "Variable-length value too long: maximum of four bytes!\n");; + return false; +} + +// Expand the size of the buffer used for SysEx/Meta events: + +static boolean ExpandBuffer(midi_file_t *file, unsigned int new_size) +{ + byte *new_buffer; + + if (file->buffer_size < new_size) + { + // Reallocate to a larger size: + + new_buffer = realloc(file->buffer, new_size); + + if (new_buffer == NULL) + { + fprintf(stderr, "ExpandBuffer: Failed to expand buffer to %u " + "bytes\n", new_size); + return false; + } + + file->buffer = new_buffer; + file->buffer_size = new_size; + } + + return true; +} + +// Read a byte sequence into the data buffer. + +static boolean ReadByteSequence(midi_file_t *file, unsigned int num_bytes) +{ + unsigned int i; + + // Check that we have enough space: + + if (!ExpandBuffer(file, num_bytes)) + { + return false; + } + + for (i=0; ibuffer[i])) + { + fprintf(stderr, "ReadByteSequence: Error while reading byte %u\n", + i); + return false; + } + } + + return true; +} + // Read a MIDI channel event. // two_param indicates that the event type takes two parameters // (three byte) otherwise it is single parameter (two byte) @@ -182,7 +296,7 @@ void MIDI_CloseFile(midi_file_t *file) static boolean ReadChannelEvent(midi_file_t *file, midi_event_t *event, int event_type, boolean two_param) { - int c; + byte b; // Set basics: @@ -191,27 +305,23 @@ static boolean ReadChannelEvent(midi_file_t *file, midi_event_t *event, // Read parameters: - c = fgetc(file->stream); - - if (c == EOF) + if (!ReadByte(file, &b)) { return false; } - event->data.channel.param1 = c; + event->data.channel.param1 = b; // Second parameter: if (two_param) { - c = fgetc(file->stream); - - if (c == EOF) + if (!ReadByte(file, &b)) { return false; } - event->data.channel.param2 = c; + event->data.channel.param2 = b; } return true; @@ -222,26 +332,79 @@ static boolean ReadChannelEvent(midi_file_t *file, midi_event_t *event, static boolean ReadSysExEvent(midi_file_t *file, midi_event_t *event, int event_type) { - // TODO - return false; + event->event_type = event_type; + + if (!ReadVariableLength(file, &event->data.sysex.length)) + { + fprintf(stderr, "ReadSysExEvent: Failed to read length of " + "SysEx block\n"); + return false; + } + + // Read the byte sequence: + + if (!ReadByteSequence(file, event->data.sysex.length)) + { + fprintf(stderr, "ReadSysExEvent: Failed while reading SysEx event\n"); + return false; + } + + event->data.sysex.data = file->buffer; + + return true; } // Read meta event: static boolean ReadMetaEvent(midi_file_t *file, midi_event_t *event) { - // TODO + byte b; + + // Read meta event type: + + if (!ReadByte(file, &b)) + { + fprintf(stderr, "ReadMetaEvent: Failed to read meta event type\n"); + return false; + } + + event->data.meta.type = b; + + // Read length of meta event data: + + if (!ReadVariableLength(file, &event->data.meta.length)) + { + fprintf(stderr, "ReadSysExEvent: Failed to read length of " + "SysEx block\n"); + return false; + } + + // Read the byte sequence: + + if (!ReadByteSequence(file, event->data.meta.length)) + { + fprintf(stderr, "ReadSysExEvent: Failed while reading SysEx event\n"); + return false; + } + + event->data.meta.data = file->buffer; + return false; } boolean MIDI_ReadEvent(midi_file_t *file, midi_event_t *event) { - int event_type; + byte event_type; - event_type = fgetc(file->stream); + if (!ReadVariableLength(file, &event->delta_time)) + { + fprintf(stderr, "MIDI_ReadEvent: Failed to read event timestamp\n"); + return false; + } - if (event_type == EOF) + if (!ReadByte(file, &event_type)) { + fprintf(stderr, "MIDI_ReadEvent: Failed to read event type\n"); return false; } @@ -277,7 +440,8 @@ boolean MIDI_ReadEvent(midi_file_t *file, midi_event_t *event) return ReadMetaEvent(file, event); } - // Fall-through deliberate - + // --- Fall-through deliberate --- + // Other 0xfx event types are unknown default: fprintf(stderr, "Unknown MIDI event type: 0x%x\n", event_type); -- cgit v1.2.3 From c4f640be999e80351f1c89c893a0d5ce39c20cd9 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 29 Mar 2009 22:55:55 +0000 Subject: Clean up error messages, minor bugs etc. Subversion-branch: /branches/opl-branch Subversion-revision: 1492 --- src/midifile.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/midifile.c b/src/midifile.c index 1b8a8786..ed60958d 100644 --- a/src/midifile.c +++ b/src/midifile.c @@ -33,6 +33,7 @@ #define HEADER_CHUNK_ID "MThd" #define TRACK_CHUNK_ID "MTrk" +#define MAX_BUFFER_SIZE 0x10000 typedef struct { @@ -68,7 +69,7 @@ static boolean CheckChunkHeader(chunk_header_t *chunk, if (!result) { - fprintf(stderr, "CheckChunkHeader: Expected '%s' chunk header!\n", + fprintf(stderr, "CheckChunkHeader: Expected '%s' chunk header\n", expected_id); } @@ -150,7 +151,7 @@ midi_file_t *MIDI_OpenFile(char *filename) if (file->stream == NULL) { - fprintf(stderr, "Failed to open '%s'\n", filename); + fprintf(stderr, "MIDI_OpenFile: Failed to open '%s'\n", filename); free(file); return NULL; } @@ -193,6 +194,7 @@ static boolean ReadByte(midi_file_t *file, byte *result) if (c == EOF) { + fprintf(stderr, "ReadByte: Unexpected end of file\n"); return false; } else @@ -216,7 +218,8 @@ static boolean ReadVariableLength(midi_file_t *file, unsigned int *result) { if (!ReadByte(file, &b)) { - fprintf(stderr, "Error while reading variable-length value\n"); + fprintf(stderr, "ReadVariableLength: Error while reading " + "variable-length value\n"); return false; } @@ -233,7 +236,8 @@ static boolean ReadVariableLength(midi_file_t *file, unsigned int *result) } } - fprintf(stderr, "Variable-length value too long: maximum of four bytes!\n");; + fprintf(stderr, "ReadVariableLength: Variable-length value too " + "long: maximum of four bytes\n"); return false; } @@ -243,6 +247,13 @@ static boolean ExpandBuffer(midi_file_t *file, unsigned int new_size) { byte *new_buffer; + if (new_size > MAX_BUFFER_SIZE) + { + fprintf(stderr, "ExpandBuffer: Tried to expand buffer to %u bytes\n", + new_size); + return false; + } + if (file->buffer_size < new_size) { // Reallocate to a larger size: @@ -294,7 +305,7 @@ static boolean ReadByteSequence(midi_file_t *file, unsigned int num_bytes) // (three byte) otherwise it is single parameter (two byte) static boolean ReadChannelEvent(midi_file_t *file, midi_event_t *event, - int event_type, boolean two_param) + byte event_type, boolean two_param) { byte b; @@ -307,6 +318,8 @@ static boolean ReadChannelEvent(midi_file_t *file, midi_event_t *event, if (!ReadByte(file, &b)) { + fprintf(stderr, "ReadChannelEvent: Error while reading channel " + "event parameters\n"); return false; } @@ -318,6 +331,8 @@ static boolean ReadChannelEvent(midi_file_t *file, midi_event_t *event, { if (!ReadByte(file, &b)) { + fprintf(stderr, "ReadChannelEvent: Error while reading channel " + "event parameters\n"); return false; } @@ -389,7 +404,7 @@ static boolean ReadMetaEvent(midi_file_t *file, midi_event_t *event) event->data.meta.data = file->buffer; - return false; + return true; } boolean MIDI_ReadEvent(midi_file_t *file, midi_event_t *event) -- cgit v1.2.3 From 9b192c44f932542ccd835936d593c43ce3e9e9e3 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 3 Apr 2009 19:58:08 +0000 Subject: Fix up MIDI reading code; add test code. Subversion-branch: /branches/opl-branch Subversion-revision: 1494 --- src/Makefile.am | 3 ++ src/midifile.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++--------- src/midifile.h | 15 ++++---- 3 files changed, 98 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 92095b5c..3874c35f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -195,3 +195,6 @@ icon.c : ../data/doom.ico endif +midiread : midifile.c + $(CC) -DTEST $(CFLAGS) @LDFLAGS@ $^ -o $@ + diff --git a/src/midifile.c b/src/midifile.c index ed60958d..e4273c12 100644 --- a/src/midifile.c +++ b/src/midifile.c @@ -27,6 +27,7 @@ #include #include +#include "doomdef.h" #include "doomtype.h" #include "i_swap.h" #include "midifile.h" @@ -39,15 +40,15 @@ typedef struct { byte chunk_id[4]; unsigned int chunk_size; -} chunk_header_t; +} PACKEDATTR chunk_header_t; typedef struct { chunk_header_t chunk_header; unsigned short format_type; unsigned short num_tracks; - unsigned int time_division; -} midi_header_t; + unsigned short time_division; +} PACKEDATTR midi_header_t; struct midi_file_s { @@ -65,12 +66,15 @@ static boolean CheckChunkHeader(chunk_header_t *chunk, { boolean result; - result = (strcmp((char *) chunk->chunk_id, expected_id) == 0); + result = (memcmp((char *) chunk->chunk_id, expected_id, 4) == 0); if (!result) { - fprintf(stderr, "CheckChunkHeader: Expected '%s' chunk header\n", - expected_id); + fprintf(stderr, "CheckChunkHeader: Expected '%s' chunk header, " + "got '%c%c%c%c'\n", + expected_id, + chunk->chunk_id[0], chunk->chunk_id[1], + chunk->chunk_id[2], chunk->chunk_id[3]); } return result; @@ -80,24 +84,26 @@ static boolean CheckChunkHeader(chunk_header_t *chunk, static boolean ReadHeaderChunk(midi_file_t *file) { - size_t bytes_read; + size_t records_read; - bytes_read = fread(&file->header, sizeof(midi_header_t), 1, file->stream); + records_read = fread(&file->header, sizeof(midi_header_t), 1, file->stream); - if (bytes_read < sizeof(midi_header_t)) + if (records_read < 1) { return false; } if (!CheckChunkHeader(&file->header.chunk_header, HEADER_CHUNK_ID) - || LONG(file->header.chunk_header.chunk_size) != 6) + || SDL_SwapBE32(file->header.chunk_header.chunk_size) != 6) { - fprintf(stderr, "ReadHeaderChunk: Invalid MIDI chunk header!\n"); + fprintf(stderr, "ReadHeaderChunk: Invalid MIDI chunk header! " + "chunk_size=%i\n", + SDL_SwapBE32(file->header.chunk_header.chunk_size)); return false; } - if (SHORT(file->header.format_type) != 0 - || SHORT(file->header.num_tracks) != 1) + if (SDL_SwapBE16(file->header.format_type) != 0 + || SDL_SwapBE16(file->header.num_tracks) != 1) { fprintf(stderr, "ReadHeaderChunk: Only single track, " "type 0 MIDI files supported!\n"); @@ -111,12 +117,12 @@ static boolean ReadHeaderChunk(midi_file_t *file) static boolean ReadTrackChunk(midi_file_t *file) { - size_t bytes_read; + size_t records_read; chunk_header_t chunk_header; - bytes_read = fread(&chunk_header, sizeof(chunk_header_t), 1, file->stream); + records_read = fread(&chunk_header, sizeof(chunk_header_t), 1, file->stream); - if (bytes_read < sizeof(chunk_header)) + if (records_read < 1) { return false; } @@ -126,7 +132,7 @@ static boolean ReadTrackChunk(midi_file_t *file) return false; } - file->data_len = LONG(chunk_header.chunk_size); + file->data_len = SDL_SwapBE32(chunk_header.chunk_size); return true; } @@ -375,6 +381,8 @@ static boolean ReadMetaEvent(midi_file_t *file, midi_event_t *event) { byte b; + event->event_type = MIDI_EVENT_META; + // Read meta event type: if (!ReadByte(file, &b)) @@ -464,3 +472,65 @@ boolean MIDI_ReadEvent(midi_file_t *file, midi_event_t *event) } } +#ifdef TEST + +int main(int argc, char *argv[]) +{ + midi_file_t *file; + midi_event_t event; + + if (argc < 2) + { + printf("Usage: %s \n", argv[0]); + exit(1); + } + + file = MIDI_OpenFile(argv[1]); + + if (file == NULL) + { + fprintf(stderr, "Failed to open %s\n", argv[1]); + exit(1); + } + + while (MIDI_ReadEvent(file, &event)) + { + printf("Event type: %i\n", event.event_type); + + switch(event.event_type) + { + case MIDI_EVENT_NOTE_OFF: + case MIDI_EVENT_NOTE_ON: + case MIDI_EVENT_AFTERTOUCH: + case MIDI_EVENT_CONTROLLER: + case MIDI_EVENT_PROGRAM_CHANGE: + case MIDI_EVENT_CHAN_AFTERTOUCH: + case MIDI_EVENT_PITCH_BEND: + printf("\tChannel: %i\n", event.data.channel.channel); + printf("\tParameter 1: %i\n", event.data.channel.param1); + printf("\tParameter 2: %i\n", event.data.channel.param2); + break; + + case MIDI_EVENT_SYSEX: + case MIDI_EVENT_SYSEX_SPLIT: + printf("\tLength: %i\n", event.data.sysex.length); + break; + + case MIDI_EVENT_META: + printf("\tMeta type: %i\n", event.data.meta.type); + printf("\tLength: %i\n", event.data.meta.length); + break; + } + + if (event.event_type == MIDI_EVENT_META + && event.data.meta.type == MIDI_META_END_OF_TRACK) + { + break; + } + } + + return 0; +} + +#endif + diff --git a/src/midifile.h b/src/midifile.h index d7df8b86..7928fdda 100644 --- a/src/midifile.h +++ b/src/midifile.h @@ -65,13 +65,14 @@ typedef enum MIDI_META_MARKER = 0x6, MIDI_META_CUE_POINT = 0x7, - MIDI_META_CHANNEL_PREFIX = 0x8, - MIDI_META_END_OF_TRACK = 0x9, - MIDI_META_SET_TEMPO = 0xa, - MIDI_META_SMPTE_OFFSET = 0xb, - MIDI_META_TIME_SIGNATURE = 0xc, - MIDI_META_KEY_SIGNATURE = 0xd, - MIDI_META_SEQUENCER_SPECIFIC = 0xe, + MIDI_META_CHANNEL_PREFIX = 0x20, + MIDI_META_END_OF_TRACK = 0x2f, + + MIDI_META_SET_TEMPO = 0x51, + MIDI_META_SMPTE_OFFSET = 0x54, + MIDI_META_TIME_SIGNATURE = 0x58, + MIDI_META_KEY_SIGNATURE = 0x59, + MIDI_META_SEQUENCER_SPECIFIC = 0x7f, } midi_meta_event_type_t; typedef struct -- cgit v1.2.3 From b2d78c296a5a2f9cf37bf8bd3dafb5c8ba166812 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 3 Apr 2009 20:07:08 +0000 Subject: Show MIDI event types with string identifiers. Subversion-branch: /branches/opl-branch Subversion-revision: 1495 --- src/midifile.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/midifile.c b/src/midifile.c index e4273c12..ef2ceb88 100644 --- a/src/midifile.c +++ b/src/midifile.c @@ -474,6 +474,36 @@ boolean MIDI_ReadEvent(midi_file_t *file, midi_event_t *event) #ifdef TEST +static char *MIDI_EventTypeToString(midi_event_type_t event_type) +{ + switch (event_type) + { + case MIDI_EVENT_NOTE_OFF: + return "MIDI_EVENT_NOTE_OFF"; + case MIDI_EVENT_NOTE_ON: + return "MIDI_EVENT_NOTE_ON"; + case MIDI_EVENT_AFTERTOUCH: + return "MIDI_EVENT_AFTERTOUCH"; + case MIDI_EVENT_CONTROLLER: + return "MIDI_EVENT_CONTROLLER"; + case MIDI_EVENT_PROGRAM_CHANGE: + return "MIDI_EVENT_PROGRAM_CHANGE"; + case MIDI_EVENT_CHAN_AFTERTOUCH: + return "MIDI_EVENT_CHAN_AFTERTOUCH"; + case MIDI_EVENT_PITCH_BEND: + return "MIDI_EVENT_PITCH_BEND"; + case MIDI_EVENT_SYSEX: + return "MIDI_EVENT_SYSEX"; + case MIDI_EVENT_SYSEX_SPLIT: + return "MIDI_EVENT_SYSEX_SPLIT"; + case MIDI_EVENT_META: + return "MIDI_EVENT_META"; + + default: + return "(unknown)"; + } +} + int main(int argc, char *argv[]) { midi_file_t *file; @@ -495,7 +525,9 @@ int main(int argc, char *argv[]) while (MIDI_ReadEvent(file, &event)) { - printf("Event type: %i\n", event.event_type); + printf("Event type: %s (%i)\n", + MIDI_EventTypeToString(event.event_type), + event.event_type); switch(event.event_type) { -- cgit v1.2.3 From 3dd7e1db9bf26e1e50bc13f6b176e54d63f2da85 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Mon, 13 Apr 2009 22:23:05 +0000 Subject: Extend MIDI file code to support reading multi-track files. Subversion-branch: /branches/opl-branch Subversion-revision: 1498 --- src/midifile.c | 562 +++++++++++++++++++++++++++++++++++---------------------- src/midifile.h | 2 +- 2 files changed, 351 insertions(+), 213 deletions(-) (limited to 'src') diff --git a/src/midifile.c b/src/midifile.c index ef2ceb88..cca0189e 100644 --- a/src/midifile.c +++ b/src/midifile.c @@ -50,17 +50,33 @@ typedef struct unsigned short time_division; } PACKEDATTR midi_header_t; +typedef struct +{ + // Length in bytes: + + unsigned int data_len; + + // Events in this track: + + midi_event_t *events; + int num_events; +} PACKEDATTR midi_track_t; + struct midi_file_s { - FILE *stream; midi_header_t header; - unsigned int data_len; + + // All tracks in this file: + midi_track_t *tracks; + unsigned int num_tracks; // Data buffer used to store data read for SysEx or meta events: byte *buffer; unsigned int buffer_size; }; +// Check the header of a chunk: + static boolean CheckChunkHeader(chunk_header_t *chunk, char *expected_id) { @@ -80,123 +96,13 @@ static boolean CheckChunkHeader(chunk_header_t *chunk, return result; } -// Read and check the header chunk. - -static boolean ReadHeaderChunk(midi_file_t *file) -{ - size_t records_read; - - records_read = fread(&file->header, sizeof(midi_header_t), 1, file->stream); - - if (records_read < 1) - { - return false; - } - - if (!CheckChunkHeader(&file->header.chunk_header, HEADER_CHUNK_ID) - || SDL_SwapBE32(file->header.chunk_header.chunk_size) != 6) - { - fprintf(stderr, "ReadHeaderChunk: Invalid MIDI chunk header! " - "chunk_size=%i\n", - SDL_SwapBE32(file->header.chunk_header.chunk_size)); - return false; - } - - if (SDL_SwapBE16(file->header.format_type) != 0 - || SDL_SwapBE16(file->header.num_tracks) != 1) - { - fprintf(stderr, "ReadHeaderChunk: Only single track, " - "type 0 MIDI files supported!\n"); - return false; - } - - return true; -} - -// Read and check the track chunk header - -static boolean ReadTrackChunk(midi_file_t *file) -{ - size_t records_read; - chunk_header_t chunk_header; - - records_read = fread(&chunk_header, sizeof(chunk_header_t), 1, file->stream); - - if (records_read < 1) - { - return false; - } - - if (!CheckChunkHeader(&chunk_header, TRACK_CHUNK_ID)) - { - return false; - } - - file->data_len = SDL_SwapBE32(chunk_header.chunk_size); - - return true; -} - -midi_file_t *MIDI_OpenFile(char *filename) -{ - midi_file_t *file; - - file = malloc(sizeof(midi_file_t)); - - if (file == NULL) - { - return NULL; - } - - file->buffer = NULL; - file->buffer_size = 0; - - // Open file - - file->stream = fopen(filename, "rb"); - - if (file->stream == NULL) - { - fprintf(stderr, "MIDI_OpenFile: Failed to open '%s'\n", filename); - free(file); - return NULL; - } - - // Read MIDI file header - - if (!ReadHeaderChunk(file)) - { - fclose(file->stream); - free(file); - return NULL; - } - - // Read track header - - if (!ReadTrackChunk(file)) - { - fclose(file->stream); - free(file); - return NULL; - } - - return file; -} - -void MIDI_CloseFile(midi_file_t *file) -{ - fclose(file->stream); - free(file->buffer); - free(file); -} - // Read a single byte. Returns false on error. -static boolean ReadByte(midi_file_t *file, byte *result) +static boolean ReadByte(byte *result, FILE *stream) { int c; - c = fgetc(file->stream); + c = fgetc(stream); if (c == EOF) { @@ -213,7 +119,7 @@ static boolean ReadByte(midi_file_t *file, byte *result) // Read a variable-length value. -static boolean ReadVariableLength(midi_file_t *file, unsigned int *result) +static boolean ReadVariableLength(unsigned int *result, FILE *stream) { int i; byte b; @@ -222,7 +128,7 @@ static boolean ReadVariableLength(midi_file_t *file, unsigned int *result) for (i=0; i<4; ++i) { - if (!ReadByte(file, &b)) + if (!ReadByte(&b, stream)) { fprintf(stderr, "ReadVariableLength: Error while reading " "variable-length value\n"); @@ -247,71 +153,46 @@ static boolean ReadVariableLength(midi_file_t *file, unsigned int *result) return false; } -// Expand the size of the buffer used for SysEx/Meta events: - -static boolean ExpandBuffer(midi_file_t *file, unsigned int new_size) -{ - byte *new_buffer; - - if (new_size > MAX_BUFFER_SIZE) - { - fprintf(stderr, "ExpandBuffer: Tried to expand buffer to %u bytes\n", - new_size); - return false; - } - - if (file->buffer_size < new_size) - { - // Reallocate to a larger size: - - new_buffer = realloc(file->buffer, new_size); - - if (new_buffer == NULL) - { - fprintf(stderr, "ExpandBuffer: Failed to expand buffer to %u " - "bytes\n", new_size); - return false; - } - - file->buffer = new_buffer; - file->buffer_size = new_size; - } - - return true; -} - // Read a byte sequence into the data buffer. -static boolean ReadByteSequence(midi_file_t *file, unsigned int num_bytes) +static void *ReadByteSequence(unsigned int num_bytes, FILE *stream) { unsigned int i; + byte *result; + + // Allocate a buffer: - // Check that we have enough space: + result = malloc(num_bytes); - if (!ExpandBuffer(file, num_bytes)) + if (result == NULL) { - return false; + fprintf(stderr, "ReadByteSequence: Failed to allocate buffer\n"); + return NULL; } + // Read the data: + for (i=0; ibuffer[i])) + if (!ReadByte(&result[i], stream)) { fprintf(stderr, "ReadByteSequence: Error while reading byte %u\n", i); - return false; + free(result); + return NULL; } } - return true; + return result; } // Read a MIDI channel event. // two_param indicates that the event type takes two parameters // (three byte) otherwise it is single parameter (two byte) -static boolean ReadChannelEvent(midi_file_t *file, midi_event_t *event, - byte event_type, boolean two_param) +static boolean ReadChannelEvent(midi_event_t *event, + byte event_type, boolean two_param, + FILE *stream) { byte b; @@ -322,7 +203,7 @@ static boolean ReadChannelEvent(midi_file_t *file, midi_event_t *event, // Read parameters: - if (!ReadByte(file, &b)) + if (!ReadByte(&b, stream)) { fprintf(stderr, "ReadChannelEvent: Error while reading channel " "event parameters\n"); @@ -335,7 +216,7 @@ static boolean ReadChannelEvent(midi_file_t *file, midi_event_t *event, if (two_param) { - if (!ReadByte(file, &b)) + if (!ReadByte(&b, stream)) { fprintf(stderr, "ReadChannelEvent: Error while reading channel " "event parameters\n"); @@ -350,12 +231,12 @@ static boolean ReadChannelEvent(midi_file_t *file, midi_event_t *event, // Read sysex event: -static boolean ReadSysExEvent(midi_file_t *file, midi_event_t *event, - int event_type) +static boolean ReadSysExEvent(midi_event_t *event, int event_type, + FILE *stream) { event->event_type = event_type; - if (!ReadVariableLength(file, &event->data.sysex.length)) + if (!ReadVariableLength(&event->data.sysex.length, stream)) { fprintf(stderr, "ReadSysExEvent: Failed to read length of " "SysEx block\n"); @@ -364,20 +245,20 @@ static boolean ReadSysExEvent(midi_file_t *file, midi_event_t *event, // Read the byte sequence: - if (!ReadByteSequence(file, event->data.sysex.length)) + event->data.sysex.data = ReadByteSequence(event->data.sysex.length, stream); + + if (event->data.sysex.data == NULL) { fprintf(stderr, "ReadSysExEvent: Failed while reading SysEx event\n"); return false; } - event->data.sysex.data = file->buffer; - return true; } // Read meta event: -static boolean ReadMetaEvent(midi_file_t *file, midi_event_t *event) +static boolean ReadMetaEvent(midi_event_t *event, FILE *stream) { byte b; @@ -385,7 +266,7 @@ static boolean ReadMetaEvent(midi_file_t *file, midi_event_t *event) // Read meta event type: - if (!ReadByte(file, &b)) + if (!ReadByte(&b, stream)) { fprintf(stderr, "ReadMetaEvent: Failed to read meta event type\n"); return false; @@ -395,7 +276,7 @@ static boolean ReadMetaEvent(midi_file_t *file, midi_event_t *event) // Read length of meta event data: - if (!ReadVariableLength(file, &event->data.meta.length)) + if (!ReadVariableLength(&event->data.meta.length, stream)) { fprintf(stderr, "ReadSysExEvent: Failed to read length of " "SysEx block\n"); @@ -404,30 +285,30 @@ static boolean ReadMetaEvent(midi_file_t *file, midi_event_t *event) // Read the byte sequence: - if (!ReadByteSequence(file, event->data.meta.length)) + event->data.meta.data = ReadByteSequence(event->data.meta.length, stream); + + if (event->data.meta.data == NULL) { fprintf(stderr, "ReadSysExEvent: Failed while reading SysEx event\n"); return false; } - event->data.meta.data = file->buffer; - return true; } -boolean MIDI_ReadEvent(midi_file_t *file, midi_event_t *event) +static boolean ReadEvent(midi_event_t *event, FILE *stream) { byte event_type; - if (!ReadVariableLength(file, &event->delta_time)) + if (!ReadVariableLength(&event->delta_time, stream)) { - fprintf(stderr, "MIDI_ReadEvent: Failed to read event timestamp\n"); + fprintf(stderr, "ReadEvent: Failed to read event timestamp\n"); return false; } - if (!ReadByte(file, &event_type)) + if (!ReadByte(&event_type, stream)) { - fprintf(stderr, "MIDI_ReadEvent: Failed to read event type\n"); + fprintf(stderr, "ReadEvent: Failed to read event type\n"); return false; } @@ -442,13 +323,13 @@ boolean MIDI_ReadEvent(midi_file_t *file, midi_event_t *event) case MIDI_EVENT_AFTERTOUCH: case MIDI_EVENT_CONTROLLER: case MIDI_EVENT_PITCH_BEND: - return ReadChannelEvent(file, event, event_type, true); + return ReadChannelEvent(event, event_type, true, stream); // Single parameter channel events: case MIDI_EVENT_PROGRAM_CHANGE: case MIDI_EVENT_CHAN_AFTERTOUCH: - return ReadChannelEvent(file, event, event_type, false); + return ReadChannelEvent(event, event_type, false, stream); // Other event types: @@ -456,11 +337,11 @@ boolean MIDI_ReadEvent(midi_file_t *file, midi_event_t *event) if (event_type == MIDI_EVENT_SYSEX || event_type == MIDI_EVENT_SYSEX_SPLIT) { - return ReadSysExEvent(file, event, event_type); + return ReadSysExEvent(event, event_type, stream); } else if (event_type == MIDI_EVENT_META) { - return ReadMetaEvent(file, event); + return ReadMetaEvent(event, stream); } // --- Fall-through deliberate --- @@ -472,6 +353,254 @@ boolean MIDI_ReadEvent(midi_file_t *file, midi_event_t *event) } } +// Free an event: + +static void FreeEvent(midi_event_t *event) +{ + // Some event types have dynamically allocated buffers assigned + // to them that must be freed. + + switch (event->event_type) + { + case MIDI_EVENT_SYSEX: + case MIDI_EVENT_SYSEX_SPLIT: + free(event->data.sysex.data); + break; + + case MIDI_EVENT_META: + free(event->data.meta.data); + break; + + default: + // Nothing to do. + break; + } +} + +// Read and check the track chunk header + +static boolean ReadTrackHeader(midi_track_t *track, FILE *stream) +{ + size_t records_read; + chunk_header_t chunk_header; + + records_read = fread(&chunk_header, sizeof(chunk_header_t), 1, stream); + + if (records_read < 1) + { + return false; + } + + if (!CheckChunkHeader(&chunk_header, TRACK_CHUNK_ID)) + { + return false; + } + + track->data_len = SDL_SwapBE32(chunk_header.chunk_size); + + return true; +} + +static boolean ReadTrack(midi_track_t *track, FILE *stream) +{ + midi_event_t *new_events; + midi_event_t *event; + + track->num_events = 0; + track->events = NULL; + + // Read the header: + + if (!ReadTrackHeader(track, stream)) + { + return false; + } + + // Then the events: + + for (;;) + { + // Resize the track slightly larger to hold another event: + + new_events = realloc(track->events, + sizeof(midi_event_t) * (track->num_events + 1)); + + if (new_events == NULL) + { + return false; + } + + track->events = new_events; + + // Read the next event: + + event = &track->events[track->num_events]; + if (!ReadEvent(event, stream)) + { + return false; + } + + ++track->num_events; + + // End of track? + + if (event->event_type == MIDI_EVENT_META + && event->data.meta.type == MIDI_META_END_OF_TRACK) + { + break; + } + } + + return true; +} + +// Free a track: + +static void FreeTrack(midi_track_t *track) +{ + unsigned int i; + + for (i=0; inum_events; ++i) + { + FreeEvent(&track->events[i]); + } + + free(track->events); +} + +static boolean ReadAllTracks(midi_file_t *file, FILE *stream) +{ + unsigned int i; + + // Allocate list of tracks and read each track: + + file->tracks = malloc(sizeof(midi_track_t) * file->num_tracks); + + if (file->tracks == NULL) + { + return false; + } + + memset(file->tracks, 0, sizeof(midi_track_t) * file->num_tracks); + + // Read each track: + + for (i=0; inum_tracks; ++i) + { + if (!ReadTrack(&file->tracks[i], stream)) + { + return false; + } + } + + return true; +} + +// Read and check the header chunk. + +static boolean ReadFileHeader(midi_file_t *file, FILE *stream) +{ + size_t records_read; + unsigned int format_type; + + records_read = fread(&file->header, sizeof(midi_header_t), 1, stream); + + if (records_read < 1) + { + return false; + } + + if (!CheckChunkHeader(&file->header.chunk_header, HEADER_CHUNK_ID) + || SDL_SwapBE32(file->header.chunk_header.chunk_size) != 6) + { + fprintf(stderr, "ReadFileHeader: Invalid MIDI chunk header! " + "chunk_size=%i\n", + SDL_SwapBE32(file->header.chunk_header.chunk_size)); + return false; + } + + format_type = SDL_SwapBE16(file->header.format_type); + file->num_tracks = SDL_SwapBE16(file->header.num_tracks); + + if ((format_type != 0 && format_type != 1) + || file->num_tracks < 1) + { + fprintf(stderr, "ReadFileHeader: Only type 0/1 " + "MIDI files supported!\n"); + return false; + } + + return true; +} + +void MIDI_FreeFile(midi_file_t *file) +{ + int i; + + if (file->tracks != NULL) + { + for (i=0; inum_tracks; ++i) + { + FreeTrack(&file->tracks[i]); + } + + free(file->tracks); + } + + free(file); +} + +midi_file_t *MIDI_OpenFile(char *filename) +{ + midi_file_t *file; + FILE *stream; + + file = malloc(sizeof(midi_file_t)); + + if (file == NULL) + { + return NULL; + } + + file->tracks = NULL; + file->num_tracks = 0; + file->buffer = NULL; + file->buffer_size = 0; + + // Open file + + stream = fopen(filename, "rb"); + + if (stream == NULL) + { + fprintf(stderr, "MIDI_OpenFile: Failed to open '%s'\n", filename); + MIDI_FreeFile(file); + return NULL; + } + + // Read MIDI file header + + if (!ReadFileHeader(file, stream)) + { + fclose(stream); + MIDI_FreeFile(file); + return NULL; + } + + // Read all tracks: + + if (!ReadAllTracks(file, stream)) + { + fclose(stream); + MIDI_FreeFile(file); + return NULL; + } + + fclose(stream); + + return file; +} + #ifdef TEST static char *MIDI_EventTypeToString(midi_event_type_t event_type) @@ -504,32 +633,20 @@ static char *MIDI_EventTypeToString(midi_event_type_t event_type) } } -int main(int argc, char *argv[]) +void PrintTrack(midi_track_t *track) { - midi_file_t *file; - midi_event_t event; - - if (argc < 2) - { - printf("Usage: %s \n", argv[0]); - exit(1); - } - - file = MIDI_OpenFile(argv[1]); + midi_event_t *event; + unsigned int i; - if (file == NULL) + for (i=0; inum_events; ++i) { - fprintf(stderr, "Failed to open %s\n", argv[1]); - exit(1); - } + event = &track->events[i]; - while (MIDI_ReadEvent(file, &event)) - { printf("Event type: %s (%i)\n", - MIDI_EventTypeToString(event.event_type), - event.event_type); + MIDI_EventTypeToString(event->event_type), + event->event_type); - switch(event.event_type) + switch(event->event_type) { case MIDI_EVENT_NOTE_OFF: case MIDI_EVENT_NOTE_ON: @@ -538,27 +655,48 @@ int main(int argc, char *argv[]) case MIDI_EVENT_PROGRAM_CHANGE: case MIDI_EVENT_CHAN_AFTERTOUCH: case MIDI_EVENT_PITCH_BEND: - printf("\tChannel: %i\n", event.data.channel.channel); - printf("\tParameter 1: %i\n", event.data.channel.param1); - printf("\tParameter 2: %i\n", event.data.channel.param2); + printf("\tChannel: %i\n", event->data.channel.channel); + printf("\tParameter 1: %i\n", event->data.channel.param1); + printf("\tParameter 2: %i\n", event->data.channel.param2); break; case MIDI_EVENT_SYSEX: case MIDI_EVENT_SYSEX_SPLIT: - printf("\tLength: %i\n", event.data.sysex.length); + printf("\tLength: %i\n", event->data.sysex.length); break; case MIDI_EVENT_META: - printf("\tMeta type: %i\n", event.data.meta.type); - printf("\tLength: %i\n", event.data.meta.length); + printf("\tMeta type: %i\n", event->data.meta.type); + printf("\tLength: %i\n", event->data.meta.length); break; } + } +} - if (event.event_type == MIDI_EVENT_META - && event.data.meta.type == MIDI_META_END_OF_TRACK) - { - break; - } +int main(int argc, char *argv[]) +{ + midi_file_t *file; + unsigned int i; + + if (argc < 2) + { + printf("Usage: %s \n", argv[0]); + exit(1); + } + + file = MIDI_OpenFile(argv[1]); + + if (file == NULL) + { + fprintf(stderr, "Failed to open %s\n", argv[1]); + exit(1); + } + + for (i=0; inum_tracks; ++i) + { + printf("\n== Track %i ==\n\n", i); + + PrintTrack(&file->tracks[i]); } return 0; diff --git a/src/midifile.h b/src/midifile.h index 7928fdda..16f911e7 100644 --- a/src/midifile.h +++ b/src/midifile.h @@ -130,7 +130,7 @@ typedef struct } midi_event_t; midi_file_t *MIDI_OpenFile(char *filename); -void MIDI_CloseFile(midi_file_t *file); +void MIDI_FreeFile(midi_file_t *file); #endif /* #ifndef MIDIFILE_H */ -- cgit v1.2.3 From d8ada5c41da3eec5ef163d9efac9cb5dbdc2cb6b Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Wed, 15 Apr 2009 19:00:25 +0000 Subject: Parse MIDI events that reuse the event type from the previous event. Subversion-branch: /branches/opl-branch Subversion-revision: 1499 --- src/midifile.c | 70 +++++++++++++++++++++++++++++++++++++++++----------------- src/midifile.h | 14 ++++++------ 2 files changed, 57 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/midifile.c b/src/midifile.c index cca0189e..1be6ec75 100644 --- a/src/midifile.c +++ b/src/midifile.c @@ -198,8 +198,8 @@ static boolean ReadChannelEvent(midi_event_t *event, // Set basics: - event->event_type = event_type >> 4; - event->data.channel.channel = event_type & 0xf; + event->event_type = event_type & 0xf0; + event->data.channel.channel = event_type & 0x0f; // Read parameters: @@ -296,7 +296,8 @@ static boolean ReadMetaEvent(midi_event_t *event, FILE *stream) return true; } -static boolean ReadEvent(midi_event_t *event, FILE *stream) +static boolean ReadEvent(midi_event_t *event, unsigned int *last_event_type, + FILE *stream) { byte event_type; @@ -312,9 +313,29 @@ static boolean ReadEvent(midi_event_t *event, FILE *stream) return false; } + // All event types have their top bit set. Therefore, if + // the top bit is not set, it is because we are using the "same + // as previous event type" shortcut to save a byte. Skip back + // a byte so that we read this byte again. + + if ((event_type & 0x80) == 0) + { + event_type = *last_event_type; + + if (fseek(stream, -1, SEEK_CUR) < 0) + { + fprintf(stderr, "ReadEvent: Unable to seek in stream\n"); + return false; + } + } + else + { + *last_event_type = event_type; + } + // Check event type: - switch (event_type >> 4) + switch (event_type & 0xf0) { // Two parameter channel events: @@ -331,26 +352,27 @@ static boolean ReadEvent(midi_event_t *event, FILE *stream) case MIDI_EVENT_CHAN_AFTERTOUCH: return ReadChannelEvent(event, event_type, false, stream); - // Other event types: + default: + break; + } + + // Specific value? - case 0xf: - if (event_type == MIDI_EVENT_SYSEX - || event_type == MIDI_EVENT_SYSEX_SPLIT) - { - return ReadSysExEvent(event, event_type, stream); - } - else if (event_type == MIDI_EVENT_META) - { - return ReadMetaEvent(event, stream); - } + switch (event_type) + { + case MIDI_EVENT_SYSEX: + case MIDI_EVENT_SYSEX_SPLIT: + return ReadSysExEvent(event, event_type, stream); - // --- Fall-through deliberate --- - // Other 0xfx event types are unknown + case MIDI_EVENT_META: + return ReadMetaEvent(event, stream); default: - fprintf(stderr, "Unknown MIDI event type: 0x%x\n", event_type); - return false; + break; } + + fprintf(stderr, "ReadEvent: Unknown MIDI event type: 0x%x\n", event_type); + return false; } // Free an event: @@ -405,6 +427,7 @@ static boolean ReadTrack(midi_track_t *track, FILE *stream) { midi_event_t *new_events; midi_event_t *event; + unsigned int last_event_type; track->num_events = 0; track->events = NULL; @@ -418,6 +441,8 @@ static boolean ReadTrack(midi_track_t *track, FILE *stream) // Then the events: + last_event_type = 0; + for (;;) { // Resize the track slightly larger to hold another event: @@ -435,7 +460,7 @@ static boolean ReadTrack(midi_track_t *track, FILE *stream) // Read the next event: event = &track->events[track->num_events]; - if (!ReadEvent(event, stream)) + if (!ReadEvent(event, &last_event_type, stream)) { return false; } @@ -642,6 +667,11 @@ void PrintTrack(midi_track_t *track) { event = &track->events[i]; + if (event->delta_time > 0) + { + printf("Delay: %i ticks\n", event->delta_time); + } + printf("Event type: %s (%i)\n", MIDI_EventTypeToString(event->event_type), event->event_type); diff --git a/src/midifile.h b/src/midifile.h index 16f911e7..490b0171 100644 --- a/src/midifile.h +++ b/src/midifile.h @@ -30,13 +30,13 @@ typedef struct midi_file_s midi_file_t; typedef enum { - MIDI_EVENT_NOTE_OFF = 0x8, - MIDI_EVENT_NOTE_ON = 0x9, - MIDI_EVENT_AFTERTOUCH = 0xa, - MIDI_EVENT_CONTROLLER = 0xb, - MIDI_EVENT_PROGRAM_CHANGE = 0xc, - MIDI_EVENT_CHAN_AFTERTOUCH = 0xd, - MIDI_EVENT_PITCH_BEND = 0xe, + MIDI_EVENT_NOTE_OFF = 0x80, + MIDI_EVENT_NOTE_ON = 0x90, + MIDI_EVENT_AFTERTOUCH = 0xa0, + MIDI_EVENT_CONTROLLER = 0xb0, + MIDI_EVENT_PROGRAM_CHANGE = 0xc0, + MIDI_EVENT_CHAN_AFTERTOUCH = 0xd0, + MIDI_EVENT_PITCH_BEND = 0xe0, MIDI_EVENT_SYSEX = 0xf0, MIDI_EVENT_SYSEX_SPLIT = 0xf7, -- cgit v1.2.3 From 324c1d8776054394d30ea987f84c1ba2f1b1ff6f Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 29 Aug 2009 21:05:36 +0000 Subject: Rename MIDI_OpenFile to MIDI_LoadFile, remove unneeded structure packing. Subversion-branch: /branches/opl-branch Subversion-revision: 1636 --- src/midifile.c | 4 ++-- src/midifile.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/midifile.c b/src/midifile.c index 1be6ec75..6eb9e7db 100644 --- a/src/midifile.c +++ b/src/midifile.c @@ -60,7 +60,7 @@ typedef struct midi_event_t *events; int num_events; -} PACKEDATTR midi_track_t; +} midi_track_t; struct midi_file_s { @@ -575,7 +575,7 @@ void MIDI_FreeFile(midi_file_t *file) free(file); } -midi_file_t *MIDI_OpenFile(char *filename) +midi_file_t *MIDI_LoadFile(char *filename) { midi_file_t *file; FILE *stream; diff --git a/src/midifile.h b/src/midifile.h index 490b0171..f9acea35 100644 --- a/src/midifile.h +++ b/src/midifile.h @@ -129,7 +129,7 @@ typedef struct } data; } midi_event_t; -midi_file_t *MIDI_OpenFile(char *filename); +midi_file_t *MIDI_LoadFile(char *filename); void MIDI_FreeFile(midi_file_t *file); #endif /* #ifndef MIDIFILE_H */ -- cgit v1.2.3 From 9cc843c60027cb9365f3eaae3028343769d17a26 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 29 Aug 2009 21:22:32 +0000 Subject: Load MIDI file. Subversion-branch: /branches/opl-branch Subversion-revision: 1637 --- src/i_oplmusic.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index f50c3322..c444dfd8 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -40,6 +40,7 @@ #include "z_zone.h" #include "opl.h" +#include "midifile.h" #define MAXMIDLENGTH (96 * 1024) #define GENMIDI_NUM_INSTRS 128 @@ -507,6 +508,11 @@ static void I_OPL_UnRegisterSong(void *handle) { return; } + + if (handle != NULL) + { + MIDI_FreeFile(handle); + } } // Determine whether memory block is a .mid file @@ -544,6 +550,7 @@ static boolean ConvertMus(byte *musdata, int len, char *filename) static void *I_OPL_RegisterSong(void *data, int len) { + midi_file_t *result; char *filename; if (!music_initialised) @@ -567,7 +574,12 @@ static void *I_OPL_RegisterSong(void *data, int len) ConvertMus(data, len, filename); } - // .... + result = MIDI_LoadFile(filename); + + if (result == NULL) + { + fprintf(stderr, "I_OPL_RegisterSong: Failed to load MID.\n"); + } // remove file now @@ -575,7 +587,7 @@ static void *I_OPL_RegisterSong(void *data, int len) Z_Free(filename); - return NULL; + return result; } // Is the song playing? -- cgit v1.2.3 From a10180a460f6425cd308719584aa58ab4fcb63fb Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 29 Aug 2009 21:26:43 +0000 Subject: Use OPL_Delay to wait 1ms for timer to expire when doing OPL detect. Subversion-branch: /branches/opl-branch Subversion-revision: 1638 --- src/i_oplmusic.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index c444dfd8..0e1b69a3 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -202,6 +202,8 @@ static boolean DetectOPL(void) GetStatus(); } + OPL_Delay(1); + // Read status result2 = GetStatus(); -- cgit v1.2.3 From f2f36117c889c19b643058a0af33070baf4b48be Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 29 Aug 2009 21:30:26 +0000 Subject: MIDI_OpenFile -> MIDI_LoadFile. Subversion-branch: /branches/opl-branch Subversion-revision: 1639 --- src/midifile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/midifile.c b/src/midifile.c index 6eb9e7db..61e06463 100644 --- a/src/midifile.c +++ b/src/midifile.c @@ -598,7 +598,7 @@ midi_file_t *MIDI_LoadFile(char *filename) if (stream == NULL) { - fprintf(stderr, "MIDI_OpenFile: Failed to open '%s'\n", filename); + fprintf(stderr, "MIDI_LoadFile: Failed to open '%s'\n", filename); MIDI_FreeFile(file); return NULL; } @@ -714,7 +714,7 @@ int main(int argc, char *argv[]) exit(1); } - file = MIDI_OpenFile(argv[1]); + file = MIDI_LoadFile(argv[1]); if (file == NULL) { -- cgit v1.2.3 From f6ce7dfea99cf32beb2afc8e7b02fb5f19f7544f Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 30 Aug 2009 01:56:33 +0000 Subject: Make some noise. Subversion-branch: /branches/opl-branch Subversion-revision: 1640 --- src/i_oplmusic.c | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 0e1b69a3..2c0efa92 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -42,6 +42,8 @@ #include "opl.h" #include "midifile.h" +#define TEST + #define MAXMIDLENGTH (96 * 1024) #define GENMIDI_NUM_INSTRS 128 @@ -405,6 +407,30 @@ static void I_OPL_ShutdownMusic(void) } } +#ifdef TEST +static void TestCallback(void *arg) +{ + opl_voice_t *voice = arg; + int note; + int wait_time; + + // Set level: + WriteRegister(OPL_REGS_LEVEL + voice->op2, 0); + + // Note off: + + WriteRegister(OPL_REGS_FREQ_2 + voice->index, 0x00); + // Note on: + + note = (rand() % (0x2ae - 0x16b)) + 0x16b; + WriteRegister(OPL_REGS_FREQ_1 + voice->index, note & 0xff); + WriteRegister(OPL_REGS_FREQ_2 + voice->index, 0x30 + (note >> 8)); + + wait_time = (rand() % 700) + 50; + OPL_SetCallback(wait_time, TestCallback, arg); +} +#endif + // Initialise music subsystem static boolean I_OPL_InitMusic(void) @@ -443,17 +469,19 @@ static boolean I_OPL_InitMusic(void) #ifdef TEST { + int i; opl_voice_t *voice; + int instr_num; - voice = GetFreeVoice(); - SetVoiceInstrument(voice, &main_instrs[34].opl2_voice); + for (i=0; i<3; ++i) + { + voice = GetFreeVoice(); + instr_num = rand() % 100; - // Set level: - WriteRegister(OPL_REGS_LEVEL + voice->op2, 0x94); + SetVoiceInstrument(voice, &main_instrs[instr_num].opl2_voice); - // Note on: - WriteRegister(OPL_REGS_FREQ_1 + voice->index, 0x65); - WriteRegister(OPL_REGS_FREQ_2 + voice->index, 0x2b); + OPL_SetCallback(0, TestCallback, voice); + } } #endif -- cgit v1.2.3 From ca23c95db8053b82fb817241e5de83a881f931e5 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 30 Aug 2009 21:34:12 +0000 Subject: Initial/basic MIDI track playback. Subversion-branch: /branches/opl-branch Subversion-revision: 1643 --- src/i_oplmusic.c | 328 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/midifile.c | 69 ++++++++++++ src/midifile.h | 28 +++++ 3 files changed, 423 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 2c0efa92..3d9a2649 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -42,7 +42,7 @@ #include "opl.h" #include "midifile.h" -#define TEST +//#define TEST #define MAXMIDLENGTH (96 * 1024) #define GENMIDI_NUM_INSTRS 128 @@ -80,6 +80,37 @@ typedef struct genmidi_voice_t opl3_voice; } PACKEDATTR genmidi_instr_t; +// Data associated with a channel of a track that is currently playing. + +typedef struct +{ + // The instrument currently used for this track. + + genmidi_instr_t *instrument; + + // Volume level + + int volume; +} opl_channel_data_t; + +// Data associated with a track that is currently playing. + +typedef struct +{ + // Data for each channel. + + opl_channel_data_t channels[MIDI_CHANNELS_PER_TRACK]; + + // Track iterator used to read new events. + + midi_track_iter_t *iter; + + // Tempo control variables + + unsigned int ticks_per_beat; + unsigned int us_per_beat; +} opl_track_data_t; + typedef struct opl_voice_s opl_voice_t; struct opl_voice_s @@ -93,6 +124,15 @@ struct opl_voice_s // Currently-loaded instrument data genmidi_instr_t *current_instr; + // The channel currently using this voice. + opl_channel_data_t *channel; + + // The note that this voice is playing. + unsigned int note; + + // The frequency value being used. + unsigned int freq; + // Next in freelist opl_voice_t *next; }; @@ -104,6 +144,22 @@ static const int voice_operators[2][OPL_NUM_VOICES] = { { 0x03, 0x04, 0x05, 0x0b, 0x0c, 0x0d, 0x13, 0x14, 0x15 } }; +// Frequency values to use for each note. + +static const unsigned int note_frequencies[] = { + + // These frequencies are only used for the first seven + // MIDI note values: + + 0x158, 0x16d, 0x183, 0x19a, 0x1b2, 0x1cc, 0x1e7, + + // These frequencies are used repeatedly, cycling around + // for each octave: + + 0x204, 0x223, 0x244, 0x266, 0x28b, 0x2b1, + 0x2da, 0x306, 0x334, 0x365, 0x398, 0x3cf, +}; + static boolean music_initialised = false; //static boolean musicpaused = false; @@ -119,6 +175,10 @@ static genmidi_instr_t *percussion_instrs; static opl_voice_t voices[OPL_NUM_VOICES]; static opl_voice_t *voice_free_list; +// Track data for playing tracks: + +static opl_track_data_t *tracks; + // In the initialisation stage, register writes are spaced by reading // from the register port (0). After initialisation, spacing is // peformed by reading from the data port instead. I have no idea @@ -310,6 +370,9 @@ static void ReleaseVoice(opl_voice_t *voice) { opl_voice_t **rover; + voice->channel = NULL; + voice->note = 0; + // Search to the end of the freelist (This is how Doom behaves!) rover = &voice_free_list; @@ -320,6 +383,7 @@ static void ReleaseVoice(opl_voice_t *voice) } *rover = voice; + voice->next = NULL; } // Load data to the specified operator @@ -498,14 +562,274 @@ static void I_OPL_SetMusicVolume(int volume) current_music_volume = volume; } +static opl_voice_t *FindVoiceForNote(opl_channel_data_t *channel, int note) +{ + unsigned int i; + + for (i=0; idata.channel.channel, + event->data.channel.param1, + event->data.channel.param2); + + channel = &track->channels[event->data.channel.channel]; + + // Find the voice being used to play the note. + + voice = FindVoiceForNote(channel, event->data.channel.param1); + + if (voice == NULL) + { + return; + } + + // Note off. + + WriteRegister(OPL_REGS_FREQ_2 + voice->index, voice->freq >> 8); + + // Finished with this voice now. + + ReleaseVoice(voice); +} + +// Given a MIDI note number, get the corresponding OPL +// frequency value to use. + +static unsigned int FrequencyForNote(unsigned int note) +{ + unsigned int octave; + unsigned int key_num; + + // The first seven frequencies in the frequencies array are used + // only for the first seven MIDI notes. After this, the frequency + // value loops around the same twelve notes, increasing the + // octave. + + if (note < 7) + { + return note_frequencies[note]; + } + else + { + octave = (note - 7) / 12; + key_num = (note - 7) % 12; + + return note_frequencies[key_num + 7] | (octave << 10); + } +} + +static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) +{ + opl_voice_t *voice; + opl_channel_data_t *channel; + + printf("note on: channel %i, %i, %i\n", + event->data.channel.channel, + event->data.channel.param1, + event->data.channel.param2); + + // The channel. + + channel = &track->channels[event->data.channel.channel]; + + // Find a voice to use for this new note. + + voice = GetFreeVoice(); + + if (voice == NULL) + { + return; + } + + // Program the voice with the instrument data: + + SetVoiceInstrument(voice, &channel->instrument->opl2_voice); + + // TODO: Set the volume level. + + WriteRegister(OPL_REGS_LEVEL + voice->op2, 0); + + // Play the note. + + voice->channel = channel; + voice->note = event->data.channel.param1; + + // Write the frequency value to turn the note on. + + voice->freq = FrequencyForNote(voice->note); + + WriteRegister(OPL_REGS_FREQ_1 + voice->index, voice->freq & 0xff); + WriteRegister(OPL_REGS_FREQ_2 + voice->index, (voice->freq >> 8) | 0x20); +} + +static void ProgramChangeEvent(opl_track_data_t *track, midi_event_t *event) +{ + int channel; + int instrument; + + // Set the instrument used on this channel. + + channel = event->data.channel.channel; + instrument = event->data.channel.param1; + track->channels[channel].instrument = &main_instrs[instrument]; + + // TODO: Look through existing voices that are turned on on this + // channel, and change the instrument. +} + +static void ControllerEvent(opl_track_data_t *track, midi_event_t *event) +{ + printf("change controller: channel %i, %i, %i\n", + event->data.channel.channel, + event->data.channel.param1, + event->data.channel.param2); + + // TODO: Volume, pan. +} + +// Process a MIDI event from a track. + +static void ProcessEvent(opl_track_data_t *track, midi_event_t *event) +{ + switch (event->event_type) + { + case MIDI_EVENT_NOTE_OFF: + NoteOffEvent(track, event); + break; + + case MIDI_EVENT_NOTE_ON: + NoteOnEvent(track, event); + break; + + case MIDI_EVENT_CONTROLLER: + ControllerEvent(track, event); + break; + + case MIDI_EVENT_PROGRAM_CHANGE: + ProgramChangeEvent(track, event); + break; + + default: + fprintf(stderr, "Unknown MIDI event type %i\n", event->event_type); + break; + } +} + +static void ScheduleTrack(opl_track_data_t *track); + +// Callback function invoked when another event needs to be read from +// a track. + +static void TrackTimerCallback(void *arg) +{ + opl_track_data_t *track = arg; + midi_event_t *event; + + // Get the next event and process it. + + if (!MIDI_GetNextEvent(track->iter, &event)) + { + return; + } + + ProcessEvent(track, event); + + // Reschedule the callback for the next event in the track. + + ScheduleTrack(track); +} + +static void ScheduleTrack(opl_track_data_t *track) +{ + unsigned int nticks; + unsigned int us; + static int total = 0; + + // Get the number of microseconds until the next event. + + nticks = MIDI_GetDeltaTime(track->iter); + us = (nticks * track->us_per_beat) / track->ticks_per_beat; + total += us; + + // Set a timer to be invoked when the next event is + // ready to play. + + OPL_SetCallback(us / 1000, TrackTimerCallback, track); +} + +// Initialise a channel. + +static void InitChannel(opl_track_data_t *track, opl_channel_data_t *channel) +{ + // TODO: Work out sensible defaults? + + channel->instrument = &main_instrs[0]; + channel->volume = 127; +} + +// Start a MIDI track playing: + +static void StartTrack(midi_file_t *file, unsigned int track_num) +{ + opl_track_data_t *track; + unsigned int i; + + track = &tracks[track_num]; + track->iter = MIDI_IterateTrack(file, track_num); + track->ticks_per_beat = MIDI_GetFileTimeDivision(file); + + // Default is 120 bpm. + // TODO: this is wrong + + track->us_per_beat = 500 * 1000 * 200; + + for (i=0; ichannels[i]); + } + + // Schedule the first event. + + ScheduleTrack(track); +} + // Start playing a mid static void I_OPL_PlaySong(void *handle, int looping) { - if (!music_initialised) + midi_file_t *file; + unsigned int i; + + if (!music_initialised || handle == NULL) { return; } + + file = handle; + + // Allocate track data. + + tracks = malloc(MIDI_NumTracks(file) * sizeof(opl_track_data_t)); + + for (i=0; i #include #include +#include #include "doomdef.h" #include "doomtype.h" @@ -62,6 +63,12 @@ typedef struct int num_events; } midi_track_t; +struct midi_track_iter_s +{ + midi_track_t *track; + unsigned int position; +}; + struct midi_file_s { midi_header_t header; @@ -626,6 +633,68 @@ midi_file_t *MIDI_LoadFile(char *filename) return file; } +// Get the number of tracks in a MIDI file. + +unsigned int MIDI_NumTracks(midi_file_t *file) +{ + return file->num_tracks; +} + +// Start iterating over the events in a track. + +midi_track_iter_t *MIDI_IterateTrack(midi_file_t *file, unsigned int track) +{ + midi_track_iter_t *iter; + + assert(track < file->num_tracks); + + iter = malloc(sizeof(*iter)); + iter->track = &file->tracks[track]; + iter->position = 0; + + return iter; +} + +// Get the time until the next MIDI event in a track. + +unsigned int MIDI_GetDeltaTime(midi_track_iter_t *iter) +{ + if (iter->position < iter->track->num_events) + { + midi_event_t *next_event; + + next_event = &iter->track->events[iter->position]; + + return next_event->delta_time; + } + else + { + return 0; + } +} + +// Get a pointer to the next MIDI event. + +int MIDI_GetNextEvent(midi_track_iter_t *iter, midi_event_t **event) +{ + if (iter->position < iter->track->num_events) + { + *event = &iter->track->events[iter->position]; + ++iter->position; + + return 1; + } + else + { + return 0; + } +} + +unsigned int MIDI_GetFileTimeDivision(midi_file_t *file) +{ + return file->header.time_division; +} + #ifdef TEST static char *MIDI_EventTypeToString(midi_event_type_t event_type) diff --git a/src/midifile.h b/src/midifile.h index f9acea35..a1ab4976 100644 --- a/src/midifile.h +++ b/src/midifile.h @@ -27,6 +27,9 @@ #define MIDIFILE_H typedef struct midi_file_s midi_file_t; +typedef struct midi_track_iter_s midi_track_iter_t; + +#define MIDI_CHANNELS_PER_TRACK 16 typedef enum { @@ -129,8 +132,33 @@ typedef struct } data; } midi_event_t; +// Load a MIDI file. + midi_file_t *MIDI_LoadFile(char *filename); + +// Free a MIDI file. + void MIDI_FreeFile(midi_file_t *file); +// Get the time division value from the MIDI header. + +unsigned int MIDI_GetFileTimeDivision(midi_file_t *file); + +// Get the number of tracks in a MIDI file. + +unsigned int MIDI_NumTracks(midi_file_t *file); + +// Start iterating over the events in a track. + +midi_track_iter_t *MIDI_IterateTrack(midi_file_t *file, unsigned int track_num); + +// Get the time until the next MIDI event in a track. + +unsigned int MIDI_GetDeltaTime(midi_track_iter_t *iter); + +// Get a pointer to the next MIDI event. + +int MIDI_GetNextEvent(midi_track_iter_t *iter, midi_event_t **event); + #endif /* #ifndef MIDIFILE_H */ -- cgit v1.2.3 From 1d045ed19c58abff063ac69db24a2e73d62ef530 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 30 Aug 2009 22:03:20 +0000 Subject: Initial, broken, volume level setting. Subversion-branch: /branches/opl-branch Subversion-revision: 1644 --- src/i_oplmusic.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- src/midifile.h | 3 +++ 2 files changed, 49 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 3d9a2649..7a619c5d 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -160,6 +160,27 @@ static const unsigned int note_frequencies[] = { 0x2da, 0x306, 0x334, 0x365, 0x398, 0x3cf, }; +// Mapping from MIDI volume level to OPL level value. + +static const unsigned int volume_mapping_table[] = { + 0x3f, 0x3f, 0x3e, 0x3d, 0x3c, 0x3b, 0x3a, 0x3a, + 0x39, 0x38, 0x37, 0x37, 0x36, 0x35, 0x34, 0x34, + 0x33, 0x32, 0x32, 0x31, 0x30, 0x2f, 0x2f, 0x2e, + 0x2d, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, + 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x22, 0x21, + 0x20, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, + 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18, 0x17, + 0x17, 0x16, 0x16, 0x16, 0x16, 0x15, 0x15, 0x14, + 0x14, 0x13, 0x13, 0x12, 0x12, 0x12, 0x11, 0x11, + 0x10, 0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, + 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0b, + 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, + 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, + 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x04, + 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, +}; + static boolean music_initialised = false; //static boolean musicpaused = false; @@ -662,7 +683,10 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) // TODO: Set the volume level. - WriteRegister(OPL_REGS_LEVEL + voice->op2, 0); + WriteRegister(OPL_REGS_LEVEL + voice->op2, + volume_mapping_table[channel->volume]); + + printf("volume = %i\n", channel->volume); // Play the note. @@ -694,12 +718,32 @@ static void ProgramChangeEvent(opl_track_data_t *track, midi_event_t *event) static void ControllerEvent(opl_track_data_t *track, midi_event_t *event) { + unsigned int controller; + unsigned int param; + opl_channel_data_t *channel; + printf("change controller: channel %i, %i, %i\n", event->data.channel.channel, event->data.channel.param1, event->data.channel.param2); - // TODO: Volume, pan. + channel = &track->channels[event->data.channel.channel]; + controller = event->data.channel.param1; + param = event->data.channel.param2; + + switch (controller) + { + case MIDI_CONTROLLER_MAIN_VOLUME: + channel->volume = param; + break; + + case MIDI_CONTROLLER_PAN: + break; + + default: + fprintf(stderr, "Unknown MIDI controller type: %i\n", controller); + break; + } } // Process a MIDI event from a track. diff --git a/src/midifile.h b/src/midifile.h index a1ab4976..faef549c 100644 --- a/src/midifile.h +++ b/src/midifile.h @@ -54,6 +54,9 @@ typedef enum MIDI_CONTROLLER_FOOT_CONTROL = 0x3, MIDI_CONTROLLER_PORTAMENTO = 0x4, MIDI_CONTROLLER_DATA_ENTRY = 0x5, + + MIDI_CONTROLLER_MAIN_VOLUME = 0x7, + MIDI_CONTROLLER_PAN = 0xa } midi_controller_t; typedef enum -- cgit v1.2.3 From 61b2e3d2b8b6b2b6ddf8b92842ac4bcf47945160 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 30 Aug 2009 22:26:30 +0000 Subject: Set the right instrument for percussion notes. Subversion-branch: /branches/opl-branch Subversion-revision: 1645 --- src/i_oplmusic.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 7a619c5d..d844fd7b 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -656,8 +656,10 @@ static unsigned int FrequencyForNote(unsigned int note) static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) { + genmidi_instr_t *instrument; opl_voice_t *voice; opl_channel_data_t *channel; + unsigned int note; printf("note on: channel %i, %i, %i\n", event->data.channel.channel, @@ -667,6 +669,23 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) // The channel. channel = &track->channels[event->data.channel.channel]; + note = event->data.channel.param1; + + // Percussion channel (10) is treated differently to normal notes. + + if (event->data.channel.channel == 9) + { + if (note < 35 || note > 81) + { + return; + } + + instrument = &percussion_instrs[note - 35]; + } + else + { + instrument = channel->instrument; + } // Find a voice to use for this new note. @@ -679,19 +698,17 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) // Program the voice with the instrument data: - SetVoiceInstrument(voice, &channel->instrument->opl2_voice); + SetVoiceInstrument(voice, &instrument->opl2_voice); // TODO: Set the volume level. WriteRegister(OPL_REGS_LEVEL + voice->op2, volume_mapping_table[channel->volume]); - printf("volume = %i\n", channel->volume); - // Play the note. voice->channel = channel; - voice->note = event->data.channel.param1; + voice->note = note; // Write the frequency value to turn the note on. @@ -840,7 +857,7 @@ static void StartTrack(midi_file_t *file, unsigned int track_num) // Default is 120 bpm. // TODO: this is wrong - track->us_per_beat = 500 * 1000 * 200; + track->us_per_beat = 500 * 1000 * 260; for (i=0; idata.channel.channel, @@ -670,6 +671,7 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) channel = &track->channels[event->data.channel.channel]; note = event->data.channel.param1; + volume = event->data.channel.param2; // Percussion channel (10) is treated differently to normal notes. @@ -703,7 +705,7 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) // TODO: Set the volume level. WriteRegister(OPL_REGS_LEVEL + voice->op2, - volume_mapping_table[channel->volume]); + volume_mapping_table[volume]); // Play the note. -- cgit v1.2.3 From ab14960e3a24208709270fb11476f0bbb629b145 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 30 Aug 2009 23:04:54 +0000 Subject: Make I_OPL_StopSong work. Subversion-branch: /branches/opl-branch Subversion-revision: 1647 --- src/i_oplmusic.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index db7e0ffc..eff3f942 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -598,6 +598,11 @@ static opl_voice_t *FindVoiceForNote(opl_channel_data_t *channel, int note) return NULL; } +static void VoiceNoteOff(opl_voice_t *voice) +{ + WriteRegister(OPL_REGS_FREQ_2 + voice->index, voice->freq >> 8); +} + static void NoteOffEvent(opl_track_data_t *track, midi_event_t *event) { opl_voice_t *voice; @@ -619,9 +624,7 @@ static void NoteOffEvent(opl_track_data_t *track, midi_event_t *event) return; } - // Note off. - - WriteRegister(OPL_REGS_FREQ_2 + voice->index, voice->freq >> 8); + VoiceNoteOff(voice); // Finished with this voice now. @@ -913,10 +916,27 @@ static void I_OPL_ResumeSong(void) static void I_OPL_StopSong(void) { + unsigned int i; + if (!music_initialised) { return; } + + // Stop all playback. + + OPL_ClearCallbacks(); + + // Free all voices. + + for (i=0; iiter); - us = (nticks * track->us_per_beat) / track->ticks_per_beat; - total += us; + ms = (nticks * track->ms_per_beat) / track->ticks_per_beat; + total += ms; // Set a timer to be invoked when the next event is // ready to play. - OPL_SetCallback(us / 1000, TrackTimerCallback, track); + OPL_SetCallback(ms, TrackTimerCallback, track); } // Initialise a channel. @@ -862,7 +862,7 @@ static void StartTrack(midi_file_t *file, unsigned int track_num) // Default is 120 bpm. // TODO: this is wrong - track->us_per_beat = 500 * 1000 * 260; + track->ms_per_beat = 500 * 260; for (i=0; ichannel = channel; + voice->note = note; + // Program the voice with the instrument data: SetVoiceInstrument(voice, &instrument->opl2_voice); @@ -710,14 +713,16 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) WriteRegister(OPL_REGS_LEVEL + voice->op2, volume_mapping_table[volume]); - // Play the note. + // Fixed pitch? - voice->channel = channel; - voice->note = note; + if ((instrument->flags & GENMIDI_FLAG_FIXED) != 0) + { + note = instrument->fixed_note; + } // Write the frequency value to turn the note on. - voice->freq = FrequencyForNote(voice->note); + voice->freq = FrequencyForNote(note); WriteRegister(OPL_REGS_FREQ_1 + voice->index, voice->freq & 0xff); WriteRegister(OPL_REGS_FREQ_2 + voice->index, (voice->freq >> 8) | 0x20); -- cgit v1.2.3 From a43e60ee11f11fa8140b7b22da70f06b598fc49d Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Mon, 31 Aug 2009 17:32:22 +0000 Subject: Make channel volume work. Subversion-branch: /branches/opl-branch Subversion-revision: 1650 --- src/i_oplmusic.c | 108 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 86 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 8a416f57..1d482595 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -133,6 +133,12 @@ struct opl_voice_s // The frequency value being used. unsigned int freq; + // The volume of the note being played on this channel. + unsigned int note_volume; + + // The current volume that has been set for this channel. + unsigned int volume; + // Next in freelist opl_voice_t *next; }; @@ -163,22 +169,22 @@ static const unsigned int note_frequencies[] = { // Mapping from MIDI volume level to OPL level value. static const unsigned int volume_mapping_table[] = { - 0x3f, 0x3f, 0x3e, 0x3d, 0x3c, 0x3b, 0x3a, 0x3a, - 0x39, 0x38, 0x37, 0x37, 0x36, 0x35, 0x34, 0x34, - 0x33, 0x32, 0x32, 0x31, 0x30, 0x2f, 0x2f, 0x2e, - 0x2d, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, - 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x22, 0x21, - 0x20, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, - 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18, 0x17, - 0x17, 0x16, 0x16, 0x16, 0x16, 0x15, 0x15, 0x14, - 0x14, 0x13, 0x13, 0x12, 0x12, 0x12, 0x11, 0x11, - 0x10, 0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, - 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0b, - 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, - 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, - 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x04, - 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, - 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, + 0, 1, 3, 5, 6, 8, 10, 11, + 13, 14, 16, 17, 19, 20, 22, 23, + 25, 26, 27, 29, 30, 32, 33, 34, + 36, 37, 39, 41, 43, 45, 47, 49, + 50, 52, 54, 55, 57, 59, 60, 61, + 63, 64, 66, 67, 68, 69, 71, 72, + 73, 74, 75, 76, 77, 79, 80, 81, + 82, 83, 84, 84, 85, 86, 87, 88, + 89, 90, 91, 92, 92, 93, 94, 95, + 96, 96, 97, 98, 99, 99, 100, 101, + 101, 102, 103, 103, 104, 105, 105, 106, + 107, 107, 108, 109, 109, 110, 110, 111, + 112, 112, 113, 113, 114, 114, 115, 115, + 116, 117, 117, 118, 118, 119, 119, 120, + 120, 121, 121, 122, 122, 123, 123, 123, + 124, 124, 125, 125, 126, 126, 127, 127 }; static boolean music_initialised = false; @@ -433,8 +439,13 @@ static void LoadOperatorData(int operator, genmidi_op_t *data, // Set the instrument for a particular voice. -static void SetVoiceInstrument(opl_voice_t *voice, genmidi_voice_t *data) +static void SetVoiceInstrument(opl_voice_t *voice, genmidi_instr_t *instr) { + genmidi_voice_t *data; + + voice->current_instr = instr; + data = &instr->opl2_voice; + // Doom loads the second operator first, then the first. LoadOperatorData(voice->op2, &data->carrier, true); @@ -446,6 +457,42 @@ static void SetVoiceInstrument(opl_voice_t *voice, genmidi_voice_t *data) WriteRegister(OPL_REGS_FEEDBACK + voice->index, data->feedback | 0x30); + + // Hack to force a volume update. + + voice->volume = 999; +} + +static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume) +{ + unsigned int full_volume; + unsigned int instr_volume; + unsigned int reg_volume; + + voice->note_volume = volume; + + // Multiply note volume and channel volume to get the actual volume. + + full_volume = (voice->note_volume * voice->channel->volume) / 127; + + // The volume of each instrument can be controlled via GENMIDI: + + instr_volume = 0x3f - voice->current_instr->opl2_voice.carrier.level; + + // The volume value to use in the register: + + reg_volume = ((instr_volume * volume_mapping_table[full_volume]) / 128); + reg_volume = (0x3f - reg_volume) + | voice->current_instr->opl2_voice.carrier.scale; + + // Update the register, if necessary: + + if (voice->volume != reg_volume) + { + voice->volume = reg_volume; + + WriteRegister(OPL_REGS_LEVEL + voice->op2, reg_volume); + } } // Initialise the voice table and freelist @@ -698,6 +745,7 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) if (voice == NULL) { + printf("\tno free voice\n"); return; } @@ -706,12 +754,11 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) // Program the voice with the instrument data: - SetVoiceInstrument(voice, &instrument->opl2_voice); + SetVoiceInstrument(voice, instrument); - // TODO: Set the volume level. + // Set the volume level. - WriteRegister(OPL_REGS_LEVEL + voice->op2, - volume_mapping_table[volume]); + SetVoiceVolume(voice, volume); // Fixed pitch? @@ -743,6 +790,23 @@ static void ProgramChangeEvent(opl_track_data_t *track, midi_event_t *event) // channel, and change the instrument. } +static void SetChannelVolume(opl_channel_data_t *channel, unsigned int volume) +{ + unsigned int i; + + channel->volume = volume; + + // Update all voices that this channel is using. + + for (i=0; ivolume = param; + SetChannelVolume(channel, param); break; case MIDI_CONTROLLER_PAN: -- cgit v1.2.3 From a26925cf8f27eb38b3266629a3bc259f098b1e19 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Mon, 31 Aug 2009 18:18:51 +0000 Subject: Set the volume on both operators for instruments that use non-modulating voice mode. Subversion-branch: /branches/opl-branch Subversion-revision: 1651 --- src/i_oplmusic.c | 70 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 1d482595..51a609d9 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -137,7 +137,8 @@ struct opl_voice_s unsigned int note_volume; // The current volume that has been set for this channel. - unsigned int volume; + unsigned int carrier_volume; + unsigned int modulator_volume; // Next in freelist opl_voice_t *next; @@ -442,14 +443,22 @@ static void LoadOperatorData(int operator, genmidi_op_t *data, static void SetVoiceInstrument(opl_voice_t *voice, genmidi_instr_t *instr) { genmidi_voice_t *data; + unsigned int modulating; voice->current_instr = instr; data = &instr->opl2_voice; + // Are we usind modulated feedback mode? + + modulating = (data->feedback & 0x01) == 0; + // Doom loads the second operator first, then the first. + // The carrier is set to minimum volume until the voice volume + // is set in SetVoiceVolume (below). If we are not using + // modulating mode, we must set both to minimum volume. LoadOperatorData(voice->op2, &data->carrier, true); - LoadOperatorData(voice->op1, &data->modulator, false); + LoadOperatorData(voice->op1, &data->modulator, !modulating); // Set feedback register that control the connection between the // two operators. Turn on bits in the upper nybble; I think this @@ -460,38 +469,61 @@ static void SetVoiceInstrument(opl_voice_t *voice, genmidi_instr_t *instr) // Hack to force a volume update. - voice->volume = 999; + voice->carrier_volume = 999; + voice->modulator_volume = 999; } -static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume) +// Calculate the volume level to use for a given operator. + +static void SetOperatorVolume(genmidi_op_t *op, unsigned int volume, + unsigned int opnum, + unsigned int *current_volume) { - unsigned int full_volume; - unsigned int instr_volume; + unsigned int op_volume; unsigned int reg_volume; - voice->note_volume = volume; - - // Multiply note volume and channel volume to get the actual volume. - - full_volume = (voice->note_volume * voice->channel->volume) / 127; - // The volume of each instrument can be controlled via GENMIDI: - instr_volume = 0x3f - voice->current_instr->opl2_voice.carrier.level; + op_volume = 0x3f - op->level; // The volume value to use in the register: - reg_volume = ((instr_volume * volume_mapping_table[full_volume]) / 128); - reg_volume = (0x3f - reg_volume) - | voice->current_instr->opl2_voice.carrier.scale; + reg_volume = ((op_volume * volume_mapping_table[volume]) / 128); + reg_volume = (0x3f - reg_volume) | op->scale; // Update the register, if necessary: - if (voice->volume != reg_volume) + if (*current_volume != reg_volume) { - voice->volume = reg_volume; + *current_volume = reg_volume; + + WriteRegister(OPL_REGS_LEVEL + opnum, reg_volume); + } +} - WriteRegister(OPL_REGS_LEVEL + voice->op2, reg_volume); +static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume) +{ + genmidi_voice_t *opl_voice; + unsigned int full_volume; + + voice->note_volume = volume; + + opl_voice = &voice->current_instr->opl2_voice; + + // Multiply note volume and channel volume to get the actual volume. + + full_volume = (voice->note_volume * voice->channel->volume) / 127; + + SetOperatorVolume(&opl_voice->carrier, full_volume, + voice->op2, &voice->carrier_volume); + + // If we are using non-modulated feedback mode, we must set the + // volume for both voices. + + if ((opl_voice->feedback & 0x01) != 0) + { + SetOperatorVolume(&opl_voice->modulator, full_volume, + voice->op1, &voice->modulator_volume); } } -- cgit v1.2.3 From fe96ae292d2b0e67020d77493ae366360cd93ec8 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Mon, 31 Aug 2009 18:33:36 +0000 Subject: Dont program an instrument if it is already set. Subversion-branch: /branches/opl-branch Subversion-revision: 1652 --- src/i_oplmusic.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 51a609d9..ab64505c 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -445,6 +445,13 @@ static void SetVoiceInstrument(opl_voice_t *voice, genmidi_instr_t *instr) genmidi_voice_t *data; unsigned int modulating; + // Instrument already set for this channel? + + if (voice->current_instr == instr) + { + return; + } + voice->current_instr = instr; data = &instr->opl2_voice; -- cgit v1.2.3 From eb2291030ae0f1e005a6014193fdfeaa796a913a Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Mon, 31 Aug 2009 22:50:28 +0000 Subject: Perform volume mapping on note and channel volumes before multiplying them. This gives voice volume values that are almost identical to Doom's. Subversion-branch: /branches/opl-branch Subversion-revision: 1653 --- src/i_oplmusic.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index ab64505c..74a76b00 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -495,7 +495,7 @@ static void SetOperatorVolume(genmidi_op_t *op, unsigned int volume, // The volume value to use in the register: - reg_volume = ((op_volume * volume_mapping_table[volume]) / 128); + reg_volume = (op_volume * volume) / 128; reg_volume = (0x3f - reg_volume) | op->scale; // Update the register, if necessary: @@ -519,7 +519,8 @@ static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume) // Multiply note volume and channel volume to get the actual volume. - full_volume = (voice->note_volume * voice->channel->volume) / 127; + full_volume = (volume_mapping_table[voice->note_volume] + * volume_mapping_table[voice->channel->volume]) / 127; SetOperatorVolume(&opl_voice->carrier, full_volume, voice->op2, &voice->carrier_volume); -- cgit v1.2.3 From 556f7291ea0199144794166af2757aa7ad832a7a Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Tue, 1 Sep 2009 18:17:11 +0000 Subject: Loop songs (when appropriate) Subversion-branch: /branches/opl-branch Subversion-revision: 1654 --- src/i_oplmusic.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/midifile.c | 10 ++++++ src/midifile.h | 8 +++++ 3 files changed, 108 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 74a76b00..1ac502f5 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -206,6 +206,9 @@ static opl_voice_t *voice_free_list; // Track data for playing tracks: static opl_track_data_t *tracks; +static unsigned int num_tracks; +static unsigned int running_tracks = 0; +static boolean song_looping; // In the initialisation stage, register writes are spaced by reading // from the register port (0). After initialisation, spacing is @@ -868,11 +871,40 @@ static void ControllerEvent(opl_track_data_t *track, midi_event_t *event) SetChannelVolume(channel, param); break; - case MIDI_CONTROLLER_PAN: + default: + fprintf(stderr, "Unknown MIDI controller type: %i\n", controller); + break; + } +} + +// Process a meta event. + +static void MetaEvent(opl_track_data_t *track, midi_event_t *event) +{ + switch (event->data.meta.type) + { + // Things we can just ignore. + + case MIDI_META_SEQUENCE_NUMBER: + case MIDI_META_TEXT: + case MIDI_META_COPYRIGHT: + case MIDI_META_TRACK_NAME: + case MIDI_META_INSTR_NAME: + case MIDI_META_LYRICS: + case MIDI_META_MARKER: + case MIDI_META_CUE_POINT: + case MIDI_META_SEQUENCER_SPECIFIC: + break; + + // End of track - actually handled when we run out of events + // in the track, see below. + + case MIDI_META_END_OF_TRACK: break; default: - fprintf(stderr, "Unknown MIDI controller type: %i\n", controller); + fprintf(stderr, "Unknown MIDI meta event type: %i\n", + event->data.meta.type); break; } } @@ -899,6 +931,16 @@ static void ProcessEvent(opl_track_data_t *track, midi_event_t *event) ProgramChangeEvent(track, event); break; + case MIDI_EVENT_META: + MetaEvent(track, event); + break; + + // SysEx events can be ignored. + + case MIDI_EVENT_SYSEX: + case MIDI_EVENT_SYSEX_SPLIT: + break; + default: fprintf(stderr, "Unknown MIDI event type %i\n", event->event_type); break; @@ -907,6 +949,21 @@ static void ProcessEvent(opl_track_data_t *track, midi_event_t *event) static void ScheduleTrack(opl_track_data_t *track); +// Restart a song from the beginning. + +static void RestartSong(void) +{ + unsigned int i; + + running_tracks = num_tracks; + + for (i=0; ievent_type == MIDI_EVENT_META + && event->data.meta.type == MIDI_META_END_OF_TRACK) + { + --running_tracks; + + // When all tracks have finished, restart the song. + + if (running_tracks <= 0 && song_looping) + { + RestartSong(); + } + + return; + } + // Reschedule the callback for the next event in the track. ScheduleTrack(track); @@ -1001,7 +1075,11 @@ static void I_OPL_PlaySong(void *handle, int looping) tracks = malloc(MIDI_NumTracks(file) * sizeof(opl_track_data_t)); - for (i=0; iheader.time_division; } +void MIDI_RestartIterator(midi_track_iter_t *iter) +{ + iter->position = 0; +} + #ifdef TEST static char *MIDI_EventTypeToString(midi_event_type_t event_type) diff --git a/src/midifile.h b/src/midifile.h index faef549c..4ee0ddb2 100644 --- a/src/midifile.h +++ b/src/midifile.h @@ -155,6 +155,10 @@ unsigned int MIDI_NumTracks(midi_file_t *file); midi_track_iter_t *MIDI_IterateTrack(midi_file_t *file, unsigned int track_num); +// Free an iterator. + +void MIDI_FreeIterator(midi_track_iter_t *iter); + // Get the time until the next MIDI event in a track. unsigned int MIDI_GetDeltaTime(midi_track_iter_t *iter); @@ -163,5 +167,9 @@ unsigned int MIDI_GetDeltaTime(midi_track_iter_t *iter); int MIDI_GetNextEvent(midi_track_iter_t *iter, midi_event_t **event); +// Reset an iterator to the beginning of a track. + +void MIDI_RestartIterator(midi_track_iter_t *iter); + #endif /* #ifndef MIDIFILE_H */ -- cgit v1.2.3 From d372d65b3de579fe40207a308c3f7c4bf3076c0a Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Mon, 7 Sep 2009 18:11:25 +0000 Subject: Implement pitch bend. Subversion-branch: /branches/opl-branch Subversion-revision: 1658 --- src/i_oplmusic.c | 262 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 213 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 1ac502f5..e9fdb395 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -91,6 +91,11 @@ typedef struct // Volume level int volume; + + // Pitch bend value: + + int bend; + } opl_channel_data_t; // Data associated with a track that is currently playing. @@ -127,7 +132,12 @@ struct opl_voice_s // The channel currently using this voice. opl_channel_data_t *channel; - // The note that this voice is playing. + // The midi key that this voice is playing. + unsigned int key; + + // The note being played. This is normally the same as + // the key, but if the instrument is a fixed pitch + // instrument, it is different. unsigned int note; // The frequency value being used. @@ -153,18 +163,120 @@ static const int voice_operators[2][OPL_NUM_VOICES] = { // Frequency values to use for each note. -static const unsigned int note_frequencies[] = { - - // These frequencies are only used for the first seven - // MIDI note values: - - 0x158, 0x16d, 0x183, 0x19a, 0x1b2, 0x1cc, 0x1e7, - - // These frequencies are used repeatedly, cycling around - // for each octave: - - 0x204, 0x223, 0x244, 0x266, 0x28b, 0x2b1, - 0x2da, 0x306, 0x334, 0x365, 0x398, 0x3cf, +static const unsigned short frequency_curve[] = { + + 0x133, 0x133, 0x134, 0x134, 0x135, 0x136, 0x136, 0x137, // -1 + 0x137, 0x138, 0x138, 0x139, 0x139, 0x13a, 0x13b, 0x13b, + 0x13c, 0x13c, 0x13d, 0x13d, 0x13e, 0x13f, 0x13f, 0x140, + 0x140, 0x141, 0x142, 0x142, 0x143, 0x143, 0x144, 0x144, + + 0x145, 0x146, 0x146, 0x147, 0x147, 0x148, 0x149, 0x149, // -2 + 0x14a, 0x14a, 0x14b, 0x14c, 0x14c, 0x14d, 0x14d, 0x14e, + 0x14f, 0x14f, 0x150, 0x150, 0x151, 0x152, 0x152, 0x153, + 0x153, 0x154, 0x155, 0x155, 0x156, 0x157, 0x157, 0x158, + + // These are used for the first seven MIDI note values: + + 0x158, 0x159, 0x15a, 0x15a, 0x15b, 0x15b, 0x15c, 0x15d, // 0 + 0x15d, 0x15e, 0x15f, 0x15f, 0x160, 0x161, 0x161, 0x162, + 0x162, 0x163, 0x164, 0x164, 0x165, 0x166, 0x166, 0x167, + 0x168, 0x168, 0x169, 0x16a, 0x16a, 0x16b, 0x16c, 0x16c, + + 0x16d, 0x16e, 0x16e, 0x16f, 0x170, 0x170, 0x171, 0x172, // 1 + 0x172, 0x173, 0x174, 0x174, 0x175, 0x176, 0x176, 0x177, + 0x178, 0x178, 0x179, 0x17a, 0x17a, 0x17b, 0x17c, 0x17c, + 0x17d, 0x17e, 0x17e, 0x17f, 0x180, 0x181, 0x181, 0x182, + + 0x183, 0x183, 0x184, 0x185, 0x185, 0x186, 0x187, 0x188, // 2 + 0x188, 0x189, 0x18a, 0x18a, 0x18b, 0x18c, 0x18d, 0x18d, + 0x18e, 0x18f, 0x18f, 0x190, 0x191, 0x192, 0x192, 0x193, + 0x194, 0x194, 0x195, 0x196, 0x197, 0x197, 0x198, 0x199, + + 0x19a, 0x19a, 0x19b, 0x19c, 0x19d, 0x19d, 0x19e, 0x19f, // 3 + 0x1a0, 0x1a0, 0x1a1, 0x1a2, 0x1a3, 0x1a3, 0x1a4, 0x1a5, + 0x1a6, 0x1a6, 0x1a7, 0x1a8, 0x1a9, 0x1a9, 0x1aa, 0x1ab, + 0x1ac, 0x1ad, 0x1ad, 0x1ae, 0x1af, 0x1b0, 0x1b0, 0x1b1, + + 0x1b2, 0x1b3, 0x1b4, 0x1b4, 0x1b5, 0x1b6, 0x1b7, 0x1b8, // 4 + 0x1b8, 0x1b9, 0x1ba, 0x1bb, 0x1bc, 0x1bc, 0x1bd, 0x1be, + 0x1bf, 0x1c0, 0x1c0, 0x1c1, 0x1c2, 0x1c3, 0x1c4, 0x1c4, + 0x1c5, 0x1c6, 0x1c7, 0x1c8, 0x1c9, 0x1c9, 0x1ca, 0x1cb, + + 0x1cc, 0x1cd, 0x1ce, 0x1ce, 0x1cf, 0x1d0, 0x1d1, 0x1d2, // 5 + 0x1d3, 0x1d3, 0x1d4, 0x1d5, 0x1d6, 0x1d7, 0x1d8, 0x1d8, + 0x1d9, 0x1da, 0x1db, 0x1dc, 0x1dd, 0x1de, 0x1de, 0x1df, + 0x1e0, 0x1e1, 0x1e2, 0x1e3, 0x1e4, 0x1e5, 0x1e5, 0x1e6, + + 0x1e7, 0x1e8, 0x1e9, 0x1ea, 0x1eb, 0x1ec, 0x1ed, 0x1ed, // 6 + 0x1ee, 0x1ef, 0x1f0, 0x1f1, 0x1f2, 0x1f3, 0x1f4, 0x1f5, + 0x1f6, 0x1f6, 0x1f7, 0x1f8, 0x1f9, 0x1fa, 0x1fb, 0x1fc, + 0x1fd, 0x1fe, 0x1ff, 0x200, 0x201, 0x201, 0x202, 0x203, + + // First note of looped range used for all octaves: + + 0x204, 0x205, 0x206, 0x207, 0x208, 0x209, 0x20a, 0x20b, // 7 + 0x20c, 0x20d, 0x20e, 0x20f, 0x210, 0x210, 0x211, 0x212, + 0x213, 0x214, 0x215, 0x216, 0x217, 0x218, 0x219, 0x21a, + 0x21b, 0x21c, 0x21d, 0x21e, 0x21f, 0x220, 0x221, 0x222, + + 0x223, 0x224, 0x225, 0x226, 0x227, 0x228, 0x229, 0x22a, // 8 + 0x22b, 0x22c, 0x22d, 0x22e, 0x22f, 0x230, 0x231, 0x232, + 0x233, 0x234, 0x235, 0x236, 0x237, 0x238, 0x239, 0x23a, + 0x23b, 0x23c, 0x23d, 0x23e, 0x23f, 0x240, 0x241, 0x242, + + 0x244, 0x245, 0x246, 0x247, 0x248, 0x249, 0x24a, 0x24b, // 9 + 0x24c, 0x24d, 0x24e, 0x24f, 0x250, 0x251, 0x252, 0x253, + 0x254, 0x256, 0x257, 0x258, 0x259, 0x25a, 0x25b, 0x25c, + 0x25d, 0x25e, 0x25f, 0x260, 0x262, 0x263, 0x264, 0x265, + + 0x266, 0x267, 0x268, 0x269, 0x26a, 0x26c, 0x26d, 0x26e, // 10 + 0x26f, 0x270, 0x271, 0x272, 0x273, 0x275, 0x276, 0x277, + 0x278, 0x279, 0x27a, 0x27b, 0x27d, 0x27e, 0x27f, 0x280, + 0x281, 0x282, 0x284, 0x285, 0x286, 0x287, 0x288, 0x289, + + 0x28b, 0x28c, 0x28d, 0x28e, 0x28f, 0x290, 0x292, 0x293, // 11 + 0x294, 0x295, 0x296, 0x298, 0x299, 0x29a, 0x29b, 0x29c, + 0x29e, 0x29f, 0x2a0, 0x2a1, 0x2a2, 0x2a4, 0x2a5, 0x2a6, + 0x2a7, 0x2a9, 0x2aa, 0x2ab, 0x2ac, 0x2ae, 0x2af, 0x2b0, + + 0x2b1, 0x2b2, 0x2b4, 0x2b5, 0x2b6, 0x2b7, 0x2b9, 0x2ba, // 12 + 0x2bb, 0x2bd, 0x2be, 0x2bf, 0x2c0, 0x2c2, 0x2c3, 0x2c4, + 0x2c5, 0x2c7, 0x2c8, 0x2c9, 0x2cb, 0x2cc, 0x2cd, 0x2ce, + 0x2d0, 0x2d1, 0x2d2, 0x2d4, 0x2d5, 0x2d6, 0x2d8, 0x2d9, + + 0x2da, 0x2dc, 0x2dd, 0x2de, 0x2e0, 0x2e1, 0x2e2, 0x2e4, // 13 + 0x2e5, 0x2e6, 0x2e8, 0x2e9, 0x2ea, 0x2ec, 0x2ed, 0x2ee, + 0x2f0, 0x2f1, 0x2f2, 0x2f4, 0x2f5, 0x2f6, 0x2f8, 0x2f9, + 0x2fb, 0x2fc, 0x2fd, 0x2ff, 0x300, 0x302, 0x303, 0x304, + + 0x306, 0x307, 0x309, 0x30a, 0x30b, 0x30d, 0x30e, 0x310, // 14 + 0x311, 0x312, 0x314, 0x315, 0x317, 0x318, 0x31a, 0x31b, + 0x31c, 0x31e, 0x31f, 0x321, 0x322, 0x324, 0x325, 0x327, + 0x328, 0x329, 0x32b, 0x32c, 0x32e, 0x32f, 0x331, 0x332, + + 0x334, 0x335, 0x337, 0x338, 0x33a, 0x33b, 0x33d, 0x33e, // 15 + 0x340, 0x341, 0x343, 0x344, 0x346, 0x347, 0x349, 0x34a, + 0x34c, 0x34d, 0x34f, 0x350, 0x352, 0x353, 0x355, 0x357, + 0x358, 0x35a, 0x35b, 0x35d, 0x35e, 0x360, 0x361, 0x363, + + 0x365, 0x366, 0x368, 0x369, 0x36b, 0x36c, 0x36e, 0x370, // 16 + 0x371, 0x373, 0x374, 0x376, 0x378, 0x379, 0x37b, 0x37c, + 0x37e, 0x380, 0x381, 0x383, 0x384, 0x386, 0x388, 0x389, + 0x38b, 0x38d, 0x38e, 0x390, 0x392, 0x393, 0x395, 0x397, + + 0x398, 0x39a, 0x39c, 0x39d, 0x39f, 0x3a1, 0x3a2, 0x3a4, // 17 + 0x3a6, 0x3a7, 0x3a9, 0x3ab, 0x3ac, 0x3ae, 0x3b0, 0x3b1, + 0x3b3, 0x3b5, 0x3b7, 0x3b8, 0x3ba, 0x3bc, 0x3bd, 0x3bf, + 0x3c1, 0x3c3, 0x3c4, 0x3c6, 0x3c8, 0x3ca, 0x3cb, 0x3cd, + + // The last note has an incomplete range, and loops round back to + // the start. Note that the last value is actually a buffer overrun + // and does not fit with the other values. + + 0x3cf, 0x3d1, 0x3d2, 0x3d4, 0x3d6, 0x3d8, 0x3da, 0x3db, // 18 + 0x3dd, 0x3df, 0x3e1, 0x3e3, 0x3e4, 0x3e6, 0x3e8, 0x3ea, + 0x3ec, 0x3ed, 0x3ef, 0x3f1, 0x3f3, 0x3f5, 0x3f6, 0x3f8, + 0x3fa, 0x3fc, 0x3fe, 0x36c, }; // Mapping from MIDI volume level to OPL level value. @@ -501,7 +613,7 @@ static void SetOperatorVolume(genmidi_op_t *op, unsigned int volume, reg_volume = (op_volume * volume) / 128; reg_volume = (0x3f - reg_volume) | op->scale; - // Update the register, if necessary: + // Update the register, if neccessary: if (*current_volume != reg_volume) { @@ -673,13 +785,13 @@ static void I_OPL_SetMusicVolume(int volume) current_music_volume = volume; } -static opl_voice_t *FindVoiceForNote(opl_channel_data_t *channel, int note) +static opl_voice_t *FindVoiceForKey(opl_channel_data_t *channel, int key) { unsigned int i; for (i=0; iindex, voice->freq >> 8); } +// Get the frequency that we should be using for a voice. + static void NoteOffEvent(opl_track_data_t *track, midi_event_t *event) { opl_voice_t *voice; @@ -707,7 +821,7 @@ static void NoteOffEvent(opl_track_data_t *track, midi_event_t *event) // Find the voice being used to play the note. - voice = FindVoiceForNote(channel, event->data.channel.param1); + voice = FindVoiceForKey(channel, event->data.channel.param1); if (voice == NULL) { @@ -721,29 +835,47 @@ static void NoteOffEvent(opl_track_data_t *track, midi_event_t *event) ReleaseVoice(voice); } -// Given a MIDI note number, get the corresponding OPL -// frequency value to use. - -static unsigned int FrequencyForNote(unsigned int note) +static unsigned int FrequencyForVoice(opl_voice_t *voice) { + unsigned int freq_index; unsigned int octave; - unsigned int key_num; + unsigned int sub_index; - // The first seven frequencies in the frequencies array are used - // only for the first seven MIDI notes. After this, the frequency - // value loops around the same twelve notes, increasing the - // octave. + freq_index = 64 + 32 * voice->note + voice->channel->bend; - if (note < 7) + // The first 7 notes use the start of the table, while + // consecutive notes loop around the latter part. + + if (freq_index < 284) { - return note_frequencies[note]; + return frequency_curve[freq_index]; } - else + + sub_index = (freq_index - 284) % (12 * 32); + octave = (freq_index - 284) / (12 * 32); + + // Calculate the resulting register value to use for the frequency. + + return frequency_curve[sub_index + 284] | (octave << 10); +} + +// Update the frequency that a voice is programmed to use. + +static void UpdateVoiceFrequency(opl_voice_t *voice) +{ + unsigned int freq; + + // Calculate the frequency to use for this voice and update it + // if neccessary. + + freq = FrequencyForVoice(voice); + + if (voice->freq != freq) { - octave = (note - 7) / 12; - key_num = (note - 7) % 12; + WriteRegister(OPL_REGS_FREQ_1 + voice->index, freq & 0xff); + WriteRegister(OPL_REGS_FREQ_2 + voice->index, (freq >> 8) | 0x20); - return note_frequencies[key_num + 7] | (octave << 10); + voice->freq = freq; } } @@ -752,7 +884,7 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) genmidi_instr_t *instrument; opl_voice_t *voice; opl_channel_data_t *channel; - unsigned int note; + unsigned int key; unsigned int volume; printf("note on: channel %i, %i, %i\n", @@ -763,19 +895,19 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) // The channel. channel = &track->channels[event->data.channel.channel]; - note = event->data.channel.param1; + key = event->data.channel.param1; volume = event->data.channel.param2; - // Percussion channel (10) is treated differently to normal notes. + // Percussion channel (10) is treated differently. if (event->data.channel.channel == 9) { - if (note < 35 || note > 81) + if (key < 35 || key > 81) { return; } - instrument = &percussion_instrs[note - 35]; + instrument = &percussion_instrs[key - 35]; } else { @@ -793,7 +925,19 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) } voice->channel = channel; - voice->note = note; + voice->key = key; + + // Work out the note to use. This is normally the same as + // the key, unless it is a fixed pitch instrument. + + if ((instrument->flags & GENMIDI_FLAG_FIXED) != 0) + { + voice->note = instrument->fixed_note; + } + else + { + voice->note = key; + } // Program the voice with the instrument data: @@ -803,19 +947,10 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) SetVoiceVolume(voice, volume); - // Fixed pitch? - - if ((instrument->flags & GENMIDI_FLAG_FIXED) != 0) - { - note = instrument->fixed_note; - } - // Write the frequency value to turn the note on. - voice->freq = FrequencyForNote(note); - - WriteRegister(OPL_REGS_FREQ_1 + voice->index, voice->freq & 0xff); - WriteRegister(OPL_REGS_FREQ_2 + voice->index, (voice->freq >> 8) | 0x20); + voice->freq = 0; + UpdateVoiceFrequency(voice); } static void ProgramChangeEvent(opl_track_data_t *track, midi_event_t *event) @@ -877,6 +1012,30 @@ static void ControllerEvent(opl_track_data_t *track, midi_event_t *event) } } +// Process a pitch bend event. + +static void PitchBendEvent(opl_track_data_t *track, midi_event_t *event) +{ + opl_channel_data_t *channel; + unsigned int i; + + // Update the channel bend value. Only the MSB of the pitch bend + // value is considered: this is what Doom does. + + channel = &track->channels[event->data.channel.channel]; + channel->bend = event->data.channel.param2 - 64; + + // Update all voices for this channel. + + for (i=0; iinstrument = &main_instrs[0]; channel->volume = 127; + channel->bend = 0; } // Start a MIDI track playing: -- cgit v1.2.3 From e97bc5db814b844ef2e4ee92f8a683031853dadb Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Tue, 8 Sep 2009 17:56:21 +0000 Subject: Program two voices for double voice instruments. Subversion-branch: /branches/opl-branch Subversion-revision: 1660 --- src/i_oplmusic.c | 164 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 91 insertions(+), 73 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index e9fdb395..8ec47b80 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -49,7 +49,7 @@ #define GENMIDI_HEADER "#OPL_II#" #define GENMIDI_FLAG_FIXED 0x0001 /* fixed pitch */ -#define GENMIDI_FLAG_2VOICE 0x0002 /* double voice (OPL3) */ +#define GENMIDI_FLAG_2VOICE 0x0004 /* double voice (OPL3) */ typedef struct { @@ -76,8 +76,7 @@ typedef struct byte fine_tuning; byte fixed_note; - genmidi_voice_t opl2_voice; - genmidi_voice_t opl3_voice; + genmidi_voice_t voices[2]; } PACKEDATTR genmidi_instr_t; // Data associated with a channel of a track that is currently playing. @@ -129,6 +128,11 @@ struct opl_voice_s // Currently-loaded instrument data genmidi_instr_t *current_instr; + // The voice number in the instrument to use. + // This is normally set to zero; if this is a double voice + // instrument, it may be one. + unsigned int current_instr_voice; + // The channel currently using this voice. opl_channel_data_t *channel; @@ -555,20 +559,25 @@ static void LoadOperatorData(int operator, genmidi_op_t *data, // Set the instrument for a particular voice. -static void SetVoiceInstrument(opl_voice_t *voice, genmidi_instr_t *instr) +static void SetVoiceInstrument(opl_voice_t *voice, + genmidi_instr_t *instr, + unsigned int instr_voice) { genmidi_voice_t *data; unsigned int modulating; // Instrument already set for this channel? - if (voice->current_instr == instr) + if (voice->current_instr == instr + && voice->current_instr_voice == instr_voice) { return; } voice->current_instr = instr; - data = &instr->opl2_voice; + voice->current_instr_voice = instr_voice; + + data = &instr->voices[instr_voice]; // Are we usind modulated feedback mode? @@ -630,7 +639,7 @@ static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume) voice->note_volume = volume; - opl_voice = &voice->current_instr->opl2_voice; + opl_voice = &voice->current_instr->voices[voice->current_instr_voice]; // Multiply note volume and channel volume to get the actual volume. @@ -765,7 +774,7 @@ static boolean I_OPL_InitMusic(void) voice = GetFreeVoice(); instr_num = rand() % 100; - SetVoiceInstrument(voice, &main_instrs[instr_num].opl2_voice); + SetVoiceInstrument(voice, &main_instrs[instr_num], 0); OPL_SetCallback(0, TestCallback, voice); } @@ -785,32 +794,18 @@ static void I_OPL_SetMusicVolume(int volume) current_music_volume = volume; } -static opl_voice_t *FindVoiceForKey(opl_channel_data_t *channel, int key) -{ - unsigned int i; - - for (i=0; iindex, voice->freq >> 8); } // Get the frequency that we should be using for a voice. -static void NoteOffEvent(opl_track_data_t *track, midi_event_t *event) +static void KeyOffEvent(opl_track_data_t *track, midi_event_t *event) { - opl_voice_t *voice; opl_channel_data_t *channel; + unsigned int key; + unsigned int i; printf("note off: channel %i, %i, %i\n", event->data.channel.channel, @@ -818,21 +813,22 @@ static void NoteOffEvent(opl_track_data_t *track, midi_event_t *event) event->data.channel.param2); channel = &track->channels[event->data.channel.channel]; + key = event->data.channel.param1; - // Find the voice being used to play the note. - - voice = FindVoiceForKey(channel, event->data.channel.param1); + // Turn off voices being used to play this key. + // If it is a double voice instrument there will be two. - if (voice == NULL) + for (i=0; idata.channel.channel, - event->data.channel.param1, - event->data.channel.param2); - - // The channel. - - channel = &track->channels[event->data.channel.channel]; - key = event->data.channel.param1; - volume = event->data.channel.param2; - - // Percussion channel (10) is treated differently. - - if (event->data.channel.channel == 9) - { - if (key < 35 || key > 81) - { - return; - } - - instrument = &percussion_instrs[key - 35]; - } - else - { - instrument = channel->instrument; - } // Find a voice to use for this new note. @@ -920,7 +893,7 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) if (voice == NULL) { - printf("\tno free voice\n"); + printf("\tno free voice for voice %i of instrument\n", instrument_voice); return; } @@ -941,7 +914,7 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) // Program the voice with the instrument data: - SetVoiceInstrument(voice, instrument); + SetVoiceInstrument(voice, instrument, 0); // Set the volume level. @@ -953,6 +926,51 @@ static void NoteOnEvent(opl_track_data_t *track, midi_event_t *event) UpdateVoiceFrequency(voice); } +static void KeyOnEvent(opl_track_data_t *track, midi_event_t *event) +{ + genmidi_instr_t *instrument; + opl_channel_data_t *channel; + unsigned int key; + unsigned int volume; + + printf("note on: channel %i, %i, %i\n", + event->data.channel.channel, + event->data.channel.param1, + event->data.channel.param2); + + // The channel. + + channel = &track->channels[event->data.channel.channel]; + key = event->data.channel.param1; + volume = event->data.channel.param2; + + // Percussion channel (10) is treated differently. + + if (event->data.channel.channel == 9) + { + if (key < 35 || key > 81) + { + return; + } + + instrument = &percussion_instrs[key - 35]; + } + else + { + instrument = channel->instrument; + } + + // Find and program a voice for this instrument. If this + // is a double voice instrument, we must do this twice. + + VoiceKeyOn(channel, instrument, 0, key, volume); + + if ((instrument->flags & GENMIDI_FLAG_2VOICE) != 0) + { + VoiceKeyOn(channel, instrument, 1, key, volume); + } +} + static void ProgramChangeEvent(opl_track_data_t *track, midi_event_t *event) { int channel; @@ -1075,11 +1093,11 @@ static void ProcessEvent(opl_track_data_t *track, midi_event_t *event) switch (event->event_type) { case MIDI_EVENT_NOTE_OFF: - NoteOffEvent(track, event); + KeyOffEvent(track, event); break; case MIDI_EVENT_NOTE_ON: - NoteOnEvent(track, event); + KeyOnEvent(track, event); break; case MIDI_EVENT_CONTROLLER: @@ -1284,7 +1302,7 @@ static void I_OPL_StopSong(void) { if (voices[i].channel != NULL) { - VoiceNoteOff(&voices[i]); + VoiceKeyOff(&voices[i]); ReleaseVoice(&voices[i]); } } -- cgit v1.2.3 From ba1accec2394c6ee70c5c79a42827099ef20e638 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Tue, 8 Sep 2009 18:12:10 +0000 Subject: Set the correct instrument voice, instead of using the first voice for both. Subversion-branch: /branches/opl-branch Subversion-revision: 1661 --- src/i_oplmusic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 8ec47b80..643f67c5 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -914,7 +914,7 @@ static void VoiceKeyOn(opl_channel_data_t *channel, // Program the voice with the instrument data: - SetVoiceInstrument(voice, instrument, 0); + SetVoiceInstrument(voice, instrument, instrument_voice); // Set the volume level. -- cgit v1.2.3 From cc0cc9fb0c806f6533dca794b1e829a626323d8b Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 11 Sep 2009 16:54:06 +0000 Subject: Implement the fine tuning field, based on research. Subversion-branch: /branches/opl-branch Subversion-revision: 1664 --- src/i_oplmusic.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 643f67c5..8cb833a2 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -839,6 +839,14 @@ static unsigned int FrequencyForVoice(opl_voice_t *voice) freq_index = 64 + 32 * voice->note + voice->channel->bend; + // If this is the second voice of a double voice instrument, the + // frequency index can be adjusted by the fine tuning field. + + if (voice->current_instr_voice != 0) + { + freq_index += (voice->current_instr->fine_tuning / 2) - 64; + } + // The first 7 notes use the start of the table, while // consecutive notes loop around the latter part. -- cgit v1.2.3 From a6a747e0ab96c45abe1a563f278f3ebc05fdec55 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 12 Sep 2009 17:22:58 +0000 Subject: Discard an existing voice when no voices are available (based on research with Vanilla). Subversion-branch: /branches/opl-branch Subversion-revision: 1668 --- src/i_oplmusic.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 110 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 8cb833a2..bed81109 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -154,7 +154,8 @@ struct opl_voice_s unsigned int carrier_volume; unsigned int modulator_volume; - // Next in freelist + // Next in linked list; a voice is always either in the + // free list or the allocated list. opl_voice_t *next; }; @@ -318,6 +319,7 @@ static genmidi_instr_t *percussion_instrs; static opl_voice_t voices[OPL_NUM_VOICES]; static opl_voice_t *voice_free_list; +static opl_voice_t *voice_alloced_list; // Track data for playing tracks: @@ -505,12 +507,42 @@ static opl_voice_t *GetFreeVoice(void) return NULL; } + // Remove from free list + result = voice_free_list; voice_free_list = voice_free_list->next; + // Add to allocated list + + result->next = voice_alloced_list; + voice_alloced_list = result; + return result; } +// Remove a voice from the allocated voices list. + +static void RemoveVoiceFromAllocedList(opl_voice_t *voice) +{ + opl_voice_t **rover; + + rover = &voice_alloced_list; + + // Search the list until we find the voice, then remove it. + + while (*rover != NULL) + { + if (*rover == voice) + { + *rover = voice->next; + voice->next = NULL; + break; + } + + rover = &(*rover)->next; + } +} + // Release a voice back to the freelist. static void ReleaseVoice(opl_voice_t *voice) @@ -520,6 +552,10 @@ static void ReleaseVoice(opl_voice_t *voice) voice->channel = NULL; voice->note = 0; + // Remove from alloced list. + + RemoveVoiceFromAllocedList(voice); + // Search to the end of the freelist (This is how Doom behaves!) rover = &voice_free_list; @@ -831,6 +867,66 @@ static void KeyOffEvent(opl_track_data_t *track, midi_event_t *event) } } +// When all voices are in use, we must discard an existing voice to +// play a new note. Find and free an existing voice. The channel +// passed to the function is the channel for the new note to be +// played. + +static opl_voice_t *ReplaceExistingVoice(opl_channel_data_t *channel) +{ + opl_voice_t *rover; + opl_voice_t *result; + + // Check the allocated voices, if we find an instrument that is + // of a lower priority to the new instrument, discard it. + // Priority is determined by MIDI instrument number; old + + result = NULL; + + for (rover = voice_alloced_list; rover != NULL; rover = rover->next) + { + if (rover->current_instr > channel->instrument) + { + result = rover; + break; + } + } + + // If we didn't find a voice, find an existing voice being used to + // play a note on the same channel, and use that. + + if (result == NULL) + { + for (rover = voice_alloced_list; rover != NULL; rover = rover->next) + { + if (rover->channel == channel) + { + result = rover; + break; + } + } + } + + // Still nothing found? Give up and just use the first voice in + // the list. + + if (result == NULL) + { + result = voice_alloced_list; + } + + // Stop playing this voice playing and release it back to the free + // list. + + VoiceKeyOff(result); + ReleaseVoice(result); + + // Re-allocate the voice again and return it. + + return GetFreeVoice(); +} + + static unsigned int FrequencyForVoice(opl_voice_t *voice) { unsigned int freq_index; @@ -899,10 +995,21 @@ static void VoiceKeyOn(opl_channel_data_t *channel, voice = GetFreeVoice(); + // If there are no more voices left, we must decide what to do. + // If this is the first voice of the instrument, free an existing + // voice and use that. Otherwise, if this is the second voice, + // it isn't as important; just discard it. + if (voice == NULL) { - printf("\tno free voice for voice %i of instrument\n", instrument_voice); - return; + if (instrument_voice == 0) + { + voice = ReplaceExistingVoice(channel); + } + else + { + return; + } } voice->channel = channel; -- cgit v1.2.3 From 6132a2a3e365ca36b7a1a7d721289763bac971dd Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 12 Sep 2009 21:13:08 +0000 Subject: Vanilla Doom plays d_introa, not d_intro. Subversion-branch: /branches/opl-branch Subversion-revision: 1670 --- src/d_main.c | 2 +- src/i_oplmusic.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/d_main.c b/src/d_main.c index c59a8fb7..46dd19ac 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -521,7 +521,7 @@ void D_DoAdvanceDemo (void) if ( gamemode == commercial ) S_StartMusic(mus_dm2ttl); else - S_StartMusic (mus_intro); + S_StartMusic (mus_introa); break; case 1: G_DeferedPlayDemo(DEH_String("demo1")); diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index bed81109..a6c03f6f 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -1513,7 +1513,7 @@ static void *I_OPL_RegisterSong(void *data, int len) // remove file now - remove(filename); +// remove(filename); Z_Free(filename); -- cgit v1.2.3 From 2b40842eeca7386f6bfe9b1b39760c8a4d69ca71 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 12 Sep 2009 23:04:57 +0000 Subject: Remove separate volume calculations for non-feedback (separate operator) voices. Doom writes the same value to both operators. Subversion-branch: /branches/opl-branch Subversion-revision: 1671 --- src/i_oplmusic.c | 69 ++++++++++++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index a6c03f6f..ce255ff7 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -150,9 +150,8 @@ struct opl_voice_s // The volume of the note being played on this channel. unsigned int note_volume; - // The current volume that has been set for this channel. - unsigned int carrier_volume; - unsigned int modulator_volume; + // The current volume (register value) that has been set for this channel. + unsigned int reg_volume; // Next in linked list; a voice is always either in the // free list or the allocated list. @@ -636,42 +635,15 @@ static void SetVoiceInstrument(opl_voice_t *voice, // Hack to force a volume update. - voice->carrier_volume = 999; - voice->modulator_volume = 999; -} - -// Calculate the volume level to use for a given operator. - -static void SetOperatorVolume(genmidi_op_t *op, unsigned int volume, - unsigned int opnum, - unsigned int *current_volume) -{ - unsigned int op_volume; - unsigned int reg_volume; - - // The volume of each instrument can be controlled via GENMIDI: - - op_volume = 0x3f - op->level; - - // The volume value to use in the register: - - reg_volume = (op_volume * volume) / 128; - reg_volume = (0x3f - reg_volume) | op->scale; - - // Update the register, if neccessary: - - if (*current_volume != reg_volume) - { - *current_volume = reg_volume; - - WriteRegister(OPL_REGS_LEVEL + opnum, reg_volume); - } + voice->reg_volume = 999; } static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume) { genmidi_voice_t *opl_voice; unsigned int full_volume; + unsigned int op_volume; + unsigned int reg_volume; voice->note_volume = volume; @@ -682,16 +654,33 @@ static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume) full_volume = (volume_mapping_table[voice->note_volume] * volume_mapping_table[voice->channel->volume]) / 127; - SetOperatorVolume(&opl_voice->carrier, full_volume, - voice->op2, &voice->carrier_volume); + // The volume of each instrument can be controlled via GENMIDI: + + op_volume = 0x3f - opl_voice->carrier.level; - // If we are using non-modulated feedback mode, we must set the - // volume for both voices. + // The volume value to use in the register: - if ((opl_voice->feedback & 0x01) != 0) + reg_volume = (op_volume * full_volume) / 128; + reg_volume = (0x3f - reg_volume) | opl_voice->carrier.scale; + + // Update the volume register(s) if necessary. + + if (reg_volume != voice->reg_volume) { - SetOperatorVolume(&opl_voice->modulator, full_volume, - voice->op1, &voice->modulator_volume); + voice->reg_volume = reg_volume; + + WriteRegister(OPL_REGS_LEVEL + voice->op2, reg_volume); + + // If we are using non-modulated feedback mode, we must set the + // volume for both voices. + // Note that the same register volume value is written for + // both voices, always calculated from the carrier's level + // value. + + if ((opl_voice->feedback & 0x01) != 0) + { + WriteRegister(OPL_REGS_LEVEL + voice->op1, reg_volume); + } } } -- cgit v1.2.3 From 7e2eaa105e270586e81c04fdf61bcafd7c96a34c Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 13 Sep 2009 00:15:24 +0000 Subject: Change intro/introa fix to be more accurate: Doom uses d_intro, but transforms this to d_introa when using OPL playback (thanks entryway) Subversion-branch: /branches/opl-branch Subversion-revision: 1672 --- src/d_main.c | 2 +- src/s_sound.c | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/d_main.c b/src/d_main.c index 46dd19ac..c59a8fb7 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -521,7 +521,7 @@ void D_DoAdvanceDemo (void) if ( gamemode == commercial ) S_StartMusic(mus_dm2ttl); else - S_StartMusic (mus_introa); + S_StartMusic (mus_intro); break; case 1: G_DeferedPlayDemo(DEH_String("demo1")); diff --git a/src/s_sound.c b/src/s_sound.c index f038e9cd..9e70ec73 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -804,6 +804,15 @@ void S_ChangeMusic(int musicnum, int looping) char namebuf[9]; void *handle; + // The Doom IWAD file has two versions of the intro music: d_intro + // and d_introa. The latter is used for OPL playback. + + if (musicnum == mus_intro && (snd_musicdevice == SNDDEVICE_ADLIB + || snd_musicdevice == SNDDEVICE_SB)) + { + musicnum = mus_introa; + } + if (musicnum <= mus_None || musicnum >= NUMMUSIC) { I_Error("Bad music number %d", musicnum); -- cgit v1.2.3 From 13314576f8d5d8eec8bf413ca4b49255b9e1a6a2 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 19 Sep 2009 20:42:45 +0000 Subject: Add octave offset table, offset notes on specific instruments down by one octave, as per research. Subversion-branch: /branches/opl-branch Subversion-revision: 1678 --- src/i_oplmusic.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index ce255ff7..09d2ae42 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -304,6 +304,31 @@ static const unsigned int volume_mapping_table[] = { 124, 124, 125, 125, 126, 126, 127, 127 }; +// For octave offset table: + +static const unsigned int octave_offset_table[2][128] = { + { + 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, // 0-15 + 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31 + 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, // 32-47 + 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // 48-63 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, // 64-79 + 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, // 80-95 + 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // 96-111 + 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, // 112-127 + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-15 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31 + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // 32-47 + 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // 48-63 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 64-79 + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-95 + 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 96-111 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 112-127 + } +}; + static boolean music_initialised = false; //static boolean musicpaused = false; @@ -832,10 +857,12 @@ static void KeyOffEvent(opl_track_data_t *track, midi_event_t *event) unsigned int key; unsigned int i; +/* printf("note off: channel %i, %i, %i\n", event->data.channel.channel, event->data.channel.param1, event->data.channel.param2); +*/ channel = &track->channels[event->data.channel.channel]; key = event->data.channel.param1; @@ -921,8 +948,25 @@ static unsigned int FrequencyForVoice(opl_voice_t *voice) unsigned int freq_index; unsigned int octave; unsigned int sub_index; + unsigned int instr_num; + unsigned int note; + + note = voice->note; + + // What instrument number is this? + // Certain instruments have all notes offset down by one octave. + // Use the octave offset table to work out if this voice should + // be offset. + + instr_num = voice->current_instr - main_instrs; + + if (instr_num < 128 && note >= 12 + && octave_offset_table[voice->current_instr_voice][instr_num]) + { + note -= 12; + } - freq_index = 64 + 32 * voice->note + voice->channel->bend; + freq_index = 64 + 32 * note + voice->channel->bend; // If this is the second voice of a double voice instrument, the // frequency index can be adjusted by the fine tuning field. @@ -1037,10 +1081,12 @@ static void KeyOnEvent(opl_track_data_t *track, midi_event_t *event) unsigned int key; unsigned int volume; +/* printf("note on: channel %i, %i, %i\n", event->data.channel.channel, event->data.channel.param1, event->data.channel.param2); +*/ // The channel. @@ -1113,10 +1159,12 @@ static void ControllerEvent(opl_track_data_t *track, midi_event_t *event) unsigned int param; opl_channel_data_t *channel; +/* printf("change controller: channel %i, %i, %i\n", event->data.channel.channel, event->data.channel.param1, event->data.channel.param2); +*/ channel = &track->channels[event->data.channel.channel]; controller = event->data.channel.param1; -- cgit v1.2.3 From dd03cbbe2b688eac26011ac0ea0ada4047e5beb0 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 20 Sep 2009 20:47:17 +0000 Subject: Use the base note offset field to offset notes, not a fixed lookup table of instruments to offset. Subversion-branch: /branches/opl-branch Subversion-revision: 1680 --- src/i_oplmusic.c | 42 +++++------------------------------------- 1 file changed, 5 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 09d2ae42..de126c63 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -34,6 +34,7 @@ #include "mus2mid.h" #include "deh_main.h" +#include "i_swap.h" #include "m_misc.h" #include "s_sound.h" #include "w_wad.h" @@ -304,31 +305,6 @@ static const unsigned int volume_mapping_table[] = { 124, 124, 125, 125, 126, 126, 127, 127 }; -// For octave offset table: - -static const unsigned int octave_offset_table[2][128] = { - { - 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, // 0-15 - 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31 - 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, // 32-47 - 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // 48-63 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, // 64-79 - 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, // 80-95 - 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // 96-111 - 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, // 112-127 - }, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-15 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31 - 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // 32-47 - 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // 48-63 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 64-79 - 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-95 - 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 96-111 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 112-127 - } -}; - static boolean music_initialised = false; //static boolean musicpaused = false; @@ -945,26 +921,18 @@ static opl_voice_t *ReplaceExistingVoice(opl_channel_data_t *channel) static unsigned int FrequencyForVoice(opl_voice_t *voice) { + genmidi_voice_t *gm_voice; unsigned int freq_index; unsigned int octave; unsigned int sub_index; - unsigned int instr_num; unsigned int note; note = voice->note; - // What instrument number is this? - // Certain instruments have all notes offset down by one octave. - // Use the octave offset table to work out if this voice should - // be offset. - - instr_num = voice->current_instr - main_instrs; + // Apply note offset: - if (instr_num < 128 && note >= 12 - && octave_offset_table[voice->current_instr_voice][instr_num]) - { - note -= 12; - } + gm_voice = &voice->current_instr->voices[voice->current_instr_voice]; + note += (signed short) SHORT(gm_voice->base_note_offset); freq_index = 64 + 32 * note + voice->channel->bend; -- cgit v1.2.3 From f894f24bcd63dc8b2a0604a9b659dd95aebbb384 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 20 Sep 2009 21:19:37 +0000 Subject: Avoid possible overflow due to base note offset. Subversion-branch: /branches/opl-branch Subversion-revision: 1683 --- src/i_oplmusic.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index de126c63..aa079614 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -934,6 +934,13 @@ static unsigned int FrequencyForVoice(opl_voice_t *voice) gm_voice = &voice->current_instr->voices[voice->current_instr_voice]; note += (signed short) SHORT(gm_voice->base_note_offset); + // Avoid possible overflow due to base note offset: + + if (note > 0x7f) + { + note = voice->note; + } + freq_index = 64 + 32 * note + voice->channel->bend; // If this is the second voice of a double voice instrument, the -- cgit v1.2.3 From fcf4a7241a742e7f3d8189c40ad852d48c674612 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 20 Sep 2009 21:21:30 +0000 Subject: Remove old test code. Subversion-branch: /branches/opl-branch Subversion-revision: 1684 --- src/i_oplmusic.c | 47 ----------------------------------------------- 1 file changed, 47 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index aa079614..b8e93534 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -43,8 +43,6 @@ #include "opl.h" #include "midifile.h" -//#define TEST - #define MAXMIDLENGTH (96 * 1024) #define GENMIDI_NUM_INSTRS 128 @@ -716,9 +714,6 @@ static void I_OPL_ShutdownMusic(void) { if (music_initialised) { -#ifdef TEST - InitRegisters(); -#endif OPL_Shutdown(); // Release GENMIDI lump @@ -729,30 +724,6 @@ static void I_OPL_ShutdownMusic(void) } } -#ifdef TEST -static void TestCallback(void *arg) -{ - opl_voice_t *voice = arg; - int note; - int wait_time; - - // Set level: - WriteRegister(OPL_REGS_LEVEL + voice->op2, 0); - - // Note off: - - WriteRegister(OPL_REGS_FREQ_2 + voice->index, 0x00); - // Note on: - - note = (rand() % (0x2ae - 0x16b)) + 0x16b; - WriteRegister(OPL_REGS_FREQ_1 + voice->index, note & 0xff); - WriteRegister(OPL_REGS_FREQ_2 + voice->index, 0x30 + (note >> 8)); - - wait_time = (rand() % 700) + 50; - OPL_SetCallback(wait_time, TestCallback, arg); -} -#endif - // Initialise music subsystem static boolean I_OPL_InitMusic(void) @@ -789,24 +760,6 @@ static boolean I_OPL_InitMusic(void) init_stage_reg_writes = false; -#ifdef TEST - { - int i; - opl_voice_t *voice; - int instr_num; - - for (i=0; i<3; ++i) - { - voice = GetFreeVoice(); - instr_num = rand() % 100; - - SetVoiceInstrument(voice, &main_instrs[instr_num], 0); - - OPL_SetCallback(0, TestCallback, voice); - } - } -#endif - music_initialised = true; return true; -- cgit v1.2.3 From 4b598f3d40e34b5d786c9236f891fbe2337c1b66 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 20 Sep 2009 21:25:59 +0000 Subject: Implement volume control. Subversion-branch: /branches/opl-branch Subversion-revision: 1685 --- src/i_oplmusic.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index b8e93534..4ac52754 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -651,7 +651,8 @@ static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume) // Multiply note volume and channel volume to get the actual volume. full_volume = (volume_mapping_table[voice->note_volume] - * volume_mapping_table[voice->channel->volume]) / 127; + * volume_mapping_table[voice->channel->volume] + * volume_mapping_table[current_music_volume]) / (127 * 127); // The volume of each instrument can be controlled via GENMIDI: @@ -769,8 +770,21 @@ static boolean I_OPL_InitMusic(void) static void I_OPL_SetMusicVolume(int volume) { + unsigned int i; + // Internal state variable. + current_music_volume = volume; + + // Update the volume of all voices. + + for (i=0; itremolo); - WriteRegister(OPL_REGS_ATTACK + operator, data->attack); - WriteRegister(OPL_REGS_SUSTAIN + operator, data->sustain); - WriteRegister(OPL_REGS_WAVEFORM + operator, data->waveform); + OPL_WriteRegister(OPL_REGS_LEVEL + operator, level); + OPL_WriteRegister(OPL_REGS_TREMOLO + operator, data->tremolo); + OPL_WriteRegister(OPL_REGS_ATTACK + operator, data->attack); + OPL_WriteRegister(OPL_REGS_SUSTAIN + operator, data->sustain); + OPL_WriteRegister(OPL_REGS_WAVEFORM + operator, data->waveform); } // Set the instrument for a particular voice. @@ -629,8 +491,8 @@ static void SetVoiceInstrument(opl_voice_t *voice, // two operators. Turn on bits in the upper nybble; I think this // is for OPL3, where it turns on channel A/B. - WriteRegister(OPL_REGS_FEEDBACK + voice->index, - data->feedback | 0x30); + OPL_WriteRegister(OPL_REGS_FEEDBACK + voice->index, + data->feedback | 0x30); // Hack to force a volume update. @@ -669,7 +531,7 @@ static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume) { voice->reg_volume = reg_volume; - WriteRegister(OPL_REGS_LEVEL + voice->op2, reg_volume); + OPL_WriteRegister(OPL_REGS_LEVEL + voice->op2, reg_volume); // If we are using non-modulated feedback mode, we must set the // volume for both voices. @@ -679,7 +541,7 @@ static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume) if ((opl_voice->feedback & 0x01) != 0) { - WriteRegister(OPL_REGS_LEVEL + voice->op1, reg_volume); + OPL_WriteRegister(OPL_REGS_LEVEL + voice->op1, reg_volume); } } } @@ -730,18 +592,8 @@ static void I_OPL_ShutdownMusic(void) static boolean I_OPL_InitMusic(void) { if (!OPL_Init(opl_io_port)) - { - return false; - } - - init_stage_reg_writes = true; - - // Doom does the detection sequence twice, for some reason: - - if (!DetectOPL() || !DetectOPL()) { printf("Dude. The Adlib isn't responding.\n"); - OPL_Shutdown(); return false; } @@ -753,14 +605,8 @@ static boolean I_OPL_InitMusic(void) return false; } - InitRegisters(); InitVoices(); - // Now that initialisation has finished, switch the - // register writing mode: - - init_stage_reg_writes = false; - music_initialised = true; return true; @@ -789,7 +635,7 @@ static void I_OPL_SetMusicVolume(int volume) static void VoiceKeyOff(opl_voice_t *voice) { - WriteRegister(OPL_REGS_FREQ_2 + voice->index, voice->freq >> 8); + OPL_WriteRegister(OPL_REGS_FREQ_2 + voice->index, voice->freq >> 8); } // Get the frequency that we should be using for a voice. @@ -947,8 +793,8 @@ static void UpdateVoiceFrequency(opl_voice_t *voice) if (voice->freq != freq) { - WriteRegister(OPL_REGS_FREQ_1 + voice->index, freq & 0xff); - WriteRegister(OPL_REGS_FREQ_2 + voice->index, (freq >> 8) | 0x20); + OPL_WriteRegister(OPL_REGS_FREQ_1 + voice->index, freq & 0xff); + OPL_WriteRegister(OPL_REGS_FREQ_2 + voice->index, (freq >> 8) | 0x20); voice->freq = freq; } -- cgit v1.2.3 From 4a70f989d2aacabffc2f02017704de042be7418a Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Thu, 1 Oct 2009 00:08:48 +0000 Subject: Convert to American English spellings. Subversion-branch: /branches/opl-branch Subversion-revision: 1700 --- src/i_oplmusic.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index e9e722e4..35ba046d 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -303,7 +303,7 @@ static const unsigned int volume_mapping_table[] = { 124, 124, 125, 125, 126, 126, 127, 127 }; -static boolean music_initialised = false; +static boolean music_initialized = false; //static boolean musicpaused = false; static int current_music_volume; @@ -546,7 +546,7 @@ static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume) } } -// Initialise the voice table and freelist +// Initialize the voice table and freelist static void InitVoices(void) { @@ -556,7 +556,7 @@ static void InitVoices(void) voice_free_list = NULL; - // Initialise each voice. + // Initialize each voice. for (i=0; i 0; +} + +// Shutdown music + +static void I_OPL_ShutdownMusic(void) +{ + if (music_initialized) + { + // Stop currently-playing track, if there is one: + + I_OPL_StopSong(); + + OPL_Shutdown(); + + // Release GENMIDI lump + + W_ReleaseLumpName("GENMIDI"); + + music_initialized = false; + } +} + +// Initialize music subsystem + +static boolean I_OPL_InitMusic(void) +{ + if (!OPL_Init(opl_io_port)) + { + printf("Dude. The Adlib isn't responding.\n"); + return false; + } + + // Load instruments from GENMIDI lump: + + if (!LoadInstrumentTable()) + { + OPL_Shutdown(); + return false; + } + + InitVoices(); + + tracks = NULL; + num_tracks = 0; + music_initialized = true; + + return true; } static snddevice_t music_opl_devices[] = -- cgit v1.2.3 From 56a6af426972ed2a334ae7001d19854aca964a75 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 11 Oct 2009 21:47:07 +0000 Subject: Don't apply base note offset if the instrument is a fixed note instrument. This should stop the ugly bleeping from the electric snare on E1M2. Subversion-branch: /branches/opl-branch Subversion-revision: 1715 --- src/i_oplmusic.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index cd331897..d32a163e 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -701,10 +701,15 @@ static unsigned int FrequencyForVoice(opl_voice_t *voice) note = voice->note; - // Apply note offset: + // Apply note offset. + // Don't apply offset if the instrument is a fixed note instrument. gm_voice = &voice->current_instr->voices[voice->current_instr_voice]; - note += (signed short) SHORT(gm_voice->base_note_offset); + + if ((voice->current_instr->flags & GENMIDI_FLAG_FIXED) == 0) + { + note += (signed short) SHORT(gm_voice->base_note_offset); + } // Avoid possible overflow due to base note offset: -- cgit v1.2.3 From e30325c40f6ea482862745db0f4555e513f2952e Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 17 Oct 2009 22:36:15 +0000 Subject: Add OPL library API function to set software emulation sample rate, and set from snd_samplerate in the configuration file. Subversion-branch: /branches/opl-branch Subversion-revision: 1723 --- src/i_oplmusic.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index d32a163e..474877d4 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -1372,6 +1372,8 @@ static void I_OPL_ShutdownMusic(void) static boolean I_OPL_InitMusic(void) { + OPL_SetSampleRate(snd_samplerate); + if (!OPL_Init(opl_io_port)) { printf("Dude. The Adlib isn't responding.\n"); -- cgit v1.2.3 From 14bcdd1008db070bc4a12be73e3f692c1990966e Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 25 Oct 2009 17:07:55 +0000 Subject: Emulate odd octave 7 behavior of Vanilla Doom. Subversion-branch: /branches/opl-branch Subversion-revision: 1725 --- src/i_oplmusic.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 474877d4..89e73f1a 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -739,6 +739,26 @@ static unsigned int FrequencyForVoice(opl_voice_t *voice) sub_index = (freq_index - 284) % (12 * 32); octave = (freq_index - 284) / (12 * 32); + // Once the seventh octave is reached, things break down. + // We can only go up to octave 7 as a maximum anyway (the OPL + // register only has three bits for octave number), but for the + // notes in octave 7, the first five bits have octave=7, the + // following notes have octave=6. This 7/6 pattern repeats in + // following octaves (which are technically impossible to + // represent anyway). + + if (octave >= 7) + { + if (sub_index < 5) + { + octave = 7; + } + else + { + octave = 6; + } + } + // Calculate the resulting register value to use for the frequency. return frequency_curve[sub_index + 284] | (octave << 10); -- cgit v1.2.3 From 299e1c5abf804e2e249c4f77b82fb7949a3f9d7b Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 31 Oct 2009 16:47:49 +0000 Subject: When replacing an existing voice, discard voices that are the second voice of a two voice instrument. Don't discard instruments from lower numbered MIDI channels for higher numbered MIDI channels. Subversion-branch: /branches/opl-branch Subversion-revision: 1727 --- src/i_oplmusic.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 89e73f1a..9501d51d 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -631,6 +631,16 @@ static void KeyOffEvent(opl_track_data_t *track, midi_event_t *event) } } +// Compare the priorities of channels, returning either -1, 0 or 1. + +static int CompareChannelPriorities(opl_channel_data_t *chan1, + opl_channel_data_t *chan2) +{ + // TODO ... + + return 1; +} + // When all voices are in use, we must discard an existing voice to // play a new note. Find and free an existing voice. The channel // passed to the function is the channel for the new note to be @@ -643,13 +653,19 @@ static opl_voice_t *ReplaceExistingVoice(opl_channel_data_t *channel) // Check the allocated voices, if we find an instrument that is // of a lower priority to the new instrument, discard it. - // Priority is determined by MIDI instrument number; old + // If a voice is being used to play the second voice of an instrument, + // use that, as second voices are non-essential. + // Lower numbered MIDI channels implicitly have a higher priority + // than higher-numbered channels, eg. MIDI channel 1 is never + // discarded for MIDI channel 2. result = NULL; for (rover = voice_alloced_list; rover != NULL; rover = rover->next) { - if (rover->current_instr > channel->instrument) + if (rover->current_instr_voice != 0 + || (rover->channel > channel + && CompareChannelPriorities(channel, rover->channel) > 0)) { result = rover; break; -- cgit v1.2.3 From 2f3b2889abdcb219ae0d27e14f233ca76bf42dec Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Mon, 8 Mar 2010 18:50:29 +0000 Subject: Use native MIDI music by default. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 1879 --- src/s_sound.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/s_sound.c b/src/s_sound.c index 6c18f65e..7ae7559a 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -120,7 +120,7 @@ static musicinfo_t *mus_playing = NULL; int numChannels = 8; -int snd_musicdevice = SNDDEVICE_SB; +int snd_musicdevice = SNDDEVICE_GENMIDI; int snd_sfxdevice = SNDDEVICE_SB; // Sound modules -- cgit v1.2.3 From 13d87ff8f1f839a9b10178e00cfbe58aca1c6124 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Thu, 22 Apr 2010 21:38:51 +0000 Subject: Disable OPL debugging messages. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 1913 --- src/i_oplmusic.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src') diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c index 9501d51d..e8d65d6d 100644 --- a/src/i_oplmusic.c +++ b/src/i_oplmusic.c @@ -43,6 +43,8 @@ #include "opl.h" #include "midifile.h" +// #define OPL_MIDI_DEBUG + #define MAXMIDLENGTH (96 * 1024) #define GENMIDI_NUM_INSTRS 128 @@ -965,7 +967,9 @@ static void ControllerEvent(opl_track_data_t *track, midi_event_t *event) break; default: +#ifdef OPL_MIDI_DEBUG fprintf(stderr, "Unknown MIDI controller type: %i\n", controller); +#endif break; } } @@ -1020,8 +1024,10 @@ static void MetaEvent(opl_track_data_t *track, midi_event_t *event) break; default: +#ifdef OPL_MIDI_DEBUG fprintf(stderr, "Unknown MIDI meta event type: %i\n", event->data.meta.type); +#endif break; } } @@ -1063,7 +1069,9 @@ static void ProcessEvent(opl_track_data_t *track, midi_event_t *event) break; default: +#ifdef OPL_MIDI_DEBUG fprintf(stderr, "Unknown MIDI event type %i\n", event->event_type); +#endif break; } } -- cgit v1.2.3 From 4070ecd92c45dadc80e048e1bc929bced925c232 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 23 Apr 2010 20:46:29 +0000 Subject: Add REJECT buffer overflow emulation, based on code from PrBoom+ (thanks entryway). Fixes YDFEAR25.LMP. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 1914 --- src/p_setup.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/p_setup.c b/src/p_setup.c index 2a3a8f85..385759f8 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -76,6 +76,7 @@ line_t* lines; int numsides; side_t* sides; +static int totallines; // BLOCKMAP // Created from axis aligned bounding box @@ -534,7 +535,6 @@ void P_GroupLines (void) line_t** linebuffer; int i; int j; - int total; line_t* li; sector_t* sector; subsector_t* ss; @@ -552,21 +552,21 @@ void P_GroupLines (void) // count number of lines in each sector li = lines; - total = 0; + totallines = 0; for (i=0 ; ifrontsector->linecount++; if (li->backsector && li->backsector != li->frontsector) { li->backsector->linecount++; - total++; + totallines++; } } // build line tables for each sector - linebuffer = Z_Malloc (total*sizeof(line_t *), PU_LEVEL, 0); + linebuffer = Z_Malloc (totallines*sizeof(line_t *), PU_LEVEL, 0); for (i=0; i> (byte_num * 8)) & 0xff; + ++dest; + } + + // We only have a limited pad size. Print a warning if the + // REJECT lump is too small. + + if (len > sizeof(rejectpad)) + { + fprintf(stderr, "PadRejectArray: REJECT lump too short to pad! (%i > %i)\n", + len, sizeof(rejectpad)); + + // Pad remaining space with 0xff. + + memset(array + sizeof(rejectpad), 0x00, len - sizeof(rejectpad)); + } +} + +static void P_LoadReject(int lumpnum) +{ + int minlength; + int lumplen; + + // Calculate the size that the REJECT lump *should* be. + + minlength = (numsectors * numsectors + 7) / 8; + + // If the lump meets the minimum length, it can be loaded directly. + // Otherwise, we need to allocate a buffer of the correct size + // and pad it with appropriate data. + + lumplen = W_LumpLength(lumpnum); + + if (lumplen >= minlength) + { + rejectmatrix = W_CacheLumpNum(lumpnum, PU_LEVEL); + } + else + { + rejectmatrix = Z_Malloc(minlength, PU_LEVEL, &rejectmatrix); + W_ReadLump(lumpnum, rejectmatrix); + + PadRejectArray(rejectmatrix + lumplen, minlength - lumplen); + } +} // // P_SetupLevel @@ -719,9 +790,9 @@ P_SetupLevel P_LoadSubsectors (lumpnum+ML_SSECTORS); P_LoadNodes (lumpnum+ML_NODES); P_LoadSegs (lumpnum+ML_SEGS); - - rejectmatrix = W_CacheLumpNum (lumpnum+ML_REJECT,PU_LEVEL); + P_GroupLines (); + P_LoadReject (lumpnum+ML_REJECT); bodyqueslot = 0; deathmatch_p = deathmatchstarts; -- cgit v1.2.3 From e7f0d2727a01c90a5552a3baf442c11f43a95ce8 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 24 Apr 2010 23:53:03 +0000 Subject: Add -reject_pad_with_ff parameter to allow padding value to be specified. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 1915 --- src/p_setup.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/p_setup.c b/src/p_setup.c index 385759f8..58edc6fd 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -33,6 +33,7 @@ #include "deh_main.h" #include "i_swap.h" +#include "m_argv.h" #include "m_bbox.h" #include "g_game.h" @@ -651,6 +652,7 @@ static void PadRejectArray(byte *array, unsigned int len) unsigned int i; unsigned int byte_num; byte *dest; + unsigned int padvalue; // Values to pad the REJECT array with: @@ -681,9 +683,18 @@ static void PadRejectArray(byte *array, unsigned int len) fprintf(stderr, "PadRejectArray: REJECT lump too short to pad! (%i > %i)\n", len, sizeof(rejectpad)); - // Pad remaining space with 0xff. + // Pad remaining space with 0 (or 0xff, if specified on command line). - memset(array + sizeof(rejectpad), 0x00, len - sizeof(rejectpad)); + if (M_CheckParm("-reject_pad_with_ff")) + { + padvalue = 0xff; + } + else + { + padvalue = 0xf00; + } + + memset(array + sizeof(rejectpad), padvalue, len - sizeof(rejectpad)); } } -- cgit v1.2.3 From 6f8ac36f6644466131b548f0a8a42e260d181aa5 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 30 Apr 2010 19:38:24 +0000 Subject: Add textscreen Doxyfile to dist. Add .desktop file to svn:ignore. Add opl ctags file to localvimrc. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 1922 --- src/.gitignore | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/.gitignore b/src/.gitignore index 973a0073..2c4a3c48 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -5,5 +5,6 @@ Makefile.in chocolate-doom chocolate-server *.exe +*.desktop tags TAGS -- cgit v1.2.3