libdmf  0.0.1
A library for processing Deflemask (dmf) files
libdmf.h
Go to the documentation of this file.
1 
11 #ifndef _DMF_PARSER_H_
12 #define _DMF_PARSER_H_
13 
14 #include <stdlib.h>
15 
16 #define MAX_DMF_SIZE 16777216
17 
18 // The system in the DMF
19 #define SYSTEM_GENESIS 0x02
20 #define SYSTEM_GENESIS_EXT_CH3 0x12
21 #define SYSTEM_SMS 0x03
22 #define SYSTEM_GAMEBOY 0x04
23 #define SYSTEM_PCENGINE 0x05
24 #define SYSTEM_NES 0x06
25 #define SYSTEM_C64_SID_8580 0x07
26 #define SYSTEM_C64_SID_6581 0x47
27 #define SYSTEM_YM2151 0x08
28 
29 #define REIGON_PAL 0x00
30 #define REIGON_NTSC 0x01
31 
32 #define MODE_STD 0x00
33 #define MODE_FM 0x01
34 
35 // Helper structs:
39 typedef struct {
40  unsigned char AM;
41  unsigned char AR;
42  unsigned char DR;
43  unsigned char MULT;
44  unsigned char RR;
45  unsigned char SL;
46  unsigned char TL;
47  unsigned char DT2;
48  unsigned char RS;
49  unsigned char DT;
50  unsigned char D2R;
51  unsigned char SSGMODE;
52 } FM_operator;
53 
57 typedef struct {
58  unsigned char name_length;
59  unsigned char *name;
60  unsigned char mode;
61 
62  // FM Info
63  unsigned char ALG;
64  unsigned char FB;
65  unsigned char LFO;
66  unsigned char LFO2;
67 
73 
74  // STD Info
75  // Volume Macro:
76  unsigned char volume_envelope_size;
77  unsigned int *volume_envelope;
81  signed char volume_loop_position;
82 
83  // Arpeggio Macro:
84  unsigned char arpeggio_envelope_size;
85  signed int *arpeggio_envelope;
86  signed char arpeggio_loop_position;
87  unsigned char arpeggio_macro_mode;
88 
89  // Duty/Noise Macro:
90  unsigned char duty_noise_envelope_size;
91  unsigned int *duty_noise_envelope;
93 
94  // Wavetable Macro:
95  unsigned char wavetable_envelope_size;
96  unsigned int *wavetable_envelope;
98 
99  // PER SYSTEM DATA:
100  // C64:
102  unsigned char c64_saw_wave_enabled;
103  unsigned char c64_pulse_wave_enabled;
104  unsigned char c64_noise_wave_enabled;
105  unsigned char c64_attack;
106  unsigned char c64_decay;
107  unsigned char c64_sustain;
108  unsigned char c64_release;
109  unsigned char c64_pulse_width;
112  unsigned char c64_to_filter;
115  //filter_globals
116  unsigned char c64_filter_resonance;
117  unsigned char c64_filter_cutoff;
118  unsigned char c64_filter_high_pass;
119  unsigned char c64_filter_band_pass;
120  unsigned char c64_filter_low_pass;
121  unsigned char c64_filter_ch2_off;
122  // GAMEBOY
123  unsigned char gb_envelope_volume;
124  unsigned char gb_envelope_direction;
125  unsigned char gb_envelope_length;
126  unsigned char gb_sound_length;
127 }instrument;
128 
129 // Wavetable shite
133 typedef struct {
134  unsigned int size;
135  unsigned int *data;
136 } wavetable;
137 
138 // Notes
142 typedef struct {
143  signed short code;
144  signed short value;
145 } note_command;
146 
150 typedef struct {
151  unsigned short note;
152  unsigned short octave;
153  signed short volume;
155  signed short instrument;
156 } note_row;
157 
161 typedef struct {
162  unsigned char effect_columns_count;
164 } note_channel;
165 
166 // Samples
170 typedef struct {
171  unsigned int size;
172  unsigned char name_length;
173  unsigned char *name;
174 
175  unsigned char rate;
176  unsigned char pitch;
177  unsigned char amp;
178  unsigned char bits;
179 
180  unsigned short *data;
181 } sample;
182 
183 // Actual DMF
187 typedef struct {
188 
189  // SYSTEM INFORMATION
190  unsigned char system;
191  unsigned char system_total_channels;
192 
193  // VISUAL INFORMATION
194  unsigned char name_length;
195  char *name;
196 
197  unsigned char author_length;
198  char *author;
199 
200  unsigned char highlight_A;
201  unsigned char highlight_B;
202 
203  // MODULE INFORMATION
204  unsigned char time_base;
205  unsigned char tick_time_1;
206  unsigned char tick_time_2;
207 
208  unsigned char reigon;
209  unsigned char custom_hz_on;
210  unsigned short custom_hz;
211 
214  unsigned char **pattern_matrix_value;
215 
216  unsigned char total_instruments;
218 
219  unsigned char total_wavetables;
221 
223 
224  unsigned char total_samples;
226 } dmf;
227 
228 int openDmfFileIntoBuffer(char *filename, unsigned char *dest, size_t *length);
236 int decompressDMF(unsigned char *src, size_t src_length, unsigned char *dest);
244 int openDMF(char *filename, unsigned char *dest);
251 int parseDMF(unsigned char *decompressed_dmf, dmf *dest);
258 int fileToDmfType(char *filename, dmf *dest);
265 // Writing files
266 int dmfToBuffer(dmf src, unsigned char *dest, size_t *size);
274 int compressDMF(const unsigned char *src, size_t src_length, unsigned char *dest, size_t *dest_length);
283 int writeDMF(char *filename, dmf src);
290 // Testing.
291 void displayDMF(dmf song);
297 int verifyDMF(char *filename);
303 int freeDMF(dmf *src);
309 #endif // _DMF_PARSER_H_
unsigned char c64_decay
C64 shite, see the name of this member.
Definition: libdmf.h:106
unsigned int size
The size of the sample in unsigned ints.
Definition: libdmf.h:171
unsigned int * wavetable_envelope
Actual wavetable envelope.
Definition: libdmf.h:96
unsigned int * data
Array of ints containing the wavetable data.
Definition: libdmf.h:135
unsigned char c64_filter_ch2_off
C64 shite, see the name of this member.
Definition: libdmf.h:121
wavetable * wavetables
The wavetable data.
Definition: libdmf.h:220
unsigned char gb_envelope_length
GB shite, see the name of this member.
Definition: libdmf.h:125
unsigned char c64_pulse_wave_enabled
C64 shite, see the name of this member.
Definition: libdmf.h:103
unsigned char volume_envelope_size
Definition: libdmf.h:76
int fileToDmfType(char *filename, dmf *dest)
A combination of openDMF() and parseDMF(). Takes a file and puts it int the dmf type.
Definition: dmfParser.c:353
unsigned char wavetable_envelope_size
Size of the wavetable envelope. Ranges from [0 - 127].
Definition: libdmf.h:95
unsigned char c64_filter_resonance
C64 shite, see the name of this member.
Definition: libdmf.h:116
int parseDMF(unsigned char *decompressed_dmf, dmf *dest)
Parse an uncompressed buffer into the dmf type.
Definition: dmfParser.c:56
signed short code
The command for a row.
Definition: libdmf.h:143
unsigned char c64_ring_modulation_enabled
C64 shite, see the name of this member.
Definition: libdmf.h:110
Definition: libdmf.h:57
unsigned char LFO2
Low Frequency Oscillator 2 (AMS on YM2612, AMS on YM2151)
Definition: libdmf.h:66
int writeDMF(char *filename, dmf src)
A combination of compressDMF() and dmfToBuffer(). Converts dmf type to compressed buffer and writes i...
Definition: dmfParser.c:659
int openDMF(char *filename, unsigned char *dest)
A combination of openDmfFileIntoBuffer() and decompressDMF()
Definition: dmfParser.c:41
unsigned char c64_use_filter_values_from_instrument
C64 shite, see the name of this member.
Definition: libdmf.h:114
unsigned int * duty_noise_envelope
Actual duty/noise envelope.
Definition: libdmf.h:91
FM_operator * FM_operators
The operator data.
Definition: libdmf.h:72
unsigned char TL
Total level, the highest amplitude of the waveform.
Definition: libdmf.h:46
unsigned char mode
The mode (0 = STD, 1 = FM)
Definition: libdmf.h:60
int decompressDMF(unsigned char *src, size_t src_length, unsigned char *dest)
Takes buffer from openDmfFileIntoBuffer() and decompresses it using miniz.h.
Definition: dmfParser.c:28
note_channel * channels
Actual note data.
Definition: libdmf.h:222
unsigned char name_length
The length of the name of the instrument.
Definition: libdmf.h:58
Definition: libdmf.h:170
unsigned char gb_sound_length
GB shite, see the name of this member.
Definition: libdmf.h:126
int openDmfFileIntoBuffer(char *filename, unsigned char *dest, size_t *length)
Use a filename to open a file into a memory buffer.
Definition: dmfParser.c:4
unsigned char total_wavetables
Total number of wavetables in the dmf.
Definition: libdmf.h:219
unsigned char pitch
Deflemasks weird pitch thing.
Definition: libdmf.h:176
signed char wavetable_loop_position
The loop position of the wavetable envelope. -1 = NO_LOOP.
Definition: libdmf.h:97
unsigned char c64_release
C64 shite, see the name of this member.
Definition: libdmf.h:108
unsigned char c64_filter_band_pass
C64 shite, see the name of this member.
Definition: libdmf.h:119
unsigned char LFO
Low Frequency Oscillator 1 (FMS on YM2612, PMS on YM2151)
Definition: libdmf.h:65
unsigned short custom_hz
The actual custom framerate.
Definition: libdmf.h:210
signed char volume_loop_position
The loop position of the volume envelope. -1 = NO_LOOP.
Definition: libdmf.h:81
unsigned char AR
Attack rate, the angle of initial amplitude increase.
Definition: libdmf.h:41
Definition: libdmf.h:39
unsigned char time_base
Base Time.
Definition: libdmf.h:204
unsigned short * data
Actual sample data.
Definition: libdmf.h:180
int verifyDMF(char *filename)
Verify a DMF&#39;s integrety (or for that matter libdmf&#39;s :/)
Definition: test.c:218
unsigned char SL
The amplitude at which the slower amplitude decrease starts.
Definition: libdmf.h:45
unsigned char c64_pulse_width
C64 shite, see the name of this member.
Definition: libdmf.h:109
unsigned char total_samples
Total number of samples in the dmf.
Definition: libdmf.h:224
sample * samples
Actual sample data.
Definition: libdmf.h:225
unsigned char c64_attack
C64 shite, see the name of this member.
Definition: libdmf.h:105
char * author
The author of the dmf.
Definition: libdmf.h:198
unsigned char ** pattern_matrix_value
The pattern matrix data, it runs down and then across.
Definition: libdmf.h:214
unsigned char c64_saw_wave_enabled
C64 shite, see the name of this member.
Definition: libdmf.h:102
signed char arpeggio_loop_position
The loop position of the arpeggio envelope. -1 = NO_LOOP.
Definition: libdmf.h:86
signed short volume
The volume of the note. -1 if empty.
Definition: libdmf.h:153
unsigned char name_length
The length of the dmf&#39;s name.
Definition: libdmf.h:194
unsigned char highlight_A
Row highlight A.
Definition: libdmf.h:200
unsigned char rate
Bitrate of the sample.
Definition: libdmf.h:175
unsigned char bits
Bit depth of sample. (8 or 16)
Definition: libdmf.h:178
unsigned char author_length
The length of the dmf&#39;s author&#39;s name.
Definition: libdmf.h:197
unsigned char c64_volume_macro_to_filter_cutoff_enabled
C64 shite, see the name of this member.
Definition: libdmf.h:113
unsigned char custom_hz_on
Whether or not the dmf has a custom framerate.
Definition: libdmf.h:209
unsigned char * name
The name of the instrument.
Definition: libdmf.h:59
unsigned char name_length
Length of the sample name.
Definition: libdmf.h:172
unsigned char c64_sustain
C64 shite, see the name of this member.
Definition: libdmf.h:107
Definition: libdmf.h:150
Definition: libdmf.h:133
int compressDMF(const unsigned char *src, size_t src_length, unsigned char *dest, size_t *dest_length)
The opposite of decompressDMF(). Converts an uncompressed buffer to a compressed buffer.
Definition: dmfParser.c:648
unsigned short note
The note. Ranges from [1-12]. 1 = C#, 12 = C.
Definition: libdmf.h:151
unsigned char gb_envelope_direction
GB shite, see the name of this member.
Definition: libdmf.h:124
signed short value
The value of that command.
Definition: libdmf.h:144
unsigned char FB
The feedback for operator 1.
Definition: libdmf.h:64
unsigned char arpeggio_macro_mode
The arpeggio macro mode (0 = Normal, 1 = Fixed)
Definition: libdmf.h:87
unsigned char DT2
Detune 2 used only on the YM2151 see DT.
Definition: libdmf.h:47
unsigned char amp
The volume of the sample.
Definition: libdmf.h:177
unsigned char c64_filter_low_pass
C64 shite, see the name of this member.
Definition: libdmf.h:120
char * name
The name of the dmf.
Definition: libdmf.h:195
unsigned char AM
Amplitude Modulation enable, whether or not this operator will allow itself to be modified by the LFO...
Definition: libdmf.h:40
unsigned char effect_columns_count
Number of effect columns for that channel.
Definition: libdmf.h:162
Definition: libdmf.h:161
void displayDMF(dmf song)
Print out everything to do with a dmf.
Definition: test.c:5
signed char duty_noise_loop_position
The loop position of the duty/noise envelope. -1 = NO_LOOP.
Definition: libdmf.h:92
unsigned char DT
Detune, a number between -3 and 3. Positive is a sharper detune, whilst negative is lower...
Definition: libdmf.h:49
unsigned short octave
The octave of the note.
Definition: libdmf.h:152
Definition: libdmf.h:142
note_row ** rows
An array of array of rows, first go down the pattern matrix, then the pattern.
Definition: libdmf.h:163
unsigned char system
The console this dmf is written for.
Definition: libdmf.h:190
int dmfToBuffer(dmf src, unsigned char *dest, size_t *size)
The opposite of parseDMF(). Converts dmf type to uncompressed dmf file.
Definition: dmfParser.c:365
Definition: libdmf.h:187
note_command * commands
A list of commands.
Definition: libdmf.h:154
instrument * instruments
The instrument data.
Definition: libdmf.h:217
signed int * arpeggio_envelope
Actual arpeggio envelope.
Definition: libdmf.h:85
unsigned char c64_sync_modulation_enabled
C64 shite, see the name of this member.
Definition: libdmf.h:111
int total_rows_per_pattern
The total rows in a pattern (defualt 64)
Definition: libdmf.h:212
unsigned char tick_time_1
Speed variable 1. e.g. tick_time_1/tick_time_2.
Definition: libdmf.h:205
unsigned char MULT
A multiplier to the frequency.
Definition: libdmf.h:43
unsigned char system_total_channels
The number of channels in that system.
Definition: libdmf.h:191
unsigned char duty_noise_envelope_size
Size of the duty/noise envelope. Ranges from [0 - 127].
Definition: libdmf.h:90
unsigned char D2R
The angle of secondary amplitude decrease. This will continue indefinitely unless ‘key off’ occurs...
Definition: libdmf.h:50
signed short instrument
The instrument of the note. -1 if empty.
Definition: libdmf.h:155
unsigned char gb_envelope_volume
GB shite, see the name of this member.
Definition: libdmf.h:123
unsigned char c64_to_filter
C64 shite, see the name of this member.
Definition: libdmf.h:112
unsigned char c64_noise_wave_enabled
C64 shite, see the name of this member.
Definition: libdmf.h:104
unsigned char c64_filter_high_pass
C64 shite, see the name of this member.
Definition: libdmf.h:118
unsigned char tick_time_2
Speed variable 2. e.g. tick_time_1/tick_time_2.
Definition: libdmf.h:206
unsigned char * name
The name of the sample.
Definition: libdmf.h:173
unsigned char RR
The final angle of amplitude decrease, after ‘key off’.
Definition: libdmf.h:44
unsigned char total_rows_in_pattern_matrix
The number of rows in the pattern matrix.
Definition: libdmf.h:213
unsigned char ALG
The FM Algorithm.
Definition: libdmf.h:63
unsigned char reigon
The reigon of the dmf. 0 = PAL, 1 = NTSC.
Definition: libdmf.h:208
int freeDMF(dmf *src)
Free a DMF&#39;s resources.
Definition: dmfParser.c:679
unsigned int size
Size of the wavetable.
Definition: libdmf.h:134
unsigned char total_instruments
Total number of instruments in the dmf.
Definition: libdmf.h:216
unsigned char highlight_B
Row highlight B.
Definition: libdmf.h:201
unsigned char c64_triangle_wave_enabled
C64 shite, see the name of this member.
Definition: libdmf.h:101
unsigned char arpeggio_envelope_size
Size of arpeggio envelope. Ranges from [0 - 127].
Definition: libdmf.h:84
unsigned char DR
The angle of initial amplitude decrease.
Definition: libdmf.h:42
unsigned char c64_filter_cutoff
C64 shite, see the name of this member.
Definition: libdmf.h:117
unsigned char SSGMODE
The SSG mode (BIT 4 = 0 Disabled, 1 Enabled, BITS 0,1,2 SSG_MODE)
Definition: libdmf.h:51
unsigned char RS
Rate scaling, the degree to which envelopes become shorter as frequencies become higher.
Definition: libdmf.h:48