-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
119 lines (103 loc) · 3.26 KB
/
Copy pathmain.c
File metadata and controls
119 lines (103 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "xm.h"
int main(int argc,char *argv[]) {
XM_Header *xm_h;
FILE *fp;
printf("header size: %ld\n", sizeof(XM_Header));
if(argc < 2) {
printf("%s: filename\n",argv[0]);
return 0;
}
fp = fopen(argv[1],"rb");
if(fp == NULL) {
printf("fp null\n");
return -1;
}
xm_h = malloc(sizeof(XM_Header));
if(xm_h == NULL) return -2;
fread(xm_h, 1, sizeof(XM_Header), fp);
printf("Name: %.20s\n", xm_h->name);
printf("Tracker Name: %.20s\n", xm_h->tracker_name);
printf("Pattern ordering:\n");
for(int i=0; i<xm_h->song_length; i++) {
printf("%d",xm_h->pattern_order[i]);
if(i < xm_h->song_length) printf("->");
}
printf("\n");
// Get Patterns
// Try to read n patterns from data
// Preallocate spaces
XM_Pattern_Data patterns[xm_h->num_patterns]; // Pre-alloc num_patters
for(int i=0; i<xm_h->num_patterns; i++) {
fread(&patterns[i].pattern_header,1,sizeof(XM_Pattern_Header),fp);
printf("Pattern %d: Size %04X Row %X\n",i,patterns[i].pattern_header.packed_patterndata_size, patterns[i].pattern_header.num_rows);
XM_Pattern_Header *header = &patterns[i].pattern_header;
if(header->packed_patterndata_size == 0) continue;
// Alloate row_data
patterns[i].pattern_data = malloc(header->num_rows * xm_h->num_channels * sizeof(row_data_t));
for(int row=0; row<header->num_rows; row++) {
//patterns[i].pattern_data = (sizeof(pattern_t) * header->num_rows);
for(int chnl=0; chnl<xm_h->num_channels; chnl++) {
//patterns[i].pattern_data[row][chnl] = malloc(sizeof(row_data_t))
row_data_t *cur_row_ch = &patterns[i].pattern_data[row + chnl];
uint8_t n;
fread(&n,1,1,fp);
printf("Row %d chnl %d, packet %02X: ",row,chnl,n);
if(n & 0x80) {
if(n == 0x80) {
printf("None");
} else {
// packed; parse
if(n & 0x01) {
printf("Note:");
fread(&cur_row_ch->note,1,1,fp);
printf("%d -> %s%d, ",cur_row_ch->note,note[cur_row_ch->note%12],cur_row_ch->note/12);
// Next byte is a note.
// Parse Note
//fseek(fp,1,SEEK_CUR);
}
if(n & 0x02) {
printf("Instr:");
fread(&cur_row_ch->instrument,1,1,fp);
printf("%d, ",cur_row_ch->instrument);
//fseek(fp,1,SEEK_CUR);
}
if(n & 0x04) {
printf("Volume ");
fread(&cur_row_ch->volume_column,1,1,fp);
// Volume command have some special meaning!
}
if(n & 0x08) {
printf("Effect Type,");
//fseek(fp,1,SEEK_CUR);
fread(&cur_row_ch->effect_type,1,1,fp);
}
if(n & 0x10) {
printf("Effect Value,");
//fseek(fp,1,SEEK_CUR);
fread(&cur_row_ch->effect_parameter,1,1,fp);
}
}
} else {
cur_row_ch->note = n;
fread(&cur_row_ch->instrument,1,1,fp);
fread(&cur_row_ch->volume_column,1,1,fp);
fread(&cur_row_ch->effect_type,1,1,fp);
fread(&cur_row_ch->effect_parameter,1,1,fp);
printf("MOD, Note %d -> %s%d, Volume, Et, Ep",n,note[n%12],n/12);
//fseek(fp,4,SEEK_CUR);
// it's MOD format. read 4 more byte.
}
printf("\n");
}
//printf("\n");
}
//fseek(fp,patterns[i].pattern_header.packed_patterndata_size,SEEK_CUR);
// Read pattern header length
}
free(xm_h);
return 0;
}