/
HippelCOSO.cs
218 lines (186 loc) · 5.05 KB
/
HippelCOSO.cs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Date: 12.8.2015, Time: 2:20 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using CreateAndPlayMIDI;
namespace IllidanS4.Amiga.Hippel
{
public class HippelCosoFile
{
public Sample[] Samples{get; private set;}
public Song[] Songs{get; private set;}
public Voice[] Voices{get; private set;}
public Pattern[] Patterns{get; private set;}
public HippelCosoFile(Stream stream, byte[] sampleData)
{
long pos = stream.Position;
var reader = new BinaryReader(stream, Encoding.ASCII);
string coso = new string(reader.ReadChars(4));
if(coso == "COSO")
{
int frqseqs = reader.ReadInt32BE();
int volseqs = reader.ReadInt32BE();
int patternsPtr = reader.ReadInt32BE();
int voicesPtr = reader.ReadInt32BE();
int songsData = reader.ReadInt32BE();
int headers = reader.ReadInt32BE();
int samplesPtr = reader.ReadInt32BE();
/*string tfmx = new string(reader.ReadChars(4));
if(tfmx == "TFMX")
{
reader.ReadInt16BE();
reader.ReadInt16BE();
reader.ReadInt16BE();
int npatterns = reader.ReadInt16BE()+1;
reader.ReadInt16BE();
reader.ReadInt16BE();
short nsongs = reader.ReadInt16BE();
short nsamples = reader.ReadInt16BE();
}*/
int nsamples = (samplesPtr-headers)/10 - 1;
stream.Position = pos+headers;
Samples = new Sample[nsamples];
for(int i = 0; i < nsamples; i++)
{
Samples[i] = new Sample(reader);
}
if(sampleData == null)
{
int endPos = Samples.Max(i => i.SourceStart+i.SourceLength);
stream.Position = samplesPtr;
sampleData = reader.ReadBytes(endPos);
}
if(sampleData.Length > 0)
{
for(int i = 0; i < nsamples; i++)
{
var inst = Samples[i];
Array.Copy(sampleData, inst.SourceStart, inst.Data, 0, inst.SourceLength);
}
}
int nsongs = (headers-songsData)/6;
stream.Position = pos+songsData;
Songs = new Song[nsongs];
for(int i = 0; i < nsongs; i++)
{
Songs[i] = new Song(reader);
}
int nvoices = (songsData-voicesPtr)/3;
stream.Position = pos+voicesPtr;
Voices = new Voice[nvoices];
for(int i = 0; i < nvoices; i++)
{
Voices[i] = new Voice(reader);
}
stream.Position = pos+patternsPtr;
int npatterns = (voicesPtr-patternsPtr)/2;
Patterns = new Pattern[npatterns];
for(int i = 0; i < npatterns; i++)
{
Patterns[i] = new Pattern(reader);
}
/*var patterns = new List<Pattern>();
while(stream.Position < pos+voicesPtr)
{
patterns.Add(new Pattern(reader));
}
int c = patterns.Count;
int x = voices.Max(v => v.PatternAddress);*/
//int npatterns = (voicesPtr-patternsPtr)/
/*stream.Position = pos+tracks;
Pattern[] notes = new Pattern[nnotes];
for(int i = 0; i < nnotes; i++)
{
notes[i] = new Pattern(reader);
}
for(int j = 0; j < 12; j++)
{
Console.WriteLine("Byte "+j);
Console.WriteLine(notes.Select(n => n.Data).Any(d => d[j] > 10));
}*/
}
}
public MIDISong ToMidi()
{
return null;
}
public static HippelCosoFile Open(string file, string samples)
{
using(var stream = new FileStream(file, FileMode.Open))
{
byte[] sampleData = null;
if(samples != null) sampleData = File.ReadAllBytes(samples);
return new HippelCosoFile(stream, sampleData);
}
}
}
public struct Pattern
{
public byte Note{get; private set;}
public byte Command{get; private set;}
public int Info{get; private set;}
public Pattern(BinaryReader reader) : this()
{
byte sig = reader.ReadByte();
switch(sig)
{
case 0xFF: //transpose
Command = 1;
Info = reader.ReadByte();
break;
case 0xFE: //speed
Command = 2;
Info = reader.ReadByte();
break;
case 0xFD: //speed, loop
Command = 3;
Info = reader.ReadByte();
break;
default:
Note = sig;
Info = reader.ReadByte();
break;
}
}
}
public struct Voice
{
public byte PatternAddress{get; private set;}
public byte TrackTranspose{get; private set;}
public byte VolTranspose{get; private set;}
public Voice(BinaryReader reader) : this()
{
PatternAddress = reader.ReadByte();
TrackTranspose = reader.ReadByte();
TrackTranspose = reader.ReadByte();
}
}
public struct Song
{
public short Start{get; private set;}
public short End{get; private set;}
public short Speed{get; private set;}
public Song(BinaryReader reader) : this()
{
Start = reader.ReadInt16BE();
End = reader.ReadInt16BE();
Speed = reader.ReadInt16BE();
}
}
public struct Sample
{
public int SourceStart{get; private set;}
public int SourceLength{get; private set;}
public int LoopStart{get; private set;}
public byte[] Data{get; private set;}
public Sample(BinaryReader reader) : this()
{
SourceStart = reader.ReadInt32BE();
SourceLength = reader.ReadUInt16BE()*2;
LoopStart = reader.ReadInt32BE();
Data = new byte[SourceLength];
}
}
}