-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paths3mfile.cc
More file actions
196 lines (163 loc) · 3.76 KB
/
s3mfile.cc
File metadata and controls
196 lines (163 loc) · 3.76 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "s3mfile.h"
#include <cstring>
#include <cassert>
void S3M::Instrument::load(FILE* fp) {
fread(&header, sizeof(header), 1, fp);
//Verify header
assert(memcmp(header.scrs,"SCRS",4)==0);
//Fix length if needed
if(header.length > 64000UL) header.length = 64000UL;
//If the sample loops, check loop values
if(header.flags & 1) { //1 == Sample loops
assert(header.loop_begin < header.length);
assert(header.loop_end <= header.length);
}
//Only PCM samples are supported
if(header.type == 1) {
//Load PCM sample data
fseek(fp, header.memseg.ptr(), SEEK_SET);
sample_data = new uint8_t[header.length];
fread(sample_data, 1, header.length, fp);
}
}
void S3M::Slot::load(uint8_t *&ptr) {
uint8_t byte = *ptr++;
channel = byte & 0x1F;
note = 255;
instrument = 0;
volume = 255;
command = 0;
infobyte = 0;
if(byte & 0x20) {
note = *ptr++;
instrument = *ptr++;
}
if(byte & 0x40) {
volume = *ptr++;
}
if(byte & 0x80) {
command = *ptr++;
infobyte = *ptr++;
}
}
void S3M::Slot::print() const {
static const char notenames[] = "C-C#D-D#E-F-F#G-G#A-A#B-12131415";
printf("c%02u ",channel);
if(note == 255)
printf("...");
else if(note == 254)
printf("^^^");
else
printf("%.2s%u", notenames+2*(note&15), note>>4);
if(instrument)
printf(" %02u", instrument);
else
printf(" ..");
if(volume == 255)
printf(" ..");
else
printf(" %02u", volume);
if(command)
printf(" %c", command+64);
else
printf(" .");
printf("%02X", infobyte);
}
void S3M::Row::load(uint8_t *&ptr) {
num_slots = 0;
while(*ptr != 0) {
slots[num_slots].load(ptr);
++num_slots;
}
//Ignore end-of-row slot
Slot x;
x.load(ptr);
}
void S3M::Row::print() const {
if(num_slots == 0) {
printf("\n");
return;
}
for(int i=0;i<num_slots;++i) {
printf("|");
slots[i].print();
}
printf("|\n");
}
void S3M::Pattern::load(FILE* fp) {
uint16_t length;
fread(&length, sizeof(uint16_t), 1, fp);
uint8_t *data = new uint8_t[length];
fread(data, 1, length, fp);
uint8_t *ptr = data;
for(int i=0;i<64;++i) {
rows[i].load(ptr);
}
delete [] data;
}
void S3M::File::load(const char* filename) {
FILE* fp = fopen(filename, "rb");
assert(fp);
setbuf(fp, NULL);
fread(&header, sizeof(header), 1, fp);
//Verify header
assert(header.eofchar == 0x1A);
assert(header.type == 16);
assert(memcmp(header.scrm,"SCRM",4)==0);
assert(header.num_orders <= 256);
assert(header.num_instruments <= 99);
assert(header.num_patterns <= 100);
//Load orders
memset(orders, 255, sizeof(orders));
fread(orders, 1, header.num_orders, fp);
//Load instrument & pattern pointers
uint16_t ins_ptrs[99];
fread(ins_ptrs, sizeof(uint16_t), header.num_instruments, fp);
uint16_t pat_ptrs[100];
fread(pat_ptrs, sizeof(uint16_t), header.num_patterns, fp);
//Panning information
//Center channel by default
for(int i=0;i<32;++i) {
panning[i] = 0.5;
}
for(int i=0;i<32;++i) {
if(header.channel_settings[i]<16) { //channel is enabled
if(header.channel_settings[i]<8) { //left oriented
panning[i] = 0.25;
} else { //right oriented
panning[i] = 0.75;
}
}
}
//If we have panning information, use it
if(header.default_panning == 0xFC) {
uint8_t pan[32];
fread(pan, sizeof(uint8_t), 32, fp);
for(int i=0;i<32;++i) {
if(pan[i] & 0x20) { //pan specified
panning[i] = (pan[i] & 0x0F) / 15.0;
}
}
}
//Track is in mono
if((header.master_volume & 0x80) == 0) {
for(int i=0;i<32;++i) {
panning[i] = 0.5;
}
}
//Load instruments
for(int i=0;i<header.num_instruments; ++i) {
auto& inst = instruments[i];
fseek(fp, ins_ptrs[i]*16UL, SEEK_SET);
inst.load(fp);
}
//Load patterns
for(int i=0;i<header.num_patterns; ++i) {
if(pat_ptrs[i]) {
auto& pat = patterns[i];
fseek(fp, pat_ptrs[i]*16UL, SEEK_SET);
pat.load(fp);
}
}
fclose(fp);
}