Skip to content

Commit

Permalink
Split out code and put them in appropriate files to make coding and d…
Browse files Browse the repository at this point in the history
…ebugging much easier. While not complete it is a huge start.

Played with experimental “auto fix” code for guspat release envelopes.
Done some tidying of code in places.
Work done on decay_samples, may not be complete.
Major work done on “log” volumes. Calculations are now based on decibels. Part of this still needs “tuning”.
  • Loading branch information
chrisisonwildcode authored and sezero committed Aug 8, 2014
1 parent cad2f9f commit d5d2b27
Show file tree
Hide file tree
Showing 13 changed files with 408 additions and 364 deletions.
38 changes: 25 additions & 13 deletions include/internal_midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#ifndef __INTERNAL_MIDI_H
#define __INTERNAL_MIDI_H

#include "wildmidi_lib.h"

struct _channel {
uint8_t bank;
struct _patch *patch;
Expand All @@ -52,9 +50,6 @@ struct _event_data {
uint32_t data;
};

struct _patch;
struct _sample;

struct _note {
uint16_t noteid;
uint8_t velocity;
Expand All @@ -70,7 +65,8 @@ struct _note {
uint8_t active;
struct _note *replay;
struct _note *next;
uint32_t vol_lvl;
uint32_t left_mix_volume;
uint32_t right_mix_volume;
uint8_t is_off;
};

Expand All @@ -83,19 +79,33 @@ struct _event {
uint32_t samples_to_next_fixed;
};

struct _WM_Info;

struct _mdi {
int lock;
uint32_t samples_to_mix;
struct _event *events;
struct _event *current_event;
uint32_t event_count;
uint32_t events_size; /* try to stay optimally ahead to prevent reallocs */

uint32_t events_size; /* try to stay optimally ahead to prevent reallocs */
struct {
/* private info */
char *copyright;
uint32_t current_sample;
uint32_t approx_total_samples;
uint16_t mixer_options;
uint32_t total_midi_time;
} extra_info;
struct {
/* public info */
/* needs to match format of struct _WM_Info in wildmidi_lib.h */
char *copyright;
uint32_t current_sample;
uint32_t approx_total_samples;
uint16_t mixer_options;
uint32_t total_midi_time;
} * tmp_info;
uint16_t midi_master_vol;
struct _WM_Info info;
struct _WM_Info *tmp_info;
// struct _WM_Info info;
// struct _WM_Info *tmp_info;
struct _channel channel[16];
struct _note *note;
struct _note note_table[2][16][128];
Expand All @@ -110,7 +120,9 @@ struct _mdi {
struct _rvb *reverb;
};


extern int16_t _WM_lin_volume[];
extern uint32_t _WM_freq_table[];

// =====================

Expand Down Expand Up @@ -172,7 +184,7 @@ extern void _WM_ResetToStart(struct _mdi *mdi);
extern void _WM_CheckEventMemoryPool(struct _mdi *mdi);
extern void _WM_do_pan_adjust(struct _mdi *mdi, uint8_t ch);
extern void _WM_do_note_off_extra(struct _note *nte);
extern uint32_t _WM_get_volume(struct _mdi *mdi, uint8_t ch, struct _note *nte);
extern void _WM_AdjustChannelVolumes(struct _mdi *mdi, uint8_t ch);


#endif /* __INTERNAL_MIDI_H */
Expand Down
2 changes: 2 additions & 0 deletions include/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#endif

struct _patch;
struct _mdi;

struct _sample {
uint32_t data_length;
Expand All @@ -67,5 +68,6 @@ extern int _WM_auto_amp_with_amp;

extern struct _sample *_WM_get_sample_data(struct _patch *sample_patch, uint32_t freq);
extern int _WM_load_sample(struct _patch *sample_patch);
extern uint32_t _WM_get_decay_samples(struct _mdi * mdi, uint8_t channel, uint8_t note);

#endif /* __SAMPLE_H */
7 changes: 6 additions & 1 deletion include/wildmidi_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,15 @@ WM_SYMBOL int WildMidi_Close (midi * handle);
WM_SYMBOL int WildMidi_Shutdown (void);

/* NOTE: Not Yet Implemented Or Tested Properly */
/*WM_SYMBOL int WildMidi_Live (midi * handle, uint32_t midi_event);*/
/* Due to delay in audio output in the player, this is not being developed
futher at the moment. Further Development will occur when output latency
has been reduced enough to "appear" instant.
WM_SYMBOL int WildMidi_Live (midi * handle, uint32_t midi_event);
*/

/* reserved for future coding
* need to change these to use a time for cmd_pos and new_cmd_pos
WM_SYMBOL int WildMidi_InsertMidiEvent (midi * handle, uint8_t char midi_cmd, *char midi_cmd_data, unsigned long int midi_cmd_data_size, unsigned long int *cmd_pos);
WM_SYMBOL int WildMidi_DeleteMidiEvent (midi * handle, uint8_t char midi_cmd, unsigned long int *cmd_pos);
WM_SYMBOL int WildMidi_MoveMidiEvent (midi * handle, , uint8_t char midi_cmd, unsigned long int *cmd_pos, unsigned long int *new_cmd_pos);
Expand Down
1 change: 0 additions & 1 deletion src/DevTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,6 @@ static int test_hmi(unsigned char * hmi_data, unsigned long int hmi_size, int ve
uint8_t hmi_running_event = 0;
uint32_t hmi_track_header_length = 0;
uint32_t hmi_file_end = hmi_size;
uint32_t hmi_tmp = 0;

// Check header
if (strncmp((char *) hmi_data,"HMI-MIDISONG061595", 18) != 0) {
Expand Down
40 changes: 31 additions & 9 deletions src/gus_pat.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ struct _sample * _WM_load_gus_pat(const char *filename, int fix_release) {
};
uint32_t tmp_loop;

UNUSED(fix_release);

SAMPLE_CONVERT_DEBUG(__FUNCTION__); SAMPLE_CONVERT_DEBUG(filename);

if ((gus_patch = (uint8_t *) _WM_BufferFile(filename, &gus_size)) == NULL) {
Expand Down Expand Up @@ -830,15 +832,35 @@ struct _sample * _WM_load_gus_pat(const char *filename, int fix_release) {

/*
FIXME: Experimental Hacky Fix
*/
//if (fix_release) {
if (env_time_table[gus_patch[gus_ptr + 40]]
< env_time_table[gus_patch[gus_ptr + 41]]) {
uint8_t tmp_hack_rate = gus_patch[gus_ptr + 41];
gus_patch[gus_ptr + 41] = gus_patch[gus_ptr + 40];
gus_patch[gus_ptr + 40] = tmp_hack_rate;
}
//}
This looks for "dodgy" release envelope settings that faulty editors
may have set and attempts to corrects it.
if (fix_release)
Lets make this automatic ...
*/
{
if ((env_time_table[gus_patch[gus_ptr + 40]] < env_time_table[gus_patch[gus_ptr + 41]]) && (env_time_table[gus_patch[gus_ptr + 41]] == env_time_table[gus_patch[gus_ptr + 42]])) {
uint8_t tmp_hack_rate = 0;
tmp_hack_rate = gus_patch[gus_ptr + 41];
gus_patch[gus_ptr + 41] = gus_patch[gus_ptr + 40];
gus_patch[gus_ptr + 42] = gus_patch[gus_ptr + 40];
gus_patch[gus_ptr + 40] = tmp_hack_rate;
tmp_hack_rate = gus_patch[gus_ptr + 47];
gus_patch[gus_ptr + 47] = gus_patch[gus_ptr + 46];
gus_patch[gus_ptr + 48] = gus_patch[gus_ptr + 46];
gus_patch[gus_ptr + 46] = tmp_hack_rate;
}
}

/*
FIXME: Experimental Hacky Fix
Seeing if this fixes the "hard" end to gus sounds.
*/
if ((env_time_table[gus_patch[gus_ptr + 41]] < 0.05) && (gus_sample->modes & SAMPLE_ENVELOPE)) {
gus_patch[gus_ptr + 41] = 78;
}

for (i = 0; i < 6; i++) {
GUSPAT_INT_DEBUG("Envelope #",i);
Expand Down
16 changes: 4 additions & 12 deletions src/hmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ _WM_ParseNewHmi(uint8_t *hmi_data, uint32_t hmi_size) {
sample_remainder = sample_count_f - (float) sample_count;

hmi_mdi->events[hmi_mdi->event_count - 1].samples_to_next += sample_count;
hmi_mdi->info.approx_total_samples += sample_count;
hmi_mdi->extra_info.approx_total_samples += sample_count;

while (hmi_tracks_ended < hmi_track_cnt) {
smallest_delta = 0;
Expand Down Expand Up @@ -338,23 +338,15 @@ _WM_ParseNewHmi(uint8_t *hmi_data, uint32_t hmi_size) {
sample_remainder = sample_count_f - (float) sample_count;

hmi_mdi->events[hmi_mdi->event_count - 1].samples_to_next += sample_count;
hmi_mdi->info.approx_total_samples += sample_count;
hmi_mdi->extra_info.approx_total_samples += sample_count;
}

/* Set total MIDI time to 1/1000's seconds */
hmi_mdi->info.total_midi_time = (hmi_mdi->info.approx_total_samples * 1000) / _WM_SampleRate;
/*mdi->info.approx_total_samples += _WM_SampleRate * 3;*/

/* Add additional samples needed for decay */
// hmp_mdi->info.approx_total_samples += decay_samples;
/*printf("decay_samples = %lu\n",decay_samples);*/


if ((hmi_mdi->reverb = _WM_init_reverb(_WM_SampleRate, _WM_reverb_room_width, _WM_reverb_room_length, _WM_reverb_listen_posx, _WM_reverb_listen_posy)) == NULL) {
_WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to init reverb", 0);
goto _hmi_end;
}

hmi_mdi->info.current_sample = 0;
hmi_mdi->extra_info.current_sample = 0;
hmi_mdi->current_event = &hmi_mdi->events[0];
hmi_mdi->samples_to_mix = 0;
hmi_mdi->note = NULL;
Expand Down
14 changes: 3 additions & 11 deletions src/hmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ _WM_ParseNewHmp(uint8_t *hmp_data, uint32_t hmp_size) {
sample_remainder = sample_count_f - (float) sample_count;

hmp_mdi->events[hmp_mdi->event_count - 1].samples_to_next += sample_count;
hmp_mdi->info.approx_total_samples += sample_count;
hmp_mdi->extra_info.approx_total_samples += sample_count;

while (end_of_chunks < hmp_chunks) {
smallest_delta = 0;
Expand Down Expand Up @@ -345,26 +345,18 @@ _WM_ParseNewHmp(uint8_t *hmp_data, uint32_t hmp_size) {
sample_remainder = sample_count_f - (float) sample_count;

hmp_mdi->events[hmp_mdi->event_count - 1].samples_to_next += sample_count;
hmp_mdi->info.approx_total_samples += sample_count;
hmp_mdi->extra_info.approx_total_samples += sample_count;

// DEBUG
// fprintf(stderr,"DEBUG: Sample Count %u\r\n",sample_count);
}

/* Set total MIDI time to 1/1000's seconds */
hmp_mdi->info.total_midi_time = (hmp_mdi->info.approx_total_samples * 1000) / _WM_SampleRate;
/*mdi->info.approx_total_samples += _WM_SampleRate * 3;*/

/* Add additional samples needed for decay */
// hmp_mdi->info.approx_total_samples += decay_samples;
/*printf("decay_samples = %lu\n",decay_samples);*/

if ((hmp_mdi->reverb = _WM_init_reverb(_WM_SampleRate, _WM_reverb_room_width, _WM_reverb_room_length, _WM_reverb_listen_posx, _WM_reverb_listen_posy)) == NULL) {
_WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to init reverb", 0);
goto _hmp_end;
}

hmp_mdi->info.current_sample = 0;
hmp_mdi->extra_info.current_sample = 0;
hmp_mdi->current_event = &hmp_mdi->events[0];
hmp_mdi->samples_to_mix = 0;
hmp_mdi->note = NULL;
Expand Down
Loading

0 comments on commit d5d2b27

Please sign in to comment.